Updated cs_CZ translation from Tomáš Begeni.
[dcpomatic.git] / src / lib / encode_server_finder.cc
index 6b288d70d75af881f5fe80d6e7b441fe16a80cdf..3f5cb74f01bddbe659dc2360446d454bdbe06725 100644 (file)
 */
 
 
-#include "encode_server_finder.h"
-#include "exceptions.h"
-#include "util.h"
 #include "config.h"
+#include "constants.h"
 #include "cross.h"
-#include "encode_server_description.h"
 #include "dcpomatic_socket.h"
+#include "encode_server_description.h"
+#include "encode_server_finder.h"
+#include "exceptions.h"
 #include <dcp/raw_convert.h>
 #include <libcxml/cxml.h>
 #include <boost/bind/placeholders.hpp>
@@ -203,35 +203,43 @@ catch (...)
 void
 EncodeServerFinder::start_accept ()
 {
-       auto socket = make_shared<Socket>();
+       _accept_socket = make_shared<Socket>();
+
        _listen_acceptor->async_accept (
-               socket->socket(),
-               boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
+               _accept_socket->socket(),
+               boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error)
                );
 }
 
 
 void
-EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
+EncodeServerFinder::handle_accept (boost::system::error_code ec)
 {
        if (ec) {
                start_accept ();
                return;
        }
 
-       uint32_t length;
-       socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
-       length = ntohl (length);
+       string server_available;
 
-       scoped_array<char> buffer(new char[length]);
-       socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
+       try {
+               uint32_t length;
+               _accept_socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
+               length = ntohl (length);
+
+               scoped_array<char> buffer(new char[length]);
+               _accept_socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
+               server_available = buffer.get();
+       } catch (NetworkError&) {
+               /* Maybe the server went away; let's just try again */
+               start_accept();
+               return;
+       }
 
-       string s (buffer.get());
        auto xml = make_shared<cxml::Document>("ServerAvailable");
-       xml->read_string (s);
+       xml->read_string(server_available);
 
-       auto const ip = socket->socket().remote_endpoint().address().to_string();
-       }
+       auto const ip = _accept_socket->socket().remote_endpoint().address().to_string();
        bool changed = false;
        {
                boost::mutex::scoped_lock lm (_servers_mutex);