mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-08 08:46:04 +00:00
Refactor to work on Linux / Unix, fix some compiler errors.
CMakeLists was also modified to make this work, but its scuffed and i will hold on to that for a while longer
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
#include "Logger.h"
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
#include "UnixCompat.h"
|
||||
|
||||
|
||||
struct Hold{
|
||||
SOCKET TCPSock{};
|
||||
bool Done = false;
|
||||
@@ -148,17 +152,31 @@ void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){
|
||||
void Identify(SOCKET TCPSock){
|
||||
auto* S = new Hold;
|
||||
RSA*Skey = GenKey();
|
||||
// this disgusting ifdef stuff is needed because for some
|
||||
// reason MSVC defines __try and __except and libg++ defines
|
||||
// __try and __catch so its all a big mess if we leave this in or undefine
|
||||
// the macros
|
||||
#ifdef __WIN32
|
||||
__try{
|
||||
#endif // __WIN32
|
||||
Identification(TCPSock,S,Skey);
|
||||
#ifdef __WIN32
|
||||
}__except(1){
|
||||
#endif // __WIN32
|
||||
|
||||
if(TCPSock != -1){
|
||||
closesocket(TCPSock);
|
||||
}
|
||||
#ifdef __WIN32
|
||||
}
|
||||
#endif // __WIN32
|
||||
|
||||
delete Skey;
|
||||
delete S;
|
||||
}
|
||||
|
||||
void TCPServerMain(){
|
||||
#ifdef __WIN32
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(514, &wsaData)){
|
||||
error(Sec("Can't start Winsock!"));
|
||||
@@ -195,4 +213,38 @@ void TCPServerMain(){
|
||||
|
||||
closesocket(client);
|
||||
WSACleanup();
|
||||
}
|
||||
#else // unix
|
||||
// wondering why we need slightly different implementations of this?
|
||||
// ask ms.
|
||||
SOCKET client, Listener = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
||||
sockaddr_in addr{};
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(Port);
|
||||
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) != 0){
|
||||
error(Sec("Can't bind socket! ") + std::string(strerror(errno)));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(-1);
|
||||
}
|
||||
if(Listener == -1){
|
||||
error(Sec("Invalid listening socket"));
|
||||
return;
|
||||
}
|
||||
if(listen(Listener,SOMAXCONN)){
|
||||
error(Sec("listener failed ")+ std::string(strerror(errno)));
|
||||
return;
|
||||
}
|
||||
info(Sec("Vehicle event network online"));
|
||||
do{
|
||||
client = accept(Listener, nullptr, nullptr);
|
||||
if(client == -1){
|
||||
warn(Sec("Got an invalid client socket on connect! Skipping..."));
|
||||
continue;
|
||||
}
|
||||
std::thread ID(Identify,client);
|
||||
ID.detach();
|
||||
}while(client);
|
||||
|
||||
closesocket(client);
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
#include "Settings.h"
|
||||
#include "Network.h"
|
||||
#include "Logger.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <sstream>
|
||||
|
||||
|
||||
int FC(const std::string& s,const std::string& p,int n) {
|
||||
auto i = s.find(p);
|
||||
int j;
|
||||
@@ -116,10 +118,14 @@ void SyncClient(Client*c){
|
||||
}
|
||||
info(c->GetName() + Sec(" is now synced!"));
|
||||
}
|
||||
void ParseVeh(Client*c, const std::string&Packet){
|
||||
void ParseVeh(Client*c, const std::string& Packet){
|
||||
#ifdef __WIN32
|
||||
__try{
|
||||
VehicleParser(c,Packet);
|
||||
}__except(Handle(GetExceptionInformation(),Sec("Vehicle Handler"))){}
|
||||
#else // unix
|
||||
VehicleParser(c,Packet);
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
void HandleEvent(Client*c ,const std::string&Data){
|
||||
@@ -189,8 +195,12 @@ void GlobalParser(Client*c, const std::string& Pack){
|
||||
}
|
||||
}
|
||||
|
||||
void GParser(Client*c, const std::string&Packet){
|
||||
void GParser(Client*c, const std::string& Packet){
|
||||
#ifdef __WIN32
|
||||
__try{
|
||||
GlobalParser(c, Packet);
|
||||
}__except(Handle(GetExceptionInformation(),Sec("Global Handler"))){}
|
||||
}
|
||||
#else
|
||||
GlobalParser(c, Packet);
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
@@ -5,8 +5,14 @@
|
||||
#include "Client.hpp"
|
||||
#include "Settings.h"
|
||||
#include "Logger.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <fstream>
|
||||
|
||||
#ifdef __linux
|
||||
// we need this for `struct stat`
|
||||
#include <sys/stat.h>
|
||||
#endif // __linux
|
||||
|
||||
void STCPSend(Client*c,std::string Data){
|
||||
if(c == nullptr)return;
|
||||
int BytesSent;
|
||||
@@ -100,4 +106,4 @@ void SyncResources(Client*c){
|
||||
except(Sec("Exception! : ") + std::string(e.what()));
|
||||
c->SetStatus(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Security/Enc.h"
|
||||
#include "Network.h"
|
||||
#include "Logger.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <thread>
|
||||
|
||||
void TCPSend(Client*c,const std::string&Data){
|
||||
@@ -18,11 +19,15 @@ void TCPSend(Client*c,const std::string&Data){
|
||||
}
|
||||
}
|
||||
void TCPHandle(Client*c,const std::string& data){
|
||||
#ifdef __WIN32
|
||||
__try{
|
||||
#endif // __WIN32
|
||||
c->Handler.Handle(c,data);
|
||||
#ifdef __WIN32
|
||||
}__except(1){
|
||||
c->Handler.clear();
|
||||
}
|
||||
#endif // __WIN32
|
||||
}
|
||||
void TCPRcv(Client*c){
|
||||
if(c == nullptr || c->GetStatus() < 0)return;
|
||||
@@ -35,7 +40,11 @@ void TCPRcv(Client*c){
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
return;
|
||||
}else if (BytesRcv < 0) {
|
||||
#ifdef __WIN32
|
||||
debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError()));
|
||||
#else // unix
|
||||
debug(Sec("(TCP) recv failed with error: ") + std::string(strerror(errno)));
|
||||
#endif // __WIN32
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
return;
|
||||
@@ -50,11 +59,15 @@ void TCPClient(Client*c){
|
||||
}
|
||||
OnConnect(c);
|
||||
while (c->GetStatus() > -1)TCPRcv(c);
|
||||
#ifdef __WIN32
|
||||
__try{
|
||||
#endif // __WIN32
|
||||
OnDisconnect(c, c->GetStatus() == -2);
|
||||
#ifdef __WIN32
|
||||
}__except(Handle(GetExceptionInformation(),Sec("OnDisconnect"))){}
|
||||
#endif // __WIN32
|
||||
}
|
||||
void InitClient(Client*c){
|
||||
std::thread NewClient(TCPClient,c);
|
||||
NewClient.detach();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,17 @@
|
||||
#include "Settings.h"
|
||||
#include "Network.h"
|
||||
#include "Logger.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
int FC(const std::string& s,const std::string& p,int n);
|
||||
struct PacketData{
|
||||
int ID;
|
||||
Client* Client;
|
||||
::Client* Client;
|
||||
std::string Data;
|
||||
int Tries;
|
||||
};
|
||||
@@ -38,10 +41,17 @@ void UDPSend(Client*c,std::string Data){
|
||||
Data = "ABG:" + CMP;
|
||||
}
|
||||
int sendOk = sendto(UDPSock, Data.c_str(), int(Data.size()), 0, (sockaddr *) &Addr, AddrSize);
|
||||
if (sendOk == SOCKET_ERROR) {
|
||||
#ifdef __WIN32
|
||||
if (sendOk != 0) {
|
||||
debug(Sec("(UDP) Send Failed Code : ") + std::to_string(WSAGetLastError()));
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
}
|
||||
#else // unix
|
||||
if (sendOk != 0) {
|
||||
debug(Sec("(UDP) Send Failed Code : ") + std::string(strerror(errno)));
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
}
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
void AckID(int ID){
|
||||
@@ -144,9 +154,13 @@ std::string UDPRcvFromClient(sockaddr_in& client){
|
||||
int clientLength = sizeof(client);
|
||||
ZeroMemory(&client, clientLength);
|
||||
std::string Ret(10240,0);
|
||||
int Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&client, &clientLength);
|
||||
int Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&client, (socklen_t*)&clientLength);
|
||||
if (Rcv == -1){
|
||||
#ifdef __WIN32
|
||||
error(Sec("(UDP) Error receiving from Client! Code : ") + std::to_string(WSAGetLastError()));
|
||||
#else // unix
|
||||
error(Sec("(UDP) Error receiving from Client! Code : ") + std::string(strerror(errno)));
|
||||
#endif // __WIN32
|
||||
return "";
|
||||
}
|
||||
return Ret;
|
||||
@@ -240,6 +254,7 @@ void LOOP(){
|
||||
}
|
||||
}
|
||||
[[noreturn]] void UDPServerMain(){
|
||||
#ifdef __WIN32
|
||||
WSADATA data;
|
||||
if (WSAStartup(514, &data)){
|
||||
error(Sec("Can't start Winsock!"));
|
||||
@@ -286,4 +301,46 @@ void LOOP(){
|
||||
/*closesocket(UDPSock);
|
||||
WSACleanup();
|
||||
return;*/
|
||||
}
|
||||
#else // unix
|
||||
UDPSock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
// Create a server hint structure for the server
|
||||
sockaddr_in serverAddr{};
|
||||
serverAddr.sin_addr.s_addr = INADDR_ANY; //Any Local
|
||||
serverAddr.sin_family = AF_INET; // Address format is IPv4
|
||||
serverAddr.sin_port = htons(Port); // Convert from little to big endian
|
||||
|
||||
// Try and bind the socket to the IP and port
|
||||
if (bind(UDPSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0){
|
||||
error(Sec("Can't bind socket!") + std::string(strerror(errno)));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(-1);
|
||||
//return;
|
||||
}
|
||||
|
||||
DataAcks.clear();
|
||||
std::thread Ack(LOOP);
|
||||
Ack.detach();
|
||||
|
||||
info(Sec("Vehicle data network online on port ")+std::to_string(Port)+Sec(" with a Max of ")+std::to_string(MaxPlayers)+Sec(" Clients"));
|
||||
while (true){
|
||||
sockaddr_in client{};
|
||||
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
||||
auto Pos = Data.find(':');
|
||||
if(Data.empty() || Pos < 0 || Pos > 2)continue;
|
||||
/*char clientIp[256];
|
||||
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
||||
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
||||
uint8_t ID = Data.at(0)-1;
|
||||
for(Client*c : CI->Clients){
|
||||
if(c != nullptr && c->GetID() == ID){
|
||||
c->SetUDPAddr(client);
|
||||
c->isConnected = true;
|
||||
UDPParser(c,Data.substr(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
/*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain?
|
||||
return;
|
||||
*/
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user