From 0d6ba62f513a809906e729a622deb1ba86415328 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Wed, 21 Feb 2018 22:17:18 +0000 Subject: [PATCH] added TCPConnection.waitForDataAsync --- source/vibe/core/net.d | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 7c5c56e..fdca9d9 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -586,6 +586,31 @@ mixin(tracer); return m_context.readBuffer.length > 0; } + void waitForDataAsync(void delegate(bool) @safe read_callback, Duration timeout = Duration.max) + { +mixin(tracer); + import vibe.core.core : runTask; + + if (!m_context) { + runTask(read_callback, false); + return; + } + if (m_context.readBuffer.length > 0) { + runTask(read_callback, true); + return; + } + + + auto mode = timeout <= 0.seconds ? IOMode.immediate : IOMode.once; + bool cancelled; + IOStatus status; + size_t nbytes; + eventDriver.sockets.waitForData(m_socket, + (sock, st, nb) { if(st != IOStatus.ok) runTask(read_callback, false); + else runTask(read_callback, true); + }); + } + const(ubyte)[] peek() { return m_context ? m_context.readBuffer.peek() : null; } void skip(ulong count)