From 698824e811b4d845941eafef94dcfb1f0922f100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 30 May 2017 11:02:22 +0200 Subject: [PATCH] Partially implement FileDescriptorStream. Currently only works for waiting on read-readiness (eventcore is not exposing an analog function for write-readiness). --- source/vibe/core/core.d | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/source/vibe/core/core.d b/source/vibe/core/core.d index d43f6e3..a8d7d62 100644 --- a/source/vibe/core/core.d +++ b/source/vibe/core/core.d @@ -940,13 +940,31 @@ struct FileDescriptorEvent { any = read|write /// Match any kind of event } - @safe nothrow: + private { + StreamSocketFD m_socket; + Trigger m_trigger; + } + + @safe: private this(int fd, Trigger event_mask) - { - assert(false); + nothrow { + m_socket = eventDriver.sockets.adoptStream(fd); } + this(this) + nothrow { + if (m_socket != StreamSocketFD.invalid) + eventDriver.sockets.addRef(m_socket); + } + + ~this() + nothrow { + if (m_socket != StreamSocketFD.invalid) + eventDriver.sockets.releaseRef(m_socket); + } + + /** Waits for the selected event to occur. Params: @@ -964,7 +982,18 @@ struct FileDescriptorEvent { /// ditto bool wait(Duration timeout, Trigger which = Trigger.any) { - assert(false); + if ((which & m_trigger) == Trigger.none) return true; + + assert((which & m_trigger) == Trigger.read, "Waiting for write event not yet supported."); + + Waitable!(IOCallback, + cb => eventDriver.sockets.waitForData(m_socket, cb), + (cb) { assert(false, "timeout not supported."); } + ) readwaiter; + + asyncAwaitAny!true(timeout, readwaiter); + + return true; } }