Skip to content

Commit 41eaed2

Browse files
committed
net: allow selection of Wait() API by specifying SocketEventsMode
No behavior has changed as we don't support any additional APIs and SEM_LT_DEFAULT preserves old behavior but upcoming commits will utilize this to able to effectuate the preferences set by `-socketevents`.
1 parent ca1ec0b commit 41eaed2

File tree

7 files changed

+42
-5
lines changed

7 files changed

+42
-5
lines changed

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ bool Session::Accept(Connection& conn)
160160

161161
while (!*m_interrupt) {
162162
Sock::Event occurred;
163-
if (!conn.sock->Wait(MAX_WAIT_FOR_IO, Sock::RECV, &occurred)) {
163+
if (!conn.sock->Wait(MAX_WAIT_FOR_IO, Sock::RECV, SEM_LT_DEFAULT, &occurred)) {
164164
errmsg = "wait on socket failed";
165165
break;
166166
}

src/netbase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen
572572
// synchronously to check for successful connection with a timeout.
573573
const Sock::Event requested = Sock::RECV | Sock::SEND;
574574
Sock::Event occurred;
575-
if (!sock.Wait(std::chrono::milliseconds{nConnectTimeout}, requested, &occurred)) {
575+
if (!sock.Wait(std::chrono::milliseconds{nConnectTimeout}, requested, SEM_LT_DEFAULT, &occurred)) {
576576
LogPrintf("wait for connect to %s failed: %s\n",
577577
dest_str,
578578
NetworkErrorString(WSAGetLastError()));

src/test/fuzz/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ bool FuzzedSock::IsSelectable() const
270270
return m_selectable;
271271
}
272272

273-
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
273+
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, SocketEventsMode event_mode, Event* occurred) const
274274
{
275275
constexpr std::array wait_errnos{
276276
EBADF,

src/test/fuzz/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class FuzzedSock : public Sock
8989

9090
bool IsSelectable() const override;
9191

92-
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
92+
bool Wait(std::chrono::milliseconds timeout, Event requested, SocketEventsMode event_mode = SEM_LT_DEFAULT, Event* occurred = nullptr) const override;
9393

9494
bool IsConnected(std::string& errmsg) const override;
9595
};

src/test/util/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class StaticContentsSock : public Sock
202202

203203
bool Wait(std::chrono::milliseconds timeout,
204204
Event requested,
205+
SocketEventsMode event_mode = SEM_LT_DEFAULT,
205206
Event* occurred = nullptr) const override
206207
{
207208
if (occurred != nullptr) {

src/util/sock.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,36 @@ bool Sock::IsSelectable() const
140140
#endif
141141
}
142142

143-
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
143+
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, SocketEventsMode event_mode, Event* occurred) const
144144
{
145+
std::string debug_str;
146+
147+
switch (event_mode)
148+
{
149+
case SocketEventsMode::Poll:
150+
#ifdef USE_POLL
151+
return WaitPoll(timeout, requested, occurred);
152+
#else
153+
debug_str += "Sock::Wait -- Support for poll not compiled in, falling back on ";
154+
break;
155+
#endif /* USE_POLL */
156+
case SocketEventsMode::Select:
157+
return WaitSelect(timeout, requested, occurred);
158+
case SocketEventsMode::EPoll:
159+
debug_str += "Sock::Wait -- Unimplemented for epoll, falling back on ";
160+
break;
161+
case SocketEventsMode::KQueue:
162+
debug_str += "Sock::Wait -- Unimplemented for kqueue, falling back on ";
163+
break;
164+
default:
165+
assert(false);
166+
}
167+
#ifdef USE_POLL
168+
debug_str += "poll";
169+
#else
170+
debug_str += "select";
171+
#endif /* USE_POLL*/
172+
LogPrint(BCLog::NET, "%s\n", debug_str);
145173
#ifdef USE_POLL
146174
return WaitPoll(timeout, requested, occurred);
147175
#else

src/util/sock.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ enum class SocketEventsMode : int8_t {
2828
Unknown = -1
2929
};
3030

31+
#ifdef USE_POLL
32+
#define SEM_LT_DEFAULT SocketEventsMode::Poll
33+
#else
34+
#define SEM_LT_DEFAULT SocketEventsMode::Select
35+
#endif /* USE_POLL */
36+
3137
/* Converts SocketEventsMode value to string with additional check to report modes not compiled for as unknown */
3238
constexpr std::string_view SEMToString(const SocketEventsMode val) {
3339
switch (val) {
@@ -212,6 +218,7 @@ class Sock
212218
* Wait for readiness for input (recv) or output (send).
213219
* @param[in] timeout Wait this much for at least one of the requested events to occur.
214220
* @param[in] requested Wait for those events, bitwise-or of `RECV` and `SEND`.
221+
* @param[in] event_mode Wait using the API specified.
215222
* @param[out] occurred If not nullptr and the function returns `true`, then this
216223
* indicates which of the requested events occurred (`ERR` will be added, even if
217224
* not requested, if an exceptional event occurs on the socket).
@@ -220,6 +227,7 @@ class Sock
220227
*/
221228
[[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
222229
Event requested,
230+
SocketEventsMode event_mode = SEM_LT_DEFAULT,
223231
Event* occurred = nullptr) const;
224232
#ifdef USE_POLL
225233
bool WaitPoll(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const;

0 commit comments

Comments
 (0)