diff --git a/include/UnixCompat.h b/include/UnixCompat.h index 1387552..65c887b 100644 --- a/include/UnixCompat.h +++ b/include/UnixCompat.h @@ -16,9 +16,14 @@ inline void ZeroMemory(void* dst, size_t len) { Assert(std::memset(dst, 0, len) != nullptr); } // provides unix equivalent of closesocket call in win32 -inline void closesocket(int socket) { +inline void CloseSocketProper(int socket) { +#ifndef WIN32 shutdown(socket, SHUT_RDWR); close(socket); +#else // WIN32 + shutdown(socket, SD_BOTH); + closesocket(socket); +#endif // WIN32 } #ifndef __try diff --git a/src/Lua/LuaSystem.cpp b/src/Lua/LuaSystem.cpp index 6cca32c..7c58b5a 100644 --- a/src/Lua/LuaSystem.cpp +++ b/src/Lua/LuaSystem.cpp @@ -348,7 +348,7 @@ int lua_dropPlayer(lua_State* L) { Respond(c, "C:Server:You have been Kicked from the server! " + Reason, true); c->SetStatus(-2); info(Sec("Closing socket due to kick")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); } else SendError(L, Sec("DropPlayer not enough arguments")); diff --git a/src/Network/Auth.cpp b/src/Network/Auth.cpp index d44881a..19f11e4 100644 --- a/src/Network/Auth.cpp +++ b/src/Network/Auth.cpp @@ -86,7 +86,7 @@ void Check(SOCKET TCPSock, std::reference_wrapper ok) { accum += 100; if (accum >= 5000) { error(Sec("Identification timed out (Check accum)")); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } } @@ -142,14 +142,14 @@ void Identification(SOCKET TCPSock, RSA* Skey) { std::string Name, DID, Role; if (!Send(TCPSock, GenerateM(Skey))) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } std::string msg = Rcv(TCPSock); auto Keys = Parse(msg); if (!Send(TCPSock, RSA_E("HC", Keys.second, Keys.first))) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } @@ -161,23 +161,23 @@ void Identification(SOCKET TCPSock, RSA* Skey) { Ver = Ver.substr(2); if (Ver.length() > 4 || Ver != GetCVer()) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } } else { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } Res = RSA_D(Res, Skey); if (Res.size() < 3 || Res.substr(0, 2) != Sec("NR")) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } if (Res.find(':') == std::string::npos) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } Name = Res.substr(2, Res.find(':') - 2); @@ -185,7 +185,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) { Role = GetRole(DID); if (Role.empty() || Role.find(Sec("Error")) != std::string::npos) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); return; } // DebugPrintTIDInternal(std::string("Client(") + Name + ")"); @@ -194,7 +194,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) { if (c != nullptr) { if (c->GetDID() == DID) { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); c->SetStatus(-2); break; } @@ -205,7 +205,7 @@ void Identification(SOCKET TCPSock, RSA* Skey) { CreateClient(TCPSock, Name, DID, Role); } else { error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); } } void Identify(SOCKET TCPSock) { @@ -222,7 +222,7 @@ void Identify(SOCKET TCPSock) { }__except(1){ if(TCPSock != -1){ error("died on " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(TCPSock); + CloseSocketProper(TCPSock); } } #endif // WIN32*/ @@ -271,7 +271,7 @@ void TCPServerMain() { } } while (client); - closesocket(client); + CloseSocketProper(client); WSACleanup(); #else // unix // wondering why we need slightly different implementations of this? @@ -313,6 +313,6 @@ void TCPServerMain() { } while (client); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); - closesocket(client); + CloseSocketProper(client); #endif // WIN32 } diff --git a/src/Network/Sync.cpp b/src/Network/Sync.cpp index cad9cca..5c7df0e 100644 --- a/src/Network/Sync.cpp +++ b/src/Network/Sync.cpp @@ -33,7 +33,7 @@ void STCPSend(Client* c, std::string Data) { if (c->GetStatus() > -1) c->SetStatus(-1); info(Sec("Closing socket, BytesSent < 0")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); } } void SendFile(Client* c, const std::string& Name) { @@ -108,13 +108,13 @@ bool STCPRecv(Client* c) { if (c->GetStatus() > -1) c->SetStatus(-1); info(Sec("Closing socket in STCP receive, BytesRcv == 0")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); return false; } else if (BytesRcv < 0) { if (c->GetStatus() > -1) c->SetStatus(-1); info(Sec("Closing socket in STCP receive, BytesRcv < 0")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); return false; } if (strcmp(buf, "Done") == 0) diff --git a/src/Network/TCPHandler.cpp b/src/Network/TCPHandler.cpp index c19a18c..f61ed5c 100644 --- a/src/Network/TCPHandler.cpp +++ b/src/Network/TCPHandler.cpp @@ -32,7 +32,7 @@ void TCPSend(Client* c, const std::string& Data) { if (c->GetStatus() > -1) c->SetStatus(-1); // info(Sec("Closing socket, Temp < 0")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); return; } Sent += Temp; @@ -55,7 +55,7 @@ bool CheckBytes(Client* c, int32_t BytesRcv) { if (c->GetStatus() > -1) c->SetStatus(-1); info(Sec("Closing socket in CheckBytes, BytesRcv < 0")); - closesocket(c->GetTCPSock()); + CloseSocketProper(c->GetTCPSock()); return false; } return true; diff --git a/src/Network/VehicleData.cpp b/src/Network/VehicleData.cpp index aaab1a8..cda6f0e 100644 --- a/src/Network/VehicleData.cpp +++ b/src/Network/VehicleData.cpp @@ -323,7 +323,7 @@ void LOOP() { error(Sec("fatal: ") + std::string(e.what())); } } - /*closesocket(UDPSock); + /*CloseSocketProper(UDPSock); WSACleanup(); return;*/ #else // unix @@ -369,7 +369,7 @@ void LOOP() { error(Sec("fatal: ") + std::string(e.what())); } } - /*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain? + /*CloseSocketProper(UDPSock); // TODO: Why not this? We did this in TCPServerMain? return; */ #endif // WIN32