Cleanup
This commit is contained in:
parent
df52d4a084
commit
7e043eb9f8
@ -8,6 +8,9 @@
|
||||
#include <string>
|
||||
|
||||
#define MAX_CONNECTIONS 16
|
||||
#define ID_LIST_SIZE 127
|
||||
#define BUFFER_LENGTH 150
|
||||
|
||||
typedef unsigned char LogDest;
|
||||
enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST };
|
||||
|
||||
|
@ -61,9 +61,6 @@ int Server::Init() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
for (auto& conn : m_conn)
|
||||
conn = nullptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -80,6 +77,12 @@ int Server::Ready() {
|
||||
|
||||
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) {
|
||||
sockaddr_in sockad;
|
||||
int addrlen = sizeof(sockad);
|
||||
@ -89,16 +92,19 @@ int Server::Ready() {
|
||||
Log("Erreur de connexion", true, false);
|
||||
else if (sock > 0) {
|
||||
std::string str = "Nouvelle connection provenant de: ";
|
||||
char* strbuf = new char[150];
|
||||
uint32_t strbuflen = 150;
|
||||
char* strbuf = new char[BUFFER_LENGTH];
|
||||
uint32_t strbuflen = BUFFER_LENGTH;
|
||||
|
||||
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) {
|
||||
netprot::LoginInfo log;
|
||||
netprot::PlayerInfo play;
|
||||
if (netprot::Deserialize(&log, buf, buflen)) {
|
||||
log.sid = ((uint64_t)rand() << 25) % 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE!
|
||||
log.tid = 123456789;
|
||||
log.sid = getUniqueId();
|
||||
|
||||
log.tid = 0;
|
||||
|
||||
str.append(" Nom: ").append(log.name);
|
||||
Log(str, false, false);
|
||||
str = "";
|
||||
@ -106,17 +112,21 @@ int Server::Ready() {
|
||||
str.append(log.name).append(" SID: [").append(std::to_string(log.sid).append("]"));
|
||||
Log(str, false, false);
|
||||
|
||||
netprot::Serialize(&log, &buf, &buflen);
|
||||
send(sock, buf, buflen, 0);
|
||||
buflen = 150;
|
||||
Sleep(300);
|
||||
netprot::GameInfo ginfo;
|
||||
ginfo.countdown = 360;
|
||||
ginfo.gameType = 1;
|
||||
ginfo.seed = 9370707;
|
||||
netprot::Serialize(&ginfo, &buf, &buflen);
|
||||
send(sock, buf, buflen, 0);
|
||||
Sleep(10000);
|
||||
//netprot::Serialize(&log, &buf, &buflen);
|
||||
//send(sock, buf, buflen, 0);
|
||||
//buflen = 150;
|
||||
sendPack<netprot::LoginInfo>(sock, &log, &buf, &buflen);
|
||||
|
||||
play.id = getUniqueId();
|
||||
memcpy(play.name, log.name, std::strlen(log.name) + 1);
|
||||
play.tid = log.tid;
|
||||
|
||||
//netprot::Serialize(&m_game, &buf, &buflen);
|
||||
//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;
|
||||
}
|
||||
}
|
||||
@ -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:
|
||||
/*
|
||||
netprot::LoginInfo log, log2;
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include "../SQCSim-common/world.h"
|
||||
#include "../SQCSim-common/netprotocol.h"
|
||||
@ -27,10 +28,27 @@ private:
|
||||
LogDest m_log;
|
||||
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;
|
||||
|
||||
std::string Timestamp();
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user