From e89aee2c29f02d8a2044c97fc5cdd4be6eb34ef0 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 8 Oct 2014 16:55:52 +0100 Subject: Use accept() properly when reading replies to server request broadcasts. Without this, some replies were being lost. --- src/lib/server_finder.cc | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'src/lib/server_finder.cc') diff --git a/src/lib/server_finder.cc b/src/lib/server_finder.cc index 744a65f59..58ad61bf9 100644 --- a/src/lib/server_finder.cc +++ b/src/lib/server_finder.cc @@ -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 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 buffer (new char[length]); - sock->read (reinterpret_cast (buffer.get()), length); + boost::asio::read (socket, boost::asio::buffer (reinterpret_cast (buffer.get ()), length)); string s (buffer.get()); shared_ptr 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 ("Threads")); _servers.push_back (sd); -- cgit v1.2.3