From 0791082b435371837bbecd50575911ce0ba16dc9 Mon Sep 17 00:00:00 2001 From: comex Date: Mon, 31 Aug 2020 10:20:44 -0400 Subject: [PATCH] network, sockets: Replace `POLL_IN`, `POLL_OUT`, etc. constants with an `enum class PollEvents` Actually, two enum classes, since for some reason there are two separate yet identical `PollFD` types used in the codebase. I get that one is ABI-compatible with the Switch while the other is an abstract type used for the host, but why not use `WSAPOLLFD` directly for the latter? Anyway, why make this change? Because on Apple platforms, `POLL_IN`, `POLL_OUT`, etc. (with an underscore) are defined as macros in . (This is inherited from FreeBSD.) So defining a variable with the same name causes a compile error. I could just rename the variables, but while I was at it I thought I might as well switch to an enum for stronger typing. Also, change the type used for values copied directly to/from the `events` and `revents` fields of the host *native* `pollfd`/`WSASPOLLFD`, from `u32` to `short`, as `short` is the correct canonical type on both Unix and Windows. --- src/core/hle/service/sockets/bsd.cpp | 8 ++-- src/core/hle/service/sockets/sockets.h | 23 +++++---- .../hle/service/sockets/sockets_translate.cpp | 48 +++++++++---------- .../hle/service/sockets/sockets_translate.h | 4 +- src/core/network/network.cpp | 44 ++++++++--------- src/core/network/network.h | 26 ++++++---- 6 files changed, 82 insertions(+), 71 deletions(-) diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index a9875b9a66..70860e7f8b 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -489,18 +489,18 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector static_cast(MAX_FD) || pollfd.fd < 0) { LOG_ERROR(Service, "File descriptor handle={} is invalid", pollfd.fd); - pollfd.revents = 0; + pollfd.revents = PollEvents{}; return {0, Errno::SUCCESS}; } const std::optional& descriptor = file_descriptors[pollfd.fd]; if (!descriptor) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", pollfd.fd); - pollfd.revents = POLL_NVAL; + pollfd.revents = PollEvents::Nval; return {0, Errno::SUCCESS}; } } @@ -510,7 +510,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vectorsocket.get(); result.events = TranslatePollEventsToHost(pollfd.events); - result.revents = 0; + result.revents = Network::PollEvents{}; return result; }); diff --git a/src/core/hle/service/sockets/sockets.h b/src/core/hle/service/sockets/sockets.h index 89a4100769..5a65ed2a9a 100644 --- a/src/core/hle/service/sockets/sockets.h +++ b/src/core/hle/service/sockets/sockets.h @@ -69,10 +69,22 @@ struct SockAddrIn { std::array zeroes; }; +enum class PollEvents : u16 { + // Using Pascal case because IN is a macro on Windows. + In = 1 << 0, + Pri = 1 << 1, + Out = 1 << 2, + Err = 1 << 3, + Hup = 1 << 4, + Nval = 1 << 5, +}; + +DECLARE_ENUM_FLAG_OPERATORS(PollEvents); + struct PollFD { s32 fd; - u16 events; - u16 revents; + PollEvents events; + PollEvents revents; }; struct Linger { @@ -80,13 +92,6 @@ struct Linger { u32 linger; }; -constexpr u16 POLL_IN = 0x01; -constexpr u16 POLL_PRI = 0x02; -constexpr u16 POLL_OUT = 0x04; -constexpr u16 POLL_ERR = 0x08; -constexpr u16 POLL_HUP = 0x10; -constexpr u16 POLL_NVAL = 0x20; - constexpr u32 FLAG_MSG_DONTWAIT = 0x80; constexpr u32 FLAG_O_NONBLOCK = 0x800; diff --git a/src/core/hle/service/sockets/sockets_translate.cpp b/src/core/hle/service/sockets/sockets_translate.cpp index 2e626fd869..37b076fcc0 100644 --- a/src/core/hle/service/sockets/sockets_translate.cpp +++ b/src/core/hle/service/sockets/sockets_translate.cpp @@ -89,43 +89,43 @@ Network::Protocol Translate(Type type, Protocol protocol) { } } -u16 TranslatePollEventsToHost(u32 flags) { - u32 result = 0; - const auto translate = [&result, &flags](u32 from, u32 to) { - if ((flags & from) != 0) { +Network::PollEvents TranslatePollEventsToHost(PollEvents flags) { + Network::PollEvents result{}; + const auto translate = [&result, &flags](PollEvents from, Network::PollEvents to) { + if (True(flags & from)) { flags &= ~from; result |= to; } }; - translate(POLL_IN, Network::POLL_IN); - translate(POLL_PRI, Network::POLL_PRI); - translate(POLL_OUT, Network::POLL_OUT); - translate(POLL_ERR, Network::POLL_ERR); - translate(POLL_HUP, Network::POLL_HUP); - translate(POLL_NVAL, Network::POLL_NVAL); + translate(PollEvents::In, Network::PollEvents::In); + translate(PollEvents::Pri, Network::PollEvents::Pri); + translate(PollEvents::Out, Network::PollEvents::Out); + translate(PollEvents::Err, Network::PollEvents::Err); + translate(PollEvents::Hup, Network::PollEvents::Hup); + translate(PollEvents::Nval, Network::PollEvents::Nval); - UNIMPLEMENTED_IF_MSG(flags != 0, "Unimplemented flags={}", flags); - return static_cast(result); + UNIMPLEMENTED_IF_MSG((u16)flags != 0, "Unimplemented flags={}", (u16)flags); + return result; } -u16 TranslatePollEventsToGuest(u32 flags) { - u32 result = 0; - const auto translate = [&result, &flags](u32 from, u32 to) { - if ((flags & from) != 0) { +PollEvents TranslatePollEventsToGuest(Network::PollEvents flags) { + PollEvents result{}; + const auto translate = [&result, &flags](Network::PollEvents from, PollEvents to) { + if (True(flags & from)) { flags &= ~from; result |= to; } }; - translate(Network::POLL_IN, POLL_IN); - translate(Network::POLL_PRI, POLL_PRI); - translate(Network::POLL_OUT, POLL_OUT); - translate(Network::POLL_ERR, POLL_ERR); - translate(Network::POLL_HUP, POLL_HUP); - translate(Network::POLL_NVAL, POLL_NVAL); + translate(Network::PollEvents::In, PollEvents::In); + translate(Network::PollEvents::Pri, PollEvents::Pri); + translate(Network::PollEvents::Out, PollEvents::Out); + translate(Network::PollEvents::Err, PollEvents::Err); + translate(Network::PollEvents::Hup, PollEvents::Hup); + translate(Network::PollEvents::Nval, PollEvents::Nval); - UNIMPLEMENTED_IF_MSG(flags != 0, "Unimplemented flags={}", flags); - return static_cast(result); + UNIMPLEMENTED_IF_MSG((u16)flags != 0, "Unimplemented flags={}", (u16)flags); + return result; } Network::SockAddrIn Translate(SockAddrIn value) { diff --git a/src/core/hle/service/sockets/sockets_translate.h b/src/core/hle/service/sockets/sockets_translate.h index e498913d4f..057d1ff223 100644 --- a/src/core/hle/service/sockets/sockets_translate.h +++ b/src/core/hle/service/sockets/sockets_translate.h @@ -31,10 +31,10 @@ Network::Type Translate(Type type); Network::Protocol Translate(Type type, Protocol protocol); /// Translate abstract poll event flags to guest poll event flags -u16 TranslatePollEventsToHost(u32 flags); +Network::PollEvents TranslatePollEventsToHost(PollEvents flags); /// Translate guest poll event flags to abstract poll event flags -u16 TranslatePollEventsToGuest(u32 flags); +PollEvents TranslatePollEventsToGuest(Network::PollEvents flags); /// Translate guest socket address structure to abstract socket address structure Network::SockAddrIn Translate(SockAddrIn value); diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 5ef2e85111..6cb5312eaf 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -238,49 +238,49 @@ SockAddrIn TranslateToSockAddrIn(sockaddr input_) { return result; } -u16 TranslatePollEvents(u32 events) { - u32 result = 0; +short TranslatePollEvents(PollEvents events) { + short result = 0; - if ((events & POLL_IN) != 0) { - events &= ~POLL_IN; + if (True(events & PollEvents::In)) { + events &= ~PollEvents::In; result |= POLLIN; } - if ((events & POLL_PRI) != 0) { - events &= ~POLL_PRI; + if (True(events & PollEvents::Pri)) { + events &= ~PollEvents::Pri; #ifdef _WIN32 LOG_WARNING(Service, "Winsock doesn't support POLLPRI"); #else - result |= POLL_PRI; + result |= POLLPRI; #endif } - if ((events & POLL_OUT) != 0) { - events &= ~POLL_OUT; + if (True(events & PollEvents::Out)) { + events &= ~PollEvents::Out; result |= POLLOUT; } - UNIMPLEMENTED_IF_MSG(events != 0, "Unhandled guest events=0x{:x}", events); + UNIMPLEMENTED_IF_MSG((u16)events != 0, "Unhandled guest events=0x{:x}", (u16)events); - return static_cast(result); + return result; } -u16 TranslatePollRevents(u32 revents) { - u32 result = 0; - const auto translate = [&result, &revents](u32 host, u32 guest) { +PollEvents TranslatePollRevents(short revents) { + PollEvents result{}; + const auto translate = [&result, &revents](short host, PollEvents guest) { if ((revents & host) != 0) { - revents &= ~host; + revents &= static_cast(~host); result |= guest; } }; - translate(POLLIN, POLL_IN); - translate(POLLPRI, POLL_PRI); - translate(POLLOUT, POLL_OUT); - translate(POLLERR, POLL_ERR); - translate(POLLHUP, POLL_HUP); + translate(POLLIN, PollEvents::In); + translate(POLLPRI, PollEvents::Pri); + translate(POLLOUT, PollEvents::Out); + translate(POLLERR, PollEvents::Err); + translate(POLLHUP, PollEvents::Hup); UNIMPLEMENTED_IF_MSG(revents != 0, "Unhandled host revents=0x{:x}", revents); - return static_cast(result); + return result; } template @@ -350,7 +350,7 @@ std::pair Poll(std::vector& pollfds, s32 timeout) { } for (size_t i = 0; i < num; ++i) { - pollfds[i].revents = TranslatePollRevents(static_cast(host_pollfds[i].revents)); + pollfds[i].revents = TranslatePollRevents(host_pollfds[i].revents); } if (result > 0) { diff --git a/src/core/network/network.h b/src/core/network/network.h index 0622e45938..76b2821f22 100644 --- a/src/core/network/network.h +++ b/src/core/network/network.h @@ -61,18 +61,24 @@ struct SockAddrIn { }; /// Cross-platform poll fd structure -struct PollFD { - Socket* socket; - u16 events; - u16 revents; + +enum class PollEvents : u16 { + // Using Pascal case because IN is a macro on Windows. + In = 1 << 0, + Pri = 1 << 1, + Out = 1 << 2, + Err = 1 << 3, + Hup = 1 << 4, + Nval = 1 << 5, }; -constexpr u16 POLL_IN = 1 << 0; -constexpr u16 POLL_PRI = 1 << 1; -constexpr u16 POLL_OUT = 1 << 2; -constexpr u16 POLL_ERR = 1 << 3; -constexpr u16 POLL_HUP = 1 << 4; -constexpr u16 POLL_NVAL = 1 << 5; +DECLARE_ENUM_FLAG_OPERATORS(PollEvents); + +struct PollFD { + Socket* socket; + PollEvents events; + PollEvents revents; +}; class NetworkInstance { public: