fix(libSceNet): report a stream whose peer closed as readable on Windows
When the peer of a stream socket closes, Winsock's WSAPoll reports POLLHUP without POLLRDNORM, whether or not the socket's own sending side is shut down. sceNetEpollWait passed that on, so a socket whose peer had closed came back as SCE_NET_EPOLLHUP alone and never as readable: a loop that waits for SCE_NET_EPOLLIN to read the end of the stream never got it, while every wait returned at once. FreeBSD's sopoll_generic reports a socket that can receive no more as readable, and adds POLLHUP only once it can send no more either. Linux does the same, so the Linux path already behaved this way. On Windows, a stream socket's POLLHUP without POLLERR now reports POLLRDNORM when reading was asked for, and keeps POLLHUP only after sceNetShutdown has shut the socket's own sending side, which libSceNet now records.
This commit is contained in:
1 parent
18c438b109
commit
a2b849e10c
2 files changed
+56
-5
No files matched your search
@@ -241,6 +241,7 @@ struct Sock {
|
||||
bool bound = false;
|
||||
bool listening = false;
|
||||
bool aborted = false;
|
||||
bool sending_shut = false;
|
||||
int rcv_timeout_us = 0;
|
||||
int snd_timeout_us = 0;
|
||||
};
|
||||
@@ -445,6 +446,14 @@ int timeout_option(int optname) {
|
||||
return optname;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
short closed_stream_events(const Sock& socket, short native, short requested) {
|
||||
if (socket.type != NET_SOCK_STREAM || (native & (POLLHUP | POLLERR)) != POLLHUP) return native;
|
||||
native = static_cast<short>(native | (requested & POLLRDNORM));
|
||||
return socket.sending_shut ? native : static_cast<short>(native & ~POLLHUP);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::int64_t message_length(const NetMsghdr* message) {
|
||||
if (!message) return fail(NET_EFAULT);
|
||||
if (message->iov_length < 0 || message->iov_length > NET_UIO_MAXIOV) return fail(NET_EMSGSIZE);
|
||||
@@ -939,7 +948,13 @@ int APS5_VABI sceNetShutdown(int s, int how) {
|
||||
if (it == g_socks.end()) return fail(NET_EBADF);
|
||||
socket = it->second;
|
||||
}
|
||||
return ::shutdown(socket.native->value, how) == 0 ? 0 : fail(native_error());
|
||||
if (::shutdown(socket.native->value, how) != 0) return fail(native_error());
|
||||
if (how != 0) {
|
||||
std::lock_guard lk(g_mutex);
|
||||
const auto it = g_socks.find(s);
|
||||
if (it != g_socks.end()) it->second.sending_shut = true;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceNetSetsockopt(int s, int level, int optname, const void* optval, uint32_t optlen) {
|
||||
@@ -1129,7 +1144,7 @@ int APS5_VABI sceNetEpollWait(int eid, NetEpollEvent* events, int maxevents, int
|
||||
: std::chrono::steady_clock::time_point::max();
|
||||
for (;;) {
|
||||
std::vector<NetEpollEvent> registered_events;
|
||||
std::vector<std::shared_ptr<NativeSocketHandle>> native_sockets;
|
||||
std::vector<Sock> sockets;
|
||||
{
|
||||
std::lock_guard lk(g_mutex);
|
||||
if (g_epolls.count(eid) == 0) return fail(NET_EBADF);
|
||||
@@ -1140,7 +1155,7 @@ int APS5_VABI sceNetEpollWait(int eid, NetEpollEvent* events, int maxevents, int
|
||||
const auto socket = g_socks.find(entry.first);
|
||||
if (socket == g_socks.end()) continue;
|
||||
registered_events.push_back({entry.second.events, 0, static_cast<std::uint64_t>(entry.first), entry.second.data});
|
||||
native_sockets.push_back(socket->second.native);
|
||||
sockets.push_back(socket->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1158,14 +1173,14 @@ int APS5_VABI sceNetEpollWait(int eid, NetEpollEvent* events, int maxevents, int
|
||||
#ifdef _WIN32
|
||||
std::vector<WSAPOLLFD> descriptors(registered_events.size());
|
||||
for (std::size_t index = 0; index < descriptors.size(); ++index) {
|
||||
descriptors[index].fd = native_sockets[index]->value;
|
||||
descriptors[index].fd = sockets[index].native->value;
|
||||
if (registered_events[index].events & 1) descriptors[index].events |= POLLRDNORM;
|
||||
if (registered_events[index].events & 2) descriptors[index].events |= POLLWRNORM;
|
||||
}
|
||||
#else
|
||||
std::vector<pollfd> descriptors(registered_events.size());
|
||||
for (std::size_t index = 0; index < descriptors.size(); ++index) {
|
||||
descriptors[index].fd = native_sockets[index]->value;
|
||||
descriptors[index].fd = sockets[index].native->value;
|
||||
if (registered_events[index].events & 1) descriptors[index].events |= POLLIN;
|
||||
if (registered_events[index].events & 2) descriptors[index].events |= POLLOUT;
|
||||
}
|
||||
@@ -1180,6 +1195,8 @@ int APS5_VABI sceNetEpollWait(int eid, NetEpollEvent* events, int maxevents, int
|
||||
}
|
||||
#ifdef _WIN32
|
||||
const int ready_count = WSAPoll(descriptors.data(), static_cast<ULONG>(descriptors.size()), wait_ms);
|
||||
for (std::size_t index = 0; index < descriptors.size(); ++index)
|
||||
descriptors[index].revents = closed_stream_events(sockets[index], descriptors[index].revents, descriptors[index].events);
|
||||
#else
|
||||
const int ready_count = ::poll(descriptors.data(), descriptors.size(), wait_ms);
|
||||
#endif
|
||||
|
||||
@@ -447,6 +447,39 @@ static void CheckEpollIdent() {
|
||||
Require(sceNetSocketClose(sender) == 0 && sceNetSocketClose(receiver) == 0);
|
||||
}
|
||||
|
||||
static void CheckEpollClosedStream() {
|
||||
std::array<std::uint8_t, 16> address{16, 2, 0, 0, 127, 0, 0, 1};
|
||||
const int listener = sceNetSocket("closed-stream-listener", 2, 1, 6);
|
||||
Require(listener >= 0);
|
||||
Require(sceNetBind_nid_postfix(listener, address.data(), address.size()) == 0 && sceNetListen(listener, 1) == 0);
|
||||
std::uint32_t size = address.size();
|
||||
Require(sceNetGetsockname(listener, address.data(), &size) == 0);
|
||||
const int client = sceNetSocket("closed-stream-client", 2, 1, 6);
|
||||
Require(client >= 0 && sceNetConnect(client, address.data(), address.size()) == 0);
|
||||
const int accepted = sceNetAccept(listener, nullptr, nullptr);
|
||||
Require(accepted >= 0);
|
||||
const int epoll = sceNetEpollCreate("epoll-closed-stream", 0);
|
||||
Require(epoll >= 0);
|
||||
NetEpollEvent registration{};
|
||||
registration.events = 1;
|
||||
registration.ident = static_cast<std::uint64_t>(accepted);
|
||||
Require(sceNetEpollControl(epoll, 1, accepted, ®istration) == 0);
|
||||
Require(sceNetSocketClose(client) == 0);
|
||||
NetEpollEvent ready{};
|
||||
Require(sceNetEpollWait(epoll, &ready, 1, 1000000) == 1 && ready.events == 1);
|
||||
char byte = 0;
|
||||
Require(sceNetRecv(accepted, &byte, 1, 0) == 0);
|
||||
registration.events = 3;
|
||||
Require(sceNetEpollControl(epoll, 2, accepted, ®istration) == 0);
|
||||
Require(sceNetEpollWait(epoll, &ready, 1, 0) == 1 && ready.events == 3);
|
||||
Require(sceNetShutdown(accepted, 1) == 0);
|
||||
registration.events = 1;
|
||||
Require(sceNetEpollControl(epoll, 2, accepted, ®istration) == 0);
|
||||
Require(sceNetEpollWait(epoll, &ready, 1, 0) == 1 && ready.events == (1 | 16));
|
||||
Require(sceNetEpollDestroy(epoll) == 0);
|
||||
Require(sceNetSocketClose(accepted) == 0 && sceNetSocketClose(listener) == 0);
|
||||
}
|
||||
|
||||
static bool ipv6Unavailable = false;
|
||||
|
||||
static bool HostIpv6LoopbackUnavailable() {
|
||||
@@ -654,6 +687,7 @@ int main() {
|
||||
CheckAddressText(28, "::1");
|
||||
CheckAddressText(28, "1234:5678:9abc:def0:1234:5678:9abc:def0");
|
||||
CheckEpollIdent();
|
||||
CheckEpollClosedStream();
|
||||
CheckLinkLocalAllNodesIpv6();
|
||||
CheckUnboundSocketName();
|
||||
CheckUnspecifiedIpv6();
|
||||
|
||||
Reference in new issue
Block a user