Use accept() properly when reading replies to server request broadcasts.
authorCarl Hetherington <cth@carlh.net>
Wed, 8 Oct 2014 15:55:52 +0000 (16:55 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 8 Oct 2014 15:55:52 +0000 (16:55 +0100)
Without this, some replies were being lost.

ChangeLog
src/lib/server.cc
src/lib/server_finder.cc

index 6c40e1de28c5be40a2f0a9085e6c3ef080b0c3af..04762860200054e2bc0a9b493c828cc73777f771 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-10-08  c.hetherington  <cth@carlh.net>
+
+       * Make server finding more reliable when
+       there are more than a few servers.
+
 2014-10-08  Carl Hetherington  <cth@carlh.net>
 
        * Version 1.74.2 released.
index 9591be1886414dc77f6f335dbb976c0c857bad93..9428ba611d70d2a96de10ef3793b348bce778aff 100644 (file)
@@ -247,6 +247,9 @@ Server::broadcast_received ()
                root->add_child("Threads")->add_child_text (raw_convert<string> (_worker_threads.size ()));
                string xml = doc.write_to_string ("UTF-8");
 
+               if (_verbose) {
+                       cout << "Offering services to master " << _broadcast.send_endpoint.address().to_string () << "\n";
+               }
                shared_ptr<Socket> socket (new Socket);
                try {
                        socket->connect (boost::asio::ip::tcp::endpoint (_broadcast.send_endpoint.address(), Config::instance()->server_port_base() + 1));
index 744a65f597300fcc809b6daab70e32f8ccd0f0a0..58ad61bf9fa963120282a2756f2ad0ff61c8fd36 100644 (file)
@@ -102,24 +102,32 @@ void
 ServerFinder::listen_thread ()
 try
 {
+       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) {
-               shared_ptr<Socket> sock (new Socket (60));
+               tcp::socket socket (io_service);
+               acceptor.accept (socket);
 
-               try {
-                       sock->accept (Config::instance()->server_port_base() + 1);
-               } catch (std::exception& e) {
-                       continue;
-               }
+               /* 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));
                
                string s (buffer.get());
                shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
                xml->read_string (s);
-
-               string const ip = sock->socket().remote_endpoint().address().to_string ();
+               
+               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);