From 539310966937f8cde47b9679f55800cddffe5bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 4 Jun 2019 15:20:54 +0200 Subject: [PATCH] Add TCPConnection.waitForDataEx. Allows to distinguish between timeout and no more data cases. --- source/vibe/core/net.d | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index f3a4e42..ec8b027 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -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() {}