Merge branch 'packet-acceptance' into udp-client

This commit is contained in:
Jack Garrard 2022-10-27 01:03:39 -07:00
commit 3e5c0c89e5
3 changed files with 35 additions and 3 deletions

View file

@ -54,6 +54,10 @@ class SocketClient : public SocketBase {
u32 getRecvCount() { return mRecvQueue.getCount(); } u32 getRecvCount() { return mRecvQueue.getCount(); }
u32 getRecvMaxCount() { return mRecvQueue.getMaxCount(); } u32 getRecvMaxCount() { return mRecvQueue.getMaxCount(); }
void clearMessageQueues();
void setQueueOpen(bool value) { mPacketQueueOpen = value; }
void setIsFirstConn(bool value) { mIsFirstConnect = value; } void setIsFirstConn(bool value) { mIsFirstConnect = value; }
private: private:
@ -68,6 +72,8 @@ class SocketClient : public SocketBase {
int mMaxBufSize = 100; int mMaxBufSize = 100;
bool mIsFirstConnect = true; bool mIsFirstConnect = true;
bool mPacketQueueOpen = true;
bool mHasRecvUdp; bool mHasRecvUdp;
s32 mUdpSocket; s32 mUdpSocket;

View file

@ -122,7 +122,13 @@ void Client::restartConnection() {
playerDC->mUserID = sInstance->mUserID; 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()) { if (sInstance->mSocket->closeSocket()) {
Logger::log("Sucessfully Closed Socket.\n"); Logger::log("Sucessfully Closed Socket.\n");

View file

@ -93,6 +93,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
udpAddress.family = 2; udpAddress.family = 2;
this->mUdpAddress = udpAddress; this->mUdpAddress = udpAddress;
this->mHasRecvUdp = false; this->mHasRecvUdp = false;
this->mPacketQueueOpen = true;
this->socket_log_state = SOCKET_LOG_CONNECTED; this->socket_log_state = SOCKET_LOG_CONNECTED;
@ -333,7 +334,7 @@ bool SocketClient::recv() {
Packet *packet = reinterpret_cast<Packet*>(packetBuf); Packet *packet = reinterpret_cast<Packet*>(packetBuf);
if (!mRecvQueue.isFull()) { if (!mRecvQueue.isFull() && mPacketQueueOpen) {
mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking); mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking);
} else { } else {
mHeap->free(packetBuf); mHeap->free(packetBuf);
@ -387,6 +388,8 @@ bool SocketClient::closeSocket() {
mHasRecvUdp = false; mHasRecvUdp = false;
mUdpAddress.port = 0; mUdpAddress.port = 0;
mPacketQueueOpen = false;
bool result = false; bool result = false;
if (!(result = SocketBase::closeSocket())) { if (!(result = SocketBase::closeSocket())) {
@ -470,7 +473,7 @@ void SocketClient::recvFunc() {
} }
bool SocketClient::queuePacket(Packet* packet) { bool SocketClient::queuePacket(Packet* packet) {
if (socket_log_state == SOCKET_LOG_CONNECTED) { if (socket_log_state == SOCKET_LOG_CONNECTED && mPacketQueueOpen) {
mSendQueue.push((s64)packet, mSendQueue.push((s64)packet,
sead::MessageQueue::BlockType::NonBlocking); // as this is non-blocking, it sead::MessageQueue::BlockType::NonBlocking); // as this is non-blocking, it
// will always return true. // will always return true.
@ -493,3 +496,20 @@ void SocketClient::trySendQueue() {
Packet* SocketClient::tryGetPacket(sead::MessageQueue::BlockType blockType) { Packet* SocketClient::tryGetPacket(sead::MessageQueue::BlockType blockType) {
return socket_log_state == SOCKET_LOG_CONNECTED ? (Packet*)mRecvQueue.pop(blockType) : nullptr; 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;
}