mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2025-08-18 12:45:36 +00:00
Safety improvements
This commit is contained in:
parent
437a654b90
commit
96668add6e
@ -10,6 +10,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
#include "UnixCompat.h"
|
#include "UnixCompat.h"
|
||||||
|
|
||||||
|
|
||||||
@ -117,6 +118,7 @@ std::string GenerateM(RSA*key){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){
|
void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){
|
||||||
|
DebugPrintTID();
|
||||||
Assert(S);
|
Assert(S);
|
||||||
Assert(Skey);
|
Assert(Skey);
|
||||||
S->TCPSock = TCPSock;
|
S->TCPSock = TCPSock;
|
||||||
@ -171,6 +173,7 @@ void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){
|
|||||||
closesocket(TCPSock);
|
closesocket(TCPSock);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
DebugPrintTIDInternal(std::string("Client(") + Name + ")");
|
||||||
debug(Sec("Name -> ") + Name + Sec(", Role -> ") + Role + Sec(", ID -> ") + DID);
|
debug(Sec("Name -> ") + Name + Sec(", Role -> ") + Role + Sec(", ID -> ") + DID);
|
||||||
for(Client*c: CI->Clients){
|
for(Client*c: CI->Clients){
|
||||||
if(c != nullptr){
|
if(c != nullptr){
|
||||||
@ -242,6 +245,7 @@ void TCPServerMain(){
|
|||||||
}
|
}
|
||||||
info(Sec("Vehicle event network online"));
|
info(Sec("Vehicle event network online"));
|
||||||
do{
|
do{
|
||||||
|
try {
|
||||||
client = accept(Listener, nullptr, nullptr);
|
client = accept(Listener, nullptr, nullptr);
|
||||||
if(client == -1){
|
if(client == -1){
|
||||||
warn(Sec("Got an invalid client socket on connect! Skipping..."));
|
warn(Sec("Got an invalid client socket on connect! Skipping..."));
|
||||||
@ -249,6 +253,9 @@ void TCPServerMain(){
|
|||||||
}
|
}
|
||||||
std::thread ID(Identify,client);
|
std::thread ID(Identify,client);
|
||||||
ID.detach();
|
ID.detach();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
error(Sec("fatal: ") + std::string(e));
|
||||||
|
}
|
||||||
}while(client);
|
}while(client);
|
||||||
|
|
||||||
closesocket(client);
|
closesocket(client);
|
||||||
@ -276,13 +283,17 @@ void TCPServerMain(){
|
|||||||
}
|
}
|
||||||
info(Sec("Vehicle event network online"));
|
info(Sec("Vehicle event network online"));
|
||||||
do{
|
do{
|
||||||
client = accept(Listener, nullptr, nullptr);
|
try {
|
||||||
if(client == -1){
|
client = accept(Listener, nullptr, nullptr);
|
||||||
warn(Sec("Got an invalid client socket on connect! Skipping..."));
|
if(client == -1){
|
||||||
continue;
|
warn(Sec("Got an invalid client socket on connect! Skipping..."));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::thread ID(Identify,client);
|
||||||
|
ID.detach();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
error(Sec("fatal: ") + std::string(e.what()));
|
||||||
}
|
}
|
||||||
std::thread ID(Identify,client);
|
|
||||||
ID.detach();
|
|
||||||
}while(client);
|
}while(client);
|
||||||
|
|
||||||
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
||||||
|
@ -301,21 +301,25 @@ void LOOP() {
|
|||||||
|
|
||||||
info(Sec("Vehicle data network online on port ") + std::to_string(Port) + Sec(" with a Max of ") + std::to_string(MaxPlayers) + Sec(" Clients"));
|
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) {
|
while (true) {
|
||||||
sockaddr_in client {};
|
try {
|
||||||
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
sockaddr_in client {};
|
||||||
auto Pos = Data.find(':');
|
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
||||||
if (Data.empty() || Pos < 0 || Pos > 2)
|
auto Pos = Data.find(':');
|
||||||
continue;
|
if (Data.empty() || Pos < 0 || Pos > 2)
|
||||||
/*char clientIp[256];
|
continue;
|
||||||
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
/*char clientIp[256];
|
||||||
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
||||||
uint8_t ID = Data.at(0) - 1;
|
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
||||||
for (Client* c : CI->Clients) {
|
uint8_t ID = Data.at(0) - 1;
|
||||||
if (c != nullptr && c->GetID() == ID) {
|
for (Client* c : CI->Clients) {
|
||||||
c->SetUDPAddr(client);
|
if (c != nullptr && c->GetID() == ID) {
|
||||||
c->isConnected = true;
|
c->SetUDPAddr(client);
|
||||||
UDPParser(c, Data.substr(2));
|
c->isConnected = true;
|
||||||
|
UDPParser(c, Data.substr(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
error(Sec("fatal: ") + std::string(e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*closesocket(UDPSock);
|
/*closesocket(UDPSock);
|
||||||
@ -343,21 +347,25 @@ void LOOP() {
|
|||||||
|
|
||||||
info(Sec("Vehicle data network online on port ") + std::to_string(Port) + Sec(" with a Max of ") + std::to_string(MaxPlayers) + Sec(" Clients"));
|
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) {
|
while (true) {
|
||||||
sockaddr_in client {};
|
try {
|
||||||
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
sockaddr_in client {};
|
||||||
size_t Pos = Data.find(':');
|
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
||||||
if (Data.empty() || Pos > 2)
|
size_t Pos = Data.find(':');
|
||||||
continue;
|
if (Data.empty() || Pos > 2)
|
||||||
/*char clientIp[256];
|
continue;
|
||||||
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
/*char clientIp[256];
|
||||||
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
||||||
uint8_t ID = uint8_t(Data.at(0)) - 1;
|
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
||||||
for (Client* c : CI->Clients) {
|
uint8_t ID = uint8_t(Data.at(0)) - 1;
|
||||||
if (c != nullptr && c->GetID() == ID) {
|
for (Client* c : CI->Clients) {
|
||||||
c->SetUDPAddr(client);
|
if (c != nullptr && c->GetID() == ID) {
|
||||||
c->isConnected = true;
|
c->SetUDPAddr(client);
|
||||||
UDPParser(c, Data.substr(2));
|
c->isConnected = true;
|
||||||
|
UDPParser(c, Data.substr(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
error(Sec("fatal: ") + std::string(e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain?
|
/*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain?
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
try {
|
||||||
DebugPrintTID();
|
DebugPrintTID();
|
||||||
// curl needs to be initialized to properly deallocate its resources later
|
// curl needs to be initialized to properly deallocate its resources later
|
||||||
Assert(curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK);
|
Assert(curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK);
|
||||||
@ -31,5 +32,9 @@ int main(int argc, char* argv[]) {
|
|||||||
NetMain();
|
NetMain();
|
||||||
// clean up curl at the end to be sure
|
// clean up curl at the end to be sure
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
error(std::string(e.what()));
|
||||||
|
throw;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user