From 35f730feddaab5ab58962f9fa4640cfdc95e659d Mon Sep 17 00:00:00 2001 From: zoeyjodon <76182954+zoeyjodon@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:33:18 -0500 Subject: [PATCH] Improve 3DS Reliability (#87) - Simplifies thread priority setup - Sets a max socket buffer size of 0x20000 for the 3DS - Fixes a bug with polling timeouts taking longer than intended - Removes 3DS global socket definition --- src/Platform.c | 2 +- src/PlatformSockets.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index d3ec2dc..f41ffde 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -279,8 +279,8 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE OSResumeThread(&thread->thread); #elif defined(__3DS__) { + size_t stack_size = 0x40000; s32 priority = 0x30; - size_t stack_size = 1024 * 1024; svcGetThreadPriority(&priority, CUR_THREAD_HANDLE); thread->thread = threadCreate(ThreadProc, ctx, diff --git a/src/PlatformSockets.c b/src/PlatformSockets.c index 6847ec8..3ba3bb1 100644 --- a/src/PlatformSockets.c +++ b/src/PlatformSockets.c @@ -37,6 +37,7 @@ DWORD (WINAPI *pfnWlanSetInterface)(HANDLE hClientHandle, CONST GUID *pInterface #ifdef __3DS__ in_port_t n3ds_udp_port = 47998; +static const int n3ds_max_buf_size = 0x20000; #endif void addrToUrlSafeString(struct sockaddr_storage* addr, char* string, size_t stringLength) @@ -149,11 +150,13 @@ int pollSockets(struct pollfd* pollFds, int pollFdsCount, int timeoutMs) { return err; #elif defined(__3DS__) int err; - for (int i = 0; i < timeoutMs; i++) { - err = poll(pollFds, pollFdsCount, 1); // need to do this on 3ds since poll will block even if socket is ready before + u64 poll_start = osGetTime(); + for (u64 i = poll_start; (i - poll_start) < timeoutMs; i = osGetTime()) { + err = poll(pollFds, pollFdsCount, 0); // This is running for 14ms if (err) { break; } + svcSleepThread(1000); } return err; #else @@ -348,6 +351,10 @@ SOCKET bindUdpSocket(int addressFamily, struct sockaddr_storage* localAddr, SOCK setSocketQos(s, socketQosType); } +#ifdef __3DS__ + if (bufferSize == 0 || bufferSize > n3ds_max_buf_size) + bufferSize = n3ds_max_buf_size; +#endif if (bufferSize != 0) { // We start at the requested recv buffer value and step down until we find // a value that the OS will accept. @@ -359,6 +366,7 @@ SOCKET bindUdpSocket(int addressFamily, struct sockaddr_storage* localAddr, SOCK } else if (bufferSize <= RCV_BUFFER_SIZE_MIN) { // Failed to set a buffer size within the allowable range + Limelog("Set rcv buffer size failed: %d\n", LastSocketError()); break; } else if (bufferSize - RCV_BUFFER_SIZE_STEP <= RCV_BUFFER_SIZE_MIN) { @@ -425,9 +433,6 @@ SOCKET createSocket(int addressFamily, int socketType, int protocol, bool nonBlo setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (char*)&val, sizeof(val)); } #endif -#ifdef __3DS__ - SOCU_AddGlobalSocket(s); -#endif if (nonBlocking) { setSocketNonBlocking(s, true);