SQCSimulator2023/SQCSim-srv/server.cpp

319 lines
8.4 KiB
C++
Raw Normal View History

2023-09-24 08:45:40 -04:00
#include "server.h"
2023-10-27 09:51:40 -04:00
Server::Server(LOG_DEST log) {
2023-09-24 11:07:03 -04:00
m_log = log;
if (log == LOG_DEST::LOGFILE) {
2023-09-24 08:45:40 -04:00
m_logfile = std::ofstream("server.log", std::ofstream::out);
if (!m_logfile.is_open()) {
2023-09-24 11:07:03 -04:00
m_log = LOG_DEST::CONSOLE; // Fallback console.
2023-09-24 08:45:40 -04:00
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)
2023-10-26 10:47:17 -04:00
closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players)
closesocket(player->getSock());
m_players.clear();
2023-09-24 08:45:40 -04:00
#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() {
2023-10-18 09:33:56 -04:00
int nbrjoueurs = 0,
nbrconn = 0;
2023-10-26 10:39:08 -04:00
bool readystart = false;
2023-10-18 09:33:56 -04:00
do {
2023-10-27 09:28:34 -04:00
Log("Entrez la duree de la partie: ", false, false);
2023-10-27 09:51:40 -04:00
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
2023-10-27 10:40:53 -04:00
try {
m_game.countdown = std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
Log(e.what(), true, false);
m_game.countdown = 0;
}
2023-10-18 09:33:56 -04:00
} while (m_game.countdown < 1);
do {
Log("Entrez le seed de la partie: ", false, false);
2023-10-27 09:51:40 -04:00
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
2023-10-27 10:40:53 -04:00
try {
m_game.seed = std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
Log(e.what(), true, false);
m_game.seed = 0;
}
2023-10-18 09:33:56 -04:00
} while (m_game.seed < 1);
do {
Log("Entrez le nombre de joueurs: ", false, false);
2023-10-27 09:51:40 -04:00
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
2023-10-27 10:40:53 -04:00
try {
nbrjoueurs = std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
Log(e.what(), true, false);
nbrjoueurs = 0;
}
if (nbrjoueurs <= 0 || nbrjoueurs > MAX_CONNECTIONS)
Log("Nombre de joueurs invalide.", true, false);
} while (nbrjoueurs <= 0 || nbrjoueurs > MAX_CONNECTIONS);
2023-10-18 09:33:56 -04:00
m_game.gameType = 1;
2023-09-24 11:07:03 -04:00
if (listen(m_sock_tcp, MAX_CONNECTIONS) < 0) {
2023-10-26 17:57:42 -04:00
Log("Ecoute sur le port TCP.", true, true);
2023-09-24 11:07:03 -04:00
return 1;
}
2023-09-28 09:15:39 -04:00
buildIdList(ID_LIST_SIZE);
2023-10-26 10:39:08 -04:00
2023-10-26 17:57:42 -04:00
Log("A l'ecoute sur le port: " + std::to_string(SRV_PORT), false, false);
2023-10-26 10:39:08 -04:00
2023-09-24 11:07:03 -04:00
while (!readystart) {
2023-09-27 11:24:41 -04:00
sockaddr_in sockad;
2023-10-24 07:32:16 -04:00
addrlen_t addrlen = sizeof(sockad);
2023-09-27 11:24:41 -04:00
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
2023-10-26 17:57:42 -04:00
std::string str = "Nouvelle connexion provenant de: ";
2023-10-27 09:51:40 -04:00
str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port));
2023-10-27 09:51:40 -04:00
if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) {
2023-10-25 12:16:14 -04:00
LoginInfo* log;
PlayerInfo play;
2023-10-27 09:51:40 -04:00
Packet pck = getPack(&m_buf);
2023-10-25 12:16:14 -04:00
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.
}
2023-10-25 12:16:14 -04:00
log = (LoginInfo*)pck.ptr;
log->sid = getUniqueId();
2023-10-27 09:28:34 -04:00
log->tid = 0; // TODO: À changer si on implemente un mode en equipe.
2023-09-28 09:15:39 -04:00
2023-10-25 12:16:14 -04:00
Log(str.append(" Nom: ").append(log->name), false, false);
str.clear();
2023-09-27 11:24:41 -04:00
2023-10-25 12:16:14 -04:00
Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false);
2023-09-28 09:15:39 -04:00
2023-10-27 09:51:40 -04:00
sendPack<LoginInfo>(sock, log, &m_buf.ptr, &m_buf.len);
2023-10-25 12:16:14 -04:00
play.id = getUniqueId();
2023-10-26 10:39:08 -04:00
strcpy(play.name, log->name);
2023-10-25 12:16:14 -04:00
play.tid = log->tid;
2023-09-28 09:15:39 -04:00
2023-10-27 09:51:40 -04:00
sendPack<GameInfo>(sock, &m_game, &m_buf.ptr, &m_buf.len);
Connection* conn = new Connection(sock, sockad, *log, play);
2023-09-28 09:15:39 -04:00
2023-10-26 10:39:08 -04:00
for (auto& [key, player] : m_players) {
2023-10-27 09:51:40 -04:00
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.
2023-10-25 12:16:14 -04:00
}
m_players[log->sid] = conn;
2023-10-25 12:16:14 -04:00
if (++nbrconn >= nbrjoueurs)
readystart = true;
2023-09-27 11:24:41 -04:00
}
}
2023-09-24 11:07:03 -04:00
}
2023-09-24 08:45:40 -04:00
return 0;
}
void Server::Run() {
2023-10-26 17:57:42 -04:00
Input in;
2023-10-23 17:49:40 -04:00
sockaddr_in sockad;
2023-10-26 10:39:08 -04:00
addrlen_t socklen = sizeof(sockad);
2023-10-23 17:49:40 -04:00
2023-10-25 12:16:14 -04:00
Log("Debut de la partie...", false, false);
2023-10-23 16:21:15 -04:00
2023-10-26 17:57:42 -04:00
for (auto& [key, conn]: m_players) { // Creation des instances de joueurs et premier sync.
2023-10-27 15:19:44 -04:00
conn->player = new Player(Vector3f(8.5f, CHUNK_SIZE_Y + 1.8f, 8.5f));
2023-10-26 17:57:42 -04:00
Player *player = conn->player;
Sync sync;
sync.position = player->GetPosition();
sync.hp = player->GetHP();
sync.sid = key;
sync.ammo = 0;
sync.timestamp = 0;
sync.timer = m_game.countdown;
2023-10-27 15:44:33 -04:00
sendPack<Sync>(conn->getSock(), &sync, &m_buf);
2023-10-26 17:57:42 -04:00
}
2023-10-23 16:21:15 -04:00
while (true) {
2023-10-27 09:51:40 -04:00
if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) {
Packet pck = getPack(&m_buf);
switch (pck.type) {
2023-10-27 12:02:29 -04:00
using enum netprot::PACKET_TYPE;
case ERR:
2023-10-27 09:51:40 -04:00
std::cout << "ERROR!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case INPUT:
2023-10-27 09:51:40 -04:00
std::cout << "INPUT!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case OUTPUT:
2023-10-27 09:51:40 -04:00
std::cout << "OUTPUT!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case SYNC:
2023-10-27 09:51:40 -04:00
std::cout << "SYNC!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case TEAMINF:
2023-10-27 09:51:40 -04:00
std::cout << "TEAMINF!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case SELFINF:
2023-10-27 09:51:40 -04:00
std::cout << "SELFINF!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case PLAYINF:
2023-10-27 09:51:40 -04:00
std::cout << "PLAYINF!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case LOGINF:
2023-10-27 09:51:40 -04:00
std::cout << "LOGINF!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case CHUNKMOD:
2023-10-27 09:51:40 -04:00
std::cout << "CHUNKMOD!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case PLAYERMOD:
2023-10-27 09:51:40 -04:00
std::cout << "PLAYERMOD!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case PICKUPMOD:
2023-10-27 09:51:40 -04:00
std::cout << "PICKUPMOD!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case GAMEINFO:
2023-10-27 09:51:40 -04:00
std::cout << "GAMEINFO!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case ENDINFO:
2023-10-27 09:51:40 -04:00
std::cout << "ENDINFO!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case CHAT:
2023-10-27 09:51:40 -04:00
std::cout << "CHAT!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case ERRLOG:
2023-10-27 09:51:40 -04:00
std::cout << "ERRLOG!" << std::endl;
break;
2023-10-27 12:02:29 -04:00
case LAST_PACK:
2023-10-27 09:51:40 -04:00
std::cout << "wtf?!" << std::endl;
break;
}
emptyPack(pck);
}
2023-10-23 16:21:15 -04:00
}
2023-09-24 08:45:40 -04:00
}
inline std::string Server::LogTimestamp() {
2023-09-24 08:45:40 -04:00
time_t rawtime;
2023-10-26 11:17:13 -04:00
tm timeinfo;
char buffer[50];
2023-09-24 08:45:40 -04:00
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) {
2023-10-27 12:02:29 -04:00
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE: [[fallthrough]]; // Pour dire que c'est voulu que ça traverse vers le case en dessous (C++17!)
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
2023-09-24 08:45:40 -04:00
}
if (is_fatal) {
2023-10-26 17:18:30 -04:00
if (m_logfile.is_open())
m_logfile.close();
2023-09-24 08:45:40 -04:00
if (m_sock_udp)
closesocket(m_sock_udp);
if (m_sock_tcp)
closesocket(m_sock_tcp);
2023-10-26 17:57:42 -04:00
for (const auto& [key, player] : m_players) {
delete player->player;
2023-10-26 10:39:08 -04:00
closesocket(player->getSock());
2023-10-26 17:57:42 -04:00
}
2023-10-26 10:39:08 -04:00
m_players.clear();
2023-09-24 08:45:40 -04:00
#ifdef _WIN32
WSACleanup();
#endif
2023-10-26 10:47:17 -04:00
exit(-1);
2023-09-24 08:45:40 -04:00
}
}
2023-09-28 09:15:39 -04:00
void Server::buildIdList(size_t size) {
std::set<uint64_t> lst;
2023-10-26 10:55:36 -04:00
srand(time(NULL));
2023-10-24 07:32:16 -04:00
do lst.insert(((uint64_t)rand() << 32 | rand())); // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIiIIiiIiINE!
2023-09-28 09:15:39 -04:00
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;
}