Minor fixes

This commit is contained in:
Lion Kortlepel
2020-11-08 01:31:34 +01:00
parent fe6cfd027e
commit a42ab67d2f
3 changed files with 54 additions and 6 deletions

View File

@@ -42,6 +42,9 @@ void VehicleParser(Client*c,const std::string& Pckt){
std::string Data = Packet.substr(3),pid,vid;
switch(Code){ //Spawned Destroyed Switched/Moved NotFound Reset
case 's':
#ifdef DEBUG
debug(std::string(Sec("got 'Os' packet: '")) + Packet + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
if(Data.at(0) == '0'){
int CarID = c->GetOpenCarID();
debug(c->GetName() + Sec(" created a car with ID ") + std::to_string(CarID));
@@ -61,6 +64,9 @@ void VehicleParser(Client*c,const std::string& Pckt){
}
return;
case 'c':
#ifdef DEBUG
debug(std::string(Sec("got 'Oc' packet: '")) + Packet + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
pid = Data.substr(0,Data.find('-'));
vid = Data.substr(Data.find('-')+1,Data.find(':',1)-Data.find('-')-1);
if(pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos){
@@ -81,6 +87,9 @@ void VehicleParser(Client*c,const std::string& Pckt){
}
return;
case 'd':
#ifdef DEBUG
debug(std::string(Sec("got 'Od' packet: '")) + Packet + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
pid = Data.substr(0,Data.find('-'));
vid = Data.substr(Data.find('-')+1);
if(pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos){
@@ -96,11 +105,14 @@ void VehicleParser(Client*c,const std::string& Pckt){
}
return;
case 'r':
#ifdef DEBUG
debug(std::string(Sec("got 'Or' packet: '")) + Packet + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
SendToAll(c,Packet,false,true);
return;
default:
#ifdef DEBUG
warn(std::string(Sec("possibly not implemented: '") + Packet + Sec("'")));
warn(std::string(Sec("possibly not implemented: '") + Packet + Sec("' (") + std::to_string(Packet.size()) + Sec(")")));
#endif // DEBUG
return;
}
@@ -161,9 +173,8 @@ void HandleEvent(Client*c ,const std::string&Data){
void GlobalParser(Client*c, const std::string& Pack){
Assert(c);
[[maybe_unused]] static int lastRecv = 0;
if(Pack.empty() || c == nullptr)return;
std::string Packet = Pack.substr(0,Pack.find(char(0)));
std::string Packet = Pack.substr(0,strlen(Pack.c_str()));
std::string pct;
char Code = Packet.at(0);
@@ -176,6 +187,9 @@ void GlobalParser(Client*c, const std::string& Pack){
switch (Code) {
case 'P': // initial connection
#ifdef DEBUG
debug(std::string(Sec("got 'P' packet: '")) + Pack + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
Respond(c, Sec("P") + std::to_string(c->GetID()),true);
SyncClient(c);
return;
@@ -190,9 +204,15 @@ void GlobalParser(Client*c, const std::string& Pack){
ParseVeh(c,Packet);
return;
case 'J':
#ifdef DEBUG
debug(std::string(Sec("got 'J' packet: '")) + Pack + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
SendToAll(c,Packet,false,true);
return;
case 'C':
#ifdef DEBUG
debug(std::string(Sec("got 'C' packet: '")) + Pack + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
if(Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)break;
if (TriggerLuaEvent(Sec("onChatMessage"), false, nullptr,
std::unique_ptr<LuaArg>(new LuaArg{
@@ -201,6 +221,9 @@ void GlobalParser(Client*c, const std::string& Pack){
SendToAll(nullptr, Packet, true, true);
return;
case 'E':
#ifdef DEBUG
debug(std::string(Sec("got 'E' packet: '")) + Pack + Sec("' (") + std::to_string(Packet.size()) + Sec(")"));
#endif
HandleEvent(c,Packet);
return;
default:

View File

@@ -54,24 +54,43 @@ bool CheckBytes(Client*c,int32_t BytesRcv){
void TCPRcv(Client*c){
Assert(c);
static int32_t Header,BytesRcv,Temp;
static thread_local 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;
#ifdef DEBUG
//debug(std::string(__func__) + Sec(": expecting ") + std::to_string(Header) + Sec(" bytes."));
#endif // DEBUG
if(!CheckBytes(c,BytesRcv)) {
#ifdef DEBUG
error(std::string(__func__) + Sec(": failed on CheckBytes"));
#endif // DEBUG
return;
}
// TODO: std::vector
char* Data = new char[Header];
Assert(Data);
BytesRcv = 0;
do{
Temp = recv(c->GetTCPSock(), Data+BytesRcv, Header-BytesRcv,0);
if(!CheckBytes(c,Temp)){
#ifdef DEBUG
error(std::string(__func__) + Sec(": failed on CheckBytes in while(BytesRcv < Header)"));
#endif // DEBUG
delete[] Data;
return;
}
#ifdef DEBUG
//debug(std::string(__func__) + Sec(": Temp: ") + std::to_string(Temp) + Sec(", BytesRcv: ") + std::to_string(BytesRcv));
#endif // DEBUG
BytesRcv += Temp;
}while(BytesRcv < Header);
#ifdef DEBUG
//debug(std::string(__func__) + Sec(": finished recv with Temp: ") + std::to_string(Temp) + Sec(", BytesRcv: ") + std::to_string(BytesRcv));
#endif // DEBUG
std::string Ret = std::string(Data,Header);
delete[] Data;
if (Ret.substr(0, 4) == "ABG:") {

View File

@@ -45,7 +45,13 @@ std::string getDate() {
<< Hour << ":"
<< Min << ":"
<< Secs
<< "] ";
<< "] "
#ifdef DEBUG
<< std::this_thread::get_id()
<< " ";
#else
;
#endif
return date.str();
}
void InitLog() {