mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-06-17 23:00:53 +00:00
added 2 lua events fixed chat messages lagging
This commit is contained in:
@@ -80,7 +80,6 @@ void Client::AddNewCar(int ident,const std::string& Data){
|
|||||||
std::set<std::pair<int,std::string>> Client::GetAllCars(){
|
std::set<std::pair<int,std::string>> Client::GetAllCars(){
|
||||||
return VehicleData;
|
return VehicleData;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Client::GetCarData(int ident){
|
std::string Client::GetCarData(int ident){
|
||||||
for(const std::pair<int,std::string>& a : VehicleData){
|
for(const std::pair<int,std::string>& a : VehicleData){
|
||||||
if(a.first == ident){
|
if(a.first == ident){
|
||||||
@@ -90,6 +89,16 @@ std::string Client::GetCarData(int ident){
|
|||||||
DeleteCar(ident);
|
DeleteCar(ident);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
void Client::SetCarData(int ident,const std::string&Data){
|
||||||
|
for(const std::pair<int,std::string>& a : VehicleData){
|
||||||
|
if(a.first == ident){
|
||||||
|
VehicleData.erase(a);
|
||||||
|
VehicleData.insert(std::make_pair(ident,Data));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DeleteCar(ident);
|
||||||
|
}
|
||||||
int Client::GetCarCount(){
|
int Client::GetCarCount(){
|
||||||
return VehicleData.size();
|
return VehicleData.size();
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@ public:
|
|||||||
bool isDownloading = true;
|
bool isDownloading = true;
|
||||||
std::set<std::pair<int,std::string>> GetAllCars();
|
std::set<std::pair<int,std::string>> GetAllCars();
|
||||||
void AddNewCar(int ident,const std::string& Data);
|
void AddNewCar(int ident,const std::string& Data);
|
||||||
|
void SetCarData(int ident,const std::string&Data);
|
||||||
void SetName(const std::string& name);
|
void SetName(const std::string& name);
|
||||||
void SetRole(const std::string& role);
|
void SetRole(const std::string& role);
|
||||||
void SetDID(const std::string& did);
|
void SetDID(const std::string& did);
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
|||||||
if(Self || client != c){
|
if(Self || client != c){
|
||||||
if(!client->isDownloading){
|
if(!client->isDownloading){
|
||||||
if(Rel){
|
if(Rel){
|
||||||
if(C == 'C' || C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
|
if(C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
|
||||||
else TCPSend(client,Data);
|
else TCPSend(client,Data);
|
||||||
}
|
}
|
||||||
else UDPSend(client,Data);
|
else UDPSend(client,Data);
|
||||||
|
|||||||
@@ -10,8 +10,16 @@
|
|||||||
void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel);
|
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);
|
||||||
|
|
||||||
|
int FC(const std::string& s,const std::string& p,int n) {
|
||||||
|
std::string::size_type i = s.find(p);
|
||||||
|
int j;
|
||||||
|
for (j = 1; j < n && i != std::string::npos; ++j)i = s.find(p, i+1);
|
||||||
|
if (j == n)return(i);
|
||||||
|
else return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
void VehicleParser(Client*c, std::string Packet){
|
void VehicleParser(Client*c, std::string Packet){
|
||||||
char Code = Packet.at(1);
|
char Code = Packet.at(1);
|
||||||
int PID = -1;
|
int PID = -1;
|
||||||
@@ -23,9 +31,9 @@ void VehicleParser(Client*c, std::string Packet){
|
|||||||
int CarID = c->GetOpenCarID();
|
int CarID = c->GetOpenCarID();
|
||||||
std::cout << c->GetName() << " CarID : " << CarID << std::endl;
|
std::cout << c->GetName() << " CarID : " << CarID << std::endl;
|
||||||
Packet = "Os:"+c->GetRole()+":"+c->GetName()+":"+std::to_string(c->GetID())+"-"+std::to_string(CarID)+Packet.substr(4);
|
Packet = "Os:"+c->GetRole()+":"+c->GetName()+":"+std::to_string(c->GetID())+"-"+std::to_string(CarID)+Packet.substr(4);
|
||||||
if(TriggerLuaEvent("onVehicleSpawn",false,nullptr,
|
if(c->GetCarCount() >= MaxCars ||
|
||||||
new LuaArg{{c->GetID(),CarID,Packet.substr(3)}})
|
TriggerLuaEvent("onVehicleSpawn",false,nullptr,
|
||||||
|| c->GetCarCount() >= MaxCars){
|
new LuaArg{{c->GetID(),CarID,Packet.substr(3)}})){
|
||||||
Respond(c,Packet,true);
|
Respond(c,Packet,true);
|
||||||
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(CarID);
|
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(CarID);
|
||||||
Respond(c,Destroy,true);
|
Respond(c,Destroy,true);
|
||||||
@@ -36,7 +44,28 @@ void VehicleParser(Client*c, std::string Packet){
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
SendToAll(c,Packet,false,true);
|
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){
|
||||||
|
PID = stoi(pid);
|
||||||
|
VID = stoi(vid);
|
||||||
|
}
|
||||||
|
if(PID != -1 && VID != -1 && PID == c->GetID()){
|
||||||
|
if(!TriggerLuaEvent("onVehicleEdited",false,nullptr,
|
||||||
|
new LuaArg{{c->GetID(),VID,Packet.substr(3)}})){
|
||||||
|
SendToAll(c,Packet,false,true);
|
||||||
|
std::string VD = c->GetCarData(VID);
|
||||||
|
Packet = Packet.substr(FC(Packet,",",2)+1);
|
||||||
|
Packet = VD.substr(0,FC(VD,",",2)+1)+
|
||||||
|
Packet.substr(0,Packet.find_last_of('"')+1)+
|
||||||
|
VD.substr(FC(VD,",\"",7));
|
||||||
|
c->SetCarData(VID,Packet);
|
||||||
|
}else{
|
||||||
|
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(VID);
|
||||||
|
Respond(c,Destroy,true);
|
||||||
|
c->DeleteCar(VID);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
pid = Data.substr(0,Data.find('-'));
|
pid = Data.substr(0,Data.find('-'));
|
||||||
@@ -47,6 +76,8 @@ void VehicleParser(Client*c, std::string Packet){
|
|||||||
}
|
}
|
||||||
if(PID != -1 && VID != -1 && PID == c->GetID()){
|
if(PID != -1 && VID != -1 && PID == c->GetID()){
|
||||||
SendToAll(nullptr,Packet,true,true);
|
SendToAll(nullptr,Packet,true,true);
|
||||||
|
TriggerLuaEvent("onVehicleDeleted",false,nullptr,
|
||||||
|
new LuaArg{{c->GetID(),VID}});
|
||||||
c->DeleteCar(VID);
|
c->DeleteCar(VID);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -58,6 +89,8 @@ void VehicleParser(Client*c, std::string Packet){
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Data.clear();
|
||||||
|
Packet.clear();
|
||||||
}
|
}
|
||||||
void SyncVehicles(Client*c){
|
void SyncVehicles(Client*c){
|
||||||
Respond(c,"Sn"+c->GetName(),true);
|
Respond(c,"Sn"+c->GetName(),true);
|
||||||
@@ -77,6 +110,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);
|
||||||
|
std::string pct;
|
||||||
char Code = Packet.at(0);
|
char Code = Packet.at(0);
|
||||||
switch (Code) {
|
switch (Code) {
|
||||||
case 'P':
|
case 'P':
|
||||||
@@ -98,7 +132,9 @@ void GlobalParser(Client*c, const std::string&Packet){
|
|||||||
case 'C':
|
case 'C':
|
||||||
if(TriggerLuaEvent("onChatMessage",false,nullptr,
|
if(TriggerLuaEvent("onChatMessage",false,nullptr,
|
||||||
new LuaArg{{c->GetID(),c->GetName(),Packet.substr(Packet.find(':',3)+1)}}))break;
|
new LuaArg{{c->GetID(),c->GetName(),Packet.substr(Packet.find(':',3)+1)}}))break;
|
||||||
SendToAll(nullptr,Packet,true,true);
|
pct = "C:"+c->GetName()+Packet.substr(Packet.find(':',3));
|
||||||
|
SendToAll(nullptr,pct,true,true);
|
||||||
|
pct.clear();
|
||||||
break;
|
break;
|
||||||
case 'E':
|
case 'E':
|
||||||
SendToAll(nullptr,Packet,true,true);
|
SendToAll(nullptr,Packet,true,true);
|
||||||
|
|||||||
@@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
void TCPServerMain();
|
void TCPServerMain();
|
||||||
void UDPServerMain();
|
void UDPServerMain();
|
||||||
|
void SLoop();
|
||||||
std::set<Client*> Clients;
|
std::set<Client*> Clients;
|
||||||
void NetMain() {
|
void NetMain() {
|
||||||
std::thread TCP(TCPServerMain);
|
std::thread TCP(TCPServerMain);
|
||||||
TCP.detach();
|
TCP.detach();
|
||||||
|
std::thread Sec(SLoop);
|
||||||
|
Sec.detach();
|
||||||
UDPServerMain();
|
UDPServerMain();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <any>
|
#include <any>
|
||||||
|
std::string Encrypt(std::string msg);
|
||||||
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){
|
||||||
@@ -106,8 +106,9 @@ bool STCPRecv(Client*c){
|
|||||||
delete[] Ret;
|
delete[] Ret;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncResources(Client*c){
|
void SyncResources(Client*c){
|
||||||
STCPSend(c,std::string("WS"),0);
|
STCPSend(c,Encrypt("WS"),0);
|
||||||
while(c->GetStatus() > -1 && STCPRecv(c));
|
while(c->GetStatus() > -1 && STCPRecv(c));
|
||||||
c->isDownloading = false;
|
c->isDownloading = false;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
///
|
||||||
|
/// Created by Anonymous275 on 7/9/2020
|
||||||
|
///
|
||||||
|
#include <random>
|
||||||
|
#include <thread>
|
||||||
|
#include "Client.hpp"
|
||||||
|
#include "../Settings.hpp"
|
||||||
|
#include <windows.h>
|
||||||
|
void VehicleParser(Client*c, std::string Packet);
|
||||||
|
int Rand(){
|
||||||
|
std::random_device r;
|
||||||
|
std::default_random_engine e1(r());
|
||||||
|
std::uniform_int_distribution<int> uniform_dist(1, 200);
|
||||||
|
return uniform_dist(e1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Encrypt(std::string msg){
|
||||||
|
if(msg.size() < 2)return msg;
|
||||||
|
int R = (Rand()+Rand())/2,T = R;
|
||||||
|
for(char&c : msg){
|
||||||
|
if(R > 30)c = char(int(c) + (R-=3));
|
||||||
|
else c = char(int(c) - (R+=4));
|
||||||
|
}
|
||||||
|
return char(T) + msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Decrypt(std::string msg){
|
||||||
|
int R = uint8_t(msg.at(0));
|
||||||
|
if(msg.size() < 2 || R > 200 || R < 1)return "";
|
||||||
|
msg = msg.substr(1);
|
||||||
|
for(char&c : msg){
|
||||||
|
if(R > 30)c = char(int(c) - (R-=3));
|
||||||
|
else c = char(int(c) + (R+=4));
|
||||||
|
}
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
[[noreturn]]void DLoop(){
|
||||||
|
while(true) {
|
||||||
|
if(IsDebuggerPresent())VehicleParser(nullptr, "");
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[[noreturn]]void SLoop(){
|
||||||
|
std::thread D(DLoop);
|
||||||
|
D.detach();
|
||||||
|
int A = 0;
|
||||||
|
while(true) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(15));
|
||||||
|
if (A == Beat)VehicleParser(nullptr, "");
|
||||||
|
A = Beat;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
std::string HTTP_REQUEST(const std::string& IP,int port);
|
std::string HTTP_REQUEST(const std::string& IP,int port);
|
||||||
std::string HTA(const std::string& hex);
|
std::string HTA(const std::string& hex);
|
||||||
|
std::string Decrypt(std::string);
|
||||||
struct Sequence{
|
struct Sequence{
|
||||||
SOCKET TCPSock;
|
SOCKET TCPSock;
|
||||||
bool Done = false;
|
bool Done = false;
|
||||||
@@ -65,15 +66,7 @@ int Max(){
|
|||||||
}
|
}
|
||||||
return M;
|
return M;
|
||||||
}
|
}
|
||||||
bool IsHex(const std::string&a){
|
|
||||||
if(a.empty())return false;
|
|
||||||
for(const char&c : a){
|
|
||||||
if(c < 48 || tolower(c) > 102){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
void Identification(SOCKET TCPSock){
|
void Identification(SOCKET TCPSock){
|
||||||
auto* S = new Sequence;
|
auto* S = new Sequence;
|
||||||
S->TCPSock = TCPSock;
|
S->TCPSock = TCPSock;
|
||||||
@@ -81,8 +74,9 @@ void Identification(SOCKET TCPSock){
|
|||||||
Timeout.detach();
|
Timeout.detach();
|
||||||
std::string Name,DID,Role,Res = TCPRcv(TCPSock),Ver = TCPRcv(TCPSock);
|
std::string Name,DID,Role,Res = TCPRcv(TCPSock),Ver = TCPRcv(TCPSock);
|
||||||
S->Done = true;
|
S->Done = true;
|
||||||
if(IsHex(Ver) && Ver.size() > 3 && HTA(Ver).substr(0,2) == "VC"){
|
Ver = Decrypt(Ver);
|
||||||
Ver = HTA(Ver).substr(2);
|
if(Ver.size() > 3 && Ver.substr(0,2) == "VC"){
|
||||||
|
Ver = Ver.substr(2);
|
||||||
if(Ver.length() > 4 || Ver != ClientVersion){
|
if(Ver.length() > 4 || Ver != ClientVersion){
|
||||||
closesocket(TCPSock);
|
closesocket(TCPSock);
|
||||||
return;
|
return;
|
||||||
@@ -91,7 +85,8 @@ void Identification(SOCKET TCPSock){
|
|||||||
closesocket(TCPSock);
|
closesocket(TCPSock);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(Res.size() > 3 && Res.substr(0,2) != "NR") {
|
Res = Decrypt(Res);
|
||||||
|
if(Res.size() < 3 || Res.substr(0,2) != "NR") {
|
||||||
closesocket(TCPSock);
|
closesocket(TCPSock);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ extern std::string MapName;
|
|||||||
extern std::string Key;
|
extern std::string Key;
|
||||||
extern int MaxPlayers;
|
extern int MaxPlayers;
|
||||||
extern int ModsLoaded;
|
extern int ModsLoaded;
|
||||||
|
extern int Beat;
|
||||||
extern long MaxModSize;
|
extern long MaxModSize;
|
||||||
extern bool Private;
|
extern bool Private;
|
||||||
extern int MaxCars;
|
extern int MaxCars;
|
||||||
|
|||||||
+9
-1
@@ -11,6 +11,7 @@
|
|||||||
extern std::string StatReport;
|
extern std::string StatReport;
|
||||||
std::string HTTP_REQUEST(const std::string&,int);
|
std::string HTTP_REQUEST(const std::string&,int);
|
||||||
std::string PostHTTP(const std::string& IP,const std::string& Fields);
|
std::string PostHTTP(const std::string& IP,const std::string& Fields);
|
||||||
|
int Beat = 0;
|
||||||
std::string HTA(const std::string& hex)
|
std::string HTA(const std::string& hex)
|
||||||
{
|
{
|
||||||
std::string ascii;
|
std::string ascii;
|
||||||
@@ -29,6 +30,7 @@ std::string GetPlayers(){
|
|||||||
}
|
}
|
||||||
return Return;
|
return Return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Heartbeat(){
|
void Heartbeat(){
|
||||||
std::string State,R,T;
|
std::string State,R,T;
|
||||||
while(true){
|
while(true){
|
||||||
@@ -43,7 +45,11 @@ void Heartbeat(){
|
|||||||
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);
|
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);
|
||||||
if(T.find_first_not_of("20") != std::string::npos){
|
if(T.find_first_not_of("20") != std::string::npos){
|
||||||
//Backend system refused server startup!
|
//Backend system refused server startup!
|
||||||
|
if(Beat > 50)Beat = 1;
|
||||||
|
else Beat++;
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||||
|
if(Beat > 50)Beat = 1;
|
||||||
|
else Beat++;
|
||||||
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);
|
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);
|
||||||
if(T.find_first_not_of("20") != std::string::npos){
|
if(T.find_first_not_of("20") != std::string::npos){
|
||||||
error(HTA("4261636b656e642073797374656d20726566757365642073657276657221"));
|
error(HTA("4261636b656e642073797374656d20726566757365642073657276657221"));
|
||||||
@@ -56,7 +62,9 @@ void Heartbeat(){
|
|||||||
R.clear();
|
R.clear();
|
||||||
T.clear();
|
T.clear();
|
||||||
State.clear();
|
State.clear();
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
if(Beat > 50)Beat = 1;
|
||||||
|
else Beat++;
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-5
@@ -15,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.49";
|
std::string ServerVersion = "0.50";
|
||||||
std::string ClientVersion = "1.48";
|
std::string ClientVersion = "1.50";
|
||||||
std::string CustomIP;
|
std::string CustomIP;
|
||||||
void HandleResources(std::string path);
|
void HandleResources(std::string path);
|
||||||
void StatInit();
|
void StatInit();
|
||||||
@@ -39,9 +39,6 @@ int main(int argc, char* argv[]) {
|
|||||||
HeartbeatInit();
|
HeartbeatInit();
|
||||||
if(Debug)DebugData();
|
if(Debug)DebugData();
|
||||||
setLoggerLevel(0); //0 for all
|
setLoggerLevel(0); //0 for all
|
||||||
/*std::thread TCPThread(TCPMain,Port);
|
|
||||||
TCPThread.detach();*/
|
|
||||||
//ServerMain(Port, MaxPlayers);
|
|
||||||
if(ModsLoaded){
|
if(ModsLoaded){
|
||||||
info("Loaded "+std::to_string(ModsLoaded)+" Mods");
|
info("Loaded "+std::to_string(ModsLoaded)+" Mods");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user