Gros refactoring sale.

This commit is contained in:
Marc-Éric Martel 2023-10-27 09:28:34 -04:00
parent 60cc69a407
commit 0bc7f29492
3 changed files with 182 additions and 156 deletions

View File

@ -1,29 +1,29 @@
#include "netprotocol.h" #include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::INPUT; *buf[0] = (char)netprot::PACKET_TYPE::INPUT;
uint64_t time = in->timestamp; uint64_t time = in->timestamp;
uint8_t time8[sizeof(uint64_t)] = {(time >> 56) & 0xFF, uint8_t time8[sizeof(uint64_t)] = {(uint8_t)((time >> 56) & 0xFF),
(time >> 48) & 0xFF, (uint8_t)((time >> 48) & 0xFF),
(time >> 40) & 0xFF, (uint8_t)((time >> 40) & 0xFF),
(time >> 32) & 0xFF, (uint8_t)((time >> 32) & 0xFF),
(time >> 24) & 0xFF, (uint8_t)((time >> 24) & 0xFF),
(time >> 16) & 0xFF, (uint8_t)((time >> 16) & 0xFF),
(time >> 8 ) & 0xFF, (uint8_t)((time >> 8 ) & 0xFF),
time & 0xFF}; (uint8_t)(time & 0xFF)};
memcpy(*buf + 1, time8, sizeof(uint64_t)); memcpy(*buf + 1, time8, sizeof(uint64_t));
uint64_t sid = in->sid; uint64_t sid = in->sid;
uint8_t sid8[sizeof(uint64_t)] = {(sid >> 56) & 0xFF, uint8_t sid8[sizeof(uint64_t)] = {(uint8_t)((sid >> 56) & 0xFF),
(sid >> 48) & 0xFF, (uint8_t)((sid >> 48) & 0xFF),
(sid >> 40) & 0xFF, (uint8_t)((sid >> 40) & 0xFF),
(sid >> 32) & 0xFF, (uint8_t)((sid >> 32) & 0xFF),
(sid >> 24) & 0xFF, (uint8_t)((sid >> 24) & 0xFF),
(sid >> 16) & 0xFF, (uint8_t)((sid >> 16) & 0xFF),
(sid >> 8 ) & 0xFF, (uint8_t)((sid >> 8 ) & 0xFF),
sid & 0xFF}; (uint8_t)( sid & 0xFF)};
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t)); memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
@ -40,21 +40,21 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
uint32_t vec[3]; uint32_t vec[3];
memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour dénaturer les floats. memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour d<EFBFBD>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = { uint8_t vec8[3 * sizeof(uint32_t)] = {
(vec[0] >> 24) & 0xFF, (uint8_t)((vec[0] >> 24) & 0xFF),
(vec[0] >> 16) & 0xFF, (uint8_t)((vec[0] >> 16) & 0xFF),
(vec[0] >> 8) & 0xFF, (uint8_t)((vec[0] >> 8) & 0xFF),
vec[0] & 0xFF, (uint8_t)(vec[0] & 0xFF),
(vec[1] >> 24) & 0xFF, (uint8_t)((vec[1] >> 24) & 0xFF),
(vec[1] >> 16) & 0xFF, (uint8_t)((vec[1] >> 16) & 0xFF),
(vec[1] >> 8) & 0xFF, (uint8_t)((vec[1] >> 8) & 0xFF),
vec[1] & 0xFF, (uint8_t)(vec[1] & 0xFF),
(vec[2] >> 24) & 0xFF, (uint8_t)((vec[2] >> 24) & 0xFF),
(vec[2] >> 16) & 0xFF, (uint8_t)((vec[2] >> 16) & 0xFF),
(vec[2] >> 8) & 0xFF, (uint8_t)((vec[2] >> 8) & 0xFF),
vec[2] & 0xFF}; (uint8_t)(vec[2] & 0xFF)};
memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3); memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
@ -62,33 +62,33 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) { void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::OUTPUT; *buf[0] = (char)netprot::PACKET_TYPE::OUTPUT;
} }
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::SYNC; *buf[0] = (char)netprot::PACKET_TYPE::SYNC;
} }
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::TEAMINF; *buf[0] = (char)netprot::PACKET_TYPE::TEAMINF;
size_t namesize = std::strlen(tinfo->name) + 1; size_t namesize = std::strlen(tinfo->name) + 1;
memcpy(*buf + 1, &tinfo->name, namesize); memcpy(*buf + 1, &tinfo->name, namesize);
uint64_t tid = tinfo->id; uint64_t tid = tinfo->id;
uint8_t tid8[sizeof(uint64_t)] = { uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF, (uint8_t)((tid >> 56) & 0xFF),
(tid >> 48) & 0xFF, (uint8_t)((tid >> 48) & 0xFF),
(tid >> 40) & 0xFF, (uint8_t)((tid >> 40) & 0xFF),
(tid >> 32) & 0xFF, (uint8_t)((tid >> 32) & 0xFF),
(tid >> 24) & 0xFF, (uint8_t)((tid >> 24) & 0xFF),
(tid >> 16) & 0xFF, (uint8_t)((tid >> 16) & 0xFF),
(tid >> 8) & 0xFF, (uint8_t)((tid >> 8) & 0xFF),
tid & 0xFF (uint8_t)(tid & 0xFF)
}; };
memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t)); memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t));
@ -97,35 +97,35 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::LOGINF; *buf[0] = (char)netprot::PACKET_TYPE::LOGINF;
size_t namesize = std::strlen(linfo->name) + 1; size_t namesize = std::strlen(linfo->name) + 1;
memcpy(*buf + 1, &linfo->name, namesize); memcpy(*buf + 1, &linfo->name, namesize);
uint64_t sid = linfo->sid; uint64_t sid = linfo->sid;
uint8_t sid8[sizeof(uint64_t)] = { uint8_t sid8[sizeof(uint64_t)] = {
(sid >> 56) & 0xFF, (uint8_t)((sid >> 56) & 0xFF),
(sid >> 48) & 0xFF, (uint8_t)((sid >> 48) & 0xFF),
(sid >> 40) & 0xFF, (uint8_t)((sid >> 40) & 0xFF),
(sid >> 32) & 0xFF, (uint8_t)((sid >> 32) & 0xFF),
(sid >> 24) & 0xFF, (uint8_t)((sid >> 24) & 0xFF),
(sid >> 16) & 0xFF, (uint8_t)((sid >> 16) & 0xFF),
(sid >> 8) & 0xFF, (uint8_t)((sid >> 8) & 0xFF),
sid & 0xFF (uint8_t)(sid & 0xFF)
}; };
memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t)); memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t));
uint64_t tid = linfo->tid; uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = { uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF, (uint8_t)((tid >> 56) & 0xFF),
(tid >> 48) & 0xFF, (uint8_t)((tid >> 48) & 0xFF),
(tid >> 40) & 0xFF, (uint8_t)((tid >> 40) & 0xFF),
(tid >> 32) & 0xFF, (uint8_t)((tid >> 32) & 0xFF),
(tid >> 24) & 0xFF, (uint8_t)((tid >> 24) & 0xFF),
(tid >> 16) & 0xFF, (uint8_t)((tid >> 16) & 0xFF),
(tid >> 8) & 0xFF, (uint8_t)((tid >> 8) & 0xFF),
tid & 0xFF (uint8_t)(tid & 0xFF)
}; };
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
@ -134,35 +134,35 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::PLAYINF; *buf[0] = (char)netprot::PACKET_TYPE::PLAYINF;
size_t namesize = std::strlen(pinfo->name) + 1; size_t namesize = std::strlen(pinfo->name) + 1;
memcpy(*buf + 1, &pinfo->name, namesize); memcpy(*buf + 1, &pinfo->name, namesize);
uint64_t id = pinfo->id; uint64_t id = pinfo->id;
uint8_t id8[sizeof(uint64_t)] = { uint8_t id8[sizeof(uint64_t)] = {
(id >> 56) & 0xFF, (uint8_t)((id >> 56) & 0xFF),
(id >> 48) & 0xFF, (uint8_t)((id >> 48) & 0xFF),
(id >> 40) & 0xFF, (uint8_t)((id >> 40) & 0xFF),
(id >> 32) & 0xFF, (uint8_t)((id >> 32) & 0xFF),
(id >> 24) & 0xFF, (uint8_t)((id >> 24) & 0xFF),
(id >> 16) & 0xFF, (uint8_t)((id >> 16) & 0xFF),
(id >> 8) & 0xFF, (uint8_t)((id >> 8) & 0xFF),
id & 0xFF (uint8_t)(id & 0xFF)
}; };
memcpy(*buf + namesize + 2, id8, sizeof(uint64_t)); memcpy(*buf + namesize + 2, id8, sizeof(uint64_t));
uint64_t tid = pinfo->tid; uint64_t tid = pinfo->tid;
uint8_t tid8[sizeof(uint64_t)] = { uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF, (uint8_t)((tid >> 56) & 0xFF),
(tid >> 48) & 0xFF, (uint8_t)((tid >> 48) & 0xFF),
(tid >> 40) & 0xFF, (uint8_t)((tid >> 40) & 0xFF),
(tid >> 32) & 0xFF, (uint8_t)((tid >> 32) & 0xFF),
(tid >> 24) & 0xFF, (uint8_t)((tid >> 24) & 0xFF),
(tid >> 16) & 0xFF, (uint8_t)((tid >> 16) & 0xFF),
(tid >> 8) & 0xFF, (uint8_t)((tid >> 8) & 0xFF),
tid & 0xFF (uint8_t)(tid & 0xFF)
}; };
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
@ -171,46 +171,46 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::GAMEINFO; *buf[0] = (char)netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed; uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = { uint8_t seed8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF, (uint8_t)((game >> 56) & 0xFF),
(game >> 48) & 0xFF, (uint8_t)((game >> 48) & 0xFF),
(game >> 40) & 0xFF, (uint8_t)((game >> 40) & 0xFF),
(game >> 32) & 0xFF, (uint8_t)((game >> 32) & 0xFF),
(game >> 24) & 0xFF, (uint8_t)((game >> 24) & 0xFF),
(game >> 16) & 0xFF, (uint8_t)((game >> 16) & 0xFF),
(game >> 8) & 0xFF, (uint8_t)((game >> 8) & 0xFF),
game & 0xFF (uint8_t)(game & 0xFF)
}; };
memcpy(*buf + 1, seed8, sizeof(uint64_t)); memcpy(*buf + 1, seed8, sizeof(uint64_t));
game = ginfo->countdown; game = ginfo->countdown;
uint8_t count8[sizeof(uint64_t)] = { uint8_t count8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF, (uint8_t)((game >> 56) & 0xFF),
(game >> 48) & 0xFF, (uint8_t)((game >> 48) & 0xFF),
(game >> 40) & 0xFF, (uint8_t)((game >> 40) & 0xFF),
(game >> 32) & 0xFF, (uint8_t)((game >> 32) & 0xFF),
(game >> 24) & 0xFF, (uint8_t)((game >> 24) & 0xFF),
(game >> 16) & 0xFF, (uint8_t)((game >> 16) & 0xFF),
(game >> 8) & 0xFF, (uint8_t)((game >> 8) & 0xFF),
game & 0xFF (uint8_t)(game & 0xFF)
}; };
memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t)); memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t));
game = ginfo->countdown; game = ginfo->countdown;
uint8_t gtype8[sizeof(uint64_t)] = { uint8_t gtype8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF, (uint8_t)((game >> 56) & 0xFF),
(game >> 48) & 0xFF, (uint8_t)((game >> 48) & 0xFF),
(game >> 40) & 0xFF, (uint8_t)((game >> 40) & 0xFF),
(game >> 32) & 0xFF, (uint8_t)((game >> 32) & 0xFF),
(game >> 24) & 0xFF, (uint8_t)((game >> 24) & 0xFF),
(game >> 16) & 0xFF, (uint8_t)((game >> 16) & 0xFF),
(game >> 8) & 0xFF, (uint8_t)((game >> 8) & 0xFF),
game & 0xFF (uint8_t)(game & 0xFF)
}; };
memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t)); memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t));
@ -219,46 +219,46 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::CHAT; *buf[0] = (char)netprot::PACKET_TYPE::CHAT;
uint64_t src = chat->src_id; uint64_t src = chat->src_id;
uint8_t src8[sizeof(uint64_t)] = { uint8_t src8[sizeof(uint64_t)] = {
(src >> 56) & 0xFF, (uint8_t)((src >> 56) & 0xFF),
(src >> 48) & 0xFF, (uint8_t)((src >> 48) & 0xFF),
(src >> 40) & 0xFF, (uint8_t)((src >> 40) & 0xFF),
(src >> 32) & 0xFF, (uint8_t)((src >> 32) & 0xFF),
(src >> 24) & 0xFF, (uint8_t)((src >> 24) & 0xFF),
(src >> 16) & 0xFF, (uint8_t)((src >> 16) & 0xFF),
(src >> 8) & 0xFF, (uint8_t)((src >> 8) & 0xFF),
src & 0xFF (uint8_t)(src & 0xFF)
}; };
memcpy(*buf + 1, src8, sizeof(uint64_t)); memcpy(*buf + 1, src8, sizeof(uint64_t));
uint64_t dst = chat->dest_id; uint64_t dst = chat->dest_id;
uint8_t dst8[sizeof(uint64_t)] = { uint8_t dst8[sizeof(uint64_t)] = {
(dst >> 56) & 0xFF, (uint8_t)((dst >> 56) & 0xFF),
(dst >> 48) & 0xFF, (uint8_t)((dst >> 48) & 0xFF),
(dst >> 40) & 0xFF, (uint8_t)((dst >> 40) & 0xFF),
(dst >> 32) & 0xFF, (uint8_t)((dst >> 32) & 0xFF),
(dst >> 24) & 0xFF, (uint8_t)((dst >> 24) & 0xFF),
(dst >> 16) & 0xFF, (uint8_t)((dst >> 16) & 0xFF),
(dst >> 8) & 0xFF, (uint8_t)((dst >> 8) & 0xFF),
dst & 0xFF (uint8_t)(dst & 0xFF)
}; };
memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t)); memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t));
uint64_t dstteam = chat->dest_id; uint64_t dstteam = chat->dest_id;
uint8_t dstt8[sizeof(uint64_t)] = { uint8_t dstt8[sizeof(uint64_t)] = {
(dstteam >> 56) & 0xFF, (uint8_t)((dstteam >> 56) & 0xFF),
(dstteam >> 48) & 0xFF, (uint8_t)((dstteam >> 48) & 0xFF),
(dstteam >> 40) & 0xFF, (uint8_t)((dstteam >> 40) & 0xFF),
(dstteam >> 32) & 0xFF, (uint8_t)((dstteam >> 32) & 0xFF),
(dstteam >> 24) & 0xFF, (uint8_t)((dstteam >> 24) & 0xFF),
(dstteam >> 16) & 0xFF, (uint8_t)((dstteam >> 16) & 0xFF),
(dstteam >> 8) & 0xFF, (uint8_t)((dstteam >> 8) & 0xFF),
dstteam & 0xFF (uint8_t)(dstteam & 0xFF)
}; };
memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t)); memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t));
@ -271,7 +271,7 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
} }
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::ERRLOG; *buf[0] = (char)netprot::PACKET_TYPE::ERRLOG;
size_t messize = std::strlen(errlog->mess) + 1; size_t messize = std::strlen(errlog->mess) + 1;
@ -313,13 +313,13 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
uint8_t keys = 0; uint8_t keys = 0;
memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t)); memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t));
in->keys.forward = keys && 0b10000000; in->keys.forward = keys & 0b10000000;
in->keys.backward = keys && 0b01000000; in->keys.backward = keys & 0b01000000;
in->keys.left = keys && 0b00100000; in->keys.left = keys & 0b00100000;
in->keys.right = keys && 0b00010000; in->keys.right = keys & 0b00010000;
in->keys.jump = keys && 0b00001000; in->keys.jump = keys & 0b00001000;
in->keys.shoot = keys && 0b00000100; in->keys.shoot = keys & 0b00000100;
in->keys.block = keys && 0b00000010; in->keys.block = keys & 0b00000010;
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12); memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
@ -555,12 +555,12 @@ bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
return true; return true;
} }
netprot::PacketType netprot::getType(char* buf, const uint32_t buflen) { netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
if (buflen < 1 || if (buflen < 1 ||
buf[0] >= netprot::PACKET_TYPE::LAST_PACK || buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK ||
buf[0] <= netprot::PACKET_TYPE::ERR) buf[0] <= (char)netprot::PACKET_TYPE::ERR)
return netprot::PACKET_TYPE::ERR; return netprot::PACKET_TYPE::ERR;
return buf[0]; return (netprot::PACKET_TYPE)buf[0];
} }
netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
@ -629,6 +629,10 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
return pck; return pck;
} }
netprot::Packet netprot::getPack(netprot::Buffer* buf) {
return netprot::getPack(buf->ptr, buf->len);
}
bool netprot::emptyPack(netprot::Packet pck) { bool netprot::emptyPack(netprot::Packet pck) {
switch (pck.type) { switch (pck.type) {
case PACKET_TYPE::INPUT: case PACKET_TYPE::INPUT:

View File

@ -1,26 +1,33 @@
#ifndef NETPROTOCOL_H__ #ifndef NETPROTOCOL_H__
#define NETPROTOCOL_H__ #define NETPROTOCOL_H__
#include "define.h"
#include <string> #include <string>
#include "define.h"
#include "vector3.h" #include "vector3.h"
/* Protocole Particulier de Partie à Plusieurs Personnes (PPPPP) */ /* Protocole Particulier de Partie a Plusieurs Personnes (PPPPP) */
// Packet: packet[0] = PacketType, packet[1..n-1] = {packet} // Packet: packet[0] = PacketType, packet[1..n-1] = {packet}
namespace netprot { namespace netprot {
typedef uint8_t PacketType; enum class PACKET_TYPE: uint8_t {
enum PACKET_TYPE {
ERR, INPUT, OUTPUT, SYNC, ERR, INPUT, OUTPUT, SYNC,
TEAMINF, SELFINF, PLAYINF, LOGINF, TEAMINF, SELFINF, PLAYINF, LOGINF,
CHUNKMOD, PLAYERMOD, PICKUPMOD, CHUNKMOD, PLAYERMOD, PICKUPMOD,
GAMEINFO, ENDINFO , CHAT, ERRLOG, GAMEINFO, ENDINFO , CHAT, ERRLOG,
LAST_PACK LAST_PACK
}; };
struct Buffer { // Pour pouvoir rendre l'utilisation des buffers plus clean.
char* ptr = new char[BUFFER_LENGTH];
int32_t len = BUFFER_LENGTH;
struct Packet { ~Buffer() { delete[] ptr; }
void* ptr = nullptr; void rstLen() { len = BUFFER_LENGTH; }
};
struct Packet { // Pour pouvoir recevoir les paquets du recv() sans avoir à les aiguiller dans la même thread.
void* ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile.
PACKET_TYPE type; PACKET_TYPE type;
}; };
@ -120,14 +127,19 @@ namespace netprot {
bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli
bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv
PacketType getType(char* buf, uint32_t buflen); PACKET_TYPE getType(char* buf, uint32_t buflen);
Packet getPack(char* buf, uint32_t buflen); Packet getPack(char* buf, uint32_t buflen);
Packet getPack(Buffer* buf);
bool emptyPack(Packet pck); bool emptyPack(Packet pck);
template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen); template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
template <class T> void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad); template <class T> void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad);
template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf);
template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad);
template <class T> template <class T>
void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
netprot::Serialize(pack, buf, buflen); netprot::Serialize(pack, buf, buflen);
@ -143,6 +155,19 @@ namespace netprot {
*buflen = BUFFER_LENGTH; *buflen = BUFFER_LENGTH;
} }
}; template <class T>
void sendPack(SOCKET sock, T* pack, Buffer* buf) {
netprot::Serialize(pack, buf->ptr, buf->len);
send(sock, buf->ptr, buf->len, 0);
buf->rstLen();
}
template <class T>
void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) {
sockaddr_in addr = *sockad;
netprot::Serialize(pack, buf->ptr, buf->len);
sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr));
buf->rstLen();
}
};
#endif #endif

