Use accept() properly when reading replies to server request broadcasts.
[dcpomatic.git] / src / lib / server_finder.cc
index 3d5825ad4d7f4a01aadbfd850a774bc2c7eb18fa..58ad61bf9fa963120282a2756f2ad0ff61c8fd36 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@
 */
 
 #include <libcxml/cxml.h>
+#include <libdcp/raw_convert.h>
 #include "server_finder.h"
 #include "exceptions.h"
 #include "util.h"
 #include "ui_signaller.h"
 
 using std::string;
-using std::stringstream;
 using std::list;
 using std::vector;
 using std::cout;
 using boost::shared_ptr;
 using boost::scoped_array;
-using boost::lexical_cast;
+using libdcp::raw_convert;
 
 ServerFinder* ServerFinder::_instance = 0;
 
@@ -62,7 +62,7 @@ try
 
        string const data = DCPOMATIC_HELLO;
        
-       while (1) {
+       while (true) {
                if (Config::instance()->use_any_servers ()) {
                        /* Broadcast to look for servers */
                        try {
@@ -82,7 +82,7 @@ try
                        }
                        try {
                                boost::asio::ip::udp::resolver resolver (io_service);
-                               boost::asio::ip::udp::resolver::query query (*i, lexical_cast<string> (Config::instance()->server_port_base() + 1));
+                               boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
                                boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
                                socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
                        } catch (...) {
@@ -102,25 +102,32 @@ void
 ServerFinder::listen_thread ()
 try
 {
-       while (1) {
-               shared_ptr<Socket> sock (new Socket (10));
-
-               try {
-                       sock->accept (Config::instance()->server_port_base() + 1);
-               } catch (std::exception& e) {
-                       dcpomatic_sleep (60);
-                       continue;
-               }
+       using namespace boost::asio::ip;
+
+       boost::asio::io_service io_service;
+       tcp::acceptor acceptor (io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1));
+
+       while (true) {
+               tcp::socket socket (io_service);
+               acceptor.accept (socket);
+
+               /* XXX: does this deadline work with synchronous reads? */
+
+               boost::asio::deadline_timer deadline (io_service);
+               deadline.expires_from_now (boost::posix_time::seconds (10));
+
+               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);