This commit is contained in:
MarcEricMartel 2023-09-28 09:15:39 -04:00
parent df52d4a084
commit 7e043eb9f8
3 changed files with 65 additions and 19 deletions

View File

@ -8,6 +8,9 @@
#include <string> #include <string>
#define MAX_CONNECTIONS 16 #define MAX_CONNECTIONS 16
#define ID_LIST_SIZE 127
#define BUFFER_LENGTH 150
typedef unsigned char LogDest; typedef unsigned char LogDest;
enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST }; enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST };

View File

@ -61,9 +61,6 @@ int Server::Init() {
return 5; return 5;
} }
for (auto& conn : m_conn)
conn = nullptr;
return 0; return 0;
} }
@ -80,6 +77,12 @@ int Server::Ready() {
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false); Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
buildIdList(ID_LIST_SIZE);
m_game.countdown = 360;
m_game.gameType = 1;
m_game.seed = 9370707;
while (!readystart) { while (!readystart) {
sockaddr_in sockad; sockaddr_in sockad;
int addrlen = sizeof(sockad); int addrlen = sizeof(sockad);
@ -89,16 +92,19 @@ int Server::Ready() {
Log("Erreur de connexion", true, false); Log("Erreur de connexion", true, false);
else if (sock > 0) { else if (sock > 0) {
std::string str = "Nouvelle connection provenant de: "; std::string str = "Nouvelle connection provenant de: ";
char* strbuf = new char[150]; char* strbuf = new char[BUFFER_LENGTH];
uint32_t strbuflen = 150; uint32_t strbuflen = BUFFER_LENGTH;
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, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port));
if (recv(sock, buf, buflen, 0) > 0) { if (recv(sock, buf, buflen, 0) > 0) {
netprot::LoginInfo log; netprot::LoginInfo log;
netprot::PlayerInfo play;
if (netprot::Deserialize(&log, buf, buflen)) { if (netprot::Deserialize(&log, buf, buflen)) {
log.sid = ((uint64_t)rand() << 25) % 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE! log.sid = getUniqueId();
log.tid = 123456789;
log.tid = 0;
str.append(" Nom: ").append(log.name); str.append(" Nom: ").append(log.name);
Log(str, false, false); Log(str, false, false);
str = ""; str = "";
@ -106,17 +112,21 @@ int Server::Ready() {
str.append(log.name).append(" SID: [").append(std::to_string(log.sid).append("]")); str.append(log.name).append(" SID: [").append(std::to_string(log.sid).append("]"));
Log(str, false, false); Log(str, false, false);
netprot::Serialize(&log, &buf, &buflen); //netprot::Serialize(&log, &buf, &buflen);
send(sock, buf, buflen, 0); //send(sock, buf, buflen, 0);
buflen = 150; //buflen = 150;
Sleep(300); sendPack<netprot::LoginInfo>(sock, &log, &buf, &buflen);
netprot::GameInfo ginfo;
ginfo.countdown = 360; play.id = getUniqueId();
ginfo.gameType = 1; memcpy(play.name, log.name, std::strlen(log.name) + 1);
ginfo.seed = 9370707; play.tid = log.tid;
netprot::Serialize(&ginfo, &buf, &buflen);
send(sock, buf, buflen, 0); //netprot::Serialize(&m_game, &buf, &buflen);
Sleep(10000); //send(sock, buf, buflen, 0);
sendPack<netprot::GameInfo>(sock, &m_game, &buf, &buflen);
Connection* conn = new Connection(sock, sockad, log, play);
m_players[log.sid] = conn;
readystart = true; readystart = true;
} }
} }
@ -171,6 +181,21 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
} }
} }
void Server::buildIdList(size_t size) {
std::set<uint64_t> lst;
do lst.insert(((uint64_t)rand() << 25) % 8675309); // 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: // Test serialize/deserialize:
/* /*
netprot::LoginInfo log, log2; netprot::LoginInfo log, log2;

View File

@ -3,6 +3,7 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <set>
#include <string> #include <string>
#include "../SQCSim-common/world.h" #include "../SQCSim-common/world.h"
#include "../SQCSim-common/netprotocol.h" #include "../SQCSim-common/netprotocol.h"
@ -27,10 +28,27 @@ private:
LogDest m_log; LogDest m_log;
std::ofstream m_logfile; std::ofstream m_logfile;
Connection* m_conn[MAX_CONNECTIONS]; std::map<uint64_t, Connection*> m_players;
std::map <Timestamp, netprot::Chat> m_chatlog;
std::vector<uint64_t> m_ids;
netprot::GameInfo m_game;
World* m_world = nullptr; World* m_world = nullptr;
std::string Timestamp(); std::string Timestamp();
void Log(std::string str, bool is_error, bool is_fatal); void Log(std::string str, bool is_error, bool is_fatal);
void buildIdList(size_t size);
uint64_t getUniqueId();
template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
}; };
template <class T>
void Server::sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
netprot::Serialize(pack, buf, buflen);
send(sock, *buf, *buflen, 0);
*buflen = BUFFER_LENGTH;
}
#endif #endif