|
|
|
@@ -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 <>
|
|
|
|
|