From 952631bb807010265162c498a39281bcbd4b8632 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 31 Mar 2022 23:48:07 +0200 Subject: [PATCH] add send timeout to client tcp socket --- src/TNetwork.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 3490d5e..356d07c 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -162,6 +162,19 @@ void TNetwork::TCPServerMain() { beammp_warn(("Got an invalid client socket on connect! Skipping...")); continue; } + // set timeout + size_t SendTimeoutMS = 30 * 1000; +#if defined(BEAMMP_WINDOWS) + ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast(&ms), sizeof(ms)); +#else // POSIX + struct timeval optval; + optval.tv_sec = (int)(SendTimeoutMS / 1000); + optval.tv_usec = (SendTimeoutMS % 1000) * 1000; + ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast(&optval), sizeof(optval)); +#endif + if (ret < 0) { + throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString()); + } std::thread ID(&TNetwork::Identify, this, client); ID.detach(); // TODO: Add to a queue and attempt to join periodically } catch (const std::exception& e) {