mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-04 00:36:14 +00:00
added "isConnected" to the Client Object
This commit is contained in:
parent
131e64b706
commit
ba47f21898
@ -12,6 +12,12 @@ void Client::SetName(const std::string& name){
|
|||||||
void Client::SetDID(const std::string& did){
|
void Client::SetDID(const std::string& did){
|
||||||
DID = did;
|
DID = did;
|
||||||
}
|
}
|
||||||
|
void Client::SetConnected(bool state){
|
||||||
|
Connected = state;
|
||||||
|
}
|
||||||
|
bool Client::isConnected(){
|
||||||
|
return Connected;
|
||||||
|
}
|
||||||
std::string Client::GetDID(){
|
std::string Client::GetDID(){
|
||||||
return DID;
|
return DID;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ class Client {
|
|||||||
private:
|
private:
|
||||||
std::set<std::pair<int,std::string>> VehicleData; //ID and Data;
|
std::set<std::pair<int,std::string>> VehicleData; //ID and Data;
|
||||||
std::string Name = "Unknown Client";
|
std::string Name = "Unknown Client";
|
||||||
|
bool Connected = false;
|
||||||
sockaddr_in UDPADDR;
|
sockaddr_in UDPADDR;
|
||||||
std::string Role;
|
std::string Role;
|
||||||
std::string DID; //Discord ID
|
std::string DID; //Discord ID
|
||||||
@ -19,6 +20,7 @@ private:
|
|||||||
int Status = 0;
|
int Status = 0;
|
||||||
int ID = -1; //PlayerID
|
int ID = -1; //PlayerID
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void AddNewCar(int ident,const std::string& Data);
|
void AddNewCar(int ident,const std::string& Data);
|
||||||
void SetName(const std::string& name);
|
void SetName(const std::string& name);
|
||||||
@ -27,6 +29,7 @@ public:
|
|||||||
std::string GetCarData(int ident);
|
std::string GetCarData(int ident);
|
||||||
void SetUDPAddr(sockaddr_in Addr);
|
void SetUDPAddr(sockaddr_in Addr);
|
||||||
void SetTCPSock(SOCKET CSock);
|
void SetTCPSock(SOCKET CSock);
|
||||||
|
void SetConnected(bool state);
|
||||||
void SetStatus(int status);
|
void SetStatus(int status);
|
||||||
void DeleteCar(int ident);
|
void DeleteCar(int ident);
|
||||||
sockaddr_in GetUDPAddr();
|
sockaddr_in GetUDPAddr();
|
||||||
@ -34,6 +37,7 @@ public:
|
|||||||
std::string GetName();
|
std::string GetName();
|
||||||
std::string GetDID();
|
std::string GetDID();
|
||||||
SOCKET GetTCPSock();
|
SOCKET GetTCPSock();
|
||||||
|
bool isConnected();
|
||||||
void SetID(int ID);
|
void SetID(int ID);
|
||||||
int GetCarCount();
|
int GetCarCount();
|
||||||
int GetStatus();
|
int GetStatus();
|
||||||
|
@ -22,16 +22,22 @@ int OpenID(){
|
|||||||
}while (!found);
|
}while (!found);
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
void TCPSendLarge(Client*c,const std::string&Data);
|
||||||
void Respond(Client*c, const std::string& MSG, bool Rel){
|
void Respond(Client*c, const std::string& MSG, bool Rel){
|
||||||
if(Rel)TCPSend(c,MSG);
|
if(Rel){
|
||||||
|
if(MSG.length() > 1000)TCPSendLarge(c,MSG);
|
||||||
|
else TCPSend(c,MSG);
|
||||||
|
}
|
||||||
else UDPSend(c,MSG);
|
else UDPSend(c,MSG);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
||||||
for(Client*client : Clients){
|
for(Client*client : Clients){
|
||||||
if(Self || client != c){
|
if(Self || client != c){
|
||||||
if(Rel)TCPSend(client,Data);
|
if(Rel){
|
||||||
|
if(Data.length() > 1000)TCPSendLarge(client,Data);
|
||||||
|
else TCPSend(client,Data);
|
||||||
|
}
|
||||||
else UDPSend(client,Data);
|
else UDPSend(client,Data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
void TCPSend(Client*c,const std::string&Data){
|
void TCPSend(Client*c,const std::string&Data){
|
||||||
int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 0);
|
int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 0);
|
||||||
if (BytesSent == 0){
|
if (BytesSent == 0){
|
||||||
@ -21,9 +22,10 @@ void TCPSend(Client*c,const std::string&Data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GlobalParser(Client*c, const std::string&Packet);
|
void GlobalParser(Client*c, const std::string&Packet);
|
||||||
|
|
||||||
void TCPRcv(Client*c){
|
void TCPRcv(Client*c){
|
||||||
char buf[10240];
|
char buf[4096];
|
||||||
int len = 10240;
|
int len = 4096;
|
||||||
ZeroMemory(buf, len);
|
ZeroMemory(buf, len);
|
||||||
int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
|
int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
|
||||||
if (BytesRcv == 0){
|
if (BytesRcv == 0){
|
||||||
|
@ -6,24 +6,45 @@
|
|||||||
#include "Client.hpp"
|
#include "Client.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
#include "../Settings.hpp"
|
#include "../Settings.hpp"
|
||||||
|
|
||||||
SOCKET UDPSock;
|
SOCKET UDPSock;
|
||||||
|
|
||||||
|
std::set<std::tuple<int,Client*,std::string>> BigDataAcks;
|
||||||
|
|
||||||
void UDPSend(Client*c,const std::string&Data){
|
void UDPSend(Client*c,const std::string&Data){
|
||||||
|
if(!c->isConnected())return;
|
||||||
sockaddr_in Addr = c->GetUDPAddr();
|
sockaddr_in Addr = c->GetUDPAddr();
|
||||||
int AddrSize = sizeof(c->GetUDPAddr());
|
int AddrSize = sizeof(c->GetUDPAddr());
|
||||||
int sendOk = sendto(UDPSock, Data.c_str(), int(Data.length()) + 1, 0, (sockaddr*)&Addr, AddrSize);
|
int sendOk = sendto(UDPSock, Data.c_str(), int(Data.length()) + 1, 0, (sockaddr*)&Addr, AddrSize);
|
||||||
if (sendOk == SOCKET_ERROR)error("(UDP) Send Error! Code : " + std::to_string(WSAGetLastError()));
|
if (sendOk == SOCKET_ERROR)error("(UDP) Send Error Code : " + std::to_string(WSAGetLastError()) + " Size : " + std::to_string(AddrSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AckID(int ID){
|
||||||
|
if(BigDataAcks.empty())return;
|
||||||
|
for(std::tuple<int,Client*,std::string> a : BigDataAcks){
|
||||||
|
if(get<0>(a) == ID)BigDataAcks.erase(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TCPSendLarge(Client*c,const std::string&Data){
|
||||||
|
static int ID = 0;
|
||||||
|
std::string Header = "BD:" + std::to_string(ID) + ":";
|
||||||
|
//BigDataAcks.insert(std::make_tuple(ID,c,Header+Data));
|
||||||
|
UDPSend(c,Header+Data);
|
||||||
|
if(ID > 483647)ID = 0;
|
||||||
|
else ID++;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string UDPRcvFromClient(sockaddr_in& client){
|
std::string UDPRcvFromClient(sockaddr_in& client){
|
||||||
char buf[4096];
|
char buf[10240];
|
||||||
int clientLength = sizeof(client);
|
int clientLength = sizeof(client);
|
||||||
ZeroMemory(&client, clientLength);
|
ZeroMemory(&client, clientLength);
|
||||||
ZeroMemory(buf, 4096);
|
ZeroMemory(buf, 10240);
|
||||||
int bytesIn = recvfrom(UDPSock, buf, 4096, 0, (sockaddr*)&client, &clientLength);
|
int bytesIn = recvfrom(UDPSock, buf, 10240, 0, (sockaddr*)&client, &clientLength);
|
||||||
if (bytesIn == -1)
|
if (bytesIn == -1)
|
||||||
{
|
{
|
||||||
error("(UDP) Error receiving from Client! Code : " + std::to_string(WSAGetLastError()));
|
error("(UDP) Error receiving from Client! Code : " + std::to_string(WSAGetLastError()));
|
||||||
@ -34,10 +55,25 @@ std::string UDPRcvFromClient(sockaddr_in& client){
|
|||||||
|
|
||||||
void GlobalParser(Client*c, const std::string&Packet);
|
void GlobalParser(Client*c, const std::string&Packet);
|
||||||
|
|
||||||
|
void UDPParser(Client*c, const std::string&Packet){
|
||||||
|
if(Packet.substr(0,4) == "ACK:"){
|
||||||
|
AckID(stoi(Packet.substr(4)));
|
||||||
|
return;
|
||||||
|
}else if(Packet.substr(0,3) == "BD:"){
|
||||||
|
int pos = Packet.find(':',4);
|
||||||
|
std::string pckt = "ACK:" + Packet.substr(3,pos-3);
|
||||||
|
UDPSend(c,pckt);
|
||||||
|
pckt = Packet.substr(pos+1);
|
||||||
|
GlobalParser(c,pckt);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GlobalParser(c,Packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StartLoop();
|
||||||
[[noreturn]] void UDPServerMain(){
|
[[noreturn]] void UDPServerMain(){
|
||||||
|
|
||||||
WSADATA data;
|
WSADATA data;
|
||||||
|
|
||||||
if (WSAStartup(514, &data)) //2.2
|
if (WSAStartup(514, &data)) //2.2
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -59,6 +95,10 @@ void GlobalParser(Client*c, const std::string&Packet);
|
|||||||
std::cout << "Can't bind socket! " << WSAGetLastError() << std::endl;
|
std::cout << "Can't bind socket! " << WSAGetLastError() << std::endl;
|
||||||
//return;
|
//return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BigDataAcks.clear();
|
||||||
|
StartLoop();
|
||||||
|
|
||||||
info("Vehicle data network online on port "+std::to_string(Port)+" with a Max of "+std::to_string(MaxPlayers)+" Clients");
|
info("Vehicle data network online on port "+std::to_string(Port)+" with a Max of "+std::to_string(MaxPlayers)+" Clients");
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -74,7 +114,8 @@ void GlobalParser(Client*c, const std::string&Packet);
|
|||||||
for(Client*c : Clients){
|
for(Client*c : Clients){
|
||||||
if(c->GetID() == ID){
|
if(c->GetID() == ID){
|
||||||
c->SetUDPAddr(client);
|
c->SetUDPAddr(client);
|
||||||
GlobalParser(c,Data.substr(2));
|
c->SetConnected(true);
|
||||||
|
UDPParser(c,Data.substr(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,3 +124,20 @@ void GlobalParser(Client*c, const std::string&Packet);
|
|||||||
WSACleanup();
|
WSACleanup();
|
||||||
return;*/
|
return;*/
|
||||||
}
|
}
|
||||||
|
#include <thread>
|
||||||
|
void LOOP(){
|
||||||
|
while(UDPSock != -1) {
|
||||||
|
for (std::tuple<int, Client *, std::string> a : BigDataAcks) {
|
||||||
|
if (get<1>(a)->GetTCPSock() == -1) {
|
||||||
|
BigDataAcks.erase(a);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//UDPSend(get<1>(a), get<2>(a));
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void StartLoop(){
|
||||||
|
std::thread Ack(LOOP);
|
||||||
|
Ack.detach();
|
||||||
|
}
|
@ -31,6 +31,7 @@ void TCPServerMain(){
|
|||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(Port);
|
addr.sin_port = htons(Port);
|
||||||
|
|
||||||
|
|
||||||
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
|
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
std::cout << "Can't bind socket! " << WSAGetLastError() << std::endl;
|
std::cout << "Can't bind socket! " << WSAGetLastError() << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user