Skip to content

Commit

Permalink
Use timer and thread sleep for 3DS polling
Browse files Browse the repository at this point in the history
Reduce thread stack sizes (match vita)
Set a maximum receive buffer size for 3DS sockets
Remove global socket setting
  • Loading branch information
zoey jodon committed Feb 2, 2024
1 parent 3acba57 commit acd1f0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct thread_context {
#endif
};

#ifdef __3DS__
static int n3ds_thread_priority = 0x30;
#endif

static int activeThreads = 0;
static int activeMutexes = 0;
static int activeEvents = 0;
Expand Down Expand Up @@ -279,13 +283,11 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
OSResumeThread(&thread->thread);
#elif defined(__3DS__)
{
s32 priority = 0x30;
size_t stack_size = 1024 * 1024;
svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);
size_t stack_size = 0x40000;
thread->thread = threadCreate(ThreadProc,
ctx,
stack_size,
priority,
n3ds_thread_priority,
-1,
false);
if (thread->thread == NULL) {
Expand Down Expand Up @@ -494,6 +496,9 @@ int initializePlatform(void) {
return err;
}

#ifdef __3DS__
svcGetThreadPriority(&n3ds_thread_priority, CUR_THREAD_HANDLE);
#endif
enterLowLatencyMode();

return 0;
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 acd1f0f

Please sign in to comment.