diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 1eddec6a44..46e3af8c66 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -18,6 +18,7 @@ #include #include #include +#include /** The states of sending the packets. */ enum SendPacketsState { @@ -65,6 +66,9 @@ public: */ class TCPConnecter { private: + std::thread resolve_thread; ///< Thread used during resolving. + std::atomic is_resolved = false; ///< Whether resolving is done. + addrinfo *ai = nullptr; ///< getaddrinfo() allocated linked-list of resolved addresses. std::vector addresses; ///< Addresses we can connect to. std::map sock_to_address; ///< Mapping of a socket to the real address it is connecting to. USed for DEBUG statements. @@ -73,7 +77,6 @@ private: std::vector sockets; ///< Pending connect() attempts. std::chrono::steady_clock::time_point last_attempt; ///< Time we last tried to connect. - std::atomic is_resolved = false; ///< Whether resolving is done. std::string connection_string; ///< Current address we are connecting to (before resolving). void Resolve(); diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index a1e369295a..96c82b5af8 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -32,13 +32,17 @@ TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_ _tcp_connecters.push_back(this); - if (!StartNewThread(nullptr, "ottd:resolve", &TCPConnecter::ResolveThunk, this)) { + if (!StartNewThread(&this->resolve_thread, "ottd:resolve", &TCPConnecter::ResolveThunk, this)) { this->Resolve(); } } TCPConnecter::~TCPConnecter() { + if (this->resolve_thread.joinable()) { + this->resolve_thread.join(); + } + for (const auto &socket : this->sockets) { closesocket(socket); } @@ -187,6 +191,7 @@ void TCPConnecter::Resolve() this->ai = ai; this->OnResolved(ai); + this->is_resolved = true; }