diff --git a/docs/changelog.txt b/docs/changelog.txt index b3c73f3b7..1d19d3f74 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -57,6 +57,7 @@ Template for new versions: ## Fixes - `buildingplan`: fix choosing the wrong mechanism (or something that isn't a mechanism) when linking a lever and manually choosing a mechanism, but then canceling the selection +- RemoteServer: don't shut down the socket prematurely, allowing continuing connections from, for example, dfhack-run ## Misc Improvements - `buildingplan`: save magma safe mechanisms for when magma safety is requested when linking levers and pressure plates to targets diff --git a/library/RemoteServer.cpp b/library/RemoteServer.cpp index 72f2a29b5..4e26b0649 100644 --- a/library/RemoteServer.cpp +++ b/library/RemoteServer.cpp @@ -80,7 +80,7 @@ namespace { struct BlockedException : std::exception { const char* what() const noexcept override { - return "Core has blocked all connection. This should have been catched."; + return "Core has blocked all connection. This should have been caught."; } }; } @@ -475,35 +475,28 @@ void ServerMainImpl::threadFn(std::promise promise, int port) { ServerMainImpl server{std::move(promise), port}; - CActiveSocket *client = nullptr; - + server.socket.SetBlocking(); try { - for (int acceptFail = 0 ; server.socket.IsSocketValid() && acceptFail < 5 ; acceptFail++) - { - if ((client = server.socket.Accept()) != NULL) - { + while (server.socket.IsSocketValid()) { + if (std::unique_ptr client{server.socket.Accept()}) { BlockGuard lock; - ServerConnection::Accepted(client); - client = nullptr; + ServerConnection::Accepted(client.release()); } - else - { - WARN(socket).print("Connection failure: %s (%d of %d)\n", server.socket.DescribeError(), acceptFail + 1, 5); + else switch (server.socket.GetSocketError()) { + case CSimpleSocket::SocketInvalidSocket: + WARN(socket).print("Listening socket invalid, shutting down RemoteServer\n"); + server.socket.Close(); + break; + case CSimpleSocket::SocketFirewallError: + case CSimpleSocket::SocketProtocolError: + WARN(socket).print("Connection failed: %s\n", server.socket.DescribeError()); + break; + default: + break; } } - } catch(BlockedException &) { - if (client) - client->Close(); - delete client; - } - - if (server.socket.IsSocketValid()) - { - WARN(socket).print("Too many failed accepts, shutting down RemoteServer\n"); } - else - { - WARN(socket).print("Listening socket invalid, shutting down RemoteServer\n"); + catch(BlockedException &) { } }