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
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
Next Next commit
Setting a socket number minimum of 20 solved the Mac problems. I gues…
…s it clashes with the adhoc code?
  • Loading branch information
hrydgard committed Jan 8, 2025
commit 3dc2a1034d86e25d48d3fdafd73ebd41b58bc085
26 changes: 7 additions & 19 deletions Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "Core/Util/PortManager.h"
#include "Core/Instance.h"

#define MIN_VALID_SOCKET 20

int inetLastErrno = 0; // TODO: since errno can only be read once, we should keep track the value to be used on sceNetInetGetErrno

bool netInetInited = false;
Expand All @@ -30,7 +32,7 @@ bool netInetInited = false;
InetSocket g_inetSockets[256];

static int AllocInetSocket() {
for (int i = 1; i < ARRAY_SIZE(g_inetSockets); i++) {
for (int i = MIN_VALID_SOCKET; i < ARRAY_SIZE(g_inetSockets); i++) {
if (g_inetSockets[i].state == SocketState::Unused) {
return i;
}
Expand All @@ -41,7 +43,7 @@ static int AllocInetSocket() {
}

static bool GetInetSocket(int sock, InetSocket **inetSocket) {
if (sock < 1 || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
*inetSocket = nullptr;
return false;
}
Expand All @@ -50,22 +52,8 @@ static bool GetInetSocket(int sock, InetSocket **inetSocket) {
}

// Simplified mappers, only really useful in select/poll
static int GetInetSocketFromHostSocket(SOCKET hostSock) {
for (int i = 1; i < ARRAY_SIZE(g_inetSockets); i++) {
if (g_inetSockets[i].state == SocketState::Used && g_inetSockets[i].sock == hostSock) {
return i;
}
}
if (hostSock == 0) {
// Map 0 to 0, special case.
return 0;
}
_dbg_assert_(false);
return -1;
}

static SOCKET GetHostSocketFromInetSocket(int sock) {
if (sock < 1 || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
_dbg_assert_(false);
return -1;
}
Expand Down Expand Up @@ -262,7 +250,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
// Save the mapping during setup.
SOCKET hostSockets[256]{};

for (int i = 0; i < nfds; i++) {
for (int i = MIN_VALID_SOCKET; i < nfds; i++) {
if (readfds && (NetInetFD_ISSET(i, readfds))) {
SOCKET sock = GetHostSocketFromInetSocket(i);
hostSockets[i] = sock;
Expand Down Expand Up @@ -311,7 +299,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
if (readfds != NULL) NetInetFD_ZERO(readfds);
if (writefds != NULL) NetInetFD_ZERO(writefds);
if (exceptfds != NULL) NetInetFD_ZERO(exceptfds);
for (int i = 0; i < nfds; i++) {
for (int i = MIN_VALID_SOCKET; i < nfds; i++) {
if (readfds && hostSockets[i] != 0 && FD_ISSET(hostSockets[i], &rdfds)) {
NetInetFD_SET(i, readfds);
}
Expand Down