Skip to content

sceNetInet socket remap #19827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use the correct count parameter for select
  • Loading branch information
hrydgard committed Jan 8, 2025
commit 440fa80c5fe3ade3da120918013daf6a01cc7cfb
4 changes: 2 additions & 2 deletions Core/HLE/SocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Keep track of who's using a socket.
enum class SocketState {
Unused,
Unused = 0,
UsedNetInet,
UsedProAdhoc,
};
Expand All @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]{};

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
}
Expand Down
Loading