Skip to content

Commit

Permalink
Merge pull request hrydgard#19833 from hrydgard/socket-remap-followup
Browse files Browse the repository at this point in the history
Add socket mapping for accept(). Don't know if games use it...
  • Loading branch information
hrydgard authored Jan 8, 2025
2 parents d4ad8c9 + 0cec39d commit a19523a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
23 changes: 23 additions & 0 deletions Core/HLE/SocketManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ InetSocket *SocketManager::CreateSocket(int *index, int *returned_errno, SocketS
return nullptr;
}

InetSocket *SocketManager::AdoptSocket(int *index, SOCKET hostSocket, const InetSocket *derive) {
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;

InetSocket *inetSock = inetSockets_ + i;
inetSock->sock = hostSocket;
inetSock->state = derive->state;
inetSock->domain = derive->domain;
inetSock->type = derive->type;
inetSock->protocol = derive->protocol;
inetSock->nonblocking = derive->nonblocking; // should we inherit blocking state?
return inetSock;
}
}

// No space? Return nullptr and let the caller handle it. Shouldn't ever happen.
*index = 0;
return nullptr;
}

bool SocketManager::Close(InetSocket *inetSocket) {
_dbg_assert_(inetSocket->state != SocketState::Unused);
if (closesocket(inetSocket->sock) != 0) {
Expand Down
3 changes: 3 additions & 0 deletions Core/HLE/SocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class SocketManager {
};

InetSocket *CreateSocket(int *index, int *returned_errno, SocketState state, int domain, int type, int protocol);
// for accept()
InetSocket *AdoptSocket(int *index, SOCKET hostSocket, const InetSocket *derive);

bool GetInetSocket(int sock, InetSocket **inetSocket);
SOCKET GetHostSocketFromInetSocket(int sock);
bool Close(InetSocket *inetSocket);
Expand Down
21 changes: 15 additions & 6 deletions Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,14 +691,23 @@ static int sceNetInetAccept(int socket, u32 addrPtr, u32 addrLenPtr) {
SockAddrIN4 saddr{};
if (srclen)
*srclen = std::min((*srclen) > 0 ? *srclen : 0, static_cast<socklen_t>(sizeof(saddr)));
int retval = accept(inetSock->sock, (struct sockaddr*)&saddr.addr, srclen);
if (retval < 0) {

int newHostSocket = accept(inetSock->sock, (struct sockaddr*)&saddr.addr, srclen);
if (newHostSocket < 0) {
inetLastErrno = socket_errno;
if (inetLastErrno == EAGAIN)
hleLogDebug(Log::sceNet, retval, "errno = %d", inetLastErrno);
hleLogDebug(Log::sceNet, newHostSocket, "errno = %d", inetLastErrno);
else
hleLogError(Log::sceNet, retval, "errno = %d", inetLastErrno);
return retval;
hleLogError(Log::sceNet, newHostSocket, "errno = %d", inetLastErrno);
return -1;
}

int newSocketId;
InetSocket *newInetSocket = g_socketManager.AdoptSocket(&newSocketId, newHostSocket, inetSock);
if (!newInetSocket) {
// Ran out of space. Shouldn't really happen.
inetLastErrno = ENOMEM;
return -1;
}

if (src) {
Expand All @@ -708,7 +717,7 @@ static int sceNetInetAccept(int socket, u32 addrPtr, u32 addrLenPtr) {
}
DEBUG_LOG(Log::sceNet, "Accept: Address = %s, Port = %d", ip2str(saddr.in.sin_addr).c_str(), ntohs(saddr.in.sin_port));

return hleLogSuccessI(Log::sceNet, retval);
return hleLogSuccessI(Log::sceNet, newSocketId);
}

static int sceNetInetShutdown(int socket, int how) {
Expand Down

0 comments on commit a19523a

Please sign in to comment.