Skip to content

Commit

Permalink
Fix EINVAL returned on connect call to connecting socket
Browse files Browse the repository at this point in the history
  • Loading branch information
RipleyTom authored and Megamouse committed Jun 11, 2023
1 parent c0e97b4 commit 5d7e75c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions rpcs3/Emu/Cell/lv2/sys_net/lv2_socket_native.cpp
Expand Up @@ -211,12 +211,20 @@ std::optional<s32> lv2_socket_native::connect(const sys_net_sockaddr& addr)
dnshook.add_dns_spy(lv2_id);
}

#ifdef _WIN32
bool was_connecting = connecting;
#endif

if (::connect(socket, reinterpret_cast<struct sockaddr*>(&native_addr), native_addr_len) == 0)
{
return CELL_OK;
}

#ifdef _WIN32
sys_net_error result = get_last_error(!so_nbio, was_connecting);
#else
sys_net_error result = get_last_error(!so_nbio);
#endif

if (result)
{
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/Cell/lv2/sys_net/network_context.cpp
Expand Up @@ -201,8 +201,8 @@ void network_thread::operator()()
fds[i].revents = 0;
#ifdef _WIN32
const auto cur_connecting = socklist[i]->is_connecting();
was_connecting[i] = connecting;
connecting[i] = connecting;
was_connecting[i] = cur_connecting;
connecting[i] = cur_connecting;
#endif
}
}
Expand Down
11 changes: 8 additions & 3 deletions rpcs3/Emu/Cell/lv2/sys_net/sys_net_helpers.cpp
Expand Up @@ -88,10 +88,15 @@ sys_net_error convert_error(bool is_blocking, int native_error, [[maybe_unused]]
}

#ifdef _WIN32
// Windows will return SYS_NET_ENOTCONN when recvfrom/sendto is called on a socket that is connecting but not yet connected
if (is_connecting && result == SYS_NET_ENOTCONN)
if (is_connecting)
{
return SYS_NET_EAGAIN;
// Windows will return SYS_NET_ENOTCONN when recvfrom/sendto is called on a socket that is connecting but not yet connected
if (result == SYS_NET_ENOTCONN)
return SYS_NET_EAGAIN;

// See https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
if (result == SYS_NET_EINVAL || result == SYS_NET_EWOULDBLOCK)
return SYS_NET_EALREADY;
}
#endif

Expand Down

0 comments on commit 5d7e75c

Please sign in to comment.