refactor: IArchNetwork turn anonymous enum into PollEventMask struct with set of static values

This commit is contained in:
sithlord48
2025-06-30 22:58:15 -04:00
committed by Chris Rizzitello
parent 47d44db497
commit 8f6f014bcd
5 changed files with 39 additions and 38 deletions

View File

@ -72,12 +72,12 @@ public:
Events for \c poll() are bitmasks and can be combined using the Events for \c poll() are bitmasks and can be combined using the
bitwise operators. bitwise operators.
*/ */
enum struct PollEventMask
{ {
kPOLLIN = 1, //!< Socket is readable inline static const int In = 1; //!< Socket is readable
kPOLLOUT = 2, //!< Socket is writable inline static const int Out = 2; //!< Socket is writable
kPOLLERR = 4, //!< The socket is in an error state inline static const int Error = 4; //!< The socket is in an error state
kPOLLNVAL = 8 //!< The socket is invalid inline static const int Invalid = 8; //!< The socket is invalid
}; };
//! A socket query for \c poll() //! A socket query for \c poll()
@ -89,8 +89,7 @@ public:
//! The events to query for //! The events to query for
/*! /*!
The events to query for can be any combination of kPOLLIN and The events to query for can be any combination of PollEventMask::In and PollEventMask::Out.
kPOLLOUT.
*/ */
unsigned short m_events; unsigned short m_events;
@ -175,9 +174,9 @@ public:
and/or writable (or indefinitely if \c timeout < 0). Returns the and/or writable (or indefinitely if \c timeout < 0). Returns the
number of sockets that were readable (if readability was being number of sockets that were readable (if readability was being
queried) or writable (if writablility was being queried) and sets queried) or writable (if writablility was being queried) and sets
the \c m_revents members of the entries. \c kPOLLERR and \c kPOLLNVAL the \c m_revents members of the entries. \c PollEventMask::Error and \c PollEventMask::Invalid
are set in \c m_revents as appropriate. If a socket indicates are set in \c m_revents as appropriate. If a socket indicates
\c kPOLLERR then \c throwErrorOnSocket() can be used to determine \c PollEventMask::Error then \c throwErrorOnSocket() can be used to determine
the type of error. Returns 0 immediately regardless of the \c timeout the type of error. Returns 0 immediately regardless of the \c timeout
if no valid sockets are selected for testing. if no valid sockets are selected for testing.

View File

@ -278,10 +278,10 @@ int ArchNetworkBSD::pollSocket(PollEntry pe[], int num, double timeout)
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
pfd[i].fd = (pe[i].m_socket == nullptr) ? -1 : pe[i].m_socket->m_fd; pfd[i].fd = (pe[i].m_socket == nullptr) ? -1 : pe[i].m_socket->m_fd;
pfd[i].events = 0; pfd[i].events = 0;
if ((pe[i].m_events & kPOLLIN) != 0) { if ((pe[i].m_events & PollEventMask::In) != 0) {
pfd[i].events |= POLLIN; pfd[i].events |= POLLIN;
} }
if ((pe[i].m_events & kPOLLOUT) != 0) { if ((pe[i].m_events & PollEventMask::Out) != 0) {
pfd[i].events |= POLLOUT; pfd[i].events |= POLLOUT;
} }
} }
@ -328,16 +328,16 @@ int ArchNetworkBSD::pollSocket(PollEntry pe[], int num, double timeout)
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
pe[i].m_revents = 0; pe[i].m_revents = 0;
if ((pfd[i].revents & POLLIN) != 0) { if ((pfd[i].revents & POLLIN) != 0) {
pe[i].m_revents |= kPOLLIN; pe[i].m_revents |= PollEventMask::In;
} }
if ((pfd[i].revents & POLLOUT) != 0) { if ((pfd[i].revents & POLLOUT) != 0) {
pe[i].m_revents |= kPOLLOUT; pe[i].m_revents |= PollEventMask::Out;
} }
if ((pfd[i].revents & POLLERR) != 0) { if ((pfd[i].revents & POLLERR) != 0) {
pe[i].m_revents |= kPOLLERR; pe[i].m_revents |= PollEventMask::Error;
} }
if ((pfd[i].revents & POLLNVAL) != 0) { if ((pfd[i].revents & POLLNVAL) != 0) {
pe[i].m_revents |= kPOLLNVAL; pe[i].m_revents |= PollEventMask::Invalid;
} }
} }

