From d50980b10f0e3965fdf518b580cf5690760c908a Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 5 May 2022 00:51:33 +0200 Subject: [PATCH 1/4] Use SO_DONTLINGER to fix bind() address in use error This will ensure the socket does not linger, which fixes the common and *super* annoying issue of "bind(): address already in use" --- src/Network/Server.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Network/Server.cpp b/src/Network/Server.cpp index 28302aa..74adc61 100644 --- a/src/Network/Server.cpp +++ b/src/Network/Server.cpp @@ -33,6 +33,11 @@ void Server::TCPClientMain() { LOG(ERROR) << "Socket failed! Error code: " << WSAGetLastError(); return; } + const char optval = 0; + int status = ::setsockopt(TCPSocket, SOL_SOCKET, SO_DONTLINGER, &optval, sizeof(optval)); + if (status < 0) { + LOG(INFO) << "Failed to set DONTLINGER: " << GetSocketApiError(); + } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr); From be219d289e65deca1ed8a4901fcec4465d390359 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 5 May 2022 00:53:42 +0200 Subject: [PATCH 2/4] Correctly format WinSock errors with a message This has lead to many(!) issues in the past, and makes life harder for everyone doing support. This will display the error code, together with an error message windows provides. --- include/Server.h | 1 + src/Network/Server.cpp | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/Server.h b/include/Server.h index 535b441..627cba8 100644 --- a/include/Server.h +++ b/include/Server.h @@ -38,6 +38,7 @@ private: void UpdateUl(bool D, const std::string& msg); std::unique_ptr UDPSockAddress; void ServerParser(const std::string& Data); + static std::string GetSocketApiError(); void TCPSend(const std::string& Data); void UDPParser(std::string Packet); void SendLarge(std::string Data); diff --git a/src/Network/Server.cpp b/src/Network/Server.cpp index 74adc61..b45b2be 100644 --- a/src/Network/Server.cpp +++ b/src/Network/Server.cpp @@ -30,7 +30,7 @@ void Server::TCPClientMain() { SOCKADDR_IN ServerAddr; TCPSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(TCPSocket == -1) { - LOG(ERROR) << "Socket failed! Error code: " << WSAGetLastError(); + LOG(ERROR) << "Socket failed! Error code: " << GetSocketApiError(); return; } const char optval = 0; @@ -41,10 +41,10 @@ void Server::TCPClientMain() { ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr); - int status = connect(TCPSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)); + status = connect(TCPSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)); if(status != 0){ UStatus = "Connection Failed!"; - LOG(ERROR) << "Connect failed! Error code: " << WSAGetLastError(); + LOG(ERROR) << "Connect failed! Error code: " << GetSocketApiError(); Close(); return; } @@ -74,7 +74,7 @@ void Server::UDPSend(std::string Data) { } std::string Packet = char(ClientID+1) + std::string(":") + Data; int sendOk = sendto(UDPSocket, Packet.c_str(), int(Packet.size()), 0, (sockaddr*)UDPSockAddress.get(), sizeof(sockaddr_in)); - if (sendOk == SOCKET_ERROR)LOG(ERROR) << "UDP Socket Error Code : " << WSAGetLastError(); + if (sendOk == SOCKET_ERROR)LOG(ERROR) << "UDP Socket Error Code : " << GetSocketApiError(); } void Server::UDPParser(std::string Packet) { @@ -141,6 +141,30 @@ void Server::SendLarge(std::string Data) { TCPSend(Data); } +static std::string Server::GetSocketApiError() { + // This will provide us with the error code and an error message, all in one. + // The resulting format is " - " + int err; + char msgbuf[256]; + msgbuf[0] = '\0'; + + err = WSAGetLastError(); + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + msgbuf, + sizeof(msgbuf), + nullptr); + + if (*msgbuf) { + return std::to_string(WSAGetLastError()) + " - " + std::string(msgbuf); + } else { + return std::to_string(WSAGetLastError()); + } +} + void Server::ServerSend(std::string Data, bool Rel) { if(Terminate || Data.empty())return; char C = 0; @@ -230,7 +254,7 @@ bool Server::CheckBytes(int32_t Bytes) { Terminate = true; return false; }else if (Bytes < 0) { - //debug("(TCP CB) recv failed with error: " + std::to_string(WSAGetLastError())); + //debug("(TCP CB) recv failed with error: " + GetSocketApiError(); KillSocket(TCPSocket); Terminate = true; return false; From 27c057c46350477b0d2a43a40409cdccb4b5792c Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 5 May 2022 01:33:17 +0200 Subject: [PATCH 3/4] actions: update vcpkg commit id --- .github/workflows/cmake-windows.yml | 2 +- include/atomic_queue | 2 +- include/cpp-httplib | 2 +- include/tomlplusplus | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 8e3a853..06a14f6 100644 --- a/.github/workflows/cmake-windows.yml +++ b/.github/workflows/cmake-windows.yml @@ -20,7 +20,7 @@ jobs: with: vcpkgArguments: 'zlib discord-rpc nlohmann-json openssl minhook' vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg' - vcpkgGitCommitId: '75522bb1f2e7d863078bcd06322348f053a9e33f' + vcpkgGitCommitId: 'b33f616f85e207012aa8229706d8e603efd5794d' vcpkgTriplet: 'x64-windows-static' - name: Create Build Environment diff --git a/include/atomic_queue b/include/atomic_queue index fad3155..7d75e9e 160000 --- a/include/atomic_queue +++ b/include/atomic_queue @@ -1 +1 @@ -Subproject commit fad31557de3acc40e704ad63bb11e7089190c16a +Subproject commit 7d75e9ed0359650224b29cdf6728c5fe0a19fffb diff --git a/include/cpp-httplib b/include/cpp-httplib index 33f53aa..fee8e97 160000 --- a/include/cpp-httplib +++ b/include/cpp-httplib @@ -1 +1 @@ -Subproject commit 33f53aa4583c132e70dc21f2d7fe004706267784 +Subproject commit fee8e97b4eeb34fe2e6e6294413d84e9e7a072a7 diff --git a/include/tomlplusplus b/include/tomlplusplus index 8e669aa..27816db 160000 --- a/include/tomlplusplus +++ b/include/tomlplusplus @@ -1 +1 @@ -Subproject commit 8e669aa6990e0ed219c169d491472d749f54c393 +Subproject commit 27816dbbd168a84a0a7a252d7d75b0ca4dc1e073 From dab8daa088e0caccf08b63a63338edf06b79eadf Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 5 May 2022 01:54:07 +0200 Subject: [PATCH 4/4] Make GetSocketApiError static ... but only in the header ;) --- src/Network/Server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Server.cpp b/src/Network/Server.cpp index b45b2be..a1885c7 100644 --- a/src/Network/Server.cpp +++ b/src/Network/Server.cpp @@ -141,7 +141,7 @@ void Server::SendLarge(std::string Data) { TCPSend(Data); } -static std::string Server::GetSocketApiError() { +std::string Server::GetSocketApiError() { // This will provide us with the error code and an error message, all in one. // The resulting format is " - " int err;