Add TCPConnection.waitForDataEx.

Allows to distinguish between timeout and no more data cases.
This commit is contained in:
Sönke Ludwig 2019-06-04 15:20:54 +02:00
parent 54208ba46f
commit 5393109669

View file

@ -568,10 +568,15 @@ struct TCPConnection {
}
bool waitForData(Duration timeout = Duration.max)
{
return waitForDataEx(timeout) == WaitForDataStatus.dataAvailable;
}
WaitForDataStatus waitForDataEx(Duration timeout = Duration.max)
{
mixin(tracer);
if (!m_context) return false;
if (m_context.readBuffer.length > 0) return true;
if (!m_context) return WaitForDataStatus.noMoreData;
if (m_context.readBuffer.length > 0) return WaitForDataStatus.dataAvailable;
auto mode = timeout <= 0.seconds ? IOMode.immediate : IOMode.once;
bool cancelled;
@ -592,7 +597,8 @@ mixin(tracer);
asyncAwaitAny!(true, waiter)(timeout);
if (cancelled || !m_context) return false;
if (!m_context) return WaitForDataStatus.noMoreData;
if (cancelled) return WaitForDataStatus.timeout;
logTrace("Socket %s, read %s bytes: %s", m_socket, nbytes, status);
@ -607,9 +613,10 @@ mixin(tracer);
case IOStatus.disconnected: break;
}
return m_context.readBuffer.length > 0;
return m_context.readBuffer.length > 0 ? WaitForDataStatus.dataAvailable : WaitForDataStatus.noMoreData;
}
/** Waits asynchronously for new data to arrive.
This function can be used to detach the `TCPConnection` from a
@ -808,6 +815,12 @@ enum WaitForDataAsyncStatus {
waiting,
}
enum WaitForDataStatus {
dataAvailable,
noMoreData,
timeout
}
unittest { // test compilation of callback with scoped destruction
static struct CB {
~this() {}