diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index f10e8f9..494215b 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -954,7 +954,6 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { std::vector lsPck; int len = 0, end = 0; char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr; - const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; while (true) { int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); @@ -978,7 +977,7 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { cursor--; next = cursor + 1; - cmp = memcmp(cursor, footer, sizeof(uint32_t)); + cmp = memcmp(cursor, Footer, sizeof(uint32_t)); if (cmp == 0) { lsPck.push_back(last); cursor += sizeof(uint32_t); @@ -996,45 +995,45 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { } template <> -void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { +void netprot::sendPack(SOCKET sock, Packet* pack, Buffer* buf) { switch (pack->type) { case PACKET_TYPE::INPUT: - sendPack(sock, (Input*)pack->ptr, buf, buflen); + sendPack(sock, (Input*)pack->ptr, buf); return; case PACKET_TYPE::OUTPUT: - sendPack(sock, (Output*)pack->ptr, buf, buflen); + sendPack(sock, (Output*)pack->ptr, buf); return; case PACKET_TYPE::SYNC: - sendPack(sock, (Sync*)pack->ptr, buf, buflen); + sendPack(sock, (Sync*)pack->ptr, buf); return; case PACKET_TYPE::TEAMINF: - sendPack(sock, (TeamInfo*)pack->ptr, buf, buflen); + sendPack(sock, (TeamInfo*)pack->ptr, buf); return; case PACKET_TYPE::PLAYINF: - sendPack(sock, (PlayerInfo*)pack->ptr, buf, buflen); + sendPack(sock, (PlayerInfo*)pack->ptr, buf); return; case PACKET_TYPE::LOGINF: - sendPack(sock, (LoginInfo*)pack->ptr, buf, buflen); + sendPack(sock, (LoginInfo*)pack->ptr, buf); return; case PACKET_TYPE::CHUNKMOD: - //sendPack(sock, (ChunkMod*)pack->ptr, buf, buflen); + //sendPack(sock, (ChunkMod*)pack->ptr, buf); case PACKET_TYPE::PLAYERMOD: - //sendPack(sock, (PlayerMod*)pack->ptr, buf, buflen); + //sendPack(sock, (PlayerMod*)pack->ptr, buf); return; case PACKET_TYPE::PICKUPMOD: - //sendPack(sock, (PickupMod*)pack->ptr, buf, buflen); + //sendPack(sock, (PickupMod*)pack->ptr, buf); return; case PACKET_TYPE::GAMEINFO: - sendPack(sock, (GameInfo*)pack->ptr, buf, buflen); + sendPack(sock, (GameInfo*)pack->ptr, buf); return; case PACKET_TYPE::ENDINFO: - //sendPack(sock, (EndInfo*)pack->ptr, buf, buflen); + //sendPack(sock, (EndInfo*)pack->ptr, buf); return; case PACKET_TYPE::CHAT: - sendPack(sock, (Chat*)pack->ptr, buf, buflen); + sendPack(sock, (Chat*)pack->ptr, buf); return; case PACKET_TYPE::ERRLOG: - sendPack(sock, (ErrorLog*)pack->ptr, buf, buflen); + sendPack(sock, (ErrorLog*)pack->ptr, buf); return; case PACKET_TYPE::LAST_PACK: [[fallthrough]]; case PACKET_TYPE::ERR: [[fallthrough]]; @@ -1044,46 +1043,46 @@ void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, u } template <> -void netprot::sendPackTo(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { +void netprot::sendPackTo(SOCKET sock, Packet* pack, Buffer* buf, sockaddr_in* sockad) { switch (pack->type) { case PACKET_TYPE::INPUT: - sendPackTo(sock, (Input*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (Input*)pack->ptr, buf, sockad); return; case PACKET_TYPE::OUTPUT: - sendPackTo(sock, (Output*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (Output*)pack->ptr, buf, sockad); return; case PACKET_TYPE::SYNC: - sendPackTo(sock, (Sync*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (Sync*)pack->ptr, buf, sockad); return; case PACKET_TYPE::TEAMINF: - sendPackTo(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (TeamInfo*)pack->ptr, buf, sockad); return; case PACKET_TYPE::PLAYINF: - sendPackTo(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (PlayerInfo*)pack->ptr, buf, sockad); return; case PACKET_TYPE::LOGINF: - sendPackTo(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (LoginInfo*)pack->ptr, buf, sockad); return; case PACKET_TYPE::CHUNKMOD: - //sendPackTo(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); + //sendPackTo(sock, (ChunkMod*)pack->ptr, buf, sockad); return; case PACKET_TYPE::PLAYERMOD: - //sendPackTo(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); + //sendPackTo(sock, (PlayerMod*)pack->ptr, buf, sockad); return; case PACKET_TYPE::PICKUPMOD: - //sendPackTo(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); + //sendPackTo(sock, (PickupMod*)pack->ptr, buf, sockad); return; case PACKET_TYPE::GAMEINFO: - sendPackTo(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (GameInfo*)pack->ptr, buf, sockad); return; case PACKET_TYPE::ENDINFO: - //sendPackTo(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); + //sendPackTo(sock, (EndInfo*)pack->ptr, buf, sockad); return; case PACKET_TYPE::CHAT: - sendPackTo(sock, (Chat*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (Chat*)pack->ptr, buf, sockad); return; case PACKET_TYPE::ERRLOG: - sendPackTo(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); + sendPackTo(sock, (ErrorLog*)pack->ptr, buf, sockad); return; case PACKET_TYPE::LAST_PACK: [[fallthrough]]; case PACKET_TYPE::ERR: [[fallthrough]]; diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 8d51ee3..cd2d508 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -31,6 +31,8 @@ namespace netprot { PACKET_TYPE type = PACKET_TYPE::ERR; }; + inline const char Footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; + struct Keys { bool forward, backward, @@ -165,39 +167,37 @@ namespace netprot { template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { netprot::Serialize(pack, buf, buflen); - memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); - buflen += (sizeof(uint64_t) + sizeof(uint8_t)); + memcpy(*buf + *buflen, Footer, sizeof(uint32_t)); + *buflen += sizeof(Footer); send(sock, *buf, *buflen, 0); *buflen = BUFFER_LENGTH; } template 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); - memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); - buflen += (sizeof(uint64_t) + sizeof(uint8_t)); + memcpy(*buf + *buflen, Footer, sizeof(uint32_t)); + *buflen += sizeof(Footer); sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); *buflen = BUFFER_LENGTH; } template 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); - memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); - buf->len += sizeof(uint32_t); + memcpy(&buf->ptr[buf->len], Footer, sizeof(uint32_t)); + buf->len += sizeof(Footer); send(sock, buf->ptr, buf->len, 0); buf->rstLen(); } template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { - const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; - sockaddr_in addr = *sockad; + const sockaddr_in addr = *sockad; netprot::Serialize(pack, &buf->ptr, &buf->len); - memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); - buf->len += sizeof(uint32_t); + memcpy(&buf->ptr[buf->len], Footer, sizeof(uint32_t)); + buf->len += sizeof(Footer); sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); buf->rstLen(); }