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

View File

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

View File

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

View File

@ -8,8 +8,6 @@
#include <fstream>
#include <any>
void GrabRole(Client*c);
void STCPSend(Client*c,std::any Data,size_t Size){
int BytesSent;
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);
}
if (BytesSent == 0){
std::cout << "(TCP) Connection closing..." << std::endl;
std::cout << "(STCPS) Connection closing..." << std::endl;
if(c->GetStatus() > -1)c->SetStatus(-1);
}
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);
closesocket(c->GetTCPSock());
}
@ -76,7 +74,9 @@ void Parse(Client*c,char*data){
case 'S':
if(SubCode == 'R'){
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;
}
@ -87,13 +87,13 @@ bool STCPRecv(Client*c){
ZeroMemory(buf, len);
int BytesRcv = recv(c->GetTCPSock(), buf, len,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);
closesocket(c->GetTCPSock());
return false;
}
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);
closesocket(c->GetTCPSock());
return false;

View File

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

View File

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

View File

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

View File

@ -22,18 +22,23 @@ std::string HTA(const std::string& hex)
}
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;
while(true)
{
while(true){
State = Private ? "true" : "false";
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+
"&clientversion="+ClientVersion+"&name="+ServerName+"&pps="+StatReport+"&modlist="+FileList+
"&modstotalsize="+std::to_string(MaxModSize)+"&modstotal="+std::to_string(ModsLoaded);
if(!CustomIP.empty())R+="&ip="+CustomIP;
// https://beamng-mp.com/heartbeatv2
//https://beamng-mp.com/heartbeatv2
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);
if(T.find_first_not_of("20") != std::string::npos){
//Backend system refused server startup!

View File

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