mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-04-19 06:09:53 +00:00
Mod Sync
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <WS2tcpip.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <set>
|
||||
|
||||
class Client {
|
||||
@@ -19,8 +20,6 @@ private:
|
||||
SOCKET TCPSOCK;
|
||||
int Status = 0;
|
||||
int ID = -1; //PlayerID
|
||||
|
||||
|
||||
public:
|
||||
std::set<std::pair<int,std::string>> GetAllCars();
|
||||
void AddNewCar(int ident,const std::string& Data);
|
||||
|
||||
@@ -37,7 +37,7 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
||||
for(Client*client : Clients){
|
||||
if(Self || client != c){
|
||||
if(Rel){
|
||||
if(C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
|
||||
if(C == 'C' || C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data);
|
||||
else TCPSend(client,Data);
|
||||
}
|
||||
else UDPSend(client,Data);
|
||||
@@ -53,7 +53,12 @@ void UpdatePlayers(){
|
||||
Packet = Packet.substr(0,Packet.length()-1);
|
||||
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;
|
||||
}
|
||||
void OnDisconnect(Client*c,bool kicked){
|
||||
std::string Packet;
|
||||
for(const std::pair<int,std::string>&a : c->GetAllCars()){
|
||||
@@ -64,13 +69,15 @@ void OnDisconnect(Client*c,bool kicked){
|
||||
Packet = "L"+c->GetName()+" Left the server!";
|
||||
SendToAll(c, Packet,false,true);
|
||||
Packet.clear();
|
||||
Clients.erase(c); ///Removes the Client from existence
|
||||
TriggerLuaEvent("onPlayerDisconnect",false,nullptr,new LuaArg{{c->GetID()}});
|
||||
Destroy(c); ///Removes the Client from existence
|
||||
}
|
||||
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller);
|
||||
void SyncResources(Client*c);
|
||||
void OnConnect(Client*c){
|
||||
c->SetID(OpenID());
|
||||
std::cout << "New Client Created! ID : " << c->GetID() << std::endl;
|
||||
Respond(c,"NR",true);
|
||||
TriggerLuaEvent("onPlayerConnecting",false,nullptr,new LuaArg{{c->GetID()}});
|
||||
SyncResources(c);
|
||||
Respond(c,"M"+MapName,true); //Send the Map on connect
|
||||
TriggerLuaEvent("onPlayerJoining",false,nullptr);
|
||||
TriggerLuaEvent("onPlayerJoining",false,nullptr,new LuaArg{{c->GetID()}});
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ void Respond(Client*c, const std::string& MSG, bool Rel);
|
||||
void UpdatePlayers();
|
||||
|
||||
|
||||
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller);
|
||||
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg);
|
||||
void VehicleParser(Client*c, std::string Packet){
|
||||
char Code = Packet.at(1);
|
||||
int PID = -1;
|
||||
@@ -23,11 +23,12 @@ void VehicleParser(Client*c, std::string Packet){
|
||||
switch(Code){ //Spawned Destroyed Switched/Moved NotFound Reset
|
||||
case 's':
|
||||
if(Data.at(0) == '0'){
|
||||
if(TriggerLuaEvent("onVehicleSpawn",false,nullptr))break;
|
||||
int CarID = c->GetOpenCarID();
|
||||
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);
|
||||
if(c->GetCarCount() >= MaxCars){
|
||||
if(TriggerLuaEvent("onVehicleSpawn",false,nullptr,
|
||||
new LuaArg{{c->GetID(),CarID,Packet.substr(3)}})
|
||||
|| c->GetCarCount() >= MaxCars){
|
||||
Respond(c,Packet,true);
|
||||
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(CarID);
|
||||
Respond(c,Destroy,true);
|
||||
@@ -37,6 +38,9 @@ void VehicleParser(Client*c, std::string Packet){
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
SendToAll(c,Packet,false,true);
|
||||
break;
|
||||
case 'd':
|
||||
pid = Data.substr(0,Data.find('-'));
|
||||
vid = Data.substr(Data.find('-')+1);
|
||||
@@ -49,17 +53,17 @@ void VehicleParser(Client*c, std::string Packet){
|
||||
c->DeleteCar(VID);
|
||||
}
|
||||
break;
|
||||
case 'm':
|
||||
break;
|
||||
case 'r':
|
||||
SendToAll(c,Packet,false,true);
|
||||
break;
|
||||
case 'm':
|
||||
break;
|
||||
}
|
||||
}
|
||||
void SyncVehicles(Client*c){
|
||||
Respond(c,"Sn"+c->GetName(),true);
|
||||
SendToAll(c,"JWelcome "+c->GetName()+"!",false,true);
|
||||
TriggerLuaEvent("onPlayerJoin",false,nullptr);
|
||||
TriggerLuaEvent("onPlayerJoin",false,nullptr,new LuaArg{{c->GetID()}});
|
||||
for (Client*client : Clients) {
|
||||
if (client != c) {
|
||||
for(const std::pair<int,std::string>&a : client->GetAllCars()){
|
||||
@@ -74,8 +78,10 @@ void HTTP(Client*c){
|
||||
std::string a = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+c->GetDID(),443);
|
||||
if(!a.empty()){
|
||||
int pos = a.find('"');
|
||||
c->SetRole(a.substr(pos+1,a.find('"',pos+1)-2));
|
||||
if(Debug)debug("ROLE -> " + c->GetRole() + " ID -> " + c->GetDID());
|
||||
if(c != nullptr){
|
||||
c->SetRole(a.substr(pos+1,a.find('"',pos+1)-2));
|
||||
if(Debug)debug("ROLE -> " + c->GetRole() + " ID -> " + c->GetDID());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +122,8 @@ void GlobalParser(Client*c, const std::string&Packet){
|
||||
SendToAll(c,Packet,false,true);
|
||||
break;
|
||||
case 'C':
|
||||
if(TriggerLuaEvent("onChatMessage",false,nullptr))break;
|
||||
if(TriggerLuaEvent("onChatMessage",false,nullptr,
|
||||
new LuaArg{{c->GetID(),c->GetName(),Packet.substr(Packet.find(':',3)+1)}}))break;
|
||||
SendToAll(nullptr,Packet,true,true);
|
||||
break;
|
||||
case 'E':
|
||||
|
||||
@@ -12,17 +12,16 @@ void TCPSend(Client*c,const std::string&Data){
|
||||
int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 0);
|
||||
if (BytesSent == 0){
|
||||
std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
c->SetStatus(-1);
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
}
|
||||
else if (BytesSent < 0) {
|
||||
std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl;
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
c->SetStatus(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalParser(Client*c, const std::string&Packet);
|
||||
|
||||
void TCPRcv(Client*c){
|
||||
char buf[4096];
|
||||
int len = 4096;
|
||||
@@ -30,12 +29,12 @@ void TCPRcv(Client*c){
|
||||
int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
c->SetStatus(-1);
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
}
|
||||
else if (BytesRcv < 0) {
|
||||
std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
c->SetStatus(-1);
|
||||
}
|
||||
GlobalParser(c, std::string(buf));
|
||||
}
|
||||
|
||||
118
src/Network 2.0/ResourceSync.cpp
Normal file
118
src/Network 2.0/ResourceSync.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 6/10/2020
|
||||
///
|
||||
#include <string>
|
||||
#include "../Settings.hpp"
|
||||
#include "Client.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
#include <any>
|
||||
|
||||
|
||||
void GrabRole(Client*c);
|
||||
|
||||
void STCPSend(Client*c,const std::any& Data,size_t Size){
|
||||
char *MSG;
|
||||
if(std::string(Data.type().name()).find("string") != std::string::npos){
|
||||
MSG = (char*)(std::any_cast<std::string>(Data).c_str());
|
||||
}else MSG = std::any_cast<char*>(Data);
|
||||
if(Size == 0)Size = strlen(MSG)+1;
|
||||
int BytesSent = send(c->GetTCPSock(), MSG, Size, 0);
|
||||
if (BytesSent == 0){
|
||||
std::cout << "(TCP) 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;
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
}
|
||||
}
|
||||
void SendFile(Client*c,const std::string&Name){
|
||||
std::cout << c->GetName() << " requesting : "
|
||||
<< Name.substr(Name.find_last_of('/')) << std::endl;
|
||||
struct stat Info{};
|
||||
if(stat(Name.c_str(), &Info) != 0){
|
||||
STCPSend(c,std::string("Cannot Open"),0);
|
||||
return;
|
||||
}
|
||||
std::ifstream f(Name.c_str(), std::ios::binary);
|
||||
f.seekg(0, std::ios_base::end);
|
||||
std::streampos fileSize = f.tellg();
|
||||
size_t Size = fileSize,Sent = 0,Diff;
|
||||
char* Data = new char[Size];
|
||||
f.seekg(0, std::ios_base::beg);
|
||||
f.read(Data, fileSize);
|
||||
f.close();
|
||||
char* Chunk;
|
||||
int Split = 64000;
|
||||
while(c->GetStatus() > -1 && Sent < Size){
|
||||
Diff = Size - Sent;
|
||||
if(Diff > Split){
|
||||
Chunk = new char[Split];
|
||||
memcpy_s(Chunk,Split,Data+Sent,Split);
|
||||
STCPSend(c,Chunk,Split);
|
||||
Sent += Split;
|
||||
}else{
|
||||
Chunk = new char[Diff];
|
||||
memcpy_s(Chunk,Diff,Data+Sent,Diff);
|
||||
STCPSend(c,Chunk,Diff);
|
||||
Sent += Diff;
|
||||
}
|
||||
}
|
||||
delete[] Data;
|
||||
delete[] Chunk;
|
||||
}
|
||||
|
||||
void Parse(Client*c,char*data){
|
||||
std::string Packet = data;
|
||||
if(Packet.empty())return;
|
||||
char Code = Packet.at(0),SubCode = 0;
|
||||
if(Packet.length() > 1)SubCode = Packet.at(1);
|
||||
switch (Code) {
|
||||
case 'f':
|
||||
SendFile(c,Packet.substr(1));
|
||||
return;
|
||||
case 'S':
|
||||
if(SubCode == 'R'){
|
||||
std::cout << "Sending File Info" << std::endl;
|
||||
STCPSend(c,FileList+FileSizes,0);
|
||||
}
|
||||
return;
|
||||
case 'N':
|
||||
if(SubCode == 'R'){
|
||||
c->SetName(Packet.substr(2,Packet.find(':')-2));
|
||||
c->SetDID(Packet.substr(Packet.find(':')+1));
|
||||
GrabRole(c);
|
||||
}
|
||||
std::cout << "Name : " << c->GetName() << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
bool STCPRecv(Client*c){
|
||||
char buf[200];
|
||||
int len = 200;
|
||||
ZeroMemory(buf, len);
|
||||
int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
return false;
|
||||
}
|
||||
else if (BytesRcv < 0) {
|
||||
std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
return false;
|
||||
}
|
||||
if(strcmp(buf,"Done") == 0)return false;
|
||||
char* Ret = new char[BytesRcv];
|
||||
memcpy_s(Ret,BytesRcv,buf,BytesRcv);
|
||||
ZeroMemory(buf, len);
|
||||
Parse(c,Ret);
|
||||
return true;
|
||||
}
|
||||
void SyncResources(Client*c){
|
||||
while(c->GetStatus() > -1 && STCPRecv(c));
|
||||
}
|
||||
Reference in New Issue
Block a user