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 Close method to socket manager
  • Loading branch information
hrydgard committed Jan 8, 2025
commit b97b0e4cf6bcc297d94ce0b53d0c2453974f81c3
11 changes: 11 additions & 0 deletions Core/HLE/SocketManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ InetSocket *SocketManager::CreateSocket(int *index, SocketState state, int domai
return nullptr;
}

bool SocketManager::Close(InetSocket *inetSocket) {
_dbg_assert_(inetSocket->state != SocketState::Unused);
if (closesocket(inetSocket->sock) != 0) {
ERROR_LOG(Log::sceNet, "closesocket(%d) failed", inetSocket->sock);
return false;
}
inetSocket->state = SocketState::Unused;
inetSocket->sock = 0;
return true;
}

bool SocketManager::GetInetSocket(int sock, InetSocket **inetSocket) {
std::lock_guard<std::mutex> guard(g_socketMutex);
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(inetSockets_) || inetSockets_[sock].state == SocketState::Unused) {
Expand Down
3 changes: 3 additions & 0 deletions Core/HLE/SocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct InetSocket {
int protocol;
};

// Only use this for sockets whose ID are exposed to the game.
// Don't really need to bother with the others, as the game doesn't know about them.
class SocketManager {
public:
enum {
Expand All @@ -29,6 +31,7 @@ class SocketManager {
InetSocket *CreateSocket(int *index, SocketState state, int domain, int type, int protocol);
bool GetInetSocket(int sock, InetSocket **inetSocket);
SOCKET GetHostSocketFromInetSocket(int sock);
bool Close(InetSocket *inetSocket);
void CloseAll();

// For debugger
Expand Down
21 changes: 5 additions & 16 deletions Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,9 @@ static int sceNetInetClose(int socket) {
return hleLogError(Log::sceNet, ERROR_INET_EBADF, "Bad socket #%d", socket);
}

int retVal = closesocket(inetSock->sock);
if (retVal == 0) {
inetSock->sock = 0;
inetSock->state = SocketState::Unused;
} else {
ERROR_LOG(Log::sceNet, "closesocket(%d) failed (socket=%d)", inetSock->sock, socket);
}
return hleLogSuccessI(Log::sceNet, retVal);
g_socketManager.Close(inetSock);

return hleLogSuccessI(Log::sceNet, 0);
}

// TODO: How is this different than just sceNetInetClose?
Expand All @@ -744,14 +739,8 @@ static int sceNetInetCloseWithRST(int socket) {
sl.l_onoff = 1; // non-zero value enables linger option in kernel
sl.l_linger = 0; // timeout interval in seconds
setsockopt(inetSock->sock, SOL_SOCKET, SO_LINGER, (const char*)&sl, sizeof(sl));
int retVal = closesocket(inetSock->sock);
if (retVal == 0) {
inetSock->sock = 0;
inetSock->state = SocketState::Unused;
} else {
ERROR_LOG(Log::sceNet, "closesocket(%d) failed (socket=%d)", inetSock->sock, socket);
}
return hleLogSuccessI(Log::sceNet, retVal);
g_socketManager.Close(inetSock);
return hleLogSuccessI(Log::sceNet, 0);
}

static int sceNetInetRecvfrom(int socket, u32 bufferPtr, int len, int flags, u32 fromPtr, u32 fromlenPtr) {
Expand Down