Skip to content

Commit

Permalink
Improve 3DS Reliability (#87)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
zoeyjodon authored Feb 3, 2024
1 parent 0f3fa30 commit 35f730f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions src/PlatformSockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 35f730f

Please sign in to comment.