Merge pull request #37 from speyejack/packet-acceptance

Packet acceptance
This commit is contained in:
Jack Garrard 2023-09-02 13:03:18 -07:00 committed by GitHub
commit d5b49b2ea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 13 deletions

View file

@ -52,6 +52,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:
@ -66,6 +70,8 @@ class SocketClient : public SocketBase {
int maxBufSize = 100;
bool mIsFirstConnect = true;
bool mPacketQueueOpen = true;
/**
* @param str a string containing an IPv4 address or a hostname that can be resolved via DNS

View file

@ -72,12 +72,13 @@ s32 SocketBase::getFd() {
bool SocketBase::closeSocket() {
this->socket_log_state = SOCKET_LOG_DISCONNECTED; // probably not safe to assume socket will be closed
if (this->socket_log_state != SOCKET_LOG_DISCONNECTED) {
nn::Result result = nn::socket::Close(this->socket_log_socket);
if (result.isSuccess()) {
this->socket_log_state = SOCKET_LOG_DISCONNECTED;
}
return result.isSuccess();
}
nn::Result result = nn::socket::Close(this->socket_log_socket);
return result.isSuccess();
return true;
}

View file

@ -80,6 +80,8 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
return result;
}
this->mPacketQueueOpen = true;
this->socket_log_state = SOCKET_LOG_CONNECTED;
Logger::log("Socket fd: %d\n", socket_log_socket);
@ -184,8 +186,7 @@ bool SocketClient::recv() {
int fullSize = header->mPacketSize + sizeof(Packet);
if (header->mType > PacketType::UNKNOWN && header->mType < PacketType::End &&
fullSize <= MAXPACKSIZE && fullSize > 0 && valread == sizeof(Packet)) {
if (fullSize <= MAXPACKSIZE && fullSize > 0 && valread == sizeof(Packet)) {
if (header->mType != PLAYERINF && header->mType != HACKCAPINF) {
Logger::log("Received packet (from %02X%02X):", header->mUserID.data[0],
@ -220,16 +221,22 @@ bool SocketClient::recv() {
}
}
Packet* packet = reinterpret_cast<Packet*>(packetBuf);
if (!(header->mType > PacketType::UNKNOWN && header->mType < PacketType::End)) {
Logger::log("Failed to acquire valid packet type! Packet Type: %d Full Packet Size %d valread size: %d", header->mType, fullSize, valread);
mHeap->free(packetBuf);
return true;
}
if (!mRecvQueue.isFull()) {
Packet *packet = reinterpret_cast<Packet*>(packetBuf);
if (!mRecvQueue.isFull() && mPacketQueueOpen) {
mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking);
} else {
mHeap->free(packetBuf);
}
}
} else {
Logger::log("Failed to aquire valid data! Packet Type: %d Full Packet Size %d valread size: %d", header->mType, fullSize, valread);
Logger::log("Failed to acquire valid data! Packet Type: %d Full Packet Size %d valread size: %d", header->mType, fullSize, valread);
}
return true;
@ -274,6 +281,8 @@ bool SocketClient::closeSocket() {
Logger::log("Closing Socket.\n");
mPacketQueueOpen = false;
bool result = false;
if (!(result = SocketBase::closeSocket())) {
@ -357,7 +366,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.
@ -380,3 +389,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;
}