Skip to content

Commit a33f88f

Browse files
committed
net: reintroduce IsSelectableSocket() and make it SEM-aware
`IsSelectableSocket()` was subsumed into `Sock` for mockability's sake but creates problems when our event-socket map uses the raw `SOCKET` and not `Sock`, so we need to bring it back. On top of that, `IsSelectableSocket()`'s behavior was defined at compile-time when it should've been sensitive to the runtime capabilities we give it. Currently, this does not cause a change in behavior but makes way for future changes.
1 parent 41eaed2 commit a33f88f

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

src/test/fuzz/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ bool FuzzedSock::SetNonBlocking() const
265265
return true;
266266
}
267267

268-
bool FuzzedSock::IsSelectable() const
268+
bool FuzzedSock::IsSelectable(bool is_select) const
269269
{
270270
return m_selectable;
271271
}

src/test/fuzz/util.h

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

8888
bool SetNonBlocking() const override;
8989

90-
bool IsSelectable() const override;
90+
bool IsSelectable(bool is_select) const override;
9191

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

src/test/util/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class StaticContentsSock : public Sock
198198

199199
bool SetNonBlocking() const override { return true; }
200200

201-
bool IsSelectable() const override { return true; }
201+
bool IsSelectable(bool is_select) const override { return true; }
202202

203203
bool Wait(std::chrono::milliseconds timeout,
204204
Event requested,

src/util/sock.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ static inline bool IOErrorIsPermanent(int err)
2424
return err != WSAEAGAIN && err != WSAEINTR && err != WSAEWOULDBLOCK && err != WSAEINPROGRESS;
2525
}
2626

27+
static inline bool IsSelectableSocket(const SOCKET& s, bool is_select)
28+
{
29+
#if defined(WIN32)
30+
return true;
31+
#else
32+
return is_select ? (s < FD_SETSIZE) : true;
33+
#endif
34+
}
35+
2736
Sock::Sock() : m_socket(INVALID_SOCKET) {}
2837

2938
Sock::Sock(SOCKET s) : m_socket(s) {}
@@ -131,13 +140,9 @@ bool Sock::SetNonBlocking() const
131140
return true;
132141
}
133142

134-
bool Sock::IsSelectable() const
143+
bool Sock::IsSelectable(bool is_select) const
135144
{
136-
#if defined(USE_POLL) || defined(WIN32)
137-
return true;
138-
#else
139-
return m_socket < FD_SETSIZE;
140-
#endif
145+
return IsSelectableSocket(m_socket, is_select);
141146
}
142147

143148
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, SocketEventsMode event_mode, Event* occurred) const
@@ -213,7 +218,7 @@ bool Sock::WaitPoll(std::chrono::milliseconds timeout, Event requested, Event* o
213218

214219
bool Sock::WaitSelect(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
215220
{
216-
if (!IsSelectable()) {
221+
if (!IsSelectableSocket(m_socket, /*is_select=*/true)) {
217222
return false;
218223
}
219224

src/util/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Sock
194194
* Check if the underlying socket can be used for `select(2)` (or the `Wait()` method).
195195
* @return true if selectable
196196
*/
197-
[[nodiscard]] virtual bool IsSelectable() const;
197+
[[nodiscard]] virtual bool IsSelectable(bool is_select = (SEM_LT_DEFAULT == SocketEventsMode::Select)) const;
198198

199199
using Event = uint8_t;
200200

0 commit comments

Comments
 (0)