View File

@ -380,16 +380,16 @@ int ArchNetworkWinsock::pollSocket(PollEntry pe[], int num, double timeout)
// set invalid flag if socket is bogus then go to next socket // set invalid flag if socket is bogus then go to next socket
if (pe[i].m_socket == nullptr) { if (pe[i].m_socket == nullptr) {
pe[i].m_revents |= kPOLLNVAL; pe[i].m_revents |= PollEventMask::Invalid;
continue; continue;
} }
// select desired events // select desired events
long socketEvents = 0; long socketEvents = 0;
if ((pe[i].m_events & kPOLLIN) != 0) { if ((pe[i].m_events & PollEventMask::In) != 0) {
socketEvents |= FD_READ | FD_ACCEPT | FD_CLOSE; socketEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
} }
if ((pe[i].m_events & kPOLLOUT) != 0) { if ((pe[i].m_events & PollEventMask::Out) != 0) {
socketEvents |= FD_WRITE | FD_CONNECT | FD_CLOSE; socketEvents |= FD_WRITE | FD_CONNECT | FD_CLOSE;
// if m_pollWrite is false then we assume the socket is // if m_pollWrite is false then we assume the socket is
@ -397,7 +397,7 @@ int ArchNetworkWinsock::pollSocket(PollEntry pe[], int num, double timeout)
// when the state changes from unwritable. // when the state changes from unwritable.
if (!pe[i].m_socket->m_pollWrite) { if (!pe[i].m_socket->m_pollWrite) {
canWrite = true; canWrite = true;
pe[i].m_revents |= kPOLLOUT; pe[i].m_revents |= PollEventMask::Out;
} }
} }
@ -462,7 +462,7 @@ int ArchNetworkWinsock::pollSocket(PollEntry pe[], int num, double timeout)
} }
for (i = 0, n = 0; i < num; ++i) { for (i = 0, n = 0; i < num; ++i) {
// skip events we didn't check // skip events we didn't check
if (pe[i].m_socket == nullptr || (pe[i].m_events & (kPOLLIN | kPOLLOUT)) == 0) { if (pe[i].m_socket == nullptr || (pe[i].m_events & (PollEventMask::In | PollEventMask::Out)) == 0) {
continue; continue;
} }
@ -472,13 +472,13 @@ int ArchNetworkWinsock::pollSocket(PollEntry pe[], int num, double timeout)
continue; continue;
} }
if ((info.lNetworkEvents & FD_READ) != 0) { if ((info.lNetworkEvents & FD_READ) != 0) {
pe[i].m_revents |= kPOLLIN; pe[i].m_revents |= PollEventMask::In;
} }
if ((info.lNetworkEvents & FD_ACCEPT) != 0) { if ((info.lNetworkEvents & FD_ACCEPT) != 0) {
pe[i].m_revents |= kPOLLIN; pe[i].m_revents |= PollEventMask::In;
} }
if ((info.lNetworkEvents & FD_WRITE) != 0) { if ((info.lNetworkEvents & FD_WRITE) != 0) {
pe[i].m_revents |= kPOLLOUT; pe[i].m_revents |= PollEventMask::Out;
// socket is now writable so don't bothing polling for // socket is now writable so don't bothing polling for
// writable until it becomes unwritable. // writable until it becomes unwritable.
@ -486,21 +486,21 @@ int ArchNetworkWinsock::pollSocket(PollEntry pe[], int num, double timeout)
} }
if ((info.lNetworkEvents & FD_CONNECT) != 0) { if ((info.lNetworkEvents & FD_CONNECT) != 0) {
if (info.iErrorCode[FD_CONNECT_BIT] != 0) { if (info.iErrorCode[FD_CONNECT_BIT] != 0) {
pe[i].m_revents |= kPOLLERR; pe[i].m_revents |= PollEventMask::Error;
} else { } else {
pe[i].m_revents |= kPOLLOUT; pe[i].m_revents |= PollEventMask::Out;
pe[i].m_socket->m_pollWrite = false; pe[i].m_socket->m_pollWrite = false;
} }
} }
if ((info.lNetworkEvents & FD_CLOSE) != 0) { if ((info.lNetworkEvents & FD_CLOSE) != 0) {
if (info.iErrorCode[FD_CLOSE_BIT] != 0) { if (info.iErrorCode[FD_CLOSE_BIT] != 0) {
pe[i].m_revents |= kPOLLERR; pe[i].m_revents |= PollEventMask::Error;
} else { } else {
if ((pe[i].m_events & kPOLLIN) != 0) { if ((pe[i].m_events & PollEventMask::In) != 0) {
pe[i].m_revents |= kPOLLIN; pe[i].m_revents |= PollEventMask::In;
} }
if ((pe[i].m_events & kPOLLOUT) != 0) { if ((pe[i].m_events & PollEventMask::Out) != 0) {
pe[i].m_revents |= kPOLLOUT; pe[i].m_revents |= PollEventMask::Out;
} }
} }
} }

