From 440fa80c5fe3ade3da120918013daf6a01cc7cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 8 Jan 2025 18:47:16 +0100 Subject: [PATCH] Use the correct count parameter for select --- Core/HLE/SocketManager.h | 4 ++-- Core/HLE/sceNetInet.cpp | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Core/HLE/SocketManager.h b/Core/HLE/SocketManager.h index ecc9055112..13f8e5630d 100644 --- a/Core/HLE/SocketManager.h +++ b/Core/HLE/SocketManager.h @@ -4,7 +4,7 @@ // Keep track of who's using a socket. enum class SocketState { - Unused, + Unused = 0, UsedNetInet, UsedProAdhoc, }; @@ -28,7 +28,7 @@ class SocketManager { public: enum { VALID_INET_SOCKET_COUNT = 256, - MIN_VALID_INET_SOCKET = 61, + MIN_VALID_INET_SOCKET = 1, }; InetSocket *CreateSocket(int *index, int *returned_errno, SocketState state, int domain, int type, int protocol); diff --git a/Core/HLE/sceNetInet.cpp b/Core/HLE/sceNetInet.cpp index 7d7805d027..ee37325b1b 100644 --- a/Core/HLE/sceNetInet.cpp +++ b/Core/HLE/sceNetInet.cpp @@ -204,6 +204,8 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr int rdcnt = 0, wrcnt = 0, excnt = 0; + int maxHostSocket = 0; + // Save the mapping during setup. SOCKET hostSockets[256]{}; @@ -212,6 +214,8 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr SOCKET sock = g_socketManager.GetHostSocketFromInetSocket(i); _dbg_assert_(sock != 0); hostSockets[i] = sock; + if (sock > maxHostSocket) + maxHostSocket = sock; DEBUG_LOG(Log::sceNet, "Input Read FD #%i (host: %d)", i, sock); if (rdcnt < FD_SETSIZE) { FD_SET(sock, &rdfds); // This might pointed to a non-existing socket or sockets belonged to other programs on Windows, because most of the time Windows socket have an id above 1k instead of 0-255 @@ -224,6 +228,8 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr SOCKET sock = g_socketManager.GetHostSocketFromInetSocket(i); _dbg_assert_(sock != 0); hostSockets[i] = sock; + if (sock > maxHostSocket) + maxHostSocket = sock; DEBUG_LOG(Log::sceNet, "Input Write FD #%i (host: %d)", i, sock); if (wrcnt < FD_SETSIZE) { FD_SET(sock, &wrfds); @@ -236,6 +242,8 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr SOCKET sock = g_socketManager.GetHostSocketFromInetSocket(i); _dbg_assert_(sock != 0); hostSockets[i] = sock; + if (sock > maxHostSocket) + maxHostSocket = sock; DEBUG_LOG(Log::sceNet, "Input Except FD #%i (host: %d)", i, sock); if (excnt < FD_SETSIZE) { FD_SET(sock, &exfds); @@ -256,10 +264,10 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr tmout.tv_sec = timeout->tv_sec; tmout.tv_usec = timeout->tv_usec; } - DEBUG_LOG(Log::sceNet, "Select: Read count: %d, Write count: %d, Except count: %d, TimeVal: %u.%u", rdcnt, wrcnt, excnt, (int)tmout.tv_sec, (int)tmout.tv_usec); + DEBUG_LOG(Log::sceNet, "Select(host: %d): Read count: %d, Write count: %d, Except count: %d, TimeVal: %u.%u", maxHostSocket + 1, rdcnt, wrcnt, excnt, (int)tmout.tv_sec, (int)tmout.tv_usec); // TODO: Simulate blocking behaviour when timeout = NULL to prevent PPSSPP from freezing // Note: select can overwrite tmout. - int retval = select(nfds, readfds ? &rdfds : nullptr, writefds ? &wrfds : nullptr, exceptfds ? &exfds : nullptr, /*(timeout == NULL) ? NULL :*/ &tmout); + int retval = select(maxHostSocket + 1, readfds ? &rdfds : nullptr, writefds ? &wrfds : nullptr, exceptfds ? &exfds : nullptr, /*(timeout == NULL) ? NULL :*/ &tmout); if (retval < 0) { ERROR_LOG(Log::sceNet, "select returned an error, TODO"); }