From 20e32cf32722f100fbdb280aefd68c86fd97a685 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Fri, 23 Feb 2018 00:27:04 +0000 Subject: [PATCH] added Timer and enum return to TCPConnection.waitForDataAsync --- source/vibe/core/net.d | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index fdca9d9..f29d4b8 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -586,29 +586,39 @@ mixin(tracer); return m_context.readBuffer.length > 0; } - void waitForDataAsync(void delegate(bool) @safe read_callback, Duration timeout = Duration.max) + enum WaitForDataAsyncStatus { + noMoreData, + dataAvailable, + waiting + } + + WaitForDataAsyncStatus waitForDataAsync(void delegate(bool) @safe read_callback, Duration timeout = Duration.max) { mixin(tracer); - import vibe.core.core : runTask; + import vibe.core.core : runTask, setTimer, createTimer; if (!m_context) { runTask(read_callback, false); - return; + return WaitForDataAsyncStatus.noMoreData; } if (m_context.readBuffer.length > 0) { runTask(read_callback, true); - return; + return WaitForDataAsyncStatus.dataAvailable; } + if (timeout <= 0.seconds) { + auto rs = waitForData(0.seconds); + auto mode = rs ? WaitForDataAsyncStatus.dataAvailable : WaitForDataAsyncStatus.noMoreData; + return mode; + } - 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); + auto tm = setTimer(timeout, { eventDriver.sockets.cancelRead(m_socket); + runTask(read_callback, false); }); + eventDriver.sockets.read(m_socket, m_context.readBuffer.peekDst(), IOMode.once, + (sock, st, nb) { tm.stop(); if(st != IOStatus.ok) runTask(read_callback, false); else runTask(read_callback, true); }); + return WaitForDataAsyncStatus.waiting; } const(ubyte)[] peek() { return m_context ? m_context.readBuffer.peek() : null; }