View File

@ -152,10 +152,10 @@ void SocketMultiplexer::serviceThread(void *)
pfd.m_socket = job->getSocket(); pfd.m_socket = job->getSocket();
pfd.m_events = 0; pfd.m_events = 0;
if (job->isReadable()) { if (job->isReadable()) {
pfd.m_events |= IArchNetwork::kPOLLIN; pfd.m_events |= IArchNetwork::PollEventMask::In;
} }
if (job->isWritable()) { if (job->isWritable()) {
pfd.m_events |= IArchNetwork::kPOLLOUT; pfd.m_events |= IArchNetwork::PollEventMask::Out;
} }
pfds.push_back(pfd); pfds.push_back(pfd);
} }
@ -187,9 +187,10 @@ void SocketMultiplexer::serviceThread(void *)
if (*jobCursor != nullptr) { if (*jobCursor != nullptr) {
// get poll state // get poll state
unsigned short revents = pfds[i].m_revents; unsigned short revents = pfds[i].m_revents;
bool read = ((revents & IArchNetwork::kPOLLIN) != 0); bool read = ((revents & int(IArchNetwork::PollEventMask::In)) != 0);
bool write = ((revents & IArchNetwork::kPOLLOUT) != 0); bool write = ((revents & int(IArchNetwork::PollEventMask::Out)) != 0);
bool error = ((revents & (IArchNetwork::kPOLLERR | IArchNetwork::kPOLLNVAL)) != 0); bool error =
((revents & (int(IArchNetwork::PollEventMask::Error) | int(IArchNetwork::PollEventMask::Invalid))) != 0);
// run job // run job
ISocketMultiplexerJob *job = *jobCursor; ISocketMultiplexerJob *job = *jobCursor;

View File

@ -84,7 +84,8 @@ TEST(ArchNetworkBSDTests, pollSocket_pfdHasRevents_copiedToEntries)
networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1); networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1);
const auto expect = IArchNetwork::kPOLLIN | IArchNetwork::kPOLLOUT | IArchNetwork::kPOLLERR | IArchNetwork::kPOLLNVAL; const auto expect = IArchNetwork::PollEventMask::In | IArchNetwork::PollEventMask::Out |
IArchNetwork::PollEventMask::Error | IArchNetwork::PollEventMask::Invalid;
EXPECT_EQ(entries[0].m_revents, expect); EXPECT_EQ(entries[0].m_revents, expect);
} }
@ -116,7 +117,7 @@ TEST(ArchNetworkBSDTests, pollSocket_eventHasPollInBit_bitWasSet)
auto deps = MockDeps::makeNice(); auto deps = MockDeps::makeNice();
ArchNetworkBSD networkBSD(deps); ArchNetworkBSD networkBSD(deps);
ArchSocketImpl socket{1, 0}; ArchSocketImpl socket{1, 0};
PollEntries entries{{&socket, IArchNetwork::kPOLLIN, 0}}; PollEntries entries{{&socket, IArchNetwork::PollEventMask::In, 0}};
networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1); networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1);
@ -128,7 +129,7 @@ TEST(ArchNetworkBSDTests, pollSocket_eventHasPollOutBit_bitWasSet)
auto deps = MockDeps::makeNice(); auto deps = MockDeps::makeNice();
ArchNetworkBSD networkBSD(deps); ArchNetworkBSD networkBSD(deps);
ArchSocketImpl socket{1, 0}; ArchSocketImpl socket{1, 0};
PollEntries entries{{&socket, IArchNetwork::kPOLLOUT, 0}}; PollEntries entries{{&socket, IArchNetwork::PollEventMask::Out, 0}};
networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1); networkBSD.pollSocket(entries.data(), static_cast<int>(entries.size()), 1);