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
Next Next commit
Add SocketManager::CreateSocket for convenience
  • Loading branch information
hrydgard committed Jan 8, 2025
commit a1744a6989a5afed1a9480aa43fbaf150f4c6ac9
27 changes: 24 additions & 3 deletions Core/HLE/SocketManager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "Common/Net/SocketCompat.h"
#include "Core/HLE/NetInetConstants.h"
#include "Core/HLE/SocketManager.h"
#include "Common/Log.h"

Expand All @@ -6,19 +8,38 @@
SocketManager g_socketManager;
static std::mutex g_socketMutex; // TODO: Remove once the adhoc thread is gone

InetSocket *SocketManager::AllocSocket(int *index) {
InetSocket *SocketManager::CreateSocket(int *index, SocketState state, int domain, int type, int protocol) {
_dbg_assert_(state != SocketState::Unused);

int hostDomain = convertSocketDomainPSP2Host(domain);
int hostType = convertSocketTypePSP2Host(type);
int hostProtocol = convertSocketProtoPSP2Host(protocol);

SOCKET hostSock = ::socket(hostDomain, hostType, hostProtocol);
if (hostSock < 0) {
return nullptr;
}

std::lock_guard<std::mutex> guard(g_socketMutex);

for (int i = MIN_VALID_INET_SOCKET; i < ARRAY_SIZE(inetSockets_); i++) {
if (inetSockets_[i].state == SocketState::Unused) {
*index = i;
return inetSockets_ + i;
InetSocket *inetSock = inetSockets_ + i;
inetSock->sock = hostSock;
inetSock->state = state;
inetSock->domain = domain;
inetSock->type = type;
inetSock->protocol = protocol;
return inetSock;
}
}
_dbg_assert_(false);

ERROR_LOG(Log::sceNet, "Ran out of socket handles! This is BAD.");
closesocket(hostSock);
*index = 0;
return 0;
return nullptr;
}

bool SocketManager::GetInetSocket(int sock, InetSocket **inetSocket) {
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/SocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SocketManager {
MIN_VALID_INET_SOCKET = 61,
};

InetSocket *AllocSocket(int *index);
InetSocket *CreateSocket(int *index, SocketState state, int domain, int type, int protocol);
bool GetInetSocket(int sock, InetSocket **inetSocket);
SOCKET GetHostSocketFromInetSocket(int sock);
void CloseAll();
Expand Down
35 changes: 8 additions & 27 deletions Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,40 +392,21 @@ static int sceNetInetSocket(int domain, int type, int protocol) {
WARN_LOG(Log::sceNet, "UNTESTED sceNetInetSocket(%i, %i, %i) at %08x", domain, type, protocol, currentMIPS->pc);
DEBUG_LOG(Log::sceNet, "Socket: Domain = %s, Type = %s, Protocol = %s", inetSocketDomain2str(domain).c_str(), inetSocketType2str(type).c_str(), inetSocketProto2str(protocol).c_str());

int hostDomain = convertSocketDomainPSP2Host(domain);
int hostType = convertSocketTypePSP2Host(type);
int hostProtocol = convertSocketProtoPSP2Host(protocol);

SOCKET hostSock = ::socket(hostDomain, hostType, hostProtocol);
if (hostSock < 0) {
inetLastErrno = socket_errno;
return hleLogError(Log::sceNet, hostSock, "errno = %d", inetLastErrno);
}

// Register the socket.

int socket;

InetSocket *inetSock = g_socketManager.AllocSocket(&socket);
if (socket < 0) {
// Alloc already logged. Let's bail.
return hleLogError(Log::sceNet, ERROR_NET_INTERNAL);
InetSocket *inetSock = g_socketManager.CreateSocket(&socket, SocketState::UsedNetInet, domain, type, protocol);
if (!inetSock) {
inetLastErrno = socket_errno;
return hleLogError(Log::sceNet, -1, "errno = %d", inetLastErrno);
}

inetSock->state = SocketState::UsedNetInet;
inetSock->sock = hostSock;
inetSock->domain = domain;
inetSock->type = type;
inetSock->protocol = protocol;

// Ignore SIGPIPE when supported (ie. BSD/MacOS)
setSockNoSIGPIPE(hostSock, 1);
setSockNoSIGPIPE(inetSock->sock, 1);
// TODO: We should always use non-blocking mode and simulate blocking mode
changeBlockingMode(hostSock, 1);
changeBlockingMode(inetSock->sock, 1);
// Enable Port Re-use, required for multiple-instance
setSockReuseAddrPort(hostSock);
setSockReuseAddrPort(inetSock->sock);
// Disable Connection Reset error on UDP to avoid strange behavior
setUDPConnReset(hostSock, false);
setUDPConnReset(inetSock->sock, false);

return hleLogSuccessI(Log::sceNet, socket);
}
Expand Down