mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 10:41:01 +00:00
Crash Handeling
This commit is contained in:
@@ -50,10 +50,10 @@ void FolderList(const std::string& Path,bool HotSwap){
|
||||
break;
|
||||
}
|
||||
if(Script->GetLastWrite() != fs::last_write_time(Script->GetFileName())){
|
||||
info("[HOTSWAP] Updated : "+
|
||||
Script->GetFileName().substr(Script->GetFileName().find('\\')));
|
||||
Script->SetLastWrite(fs::last_write_time(Script->GetFileName()));
|
||||
Script->Reload();
|
||||
info("[HOTSWAP] Updated : "+
|
||||
Script->GetFileName().substr(Script->GetFileName().find('\\')));
|
||||
}
|
||||
}
|
||||
FolderList(path,true);
|
||||
|
||||
@@ -24,6 +24,7 @@ LuaArg* CreateArg(lua_State *L,int T){
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg){
|
||||
int R = 0;
|
||||
for(Lua*Script : PluginEngine){
|
||||
@@ -32,7 +33,7 @@ int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg){
|
||||
if (Script->GetPluginName() == Caller->GetPluginName()){
|
||||
R += Script->CallFunction(Script->GetRegistered(Event),arg);
|
||||
}
|
||||
}else R += Script->CallFunction(Script->GetRegistered(Event),arg);
|
||||
}else R += Script->CallFunction(Script->GetRegistered(Event), arg);
|
||||
}
|
||||
}
|
||||
return R;
|
||||
@@ -102,15 +103,7 @@ void CallAsync(Lua* lua,const std::string& FuncName,LuaArg* args){
|
||||
}
|
||||
lua->HasThread = true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
lua_getglobal(lua->GetState(), FuncName.c_str());
|
||||
if(lua_isfunction(lua->GetState(), -1)) {
|
||||
int Size = 0;
|
||||
if(args != nullptr){
|
||||
Size = args->args.size();
|
||||
args->PushArgs(lua->GetState());
|
||||
}
|
||||
CheckLua(lua->GetState(), lua_pcall(lua->GetState(), Size, 0, 0));
|
||||
}
|
||||
lua->CallFunction(FuncName,args);
|
||||
lua->HasThread = false;
|
||||
}
|
||||
int lua_CreateThread(lua_State *L){
|
||||
@@ -120,8 +113,6 @@ int lua_CreateThread(lua_State *L){
|
||||
std::string STR = lua_tostring(L,1);
|
||||
std::thread t1(CallAsync,GetScript(L),STR,CreateArg(L,Args));
|
||||
t1.detach();
|
||||
//auto s = std::async();
|
||||
///TODO FIGURE OUT THREAD
|
||||
}else SendError(L,"CreateThread wrong argument [1] need string");
|
||||
}else SendError(L,"CreateThread not enough arguments");
|
||||
return 0;
|
||||
@@ -305,7 +296,14 @@ void Lua::Reload(){
|
||||
CallFunction("onInit",{});
|
||||
}
|
||||
}
|
||||
|
||||
int Handle(EXCEPTION_POINTERS *ep,char* Origin);
|
||||
char* Lua::GetOrigin(){
|
||||
std::string T = GetFileName().substr(GetFileName().find('\\'));
|
||||
char* Data = new char[T.size()];
|
||||
ZeroMemory(Data,T.size());
|
||||
memcpy_s(Data,T.size(),T.c_str(),T.size());
|
||||
return Data;
|
||||
}
|
||||
int Lua::CallFunction(const std::string& FuncName,LuaArg* Arg){
|
||||
lua_getglobal(luaState, FuncName.c_str());
|
||||
if(lua_isfunction(luaState, -1)) {
|
||||
@@ -314,7 +312,13 @@ int Lua::CallFunction(const std::string& FuncName,LuaArg* Arg){
|
||||
Size = Arg->args.size();
|
||||
Arg->PushArgs(luaState);
|
||||
}
|
||||
if (CheckLua(luaState, lua_pcall(luaState, Size, 1, 0))) {
|
||||
int R = 0;
|
||||
char* Origin = GetOrigin();
|
||||
__try{
|
||||
R = lua_pcall(luaState, Size, 1, 0);
|
||||
}__except(Handle(GetExceptionInformation(),Origin)){}
|
||||
delete [] Origin;
|
||||
if (CheckLua(luaState, R)){
|
||||
if (lua_isnumber(luaState, -1)) {
|
||||
return lua_tointeger(luaState, -1);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ public:
|
||||
std::string GetFileName();
|
||||
bool HasThread = false;
|
||||
lua_State* GetState();
|
||||
char* GetOrigin();
|
||||
void Reload();
|
||||
void Init();
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ void SendLarge(Client*c,const std::string&Data);
|
||||
void Respond(Client*c, const std::string& MSG, bool Rel){
|
||||
char C = MSG.at(0);
|
||||
if(Rel){
|
||||
if(C == 'O' || C == 'T' || MSG.length() > 1000)SendLarge(c,MSG);
|
||||
if(C == 'C' || C == 'O' || C == 'T' || MSG.length() > 1000)SendLarge(c,MSG);
|
||||
else TCPSend(c,MSG);
|
||||
}else UDPSend(c,MSG);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
||||
if(Self || client != c){
|
||||
if(!client->isDownloading){
|
||||
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);
|
||||
|
||||
@@ -19,8 +19,24 @@ int FC(const std::string& s,const std::string& p,int n) {
|
||||
if (j == n)return(i);
|
||||
else return(-1);
|
||||
}
|
||||
int Handle(EXCEPTION_POINTERS *ep,char* Origin);
|
||||
|
||||
void VehicleParser(Client*c, std::string Packet){
|
||||
void Apply(Client*c,int VID,const std::string& pckt){
|
||||
std::string Packet = pckt;
|
||||
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);
|
||||
}
|
||||
void UpdateCarData(Client*c,int VID,const std::string& Packet){
|
||||
__try{
|
||||
Apply(c,VID,Packet);
|
||||
}__except(Handle(GetExceptionInformation(),(char*)"Car Data Updater")){}
|
||||
}
|
||||
void VehicleParser(Client*c,const std::string& Pckt){
|
||||
std::string Packet = Pckt;
|
||||
char Code = Packet.at(1);
|
||||
int PID = -1;
|
||||
int VID = -1;
|
||||
@@ -52,19 +68,15 @@ void VehicleParser(Client*c, std::string Packet){
|
||||
}
|
||||
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);
|
||||
new LuaArg{{c->GetID(),VID,Packet.substr(3)}})) {
|
||||
SendToAll(c, Packet, false, true);
|
||||
UpdateCarData(c,VID,Packet);
|
||||
}else{
|
||||
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(VID);
|
||||
Respond(c,Destroy,true);
|
||||
c->DeleteCar(VID);
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
@@ -105,8 +117,14 @@ void SyncVehicles(Client*c){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extern int PPS;
|
||||
|
||||
void ParseVeh(Client*c, const std::string&Packet){
|
||||
__try{
|
||||
VehicleParser(c,Packet);
|
||||
}__except(Handle(GetExceptionInformation(),(char*)"Vehicle Handler")){}
|
||||
}
|
||||
|
||||
void GlobalParser(Client*c, const std::string&Packet){
|
||||
if(Packet.empty())return;
|
||||
if(Packet.find("TEST")!=std::string::npos)SyncVehicles(c);
|
||||
@@ -124,16 +142,16 @@ void GlobalParser(Client*c, const std::string&Packet){
|
||||
if(Packet.length() > 1000) {
|
||||
std::cout << "Received data from: " << c->GetName() << " Size: " << Packet.length() << std::endl;
|
||||
}
|
||||
VehicleParser(c,Packet);
|
||||
ParseVeh(c,Packet);
|
||||
return;
|
||||
case 'J':
|
||||
SendToAll(c,Packet,false,true);
|
||||
break;
|
||||
case 'C':
|
||||
if(TriggerLuaEvent("onChatMessage",false,nullptr,
|
||||
new LuaArg{{c->GetID(),c->GetName(),Packet.substr(Packet.find(':',3)+1)}}))break;
|
||||
pct = "C:"+c->GetName()+Packet.substr(Packet.find(':',3));
|
||||
SendToAll(nullptr,pct,true,true);
|
||||
pct = "C:" + c->GetName() + Packet.substr(Packet.find(':', 3));
|
||||
if (TriggerLuaEvent("onChatMessage", false, nullptr,
|
||||
new LuaArg{{ c->GetID(), c->GetName(), pct.substr(pct.find(':', 3) + 1) }}))break;
|
||||
SendToAll(nullptr, pct, true, true);
|
||||
pct.clear();
|
||||
break;
|
||||
case 'E':
|
||||
@@ -148,4 +166,4 @@ void GlobalParser(Client*c, const std::string&Packet){
|
||||
SendToAll(c,Packet,false,false);
|
||||
}
|
||||
if(Debug)debug("Vehicle Data Received from " + c->GetName());
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,10 @@
|
||||
#include "Client.hpp"
|
||||
#include <thread>
|
||||
|
||||
void TCPServerMain();
|
||||
void UDPServerMain();
|
||||
void SLoop();
|
||||
std::set<Client*> Clients;
|
||||
void NetMain() {
|
||||
std::thread TCP(TCPServerMain);
|
||||
TCP.detach();
|
||||
std::thread Sec(SLoop);
|
||||
Sec.detach();
|
||||
UDPServerMain();
|
||||
}
|
||||
|
||||
@@ -4,9 +4,14 @@
|
||||
#include <random>
|
||||
#include <thread>
|
||||
#include "Client.hpp"
|
||||
#include "../logger.h"
|
||||
#include "../Settings.hpp"
|
||||
#include <windows.h>
|
||||
void VehicleParser(Client*c, std::string Packet);
|
||||
void VehicleParser(Client*c,const std::string& Pckt);
|
||||
int Handle(EXCEPTION_POINTERS *ep,char* Origin){
|
||||
Exception(ep->ExceptionRecord->ExceptionCode,Origin);
|
||||
return 1;
|
||||
}
|
||||
int Rand(){
|
||||
std::random_device r;
|
||||
std::default_random_engine e1(r());
|
||||
@@ -43,9 +48,9 @@ std::string Decrypt(std::string msg){
|
||||
[[noreturn]]void SLoop(){
|
||||
std::thread D(DLoop);
|
||||
D.detach();
|
||||
int A = 0;
|
||||
int A = -1;
|
||||
while(true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(15));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(20));
|
||||
if (A == Beat)VehicleParser(nullptr, "");
|
||||
A = Beat;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,6 @@ SplitData*GetSplit(int SplitID){
|
||||
SplitPackets.insert(SP);
|
||||
return SP;
|
||||
}
|
||||
|
||||
void GlobalParser(Client*c, const std::string&Packet);
|
||||
void HandleChunk(Client*c,const std::string&Data){
|
||||
int pos1 = int(Data.find(':'))+1,pos2 = Data.find(':',pos1),pos3 = Data.find('/');
|
||||
@@ -178,7 +177,6 @@ void HandleChunk(Client*c,const std::string&Data){
|
||||
SplitPackets.erase(SData);
|
||||
}
|
||||
}
|
||||
|
||||
void UDPParser(Client*c, const std::string&Packet){
|
||||
if(Packet.substr(0,4) == "ACK:"){
|
||||
AckID(stoi(Packet.substr(4)));
|
||||
@@ -194,19 +192,18 @@ void UDPParser(Client*c, const std::string&Packet){
|
||||
}
|
||||
return;
|
||||
}else if(Packet.substr(0,2) == "SC"){
|
||||
|
||||
HandleChunk(c,Packet);
|
||||
return;
|
||||
}
|
||||
GlobalParser(c,Packet);
|
||||
}
|
||||
|
||||
void StartLoop();
|
||||
[[noreturn]] void UDPServerMain(){
|
||||
|
||||
[[noreturn]] void UDPServerMain(){
|
||||
WSADATA data;
|
||||
if (WSAStartup(514, &data)) //2.2
|
||||
{
|
||||
|
||||
std::cout << "Can't start Winsock!" << std::endl;
|
||||
//return;
|
||||
}
|
||||
@@ -275,4 +272,4 @@ void LOOP(){
|
||||
void StartLoop(){
|
||||
std::thread Ack(LOOP);
|
||||
Ack.detach();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <fstream>
|
||||
#include "logger.h"
|
||||
#include <string>
|
||||
|
||||
void addToLog(const std::string& Data);
|
||||
int loggerlevel;
|
||||
|
||||
@@ -75,7 +76,6 @@ void error(const std::string& toPrint) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void warn(const std::string& toPrint) {
|
||||
if (loggerlevel <= 3) {
|
||||
std::string Print = getDate().str() + "[WARN] " + toPrint + "\n";
|
||||
@@ -83,12 +83,19 @@ void warn(const std::string& toPrint) {
|
||||
addToLog(Print);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void debug(const std::string& toPrint) {
|
||||
if (loggerlevel <= 1) {
|
||||
std::string Print = getDate().str() + "[DEBUG] " + toPrint + "\n";
|
||||
std::cout << Print;
|
||||
addToLog(Print);
|
||||
}
|
||||
}
|
||||
void Exception(unsigned long Code,char* Origin) {
|
||||
char* hex_string = new char[100];
|
||||
sprintf(hex_string, "%lX", Code); //convert number to hex
|
||||
if (loggerlevel <= 4) {
|
||||
std::string Print = getDate().str() + "[EXCEP] code " + hex_string + " Origin: "+ std::string(Origin) +"\n";
|
||||
std::cout << Print;
|
||||
addToLog(Print);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
/// Created by Anonymous275 on 4/2/2020.
|
||||
///
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
|
||||
extern int loggerlevel;
|
||||
std::stringstream getDate();
|
||||
void setLoggerLevel(int level);
|
||||
@@ -13,3 +13,4 @@ void info(const std::string& toPrint);
|
||||
void warn(const std::string& toPrint);
|
||||
void error(const std::string& toPrint);
|
||||
void debug(const std::string& toPrint);
|
||||
void Exception(unsigned long Code,char* Origin);
|
||||
@@ -1,7 +1,6 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 28/01/2020
|
||||
///
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
@@ -15,7 +14,7 @@ void ParseConfig();
|
||||
void addToLog(const std::string& Data);
|
||||
//void ServerMain(int Port, int MaxClients);
|
||||
void HeartbeatInit();
|
||||
std::string ServerVersion = "0.50";
|
||||
std::string ServerVersion = "0.51";
|
||||
std::string ClientVersion = "1.50";
|
||||
std::string CustomIP;
|
||||
void HandleResources(std::string path);
|
||||
@@ -72,4 +71,4 @@ void addToLog(const std::string& Data){
|
||||
LFS.open ("Server.log", std::ios_base::app);
|
||||
LFS << Data.c_str();
|
||||
LFS.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user