SQCSimulator2023/SQCSim-srv/server.cpp
2023-10-26 11:17:13 -04:00

274 lines
7.1 KiB
C++

#include "server.h"
Server::Server(LogDest log) {
m_log = log;
if (log == LOG_DEST::LOGFILE) {
m_logfile = std::ofstream("server.log", std::ofstream::out);
if (!m_logfile.is_open()) {
m_log = LOG_DEST::CONSOLE; // Fallback console.
Log("Ouverture fichier log: repli vers console.", true, false);
}
}
}
Server::~Server() {
if (m_logfile.is_open())
m_logfile.close();
if (m_sock_udp)
closesocket(m_sock_udp);
if (m_sock_tcp)
closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players)
closesocket(player->getSock());
m_players.clear();
#ifdef _WIN32
WSACleanup();
#endif
}
int Server::Init() {
Log("Initialisation du serveur...", false, false);
#ifdef _WIN32
if (WSAStartup(MAKEWORD(2, 2), &m_wsaData) != 0) { /* Initialisation de l'environnement reseau (Windows only) */
Log("Initialisation WinSock.", true, true);
return 1;
}
#endif
m_sock_udp = socket(AF_INET, SOCK_DGRAM, 0);
if (m_sock_udp == INVALID_SOCKET) { /* Creation du socket UDP */
Log("Creation Socket UDP.", true, true);
return 2;
}
m_sock_tcp = socket(AF_INET, SOCK_STREAM, 0);
if (m_sock_tcp == INVALID_SOCKET) { /* Creation du socket TCP */
Log("Creation Socket TCP.", true, true);
return 3;
}
/* Creation structure donnes descripteur du socket serveur */
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(SRV_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */
Log("Association Socket UDP.", true, true);
return 4;
}
if (bind(m_sock_tcp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket TCP au port */
Log("Association Socket TCP.", true, true);
return 5;
}
return 0;
}
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;
std::cout << std::endl;
} while (m_game.countdown < 1);
do {
Log("Entrez le seed de la partie: ", false, false);
std::cin >> m_game.seed;
std::cout << std::endl;
} while (m_game.seed < 1);
do {
Log("Entrez le nombre de joueurs: ", false, false);
std::cin >> nbrjoueurs;
std::cout << std::endl;
} while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS);
m_game.gameType = 1;
if (listen(m_sock_tcp, MAX_CONNECTIONS) < 0) {
Log("Écoute sur le port TCP.", true, true);
return 1;
}
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);
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
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));
if (recv(sock, buf, buflen, 0) > 0) {
LoginInfo* log;
PlayerInfo play;
Packet pck = getPack(buf, buflen);
if (pck.type != PACKET_TYPE::LOGINF) {
Log("Paquet invalide.", true, false);
if (pck.type != PACKET_TYPE::ERR)
emptyPack(pck);
continue; // Passer au prochain appel si c'est pas un LoginInfo ou un LoginInfo invalide qui rentre.
}
log = (LoginInfo*)pck.ptr;
log->sid = getUniqueId();
log->tid = 0;
Log(str.append(" Nom: ").append(log->name), false, false);
str.clear();
Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false);
sendPack<LoginInfo>(sock, log, &buf, &buflen);
play.id = getUniqueId();
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& [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->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
buflen = BUFFER_LENGTH;
}
m_players[log->sid] = conn;
delete log; // le pck va se supprimer tout seul, mais le pointer du log qui vient de lui, non.
if (++nbrconn >= nbrjoueurs)
readystart = true;
}
}
}
delete[] buf;
delete[] strbuf;
return 0;
}
void Server::Run() {
char* buf = new char[BUFFER_LENGTH];
uint32_t buflen = BUFFER_LENGTH;
netprot::Input in;
sockaddr_in sockad;
addrlen_t socklen = sizeof(sockad);
Log("Debut de la partie...", false, false);
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.
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;
}
}
inline std::string Server::LogTimestamp() {
time_t rawtime;
tm timeinfo;
char buffer[50];
time(&rawtime);
#ifdef _WIN32
localtime_s(&timeinfo, &rawtime);
#else
localtime_r(&rawtime, &timeinfo);
#endif
strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", &timeinfo);
std::string str(buffer);
return "[" + str + "] ";
}
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
switch (m_log) {
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE:
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
}
if (is_fatal) {
if (m_sock_udp)
closesocket(m_sock_udp);
if (m_sock_tcp)
closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players)
closesocket(player->getSock());
m_players.clear();
#ifdef _WIN32
WSACleanup();
#endif
exit(-1);
}
}
void Server::buildIdList(size_t size) {
std::set<uint64_t> lst;
srand(time(NULL));
do lst.insert(((uint64_t)rand() << 32 | rand())); // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIiIIiiIiINE!
while (lst.size() < size);
m_ids = std::vector<uint64_t>(lst.begin(), lst.end());
}
uint64_t Server::getUniqueId() {
uint64_t id = m_ids.back();
m_ids.pop_back();
return id;
}
// Test serialize/deserialize:
/*
netprot::LoginInfo log, log2;
std::cout << "Nom? ";
std::cin.getline(log.name, 32); // NO! STD::CIN >> VARIABLE;! EVEEEEEERRRR!!!
log.sid = 12345;
char* buf = new char[150];
uint32_t buflen = 150;
netprot::Serialize(&log, &buf, &buflen);
bool is_work = netprot::Deserialize(&log2, buf, buflen);
std::string str;
str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid));
Log(str, false, false);
*/