net: convert standalone IsSelectableSocket() to Sock::IsSelectable()

This makes the callers mockable.
This commit is contained in:
Vasil Dimov
2021-04-13 14:29:14 +02:00
parent 5db7d2ca0a
commit b4bac55679
7 changed files with 30 additions and 10 deletions

View File

@@ -117,11 +117,12 @@ int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const
return getsockname(m_socket, name, name_len);
}
bool IsSelectableSocket(const SOCKET& s) {
bool Sock::IsSelectable() const
{
#if defined(USE_POLL) || defined(WIN32)
return true;
#else
return (s < FD_SETSIZE);
return m_socket < FD_SETSIZE;
#endif
}
@@ -193,10 +194,10 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
SOCKET socket_max{0};
for (const auto& [sock, events] : events_per_sock) {
const auto& s = sock->m_socket;
if (!IsSelectableSocket(s)) {
if (!sock->IsSelectable()) {
return false;
}
const auto& s = sock->m_socket;
if (events.requested & RECV) {
FD_SET(s, &recv);
}

View File

@@ -133,6 +133,12 @@ public:
*/
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
/**
* Check if the underlying socket can be used for `select(2)` (or the `Wait()` method).
* @return true if selectable
*/
[[nodiscard]] virtual bool IsSelectable() const;
using Event = uint8_t;
/**
@@ -267,8 +273,6 @@ private:
void Close();
};
bool IsSelectableSocket(const SOCKET& s);
/** Return readable error string for a network error code */
std::string NetworkErrorString(int err);