View File

@ -75,21 +75,20 @@ int Server::Ready() {
uint32_t buflen = BUFFER_LENGTH, uint32_t buflen = BUFFER_LENGTH,
strbuflen = BUFFER_LENGTH; strbuflen = BUFFER_LENGTH;
bool readystart = false; bool readystart = false;
do { do {
Log("Entrez la dur<EFBFBD>e de la partie: ", false, false); Log("Entrez la duree de la partie: ", false, false);
std::cin >> m_game.countdown; std::cin.getline(strbuf, BUFFER_LENGTH);
std::cout << std::endl; m_game.countdown = std::stoi(strbuf);
} while (m_game.countdown < 1); } while (m_game.countdown < 1);
do { do {
Log("Entrez le seed de la partie: ", false, false); Log("Entrez le seed de la partie: ", false, false);
std::cin >> m_game.seed; std::cin.getline(strbuf, BUFFER_LENGTH);
std::cout << std::endl; m_game.seed = std::stoi(strbuf);
} while (m_game.seed < 1); } while (m_game.seed < 1);
do { do {
Log("Entrez le nombre de joueurs: ", false, false); Log("Entrez le nombre de joueurs: ", false, false);
std::cin >> nbrjoueurs; std::cin.getline(strbuf, BUFFER_LENGTH);
std::cout << std::endl; nbrjoueurs = std::stoi(strbuf);
} while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS); } while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS);
m_game.gameType = 1; m_game.gameType = 1;
@ -128,7 +127,7 @@ int Server::Ready() {
log = (LoginInfo*)pck.ptr; log = (LoginInfo*)pck.ptr;
log->sid = getUniqueId(); log->sid = getUniqueId();
log->tid = 0; log->tid = 0; // TODO: À changer si on implemente un mode en equipe.
Log(str.append(" Nom: ").append(log->name), false, false); Log(str.append(" Nom: ").append(log->name), false, false);
str.clear(); str.clear();
@ -154,8 +153,6 @@ int Server::Ready() {
m_players[log->sid] = conn; 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) if (++nbrconn >= nbrjoueurs)
readystart = true; readystart = true;
} }