Le seed est pas bon, mais ON A LES DROITS!

This commit is contained in:
MarcEricMartel 2023-10-31 14:38:30 -04:00
parent eb7116bac8
commit 54307b7c17
4 changed files with 245 additions and 214 deletions

View File

@ -7,7 +7,7 @@
#define SRV_PORT 10000
#define CLI_PORT 10001
#define BUFFER_LENGTH 150
#define BUFFER_LENGTH 1500
#define CHUNK_SIZE_X 4
#define CHUNK_SIZE_Y 64

View File

@ -4,26 +4,26 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
*buf[0] = (char)netprot::PACKET_TYPE::INPUT;
uint64_t time = in->timestamp;
uint8_t time8[sizeof(uint64_t)] = {(uint8_t)((time >> 56) & 0xFF),
uint8_t time8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8 ) & 0xFF),
(uint8_t)(time & 0xFF)};
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
memcpy(*buf + 1, time8, sizeof(uint64_t));
uint64_t sid = in->sid;
uint8_t sid8[sizeof(uint64_t)] = {(uint8_t)((sid >> 56) & 0xFF),
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8 ) & 0xFF),
(uint8_t)( sid & 0xFF)};
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
@ -35,7 +35,7 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
keys.right & 0b00010000 |
keys.jump & 0b00001000 |
keys.shoot & 0b00000100 |
keys.block & 0b00000010 ;
keys.block & 0b00000010;
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
@ -54,7 +54,7 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF)};
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
@ -118,7 +118,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
uint32_t timer = sync->timer;
uint8_t time8[sizeof(uint32_t)] = {(uint8_t)((timer >> 24) & 0xFF),
uint8_t time8[sizeof(uint32_t)] = { (uint8_t)((timer >> 24) & 0xFF),
(uint8_t)((timer >> 16) & 0xFF),
(uint8_t)((timer >> 8) & 0xFF),
(uint8_t)(timer & 0xFF) };
@ -366,8 +366,8 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(Input))
bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(Input))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
@ -417,19 +417,21 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11]};
(uint32_t)subvec[11] };
memcpy(&in->direction, vec, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3;
return true;
}
bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) {
return false;
}
bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(Sync))
bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(Sync))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
@ -487,11 +489,13 @@ bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
memcpy(&sync->position, vec, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 2;
return true;
}
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(LoginInfo))
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(LoginInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
@ -513,11 +517,13 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = namesize + sizeof(uint64_t) + 2;
return true;
}
bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(LoginInfo))
bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(LoginInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
@ -550,11 +556,13 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
return true;
}
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(PlayerInfo))
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(PlayerInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
@ -587,11 +595,13 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
return true;
}
bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(GameInfo))
bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(GameInfo))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
@ -628,11 +638,13 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = sizeof(uint64_t) * 3 + 1;
return true;
}
bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(Chat))
bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(Chat))
return false;
uint8_t src[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
@ -677,11 +689,14 @@ bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) {
return false;
memcpy(&chat->mess, &buf[1 + sizeof(uint64_t) * 3], messsize);
*buflen = messsize + sizeof(uint64_t) * 3 + 2;
return true;
}
bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(ErrorLog))
bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(ErrorLog))
return false;
size_t messsize = std::strlen(buf) + 1;
@ -692,6 +707,8 @@ bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
memcpy(&errlog->mess, &buf[1], messsize);
memcpy(&errlog->is_fatal, &buf[1 + messsize], sizeof(bool));
*buflen = messsize + sizeof(uint64_t) * 3 + 2;
return true;
}
@ -703,7 +720,7 @@ netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
return (netprot::PACKET_TYPE)buf[0];
}
netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
netprot::Packet netprot::getPack(char* buf, uint32_t *buflen) {
Packet pck = { nullptr, PACKET_TYPE::ERR };
Input* in = nullptr;
Output* out = nullptr;
@ -713,7 +730,7 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
ErrorLog* errlog = nullptr;
LoginInfo* loginf = nullptr;
switch (getType(buf, buflen)) {
switch (getType(buf, *buflen)) {
case PACKET_TYPE::INPUT:
in = new Input();
if (netprot::Deserialize(in, buf, buflen)) {
@ -770,7 +787,7 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
}
netprot::Packet netprot::getPack(netprot::Buffer* buf) {
return netprot::getPack(buf->ptr, buf->len);
return netprot::getPack(buf->ptr, &buf->len);
}
bool netprot::emptyPack(netprot::Packet pck) {
@ -808,43 +825,53 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
return pck;
}
std::vector<netprot::Packet> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<Packet> lsPck;
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<char*> lsPck;
int len = 0, end = 0;
bool pck_received = false;
char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr;
const char footer[sizeof(uint64_t) + sizeof(uint8_t)] = { '\0', '\0', '\0', '\0', '\0' };
while (true) {
while (!pck_received) {
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
if (bytes < 0) // si recv() retourne -1; ça veut dire qu'il y a plus rien a lire.
if (bytes <= 0) // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité.
return lsPck;
len += bytes;
// TODO: Voir si on trouve un footer (5 '\0' d'affilee).
// Si oui, mettre l'index du debut du footer dans
// la variable "end" et mettre pck_received a true.
end = len;
for (int x = 0; x < len; ++x)
std::putchar(buf->ptr[x]);
std::puts("");
while (true) {
int cmp = 0;
//buf->ptr
if (cursor)
end -= (cursor - buf->ptr);
//end = len;
//pck_received = true;
}
char* cursor = nullptr;
if (end < 0)
break;
cursor = &buf->ptr[end];
cursor = (char*)memchr(next, '\0', end);
next = cursor + 1;
lsPck.push_back(getPack(buf));
while (*cursor == '\0')
cursor++, end++;
if (cursor) { // mettre le début du prochain paquet au début du buffer.
memcpy(buf->ptr, cursor, buf->len - end); // et voir s'il y a en un autre
len = 0;
pck_received = false;
if (cursor) {
cmp = memcmp(cursor, &footer, sizeof(uint64_t) + sizeof(uint8_t));
if (cmp == 0) {
lsPck.push_back(last);
cursor += sizeof(uint64_t) + sizeof(uint8_t) + 1;
last = cursor;
next = cursor + 1;
}
}
else {
cursor = &buf->ptr[len];
next = cursor + 1;
break;
};
}
}
}
template <>

View File

@ -135,19 +135,19 @@ namespace netprot {
void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv
bool Deserialize(Input* in, char* buf, const uint32_t buflen); // srv
bool Deserialize(Output* out, char* buf, const uint32_t buflen); // cli
bool Deserialize(Sync* sync, char* buf, const uint32_t buflen); // cli
bool Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen); // cli/srv
bool Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen); // cli/srv
bool Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen); // cli
bool Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen); // 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(Input* in, char* buf, uint32_t* buflen); // srv
bool Deserialize(Output* out, char* buf, uint32_t* buflen); // cli
bool Deserialize(Sync* sync, char* buf, uint32_t* buflen); // cli
bool Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv
bool Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen); // cli/srv
bool Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // cli
bool Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli
bool Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli
bool Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv
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);
@ -160,7 +160,7 @@ namespace netprot {
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);
std::vector<Packet> recvPacks(SOCKET sock, Buffer* buf);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf);
template <class T>
void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {

View File

@ -71,42 +71,46 @@ int Connector::Connect(const char* srv_addr, std::string name) {
bool ready = false;
int errors = 0;
std::vector<netprot::Packet> lsPck;
std::vector<char*> lsPck;
while (!ready) {
lsPck = netprot::recvPacks(m_sock_tcp, &bf);
for (auto& pck : lsPck) {
bool bypass_delete = false;
uint32_t bsize = bf.len - (pck - bf.ptr);
netprot::PlayerInfo* pl = nullptr;
switch (pck.type) {
switch (netprot::getType(pck, 1)) {
using enum netprot::PACKET_TYPE;
case LOGINF:
m_loginfo = netprot::LoginInfo((netprot::LoginInfo*)pck.ptr);
if (!netprot::Deserialize(&m_loginfo, pck, &bsize))
++errors;
break;
case GAMEINFO:
m_gameinfo = netprot::GameInfo((netprot::GameInfo*)pck.ptr);
if (!netprot::Deserialize(&m_gameinfo, pck, &bsize))
++errors;
break;
case PLAYINF:
pl = (netprot::PlayerInfo*)pck.ptr;
m_players[pl->id] = pl;
bypass_delete = true;
pl = new netprot::PlayerInfo();
if (!netprot::Deserialize(pl, pck, &bsize))
++errors;
else m_players[pl->id] = pl;
break;
case TEAMINF:
// TODO: Faire dequoi avec TeamInfo si on fini par avoir des teams.
break;
case SYNC:
m_origin = netprot::Sync((netprot::Sync*)pck.ptr);
if (!netprot::Deserialize(&m_origin, pck, &bsize))
++errors;
ready = true;
break;
default:
errors++;
break;
}
if (!bypass_delete)
netprot::emptyPack(pck);
}
if (lsPck.empty())
errors++;
lsPck.clear();
if (errors > 100)
if (errors > 1000000)
return 4;
}
return 0;