From 4a5a10f910337e9d189dcf8b0400d051fb90ce82 Mon Sep 17 00:00:00 2001 From: Jack Garrard Date: Wed, 26 Oct 2022 03:33:42 -0700 Subject: [PATCH] Convert receive buffer to be on heap --- include/packets/Packet.h | 2 +- include/server/SocketClient.hpp | 1 + source/server/SocketClient.cpp | 14 +++++++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/packets/Packet.h b/include/packets/Packet.h index 92fb053..77a9016 100644 --- a/include/packets/Packet.h +++ b/include/packets/Packet.h @@ -10,7 +10,7 @@ #define PACKBUFSIZE 0x30 #define COSTUMEBUFSIZE 0x20 -#define MAXPACKSIZE 0x100 +#define MAXPACKSIZE 0x50 enum PacketType : short { UNKNOWN, diff --git a/include/server/SocketClient.hpp b/include/server/SocketClient.hpp index 53d9b34..218d518 100644 --- a/include/server/SocketClient.hpp +++ b/include/server/SocketClient.hpp @@ -64,6 +64,7 @@ class SocketClient : public SocketBase { sead::MessageQueue mRecvQueue; sead::MessageQueue mSendQueue; + char* recvBuf = nullptr; int maxBufSize = 100; bool mIsFirstConnect = true; diff --git a/source/server/SocketClient.cpp b/source/server/SocketClient.cpp index ee582ac..a801480 100644 --- a/source/server/SocketClient.cpp +++ b/source/server/SocketClient.cpp @@ -21,6 +21,7 @@ SocketClient::SocketClient(const char* name, sead::Heap* heap) : mHeap(heap), So mRecvQueue.allocate(maxBufSize, mHeap); mSendQueue.allocate(maxBufSize, mHeap); + recvBuf = (char*)mHeap->alloc(MAXPACKSIZE+1); }; nn::Result SocketClient::init(const char* ip, u16 port) { @@ -198,7 +199,6 @@ bool SocketClient::recv() { } int headerSize = sizeof(Packet); - char headerBuf[MAXPACKSIZE + 1] = {0}; int valread = 0; const int fd_count = 2; @@ -229,12 +229,12 @@ bool SocketClient::recv() { } if (index == 1) { - int result = nn::socket::Recv(fd, headerBuf, sizeof(headerBuf), this->sock_flags); + int result = nn::socket::Recv(fd, recvBuf, sizeof(MAXPACKSIZE), this->sock_flags); if (result < headerSize){ return true; } - Packet* header = reinterpret_cast(headerBuf); + Packet* header = reinterpret_cast(recvBuf); int fullSize = header->mPacketSize + sizeof(Packet); // Verify packet size is appropriate if (result < fullSize || result > MAXPACKSIZE || fullSize > MAXPACKSIZE){ @@ -251,7 +251,7 @@ bool SocketClient::recv() { char* packetBuf = (char*)mHeap->alloc(fullSize); if (packetBuf){ - memcpy(packetBuf, headerBuf, fullSize); + memcpy(packetBuf, recvBuf, fullSize); Packet *packet = reinterpret_cast(packetBuf); @@ -268,7 +268,7 @@ bool SocketClient::recv() { // read only the size of a header while(valread < headerSize) { int result = 0; - result = nn::socket::Recv(fd, headerBuf + valread, + result = nn::socket::Recv(fd, recvBuf + valread, headerSize - valread, this->sock_flags); this->socket_errno = nn::socket::GetLastErrno(); @@ -286,7 +286,7 @@ bool SocketClient::recv() { } if(valread > 0) { - Packet* header = reinterpret_cast(headerBuf); + Packet* header = reinterpret_cast(recvBuf); int fullSize = header->mPacketSize + sizeof(Packet); @@ -307,7 +307,7 @@ bool SocketClient::recv() { if (packetBuf) { - memcpy(packetBuf, headerBuf, sizeof(Packet)); + memcpy(packetBuf, recvBuf, sizeof(Packet)); while (valread < fullSize) {