diff --git a/include/server/SocketClient.hpp b/include/server/SocketClient.hpp index 8ab5e2b..a79b388 100644 --- a/include/server/SocketClient.hpp +++ b/include/server/SocketClient.hpp @@ -54,6 +54,10 @@ class SocketClient : public SocketBase { u32 getRecvCount() { return mRecvQueue.getCount(); } u32 getRecvMaxCount() { return mRecvQueue.getMaxCount(); } + void clearMessageQueues(); + void setQueueOpen(bool value) { mPacketQueueOpen = value; } + + void setIsFirstConn(bool value) { mIsFirstConnect = value; } private: @@ -68,6 +72,8 @@ class SocketClient : public SocketBase { int mMaxBufSize = 100; bool mIsFirstConnect = true; + bool mPacketQueueOpen = true; + bool mHasRecvUdp; s32 mUdpSocket; diff --git a/source/server/Client.cpp b/source/server/Client.cpp index 33abed1..ffe444e 100644 --- a/source/server/Client.cpp +++ b/source/server/Client.cpp @@ -122,7 +122,13 @@ void Client::restartConnection() { playerDC->mUserID = sInstance->mUserID; - sInstance->mSocket->queuePacket(playerDC); + + sInstance->mSocket->setQueueOpen(false); + sInstance->mSocket->clearMessageQueues(); + + sInstance->mSocket->send(playerDC); + + sInstance->mHeap->free(playerDC); if (sInstance->mSocket->closeSocket()) { Logger::log("Sucessfully Closed Socket.\n"); diff --git a/source/server/SocketClient.cpp b/source/server/SocketClient.cpp index 3c0042d..6d5e5d3 100644 --- a/source/server/SocketClient.cpp +++ b/source/server/SocketClient.cpp @@ -93,6 +93,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) { udpAddress.family = 2; this->mUdpAddress = udpAddress; this->mHasRecvUdp = false; + this->mPacketQueueOpen = true; this->socket_log_state = SOCKET_LOG_CONNECTED; @@ -333,7 +334,7 @@ bool SocketClient::recv() { Packet *packet = reinterpret_cast(packetBuf); - if (!mRecvQueue.isFull()) { + if (!mRecvQueue.isFull() && mPacketQueueOpen) { mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking); } else { mHeap->free(packetBuf); @@ -387,6 +388,8 @@ bool SocketClient::closeSocket() { mHasRecvUdp = false; mUdpAddress.port = 0; + mPacketQueueOpen = false; + bool result = false; if (!(result = SocketBase::closeSocket())) { @@ -470,7 +473,7 @@ void SocketClient::recvFunc() { } bool SocketClient::queuePacket(Packet* packet) { - if (socket_log_state == SOCKET_LOG_CONNECTED) { + if (socket_log_state == SOCKET_LOG_CONNECTED && mPacketQueueOpen) { mSendQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking); // as this is non-blocking, it // will always return true. @@ -493,3 +496,20 @@ void SocketClient::trySendQueue() { Packet* SocketClient::tryGetPacket(sead::MessageQueue::BlockType blockType) { return socket_log_state == SOCKET_LOG_CONNECTED ? (Packet*)mRecvQueue.pop(blockType) : nullptr; } + +void SocketClient::clearMessageQueues() { + bool prevQueueOpenness = this->mPacketQueueOpen; + this->mPacketQueueOpen = false; + + while (mSendQueue.getCount() > 0) { + Packet* curPacket = (Packet*)mSendQueue.pop(sead::MessageQueue::BlockType::Blocking); + mHeap->free(curPacket); + } + + while (mRecvQueue.getCount() > 0) { + Packet* curPacket = (Packet*)mRecvQueue.pop(sead::MessageQueue::BlockType::Blocking); + mHeap->free(curPacket); + } + + this->mPacketQueueOpen = prevQueueOpenness; +}