Ev'ryday I'm bufferin'.

This commit is contained in:
Marc-Eric Martel
2023-10-27 09:51:40 -04:00
parent 0bc7f29492
commit 9ef44a3993
4 changed files with 82 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
#include "server.h"
Server::Server(LogDest log) {
Server::Server(LOG_DEST log) {
m_log = log;
if (log == LOG_DEST::LOGFILE) {
m_logfile = std::ofstream("server.log", std::ofstream::out);
@@ -70,25 +70,21 @@ int Server::Init() {
int Server::Ready() {
int nbrjoueurs = 0,
nbrconn = 0;
char *buf = new char[BUFFER_LENGTH],
*strbuf = new char[BUFFER_LENGTH];
uint32_t buflen = BUFFER_LENGTH,
strbuflen = BUFFER_LENGTH;
bool readystart = false;
do {
Log("Entrez la duree de la partie: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH);
m_game.countdown = std::stoi(strbuf);
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
m_game.countdown = std::stoi(m_buf.ptr);
} while (m_game.countdown < 1);
do {
Log("Entrez le seed de la partie: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH);
m_game.seed = std::stoi(strbuf);
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
m_game.seed = std::stoi(m_buf.ptr);
} while (m_game.seed < 1);
do {
Log("Entrez le nombre de joueurs: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH);
nbrjoueurs = std::stoi(strbuf);
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
nbrjoueurs = std::stoi(m_buf.ptr);
} while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS);
m_game.gameType = 1;
@@ -111,13 +107,13 @@ int Server::Ready() {
Log("Erreur de connexion", true, false);
else if (sock > 0) {
std::string str = "Nouvelle connexion provenant de: ";
str.append(inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port));
str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port));
if (recv(sock, buf, buflen, 0) > 0) {
if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) {
LoginInfo* log;
PlayerInfo play;
Packet pck = getPack(buf, buflen);
Packet pck = getPack(&m_buf);
if (pck.type != PACKET_TYPE::LOGINF) {
Log("Paquet invalide.", true, false);
if (pck.type != PACKET_TYPE::ERR)
@@ -134,21 +130,19 @@ int Server::Ready() {
Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false);
sendPack<LoginInfo>(sock, log, &buf, &buflen);
sendPack<LoginInfo>(sock, log, &m_buf.ptr, &m_buf.len);
play.id = getUniqueId();
strcpy(play.name, log->name);
play.tid = log->tid;
sendPack<GameInfo>(sock, &m_game, &buf, &buflen);
sendPack<GameInfo>(sock, &m_game, &m_buf.ptr, &m_buf.len);
Connection* conn = new Connection(sock, sockad, *log, play);
for (auto& [key, player] : m_players) {
sendPack<PlayerInfo>(player->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
buflen = BUFFER_LENGTH;
sendPack<PlayerInfo>(sock, player->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
buflen = BUFFER_LENGTH;
sendPack<PlayerInfo>(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
sendPack<PlayerInfo>(sock, player->getInfo(), &m_buf); // et envoyer les infos des joueurs distants au nouveau joueur.
}
m_players[log->sid] = conn;
@@ -158,14 +152,10 @@ int Server::Ready() {
}
}
}
delete[] buf;
delete[] strbuf;
return 0;
}
void Server::Run() {
char* buf = new char[BUFFER_LENGTH];
uint32_t buflen = BUFFER_LENGTH;
Input in;
sockaddr_in sockad;
addrlen_t socklen = sizeof(sockad);
@@ -182,16 +172,64 @@ void Server::Run() {
sync.ammo = 0;
sync.timestamp = 0;
sync.timer = m_game.countdown;
sendPackTo<Sync>(conn->getSock(), &sync, &buf, &buflen, conn->getAddr());
sendPackTo<Sync>(conn->getSock(), &sync, &m_buf, conn->getAddr());
}
while (true) {
if (recvfrom(m_sock_udp, buf, BUFFER_LENGTH, 0, (sockaddr*)&sockad, &socklen) > 0) {
Deserialize(&in, buf, buflen);
std::cout << "Id: " << in.sid << "\r\n"
<< "Direction: { " << in.direction.x << ", " << in.direction.y << ", " << in.direction.z << " }" << "\r\n";
}
std::cout << "!" << std::endl;
if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) {
Packet pck = getPack(&m_buf);
switch (pck.type) {
case netprot::PACKET_TYPE::ERR:
std::cout << "ERROR!" << std::endl;
break;
case netprot::PACKET_TYPE::INPUT:
std::cout << "INPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::OUTPUT:
std::cout << "OUTPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::SYNC:
std::cout << "SYNC!" << std::endl;
break;
case netprot::PACKET_TYPE::TEAMINF:
std::cout << "TEAMINF!" << std::endl;
break;
case netprot::PACKET_TYPE::SELFINF:
std::cout << "SELFINF!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYINF:
std::cout << "PLAYINF!" << std::endl;
break;
case netprot::PACKET_TYPE::LOGINF:
std::cout << "LOGINF!" << std::endl;
break;
case netprot::PACKET_TYPE::CHUNKMOD:
std::cout << "CHUNKMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYERMOD:
std::cout << "PLAYERMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PICKUPMOD:
std::cout << "PICKUPMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::GAMEINFO:
std::cout << "GAMEINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::ENDINFO:
std::cout << "ENDINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::CHAT:
std::cout << "CHAT!" << std::endl;
break;
case netprot::PACKET_TYPE::ERRLOG:
std::cout << "ERRLOG!" << std::endl;
break;
case netprot::PACKET_TYPE::LAST_PACK:
std::cout << "wtf?!" << std::endl;
break;
}
emptyPack(pck);
}
}
}