Merge pull request #159 from vibe-d/waitfordataex

Add TCPConnection.waitForDataEx
This commit is contained in:
Leonid Kramer 2019-06-12 20:41:16 +02:00 committed by GitHub
commit 1d917568e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View file

@ -4,7 +4,7 @@ authors "Sönke Ludwig"
copyright "Copyright © 2016-2018, rejectedsoftware e.K."
license "MIT"
dependency "eventcore" version="~>0.8.40"
dependency "eventcore" version="~>0.8.43"
dependency "stdx-allocator" version="~>2.77.0"
targetName "vibe_core"

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() {}