stability improvements, tweaks

This commit is contained in:
Anonymous275 2020-06-27 16:58:02 +03:00
parent c5e1175f1a
commit 1b8c7abea5
9 changed files with 68 additions and 59 deletions

View File

@ -5,7 +5,6 @@
#include "../logger.h" #include "../logger.h"
#include "../Settings.hpp" #include "../Settings.hpp"
#include "../Lua System/LuaSystem.hpp" #include "../Lua System/LuaSystem.hpp"
#include <thread>
void UDPSend(Client*c,const std::string&Data); void UDPSend(Client*c,const std::string&Data);
void TCPSend(Client*c,const std::string&Data); void TCPSend(Client*c,const std::string&Data);
@ -56,7 +55,6 @@ void UpdatePlayers(){
SendToAll(nullptr, Packet,true,true); SendToAll(nullptr, Packet,true,true);
} }
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg); int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg);
void Destroy(Client*c){ void Destroy(Client*c){
Clients.erase(c); Clients.erase(c);
delete c; delete c;
@ -75,13 +73,13 @@ void OnDisconnect(Client*c,bool kicked){
Destroy(c); ///Removes the Client from existence Destroy(c); ///Removes the Client from existence
} }
void SyncResources(Client*c); void SyncResources(Client*c);
#include <thread>
void OnConnect(Client*c){ void OnConnect(Client*c){
c->SetID(OpenID()); c->SetID(OpenID());
std::cout << "New Client Created! ID : " << c->GetID() << std::endl; std::cout << "New Client Created! ID : " << c->GetID() << std::endl;
TriggerLuaEvent("onPlayerConnecting",false,nullptr,new LuaArg{{c->GetID()}}); TriggerLuaEvent("onPlayerConnecting",false,nullptr,new LuaArg{{c->GetID()}});
SyncResources(c); SyncResources(c);
Respond(c,"M"+MapName,true); //Send the Map on connect Respond(c,"M"+MapName,true); //Send the Map on connect
info(c->GetName() + " : Connected");
TriggerLuaEvent("onPlayerJoining",false,nullptr,new LuaArg{{c->GetID()}}); TriggerLuaEvent("onPlayerJoining",false,nullptr,new LuaArg{{c->GetID()}});
} }

View File

@ -1,7 +1,6 @@
/// ///
/// Created by Anonymous275 on 4/2/2020 /// Created by Anonymous275 on 4/2/2020
/// ///
#include <thread>
#include <iostream> #include <iostream>
#include "Client.hpp" #include "Client.hpp"
#include "../logger.h" #include "../logger.h"
@ -12,7 +11,6 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel);
void Respond(Client*c, const std::string& MSG, bool Rel); void Respond(Client*c, const std::string& MSG, bool Rel);
void UpdatePlayers(); void UpdatePlayers();
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg); int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg);
void VehicleParser(Client*c, std::string Packet){ void VehicleParser(Client*c, std::string Packet){
char Code = Packet.at(1); char Code = Packet.at(1);
@ -55,7 +53,9 @@ void VehicleParser(Client*c, std::string Packet){
case 'r': case 'r':
SendToAll(c,Packet,false,true); SendToAll(c,Packet,false,true);
break; break;
case 'm': //case 'm':
// break;
default:
break; break;
} }
} }
@ -77,8 +77,7 @@ extern int PPS;
void GlobalParser(Client*c, const std::string&Packet){ void GlobalParser(Client*c, const std::string&Packet){
if(Packet.empty())return; if(Packet.empty())return;
if(Packet.find("TEST")!=std::string::npos)SyncVehicles(c); if(Packet.find("TEST")!=std::string::npos)SyncVehicles(c);
char Code = Packet.at(0),SubCode = 0; char Code = Packet.at(0);
if(Packet.length() > 1)SubCode = Packet.at(1);
switch (Code) { switch (Code) {
case 'P': case 'P':
Respond(c, "P" + std::to_string(c->GetID()),true); Respond(c, "P" + std::to_string(c->GetID()),true);
@ -104,6 +103,8 @@ void GlobalParser(Client*c, const std::string&Packet){
case 'E': case 'E':
SendToAll(nullptr,Packet,true,true); SendToAll(nullptr,Packet,true,true);
break; break;
default:
break;
} }
//V to Z //V to Z
if(Code <= 90 && Code >= 86){ if(Code <= 90 && Code >= 86){

View File

@ -7,7 +7,6 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
void TCPSend(Client*c,const std::string&Data){ void TCPSend(Client*c,const std::string&Data){
int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 0); int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 0);
if (BytesSent == 0){ if (BytesSent == 0){

View File

@ -8,8 +8,6 @@
#include <fstream> #include <fstream>
#include <any> #include <any>
void GrabRole(Client*c);
void STCPSend(Client*c,std::any Data,size_t Size){ void STCPSend(Client*c,std::any Data,size_t Size){
int BytesSent; int BytesSent;
if(std::string(Data.type().name()).find("string") != std::string::npos){ if(std::string(Data.type().name()).find("string") != std::string::npos){
@ -19,11 +17,11 @@ void STCPSend(Client*c,std::any Data,size_t Size){
BytesSent = send(c->GetTCPSock(), std::any_cast<char*>(Data), Size, 0); BytesSent = send(c->GetTCPSock(), std::any_cast<char*>(Data), Size, 0);
} }
if (BytesSent == 0){ if (BytesSent == 0){
std::cout << "(TCP) Connection closing..." << std::endl; std::cout << "(STCPS) Connection closing..." << std::endl;
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);
} }
else if (BytesSent < 0) { else if (BytesSent < 0) {
std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl; std::cout << "(STCPS) send failed with error: " << WSAGetLastError() << std::endl;
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);
closesocket(c->GetTCPSock()); closesocket(c->GetTCPSock());
} }
@ -76,7 +74,9 @@ void Parse(Client*c,char*data){
case 'S': case 'S':
if(SubCode == 'R'){ if(SubCode == 'R'){
std::cout << "Sending File Info" << std::endl; std::cout << "Sending File Info" << std::endl;
STCPSend(c,std::string(FileList+FileSizes),0); std::string ToSend = FileList+FileSizes;
if(ToSend.empty())ToSend = "-";
STCPSend(c,ToSend,0);
} }
return; return;
} }
@ -87,13 +87,13 @@ bool STCPRecv(Client*c){
ZeroMemory(buf, len); ZeroMemory(buf, len);
int BytesRcv = recv(c->GetTCPSock(), buf, len,0); int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
if (BytesRcv == 0){ if (BytesRcv == 0){
std::cout << "(TCP) Connection closing..." << std::endl; std::cout << "(STCPR) Connection closing..." << std::endl;
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);
closesocket(c->GetTCPSock()); closesocket(c->GetTCPSock());
return false; return false;
} }
else if (BytesRcv < 0) { else if (BytesRcv < 0) {
std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl; std::cout << "(STCPR) recv failed with error: " << WSAGetLastError() << std::endl;
if(c->GetStatus() > -1)c->SetStatus(-1); if(c->GetStatus() > -1)c->SetStatus(-1);
closesocket(c->GetTCPSock()); closesocket(c->GetTCPSock());
return false; return false;

View File

@ -2,6 +2,7 @@
/// Created by Anonymous275 on 6/18/2020 /// Created by Anonymous275 on 6/18/2020
/// ///
#include "Client.hpp" #include "Client.hpp"
#include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
std::string StatReport = "-"; std::string StatReport = "-";
@ -12,7 +13,7 @@ int PPS = 0;
if(Clients.empty()){ if(Clients.empty()){
StatReport = "-"; StatReport = "-";
}else{ }else{
C = 0; C = 0;V = 0;
for(Client *c : Clients){ for(Client *c : Clients){
if(c->GetCarCount() > 0){ if(c->GetCarCount() > 0){
C++; C++;
@ -23,9 +24,10 @@ int PPS = 0;
StatReport = "-"; StatReport = "-";
}else{ }else{
R = (PPS/C)/V; R = (PPS/C)/V;
std::cout << PPS << std::endl;
StatReport = std::to_string(R); StatReport = std::to_string(R);
PPS = 0;
} }
PPS = 0;
} }
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }

View File

@ -17,8 +17,8 @@ struct PacketData{
int Tries; int Tries;
}; };
struct SplitData{ struct SplitData{
int Total; int Total{};
int ID; int ID{};
std::set<std::pair<int,std::string>> Fragments; std::set<std::pair<int,std::string>> Fragments;
}; };
@ -83,11 +83,10 @@ void SendLarge(Client*c,const std::string&Data){
struct HandledC{ struct HandledC{
int Pos = 0; int Pos = 0;
Client *c; Client *c{};
std::array<int, 50> HandledIDs; std::array<int, 50> HandledIDs{};
}; };
std::set<HandledC*> HandledIDs; std::set<HandledC*> HandledIDs;
void ResetIDs(HandledC*H){ void ResetIDs(HandledC*H){
for(int C = 0;C < 50;C++){ for(int C = 0;C < 50;C++){
H->HandledIDs.at(C) = -1; H->HandledIDs.at(C) = -1;
@ -149,15 +148,16 @@ SplitData*GetSplit(int SplitID){
for(SplitData* a : SplitPackets){ for(SplitData* a : SplitPackets){
if(a->ID == SplitID)return a; if(a->ID == SplitID)return a;
} }
SplitData* a = new SplitData(); auto* SP = new SplitData();
SplitPackets.insert(a); SplitPackets.insert(SP);
return a; return SP;
} }
void GlobalParser(Client*c, const std::string&Packet); void GlobalParser(Client*c, const std::string&Packet);
void HandleChunk(Client*c,const std::string&Data){ void HandleChunk(Client*c,const std::string&Data){
int pos1 = Data.find(':')+1,pos2 = Data.find(':',pos1),pos3 = Data.find('/'); int pos1 = int(Data.find(':'))+1,pos2 = Data.find(':',pos1),pos3 = Data.find('/');
int pos4 = Data.find('|'); int pos4 = Data.find('|');
if(pos1 == std::string::npos)return;
int Max = stoi(Data.substr(pos3+1,pos1-pos3-2)); int Max = stoi(Data.substr(pos3+1,pos1-pos3-2));
int Current = stoi(Data.substr(2,pos3-2)); int Current = stoi(Data.substr(2,pos3-2));
int ID = stoi(Data.substr(pos1,pos4-pos1)); int ID = stoi(Data.substr(pos1,pos4-pos1));
@ -171,7 +171,7 @@ void HandleChunk(Client*c,const std::string&Data){
SData->Fragments.insert(std::make_pair(Current,Data.substr(pos2+1))); SData->Fragments.insert(std::make_pair(Current,Data.substr(pos2+1)));
if(SData->Fragments.size() == SData->Total){ if(SData->Fragments.size() == SData->Total){
std::string ToHandle; std::string ToHandle;
for(std::pair<int,std::string> a : SData->Fragments){ for(const std::pair<int,std::string>& a : SData->Fragments){
ToHandle += a.second; ToHandle += a.second;
} }
GlobalParser(c,ToHandle); GlobalParser(c,ToHandle);

View File

@ -45,7 +45,7 @@ std::string HTTP(const std::string &DID){
int pos = a.find('"'); int pos = a.find('"');
if(pos != std::string::npos){ if(pos != std::string::npos){
return a.substr(pos+1,a.find('"',pos+1)-2); return a.substr(pos+1,a.find('"',pos+1)-2);
} }else if(a == "[]")return ""; //Member
} }
} }
return ""; return "";
@ -57,6 +57,13 @@ void Check(Sequence* S){
delete S; delete S;
} }
} }
int Max(){
int T = MaxPlayers;
for(Client*c : Clients){
if(c->GetRole() == "MDEV")T--;
}
return T;
}
void Identification(SOCKET TCPSock){ void Identification(SOCKET TCPSock){
auto* S = new Sequence; auto* S = new Sequence;
S->TCPSock = TCPSock; S->TCPSock = TCPSock;
@ -74,7 +81,10 @@ void Identification(SOCKET TCPSock){
closesocket(TCPSock); closesocket(TCPSock);
return; return;
} }
if(Res.size() > 3 && Res.substr(0,2) == "NR"){ if(Res.size() > 3 && Res.substr(0,2) != "NR") {
closesocket(TCPSock);
return;
}
if(Res.find(':') == std::string::npos){ if(Res.find(':') == std::string::npos){
closesocket(TCPSock); closesocket(TCPSock);
return; return;
@ -91,11 +101,7 @@ void Identification(SOCKET TCPSock){
CreateClient(TCPSock,Name,DID,Role); CreateClient(TCPSock,Name,DID,Role);
return; return;
} }
}else{ if(Clients.size() < Max())CreateClient(TCPSock,Name,DID,Role);
closesocket(TCPSock);
return;
}
if(Clients.size() < MaxPlayers)CreateClient(TCPSock,Name,DID,Role);
} }
void TCPServerMain(){ void TCPServerMain(){

View File

@ -22,11 +22,16 @@ std::string HTA(const std::string& hex)
} }
return ascii; return ascii;
} }
void Heartbeat() std::string GetPlayers(){
{ std::string Return;
for(Client* c : Clients){
Return += c->GetName() + ";";
}
return Return;
}
void Heartbeat(){
std::string State,R,T; std::string State,R,T;
while(true) while(true){
{
State = Private ? "true" : "false"; State = Private ? "true" : "false";
R = "uuid="+Key+"&players="+std::to_string(Clients.size())+"&maxplayers="+std::to_string(MaxPlayers)+"&port=" R = "uuid="+Key+"&players="+std::to_string(Clients.size())+"&maxplayers="+std::to_string(MaxPlayers)+"&port="
+ std::to_string(Port) + "&map=" + MapName + "&private="+State+"&version="+ServerVersion+ + std::to_string(Port) + "&map=" + MapName + "&private="+State+"&version="+ServerVersion+

View File

@ -2,11 +2,9 @@
/// Created by Anonymous275 on 28/01/2020 /// Created by Anonymous275 on 28/01/2020
/// ///
#include <iostream>
#include <string> #include <string>
#include <fstream>
#include <chrono> #include <chrono>
#include <thread> #include <fstream>
#include "logger.h" #include "logger.h"
#include <algorithm> #include <algorithm>
#include "Settings.hpp" #include "Settings.hpp"
@ -17,8 +15,8 @@ void ParseConfig();
void addToLog(const std::string& Data); void addToLog(const std::string& Data);
//void ServerMain(int Port, int MaxClients); //void ServerMain(int Port, int MaxClients);
void HeartbeatInit(); void HeartbeatInit();
std::string ServerVersion = "0.4"; std::string ServerVersion = "0.42";
std::string ClientVersion = "1.4"; std::string ClientVersion = "1.41";
std::string CustomIP; std::string CustomIP;
void HandleResources(std::string path); void HandleResources(std::string path);
void StatInit(); void StatInit();