mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-04-08 12:56:01 +00:00
Major rewrite of the network
This commit is contained in:
64
src/Network 2.0/ClientInterface.cpp
Normal file
64
src/Network 2.0/ClientInterface.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 2/4/2020.
|
||||
///
|
||||
#include "Client.hpp"
|
||||
#include "../logger.h"
|
||||
#include "../Settings.hpp"
|
||||
|
||||
void UDPSend(Client*c,const std::string&Data);
|
||||
void TCPSend(Client*c,const std::string&Data);
|
||||
|
||||
int OpenID(){
|
||||
int ID = 0;
|
||||
bool found;
|
||||
do {
|
||||
found = true;
|
||||
for (Client *c : Clients) {
|
||||
if(c->GetID() == ID){
|
||||
found = false;
|
||||
ID++;
|
||||
}
|
||||
}
|
||||
}while (!found);
|
||||
return ID;
|
||||
}
|
||||
|
||||
void Respond(Client*c, const std::string& MSG, bool Rel){
|
||||
if(Rel)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);
|
||||
else UDPSend(client,Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePlayers(){
|
||||
std::string Packet = "Ss" + std::to_string(Clients.size())+"/"+std::to_string(MaxPlayers) + ":";
|
||||
for (Client*c : Clients) {
|
||||
Packet += c->GetName() + ",";
|
||||
}
|
||||
Packet = Packet.substr(0,Packet.length()-1);
|
||||
SendToAll(nullptr, Packet,true,true);
|
||||
}
|
||||
|
||||
void OnDisconnect(Client*c,bool Timed){
|
||||
std::string Packet = "Od:" + std::to_string(c->GetID());
|
||||
SendToAll(c, Packet,false,true);
|
||||
//if(Timed)Packet = "L"+c->GetName()+" Timed out!";
|
||||
Packet = "L"+c->GetName()+" Left the server!";
|
||||
SendToAll(c, Packet,false,true);
|
||||
Packet.clear();
|
||||
Clients.erase(c); ///Removes the Client from the list
|
||||
}
|
||||
|
||||
void OnConnect(Client*c){
|
||||
c->SetID(OpenID());
|
||||
std::cout << "New Client Created! ID : " << c->GetID() << std::endl;
|
||||
Respond(c,"NR",true);
|
||||
Respond(c,"M"+MapName,true); //Send the Map on connect
|
||||
}
|
||||
Reference in New Issue
Block a user