Replies: 1 comment
-
|
Good observation! Let me explain the behavior: How
|
| Overload | Timeout Source | Timeout Behavior |
|---|---|---|
receiveBytes(void*, int, int) |
Socket receive timeout | Throws TimeoutException |
receiveBytes(Buffer<char>&, int, Timespan) |
Parameter + socket | Returns 0 on poll timeout, throws on recv timeout |
receiveBytes(SocketBufVec&, int) |
Socket receive timeout | Throws TimeoutException |
Is this by design?
Yes, this appears intentional. The Buffer<char>& overload is designed for polling-style usage where you want to check if data is available without blocking indefinitely. The function:
- Auto-resizes the buffer based on
available()bytes - Returns 0 if no data arrives within the timeout (non-exceptional case)
How to get TimeoutException behavior with Buffer<char>&?
Option 1: Use a very large timeout and rely on socket timeout:
socket.setReceiveTimeout(Poco::Timespan(5, 0)); // 5 seconds
int n = socket.receiveBytes(buffer, 0, Poco::Timespan(60, 0)); // 60s poll
// Socket timeout will fire first and throw TimeoutExceptionOption 2: Check return value and throw manually:
int n = socket.receiveBytes(buffer, 0, Poco::Timespan(5, 0));
if (n == 0 && /* expected data */) {
throw Poco::TimeoutException();
}Option 3: Use the void* overload:
char buf[4096];
int n = socket.receiveBytes(buf, sizeof(buf)); // Throws on timeout
buffer.assign(buf, n);Documentation improvement?
Yes, the documentation could be clearer. A PR to improve the docstring would be welcome:
/// Receives data from the socket and stores it in the buffer.
/// Uses poll() to wait for data availability up to the specified timeout.
/// Returns 0 if no data is available within the timeout (does not throw).
/// May throw TimeoutException if the socket's receive timeout expires during recv().
/// ...Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand the exact behavior intended for this function:
poco/Net/include/Poco/Net/SocketImpl.h
Lines 224 to 231 in 3f76ad6
Does it simply return
0in case of a timeout? Is it intended as a polling function?Similar functions throw a
TimeoutExceptionin case of timeout, and this limit would be set bysetReceiveTimeout. This method here takes a separate timeout parameter, though, with a default value, and it might seem at first that it would supersede the socket timeout attribute, but it's not exactly that. First of all, there's still a timeout test happening inside the function body. But like I mentioned before, the behavior seems to be returning0in case the parameter-passed timeout is reached.Is this by design, should I make a patch to update the documentation?
And in this case, how would I obtain the behavior of receiving a
TimeoutExceptionif I'm using aPoco::Buffer<char>&instead of aSocketBufVec&or avoid * buffer, int lengthto store the output? This seems like an inconsistent behavior depending only on the choice of the output data structure.Beta Was this translation helpful? Give feedback.
All reactions