Vehicle ghost fix, player list fix

This commit is contained in:
Anonymous275
2020-11-07 23:29:06 +02:00
parent 2021f0b461
commit a08d29a0ae
7 changed files with 84 additions and 125 deletions

View File

@@ -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 <thread>
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<int>(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<char*>(&Header), sizeof(Header),0);
#else
BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<void*>(&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);