Basic adaptations for changes to libdcp1 colour conversion handling.
[dcpomatic.git] / src / lib / server_finder.cc
index de8a3852c5cf45c0abd52faaf94b4f08ee24e94c..35ce9e1a16e8686b51cae35f34783a6ef459946a 100644 (file)
@@ -26,8 +26,9 @@
 #include "cross.h"
 #include "ui_signaller.h"
 
+#include "i18n.h"
+
 using std::string;
-using std::stringstream;
 using std::list;
 using std::vector;
 using std::cout;
@@ -103,24 +104,36 @@ void
 ServerFinder::listen_thread ()
 try
 {
-       while (1) {
-               shared_ptr<Socket> sock (new Socket (60));
+       using namespace boost::asio::ip;
 
-               try {
-                       sock->accept (Config::instance()->server_port_base() + 1);
-               } catch (std::exception& e) {
-                       continue;
-               }
+       boost::asio::io_service io_service;
+       boost::scoped_ptr<tcp::acceptor> acceptor;
+       try {
+               acceptor.reset (new tcp::acceptor (io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
+       } catch (...) {
+               boost::throw_exception (NetworkError (_("Could not listen for remote encode servers.  Perhaps another instance of DCP-o-matic is running.")));
+       }
+
+       while (true) {
+               tcp::socket socket (io_service);
+               acceptor->accept (socket);
+
+               /* XXX: these reads should have timeouts, otherwise we will stop finding servers
+                  if one dies during this conversation
+               */
+
+               uint32_t length = 0;
+               boost::asio::read (socket, boost::asio::buffer (&length, sizeof (uint32_t)));
+               length = ntohl (length);
 
-               uint32_t length = sock->read_uint32 ();
                scoped_array<char> buffer (new char[length]);
-               sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
+               boost::asio::read (socket, boost::asio::buffer (reinterpret_cast<uint8_t*> (buffer.get ()), length));
                
-               stringstream s (buffer.get());
+               string s (buffer.get());
                shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
-               xml->read_stream (s);
-
-               string const ip = sock->socket().remote_endpoint().address().to_string ();
+               xml->read_string (s);
+               
+               string const ip = socket.remote_endpoint().address().to_string ();
                if (!server_found (ip)) {
                        ServerDescription sd (ip, xml->number_child<int> ("Threads"));
                        _servers.push_back (sd);