From a2b6b29ea1dad044d6c3d08e766c0fa141d818aa Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Sat, 7 Nov 2020 23:29:42 +0200 Subject: [PATCH] Vehicle ghost fix, server list fix --- include/Buffer.h | 40 ------------------- include/Network/network.h | 2 - src/Network/Core.cpp | 3 +- src/Network/Http.cpp | 6 +++ src/Network/Resources.cpp | 1 - src/Network/VehicleData.cpp | 27 ++----------- src/Network/VehicleEvent.cpp | 75 ++++++++++++++++++++++-------------- src/Startup.cpp | 4 +- 8 files changed, 60 insertions(+), 98 deletions(-) delete mode 100644 include/Buffer.h diff --git a/include/Buffer.h b/include/Buffer.h deleted file mode 100644 index 9e2ab95..0000000 --- a/include/Buffer.h +++ /dev/null @@ -1,40 +0,0 @@ -/// -/// Created by Anonymous275 on 8/25/2020 -/// -#pragma once -void ServerParser(const std::string& Data); -class Buffer{ -public: - void Handle(const std::string& Data){ - Buf += Data; - Manage(); - } - void clear(){ - Buf.clear(); - } -private: - std::string Buf; - void Manage(){ - if(!Buf.empty()){ - std::string::size_type p; - if (Buf.at(0) == '\n'){ - p = Buf.find('\n',1); - if(p != -1){ - std::string R = Buf.substr(1,p-1); - std::string_view B(R.c_str(),R.find(char(0))); - ServerParser(B.data()); - Buf = Buf.substr(p+1); - Manage(); - } - }else{ - p = Buf.find('\n'); - if(p == -1)Buf.clear(); - else{ - Buf = Buf.substr(p); - Manage(); - } - } - } - } -}; - diff --git a/include/Network/network.h b/include/Network/network.h index bc8ae1f..5543b1a 100644 --- a/include/Network/network.h +++ b/include/Network/network.h @@ -3,13 +3,11 @@ /// #pragma once #include -#include "Buffer.h" void NetReset(); extern long long ping; extern bool Dev; void ClearAll(); extern int ClientID; -extern Buffer Handler; extern bool ModLoaded; extern bool Terminate; extern int DEFAULT_PORT; diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp index 00a3988..6b57509 100644 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -21,7 +21,7 @@ std::string MStatus; bool once = false; bool ModLoaded; long long ping = -1; -Buffer Handler; + void StartSync(const std::string &Data){ std::string IP = GetAddr(Data.substr(1,Data.find(':')-1)); if(IP.find('.') == -1){ @@ -35,7 +35,6 @@ void StartSync(const std::string &Data){ TCPTerminate = false; Terminate = false; ConfList->clear(); - Handler.clear(); ping = -1; std::thread GS(TCPGameServer,IP,std::stoi(Data.substr(Data.find(':')+1))); GS.detach(); diff --git a/src/Network/Http.cpp b/src/Network/Http.cpp index 3b65b1c..edbb797 100644 --- a/src/Network/Http.cpp +++ b/src/Network/Http.cpp @@ -6,11 +6,16 @@ #include "Security/Enc.h" #include #include +#include "Logger.h" +#include static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){ ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } + std::string HTTP_REQUEST(const std::string& IP,int port){ + static std::mutex Lock; + Lock.lock(); CURL *curl; CURLcode res; std::string readBuffer; @@ -27,6 +32,7 @@ std::string HTTP_REQUEST(const std::string& IP,int port){ if(res != CURLE_OK)return "-1"; } curl_global_cleanup(); + Lock.unlock(); return readBuffer; } diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 5774f68..209430f 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -261,7 +261,6 @@ void SyncResources(SOCKET Sock){ LFS.write(File, Recv); LFS.close(); } - ZeroMemory(File,Size); delete[] File; }while(fs::file_size(a) != std::stoi(*FS) && !Terminate); if(!Terminate)fs::copy_file(a,Sec("BeamNG/mods")+a.substr(a.find_last_of('/')), fs::copy_options::overwrite_existing); diff --git a/src/Network/VehicleData.cpp b/src/Network/VehicleData.cpp index c9bba0c..96dd88a 100644 --- a/src/Network/VehicleData.cpp +++ b/src/Network/VehicleData.cpp @@ -106,30 +106,11 @@ int SplitID(){ } void SendLarge(std::string Data){ Data = Data.substr(0,Data.find(char(0))); - int ID = PackID(); - 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); - BigDataAcks.insert(new PacketData{ID,Packet,1}); - UDPSend(Packet); - pckt = pckt.substr(1000); - S++; - ID = PackID(); - } - Packet = "SC|"+std::to_string(S)+"|"+std::to_string(Split)+"|"+ - std::to_string(ID)+"|"+std::to_string(SID)+"|"+pckt; - BigDataAcks.insert(new PacketData{ID,Packet,1}); - UDPSend(Packet); - }else{ - Packet = "BD:" + std::to_string(ID) + ":" + Data; - BigDataAcks.insert(new PacketData{ID,Packet,1}); - UDPSend(Packet); + if(Data.length() > 400){ + std::string CMP(Comp(Data)); + Data = "ABG:" + CMP; } + TCPSend(Data); } std::array HandledIDs = {-1}; int APos = 0; diff --git a/src/Network/VehicleEvent.cpp b/src/Network/VehicleEvent.cpp index 91e44a8..f84dd39 100644 --- a/src/Network/VehicleEvent.cpp +++ b/src/Network/VehicleEvent.cpp @@ -6,52 +6,71 @@ #include "Logger.h" #include #include +#include #include "Security/Enc.h" #include "Network/network.h" SOCKET TCPSock; +bool CheckBytes(int32_t Bytes){ + if (Bytes == 0){ + debug(Sec("(TCP) Connection closing...")); + Terminate = true; + return false; + }else if (Bytes < 0) { + debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError())); + closesocket(TCPSock); + Terminate = true; + return false; + } + return true; +} void TCPSend(const std::string&Data){ if(TCPSock == -1){ Terminate = true; return; } - std::string Send = "\n" + Data.substr(0,Data.find(char(0))) + "\n"; - size_t Sent = send(TCPSock, Send.c_str(), int(Send.size()), 0); - if (Sent == 0){ - debug(Sec("(TCP) Connection closing...")); - Terminate = true; - return; - } - else if (Sent < 0) { - debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError())); - closesocket(TCPSock); - Terminate = true; - return; - } + 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(TCPSock, &Send[Sent], Size - Sent, 0); + if(!CheckBytes(Temp))return; + Sent += Temp; + }while(Sent < Size); + } + void TCPRcv(){ - char buf[4096]; - int len = 4096; - ZeroMemory(buf, len); if(TCPSock == -1){ Terminate = true; return; } - int BytesRcv = recv(TCPSock, buf, len,0); - if (BytesRcv == 0){ - debug(Sec("(TCP) Connection closing...")); - Terminate = true; - return; + static int32_t Header,BytesRcv,Temp; + BytesRcv = recv(TCPSock, reinterpret_cast(&Header), sizeof(Header),0); + + if(!CheckBytes(BytesRcv))return; + char* Data = new char[Header]; + BytesRcv = 0; + do{ + Temp = recv(TCPSock,Data+BytesRcv,Header-BytesRcv,0); + if(!CheckBytes(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)); } - else if (BytesRcv < 0) { - debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError())); - closesocket(TCPSock); - Terminate = true; - return; - } - Handler.Handle(std::string(buf)); + ServerParser(Ret); } + void SyncResources(SOCKET TCPSock); void TCPClientMain(const std::string& IP,int Port){ WSADATA wsaData; diff --git a/src/Startup.cpp b/src/Startup.cpp index ac762b0..cf6b28e 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -20,11 +20,11 @@ std::string GetEN(){ return r; } std::string GetVer(){ - static std::string r = Sec("1.63"); + static std::string r = Sec("1.70"); return r; } std::string GetPatch(){ - static std::string r = Sec(".7"); + static std::string r = Sec(""); return r; } void ReLaunch(int argc,char*args[]){