Skip to content

Commit ea18453

Browse files
committed
net: extend Sock::Wait() to report a timeout
Previously `Sock::Wait()` would not have signaled to the caller whether a timeout or one of the requested events occurred since that was not needed by any of the callers. Such functionality will be needed in the I2P implementation, thus extend the `Sock::Wait()` method.
1 parent 78fdfbe commit ea18453

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/util/sock.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ssize_t Sock::Recv(void* buf, size_t len, int flags) const
5959
return recv(m_socket, static_cast<char*>(buf), len, flags);
6060
}
6161

62-
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested) const
62+
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
6363
{
6464
#ifdef USE_POLL
6565
pollfd fd;
@@ -72,7 +72,21 @@ bool Sock::Wait(std::chrono::milliseconds timeout, Event requested) const
7272
fd.events |= POLLOUT;
7373
}
7474

75-
return poll(&fd, 1, count_milliseconds(timeout)) != SOCKET_ERROR;
75+
if (poll(&fd, 1, count_milliseconds(timeout)) == SOCKET_ERROR) {
76+
return false;
77+
}
78+
79+
if (occurred != nullptr) {
80+
*occurred = 0;
81+
if (fd.revents & POLLIN) {
82+
*occurred |= RECV;
83+
}
84+
if (fd.revents & POLLOUT) {
85+
*occurred |= SEND;
86+
}
87+
}
88+
89+
return true;
7690
#else
7791
if (!IsSelectableSocket(m_socket)) {
7892
return false;
@@ -93,7 +107,21 @@ bool Sock::Wait(std::chrono::milliseconds timeout, Event requested) const
93107

94108
timeval timeout_struct = MillisToTimeval(timeout);
95109

96-
return select(m_socket + 1, &fdset_recv, &fdset_send, nullptr, &timeout_struct) != SOCKET_ERROR;
110+
if (select(m_socket + 1, &fdset_recv, &fdset_send, nullptr, &timeout_struct) == SOCKET_ERROR) {
111+
return false;
112+
}
113+
114+
if (occurred != nullptr) {
115+
*occurred = 0;
116+
if (FD_ISSET(m_socket, &fdset_recv)) {
117+
*occurred |= RECV;
118+
}
119+
if (FD_ISSET(m_socket, &fdset_send)) {
120+
*occurred |= SEND;
121+
}
122+
}
123+
124+
return true;
97125
#endif /* USE_POLL */
98126
}
99127

src/util/sock.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ class Sock
105105
* Wait for readiness for input (recv) or output (send).
106106
* @param[in] timeout Wait this much for at least one of the requested events to occur.
107107
* @param[in] requested Wait for those events, bitwise-or of `RECV` and `SEND`.
108+
* @param[out] occurred If not nullptr and `true` is returned, then upon return this
109+
* indicates which of the requested events occurred. A timeout is indicated by return
110+
* value of `true` and `occurred` being set to 0.
108111
* @return true on success and false otherwise
109112
*/
110-
virtual bool Wait(std::chrono::milliseconds timeout, Event requested) const;
113+
virtual bool Wait(std::chrono::milliseconds timeout,
114+
Event requested,
115+
Event* occurred = nullptr) const;
111116

112117
private:
113118
/**

0 commit comments

Comments
 (0)