Major rewrite of the network

This commit is contained in:
Anonymous275
2020-05-10 23:37:45 +03:00
parent b0c6c2bac4
commit 131e64b706
18 changed files with 549 additions and 6400 deletions

View 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
}