Cleanup de srv

This commit is contained in:
Marc-Éric Martel 2023-10-26 10:39:08 -04:00
parent 9b4defba2a
commit f5a1906877
3 changed files with 29 additions and 20 deletions

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.18.4)
project(SQCSim-Server VERSION 0.1)
add_compile_options("-Wno-narrowing")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_STANDARD_REQUIRED True)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../out")

View File

@ -11,6 +11,10 @@
#define ID_LIST_SIZE 127
#define SRV_MANUAL_SETUP true
#ifdef _WIN32
#define strcpy strcpy_s
#endif
typedef unsigned char LogDest;
enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST };

View File

@ -67,6 +67,12 @@ 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 durée de la partie: ", false, false);
std::cin >> m_game.countdown;
@ -90,15 +96,11 @@ int Server::Ready() {
return 1;
}
char* buf = new char[150];
uint32_t buflen = 150;
bool readystart = false;
srand(time(NULL));
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
buildIdList(ID_LIST_SIZE);
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
while (!readystart) {
sockaddr_in sockad;
addrlen_t addrlen = sizeof(sockad);
@ -107,9 +109,6 @@ int Server::Ready() {
if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
static char* strbuf = new char[BUFFER_LENGTH];
uint32_t strbuflen = BUFFER_LENGTH;
std::string str = "Nouvelle connection provenant de: ";
str.append(inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port));
@ -137,17 +136,17 @@ int Server::Ready() {
sendPack<LoginInfo>(sock, log, &buf, &buflen);
play.id = getUniqueId();
strcpy_s(play.name, log->name);
strcpy(play.name, log->name);
play.tid = log->tid;
sendPack<GameInfo>(sock, &m_game, &buf, &buflen);
Connection* conn = new Connection(sock, sockad, *log, play);
for (auto& player : m_players) {
sendPack<PlayerInfo>(player.second->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs déjà connectés
for (auto& [key, player] : m_players) {
sendPack<PlayerInfo>(player->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs déjà connectés
buflen = BUFFER_LENGTH;
sendPack<PlayerInfo>(sock, player.second->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
sendPack<PlayerInfo>(sock, player->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
buflen = BUFFER_LENGTH;
}
@ -160,6 +159,8 @@ int Server::Ready() {
}
}
}
delete buf;
delete strbuf;
return 0;
}
@ -168,12 +169,12 @@ void Server::Run() {
uint32_t buflen = BUFFER_LENGTH;
netprot::Input in;
sockaddr_in sockad;
int socklen = sizeof(sockad);
addrlen_t socklen = sizeof(sockad);
Log("Debut de la partie...", false, false);
for (auto& conn : m_players) // Gérer le point de spawn des joueurs.
conn.second->player = new Player(Vector3f(64., 128., 64.));
for (auto& [key, conn]: m_players) // Gérer le point de spawn des joueurs.
conn->player = new Player(Vector3f(64., 128., 64.));
// TODO: Faire un premier sync pour que les joueurs partent à la bonne place.
@ -208,10 +209,11 @@ inline std::string Server::LogTimestamp() {
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
switch (m_log) {
case LOG_DEST::LOGFILE:
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case LOG_DEST::CONSOLE:
case CONSOLE:
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
@ -222,6 +224,9 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
closesocket(m_sock_udp);
if (m_sock_tcp)
closesocket(m_sock_tcp);
for (auto& [key, player] : m_players)
closesocket(player->getSock());
m_players.clear();
#ifdef _WIN32
WSACleanup();
#endif