mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +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){
|
||||
DID = did;
|
||||
}
|
||||
void Client::SetConnected(bool state){
|
||||
Connected = state;
|
||||
}
|
||||
bool Client::isConnected(){
|
||||
return Connected;
|
||||
}
|
||||
std::string Client::GetDID(){
|
||||
return DID;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ class Client {
|
||||
private:
|
||||
std::set<std::pair<int,std::string>> VehicleData; //ID and Data;
|
||||
std::string Name = "Unknown Client";
|
||||
bool Connected = false;
|
||||
sockaddr_in UDPADDR;
|
||||
std::string Role;
|
||||
std::string DID; //Discord ID
|
||||
@ -19,6 +20,7 @@ private:
|
||||
int Status = 0;
|
||||
int ID = -1; //PlayerID
|
||||
|
||||
|
||||
public:
|
||||
void AddNewCar(int ident,const std::string& Data);
|
||||
void SetName(const std::string& name);
|
||||
@ -27,6 +29,7 @@ public:
|
||||
std::string GetCarData(int ident);
|
||||
void SetUDPAddr(sockaddr_in Addr);
|
||||
void SetTCPSock(SOCKET CSock);
|
||||
void SetConnected(bool state);
|
||||
void SetStatus(int status);
|
||||
void DeleteCar(int ident);
|
||||
sockaddr_in GetUDPAddr();
|
||||
@ -34,6 +37,7 @@ public:
|
||||
std::string GetName();
|
||||
std::string GetDID();
|
||||
SOCKET GetTCPSock();
|
||||
bool isConnected();
|
||||
void SetID(int ID);
|
||||
int GetCarCount();
|
||||
int GetStatus();
|
||||
|
@ -22,16 +22,22 @@ int OpenID(){
|
||||
}while (!found);
|
||||
return ID;
|
||||
}
|
||||
|
||||
void TCPSendLarge(Client*c,const std::string&Data);
|
||||
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);
|
||||
}
|
||||
|
||||
void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
|
||||
for(Client*client : Clients){
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
|
||||
void TCPSend(Client*c,const std::string&Data){
|
||||
int BytesSent = send(c->GetTCPSock(), Data.c_str(), int(Data.length())+1, 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 TCPRcv(Client*c){
|
||||
char buf[10240];
|
||||
int len = 10240;
|
||||
char buf[4096];
|
||||
int len = 4096;
|
||||
ZeroMemory(buf, len);
|
||||
int BytesRcv = recv(c->GetTCPSock(), buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
|
@ -6,24 +6,45 @@
|
||||
#include "Client.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
#include "../logger.h"
|
||||
#include "../Settings.hpp"
|
||||
|
||||
SOCKET UDPSock;
|
||||
|
||||
std::set<std::tuple<int,Client*,std::string>> BigDataAcks;
|
||||
|
||||
void UDPSend(Client*c,const std::string&Data){
|
||||
if(!c->isConnected())return;
|
||||
sockaddr_in Addr = c->GetUDPAddr();
|
||||
int AddrSize = sizeof(c->GetUDPAddr());
|
||||
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){
|
||||
char buf[4096];
|
||||
char buf[10240];
|
||||
int clientLength = sizeof(client);
|
||||
ZeroMemory(&client, clientLength);
|
||||
ZeroMemory(buf, 4096);
|
||||
int bytesIn = recvfrom(UDPSock, buf, 4096, 0, (sockaddr*)&client, &clientLength);
|
||||
ZeroMemory(buf, 10240);
|
||||
int bytesIn = recvfrom(UDPSock, buf, 10240, 0, (sockaddr*)&client, &clientLength);
|
||||
if (bytesIn == -1)
|
||||
{
|
||||
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 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(){
|
||||
|
||||
WSADATA data;
|
||||
|
||||
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;
|
||||
//return;
|
||||
}
|
||||
|
||||
BigDataAcks.clear();
|
||||
StartLoop();
|
||||
|
||||
info("Vehicle data network online on port "+std::to_string(Port)+" with a Max of "+std::to_string(MaxPlayers)+" Clients");
|
||||
while (true)
|
||||
{
|
||||
@ -74,7 +114,8 @@ void GlobalParser(Client*c, const std::string&Packet);
|
||||
for(Client*c : Clients){
|
||||
if(c->GetID() == ID){
|
||||
c->SetUDPAddr(client);
|
||||
GlobalParser(c,Data.substr(2));
|
||||
c->SetConnected(true);
|
||||
UDPParser(c,Data.substr(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -82,4 +123,21 @@ void GlobalParser(Client*c, const std::string&Packet);
|
||||
/*closesocket(UDPSock);
|
||||
WSACleanup();
|
||||
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();
|
||||
}
|
@ -25,12 +25,13 @@ void TCPServerMain(){
|
||||
std::cout << "Can't start Winsock!" << std::endl;
|
||||
return;
|
||||
}
|
||||
SOCKET client,Listener = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
||||
SOCKET client, Listener = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
||||
sockaddr_in addr{};
|
||||
addr.sin_addr.S_un.S_addr = ADDR_ANY;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(Port);
|
||||
|
||||
|
||||
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
|
||||
{
|
||||
std::cout << "Can't bind socket! " << WSAGetLastError() << std::endl;
|
||||
|
Loading…
x
Reference in New Issue
Block a user