diff --git a/SQCSim-srv/cmake/CMakeLists.txt b/SQCSim-srv/cmake/CMakeLists.txt index 4a1f624..ef8f3f2 100644 --- a/SQCSim-srv/cmake/CMakeLists.txt +++ b/SQCSim-srv/cmake/CMakeLists.txt @@ -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") diff --git a/SQCSim-srv/define.h b/SQCSim-srv/define.h index b1f4621..eaf044e 100644 --- a/SQCSim-srv/define.h +++ b/SQCSim-srv/define.h @@ -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 }; diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index a9e3538..0ed0387 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -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(sock, log, &buf, &buflen); play.id = getUniqueId(); - strcpy_s(play.name, log->name); + strcpy(play.name, log->name); play.tid = log->tid; sendPack(sock, &m_game, &buf, &buflen); Connection* conn = new Connection(sock, sockad, *log, play); - for (auto& player : m_players) { - sendPack(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(player->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs déjà connectés buflen = BUFFER_LENGTH; - sendPack(sock, player.second->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur. + sendPack(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