This commit is contained in:
MarcEricMartel 2023-11-01 09:19:26 -04:00
parent e5d9eb2fa6
commit 88acc268b8
2 changed files with 41 additions and 42 deletions

View File

@ -954,7 +954,6 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<char*> lsPck; std::vector<char*> lsPck;
int len = 0, end = 0; int len = 0, end = 0;
char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr; char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr;
const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' };
while (true) { while (true) {
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
@ -978,7 +977,7 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
cursor--; cursor--;
next = cursor + 1; next = cursor + 1;
cmp = memcmp(cursor, footer, sizeof(uint32_t)); cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) { if (cmp == 0) {
lsPck.push_back(last); lsPck.push_back(last);
cursor += sizeof(uint32_t); cursor += sizeof(uint32_t);
@ -996,45 +995,45 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
} }
template <> template <>
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, Buffer* buf) {
switch (pack->type) { switch (pack->type) {
case PACKET_TYPE::INPUT: case PACKET_TYPE::INPUT:
sendPack<Input>(sock, (Input*)pack->ptr, buf, buflen); sendPack<Input>(sock, (Input*)pack->ptr, buf);
return; return;
case PACKET_TYPE::OUTPUT: case PACKET_TYPE::OUTPUT:
sendPack<Output>(sock, (Output*)pack->ptr, buf, buflen); sendPack<Output>(sock, (Output*)pack->ptr, buf);
return; return;
case PACKET_TYPE::SYNC: case PACKET_TYPE::SYNC:
sendPack<Sync>(sock, (Sync*)pack->ptr, buf, buflen); sendPack<Sync>(sock, (Sync*)pack->ptr, buf);
return; return;
case PACKET_TYPE::TEAMINF: case PACKET_TYPE::TEAMINF:
sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen); sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf);
return; return;
case PACKET_TYPE::PLAYINF: case PACKET_TYPE::PLAYINF:
sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen); sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf);
return; return;
case PACKET_TYPE::LOGINF: case PACKET_TYPE::LOGINF:
sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen); sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf);
return; return;
case PACKET_TYPE::CHUNKMOD: case PACKET_TYPE::CHUNKMOD:
//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen); //sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf);
case PACKET_TYPE::PLAYERMOD: case PACKET_TYPE::PLAYERMOD:
//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen); //sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf);
return; return;
case PACKET_TYPE::PICKUPMOD: case PACKET_TYPE::PICKUPMOD:
//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen); //sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf);
return; return;
case PACKET_TYPE::GAMEINFO: case PACKET_TYPE::GAMEINFO:
sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen); sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf);
return; return;
case PACKET_TYPE::ENDINFO: case PACKET_TYPE::ENDINFO:
//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen); //sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf);
return; return;
case PACKET_TYPE::CHAT: case PACKET_TYPE::CHAT:
sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen); sendPack<Chat>(sock, (Chat*)pack->ptr, buf);
return; return;
case PACKET_TYPE::ERRLOG: case PACKET_TYPE::ERRLOG:
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen); sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf);
return; return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]]; case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]]; case PACKET_TYPE::ERR: [[fallthrough]];
@ -1044,46 +1043,46 @@ void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, u
} }
template <> template <>
void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, Buffer* buf, sockaddr_in* sockad) {
switch (pack->type) { switch (pack->type) {
case PACKET_TYPE::INPUT: case PACKET_TYPE::INPUT:
sendPackTo<Input>(sock, (Input*)pack->ptr, buf, buflen, sockad); sendPackTo<Input>(sock, (Input*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::OUTPUT: case PACKET_TYPE::OUTPUT:
sendPackTo<Output>(sock, (Output*)pack->ptr, buf, buflen, sockad); sendPackTo<Output>(sock, (Output*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::SYNC: case PACKET_TYPE::SYNC:
sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, buflen, sockad); sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::TEAMINF: case PACKET_TYPE::TEAMINF:
sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::PLAYINF: case PACKET_TYPE::PLAYINF:
sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::LOGINF: case PACKET_TYPE::LOGINF:
sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::CHUNKMOD: case PACKET_TYPE::CHUNKMOD:
//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); //sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::PLAYERMOD: case PACKET_TYPE::PLAYERMOD:
//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); //sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::PICKUPMOD: case PACKET_TYPE::PICKUPMOD:
//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); //sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::GAMEINFO: case PACKET_TYPE::GAMEINFO:
sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::ENDINFO: case PACKET_TYPE::ENDINFO:
//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); //sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::CHAT: case PACKET_TYPE::CHAT:
sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, buflen, sockad); sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::ERRLOG: case PACKET_TYPE::ERRLOG:
sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, sockad);
return; return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]]; case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]]; case PACKET_TYPE::ERR: [[fallthrough]];

View File

@ -31,6 +31,8 @@ namespace netprot {
PACKET_TYPE type = PACKET_TYPE::ERR; PACKET_TYPE type = PACKET_TYPE::ERR;
}; };
inline const char Footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' };
struct Keys { struct Keys {
bool forward, bool forward,
backward, backward,
@ -165,39 +167,37 @@ namespace netprot {
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);
memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); memcpy(*buf + *buflen, Footer, sizeof(uint32_t));
buflen += (sizeof(uint64_t) + sizeof(uint8_t)); *buflen += sizeof(Footer);
send(sock, *buf, *buflen, 0); send(sock, *buf, *buflen, 0);
*buflen = BUFFER_LENGTH; *buflen = BUFFER_LENGTH;
} }
template <class T> template <class T>
void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
sockaddr_in addr = *sockad; const sockaddr_in addr = *sockad;
netprot::Serialize(pack, buf, buflen); netprot::Serialize(pack, buf, buflen);
memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); memcpy(*buf + *buflen, Footer, sizeof(uint32_t));
buflen += (sizeof(uint64_t) + sizeof(uint8_t)); *buflen += sizeof(Footer);
sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr));
*buflen = BUFFER_LENGTH; *buflen = BUFFER_LENGTH;
} }
template <class T> template <class T>
void sendPack(SOCKET sock, T* pack, Buffer* buf) { void sendPack(SOCKET sock, T* pack, Buffer* buf) {
const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' };
netprot::Serialize(pack, &buf->ptr, &buf->len); netprot::Serialize(pack, &buf->ptr, &buf->len);
memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); memcpy(&buf->ptr[buf->len], Footer, sizeof(uint32_t));
buf->len += sizeof(uint32_t); buf->len += sizeof(Footer);
send(sock, buf->ptr, buf->len, 0); send(sock, buf->ptr, buf->len, 0);
buf->rstLen(); buf->rstLen();
} }
template <class T> template <class T>
void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) {
const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; const sockaddr_in addr = *sockad;
sockaddr_in addr = *sockad;
netprot::Serialize(pack, &buf->ptr, &buf->len); netprot::Serialize(pack, &buf->ptr, &buf->len);
memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); memcpy(&buf->ptr[buf->len], Footer, sizeof(uint32_t));
buf->len += sizeof(uint32_t); buf->len += sizeof(Footer);
sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr));
buf->rstLen(); buf->rstLen();
} }