From a08d29a0aed88d4002ab2fa2cb662bd08c6cb664 Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Sat, 7 Nov 2020 23:29:06 +0200 Subject: [PATCH] Vehicle ghost fix, player list fix --- include/Buffer.h | 44 --------------- include/Client.hpp | 2 - src/Init/Heartbeat.cpp | 19 ++++--- src/Network/InitClient.cpp | 7 ++- src/Network/TCPHandler.cpp | 105 +++++++++++++++++++++--------------- src/Network/VehicleData.cpp | 25 ++------- src/main.cpp | 7 ++- 7 files changed, 84 insertions(+), 125 deletions(-) delete mode 100644 include/Buffer.h diff --git a/include/Buffer.h b/include/Buffer.h deleted file mode 100644 index 204e299..0000000 --- a/include/Buffer.h +++ /dev/null @@ -1,44 +0,0 @@ -/// -/// Created by Anonymous275 on 8/25/2020 -/// -#pragma once -#include -#include "CustomAssert.h" -class Client; -void GParser(Client*c, const std::string&Packet); -class Buffer{ -public: - void Handle(Client*c,const std::string& Data){ - Assert(c); - Buf += Data; - Manage(c); - } - void clear(){ - Buf.clear(); - } -private: - std::string Buf; - void Manage(Client*c){ - Assert(c); - if(!Buf.empty()){ - std::string::size_type p; - if (Buf.at(0) == '\n'){ - p = Buf.find('\n',1); - if(p != std::string::npos){ - std::string R = Buf.substr(1,p-1); - std::string_view B(R.c_str(),R.find(char(0))); - GParser(c, B.data()); - Buf = Buf.substr(p+1); - Manage(c); - } - }else{ - p = Buf.find('\n'); - if(p == std::string::npos)Buf.clear(); - else{ - Buf = Buf.substr(p); - Manage(c); - } - } - } - } -}; diff --git a/include/Client.hpp b/include/Client.hpp index 6015499..0cf733d 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -9,7 +9,6 @@ #include #define SOCKET int #endif -#include "Buffer.h" #include "CustomAssert.h" #include #include @@ -55,7 +54,6 @@ public: int GetCarCount(); void ClearCars(); int GetStatus(); - Buffer Handler; int GetID(); }; struct ClientInterface{ diff --git a/src/Init/Heartbeat.cpp b/src/Init/Heartbeat.cpp index 9645ce5..763dd47 100644 --- a/src/Init/Heartbeat.cpp +++ b/src/Init/Heartbeat.cpp @@ -6,6 +6,7 @@ #include "Client.hpp" #include "Settings.h" #include "Logger.h" +#include #include #include #include @@ -21,14 +22,16 @@ std::string GetPlayers(){ return Return; } std::string GenerateCall(){ - std::string State = Private ? "true" : "false"; - std::string ret = "uuid="; - ret += Key+"&players="+std::to_string(CI->Size())+"&maxplayers="+std::to_string(MaxPlayers)+"&port=" - + std::to_string(Port) + "&map=" + MapName + "&private="+State+"&version="+GetSVer()+ - "&clientversion="+GetCVer()+"&name="+ServerName+"&pps="+StatReport+"&modlist="+FileList+ - "&modstotalsize="+std::to_string(MaxModSize)+"&modstotal="+std::to_string(ModsLoaded) - +"&playerslist="+GetPlayers()+"&desc="+ServerDesc; - return ret; + std::stringstream Ret; + Ret << "uuid=" << Key << "&players=" << CI->Size() + << "&maxplayers=" << MaxPlayers << "&port=" << Port + << "&map=" << MapName << "&private=" << (Private ? "true" : "false") + << "&version=" << GetSVer() << "&clientversion=" << GetCVer() + << "&name=" << ServerName << "&pps=" << StatReport + << "&modlist=" << FileList << "&modstotalsize=" << MaxModSize + << "&modstotal=" << ModsLoaded << "&playerslist=" << GetPlayers() + << "&desc=" << ServerDesc; + return Ret.str(); } std::string RunPromise(const std::string& IP, const std::string& R) { std::packaged_task task([&]() { return PostHTTP(IP,R); }); diff --git a/src/Network/InitClient.cpp b/src/Network/InitClient.cpp index 163e00a..8fda185 100644 --- a/src/Network/InitClient.cpp +++ b/src/Network/InitClient.cpp @@ -30,16 +30,15 @@ void Respond(Client*c, const std::string& MSG, bool Rel){ if(C == 'O' || C == 'T' || MSG.length() > 1000)SendLarge(c,MSG); else TCPSend(c,MSG); }else UDPSend(c,MSG); + } void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){ - if (!Self) { - Assert(c); - } + if (!Self)Assert(c); char C = Data.at(0); for(Client*client : CI->Clients){ if(client != nullptr) { if (Self || client != c) { - if (client->isSynced || (C == 'O' && Data.at(1) == 's')) { + if (client->isSynced) { if (Rel || C == 'W' || C == 'Y' || C == 'V' || C == 'E') { if (C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client, Data); diff --git a/src/Network/TCPHandler.cpp b/src/Network/TCPHandler.cpp index f119938..fa67258 100644 --- a/src/Network/TCPHandler.cpp +++ b/src/Network/TCPHandler.cpp @@ -2,67 +2,84 @@ /// Created by Anonymous275 on 8/1/2020 /// #include "Security/Enc.h" +#include "UnixCompat.h" +#include "Compressor.h" #include "Network.h" #include "Logger.h" -#include "UnixCompat.h" #include + void TCPSend(Client*c,const std::string&Data){ Assert(c); if(c == nullptr)return; - std::string Send = "\n" + Data.substr(0,Data.find(char(0))) + "\n"; -#ifdef WIN32 - int Sent; - int len = static_cast(Send.size()); -#else - int64_t Sent; - size_t len = Send.size(); -#endif // WIN32 - Sent = send(c->GetTCPSock(), Send.c_str(), len, 0); - if (Sent == 0){ - if(c->GetStatus() > -1)c->SetStatus(-1); - }else if (Sent < 0) { - if(c->GetStatus() > -1)c->SetStatus(-1); - closesocket(c->GetTCPSock()); - } + auto Size = int32_t(Data.size()); + std::string Send(4,0); + memcpy(&Send[0],&Size,sizeof(Size)); + Send += Data; + Size = int32_t(Send.size()); + int32_t Sent = 0,Temp; + + do { + Temp = send(c->GetTCPSock(), &Send[Sent], Size - Sent, 0); + if (Temp == 0) { + if (c->GetStatus() > -1)c->SetStatus(-1); + return; + } else if (Sent < 0) { + if (c->GetStatus() > -1)c->SetStatus(-1); + closesocket(c->GetTCPSock()); + return; + } + Sent += Temp; + }while(Sent < Size); } -void TCPHandle(Client*c,const std::string& data){ + +bool CheckBytes(Client*c,int32_t BytesRcv){ Assert(c); -#ifdef WIN32 - __try{ -#endif // WIN32 - c->Handler.Handle(c,data); -#ifdef WIN32 - }__except(1){ - c->Handler.clear(); - } -#endif // WIN32 -} -void TCPRcv(Client*c){ - Assert(c); - if(c == nullptr || c->GetStatus() < 0)return; - #define len 4096 - char buf[len]; - ZeroMemory(buf, len); - int64_t BytesRcv = recv(c->GetTCPSock(), buf, len,0); - #undef len if (BytesRcv == 0){ debug(Sec("(TCP) Connection closing...")); if(c->GetStatus() > -1)c->SetStatus(-1); - return; + return false; }else if (BytesRcv < 0) { -#ifdef WIN32 - debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError())); -#else // unix - debug(Sec("(TCP) recv failed with error: ") + std::string(strerror(errno))); -#endif // WIN32 + #ifdef WIN32 + debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError())); + #else // unix + debug(Sec("(TCP) recv failed with error: ") + std::string(strerror(errno))); + #endif // WIN32 if(c->GetStatus() > -1)c->SetStatus(-1); closesocket(c->GetTCPSock()); - return; + return false; } - std::string Buf(buf,(size_t(BytesRcv))); - TCPHandle(c,Buf); + return true; } + +void TCPRcv(Client*c){ + Assert(c); + static int32_t Header,BytesRcv,Temp; + if(c == nullptr || c->GetStatus() < 0)return; + #ifdef WIN32 + BytesRcv = recv(c->GetTCPSock(), reinterpret_cast(&Header), sizeof(Header),0); + #else + BytesRcv = recv(c->GetTCPSock(), reinterpret_cast(&Header), sizeof(Header), 0); + #endif + if(!CheckBytes(c,BytesRcv))return; + char* Data = new char[Header]; + BytesRcv = 0; + do{ + Temp = recv(c->GetTCPSock(), Data+BytesRcv, Header-BytesRcv,0); + if(!CheckBytes(c,Temp)){ + delete[] Data; + return; + } + BytesRcv += Temp; + }while(BytesRcv < Header); + std::string Ret = std::string(Data,Header); + delete[] Data; + if (Ret.substr(0, 4) == "ABG:") { + Ret = DeComp(Ret.substr(4)); + } + GParser(c,Ret); +} + void TCPClient(Client*c){ DebugPrintTID(); Assert(c); diff --git a/src/Network/VehicleData.cpp b/src/Network/VehicleData.cpp index 3d4fcb2..703e7f7 100644 --- a/src/Network/VehicleData.cpp +++ b/src/Network/VehicleData.cpp @@ -101,28 +101,11 @@ int SplitID() { void SendLarge(Client* c, std::string Data) { Assert(c); Data = Data.substr(0, Data.find(char(0))); - int ID = PacktID(); - std::string Packet; - if (Data.length() > 1000) { - std::string pckt = Data; - int S = 1, Split = int(ceil(float(pckt.length()) / 1000)); - int SID = SplitID(); - while (pckt.length() > 1000) { - Packet = "SC|" + std::to_string(S) + "|" + std::to_string(Split) + "|" + std::to_string(ID) + "|" + std::to_string(SID) + "|" + pckt.substr(0, 1000); - DataAcks.insert(new PacketData { ID, c, Packet, 1 }); - UDPSend(c, Packet); - pckt = pckt.substr(1000); - S++; - ID = PacktID(); - } - Packet = "SC|" + std::to_string(S) + "|" + std::to_string(Split) + "|" + std::to_string(ID) + "|" + std::to_string(SID) + "|" + pckt; - DataAcks.insert(new PacketData { ID, c, Packet, 1 }); - UDPSend(c, Packet); - } else { - Packet = "BD:" + std::to_string(ID) + ":" + Data; - DataAcks.insert(new PacketData { ID, c, Packet, 1 }); - UDPSend(c, Packet); + if (Data.length() > 400) { + std::string CMP(Comp(Data)); + Data = "ABG:" + CMP; } + TCPSend(c,Data); } struct HandledC { size_t Pos = 0; diff --git a/src/main.cpp b/src/main.cpp index 1683be1..ee1243f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,10 @@ -#include "Startup.h" + #include "CustomAssert.h" #include -#include +#include "Startup.h" #include +#include + [[noreturn]] void loop(){ DebugPrintTID(); while(true){ @@ -10,6 +12,7 @@ std::this_thread::sleep_for(std::chrono::milliseconds(600)); } } + int main(int argc, char* argv[]) { DebugPrintTID(); // curl needs to be initialized to properly deallocate its resources later