-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TCPConnection.waitForDataAsync #62
Changes from 2 commits
0d6ba62
20e32cf
99e2873
e33cf56
57d516a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,6 +586,41 @@ mixin(tracer); | |
return m_context.readBuffer.length > 0; | ||
} | ||
|
||
enum WaitForDataAsyncStatus { | ||
noMoreData, | ||
dataAvailable, | ||
waiting | ||
} | ||
|
||
WaitForDataAsyncStatus waitForDataAsync(void delegate(bool) @safe read_callback, Duration timeout = Duration.max) | ||
{ | ||
mixin(tracer); | ||
import vibe.core.core : runTask, setTimer, createTimer; | ||
|
||
if (!m_context) { | ||
runTask(read_callback, false); | ||
return WaitForDataAsyncStatus.noMoreData; | ||
} | ||
if (m_context.readBuffer.length > 0) { | ||
runTask(read_callback, true); | ||
return WaitForDataAsyncStatus.dataAvailable; | ||
} | ||
|
||
if (timeout <= 0.seconds) { | ||
auto rs = waitForData(0.seconds); | ||
auto mode = rs ? WaitForDataAsyncStatus.dataAvailable : WaitForDataAsyncStatus.noMoreData; | ||
return mode; | ||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two lines from above
should be executed right after the |
||
else runTask(read_callback, true); | ||
}); | ||
return WaitForDataAsyncStatus.waiting; | ||
} | ||
|
||
const(ubyte)[] peek() { return m_context ? m_context.readBuffer.peek() : null; } | ||
|
||
void skip(ulong count) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce the amount of text to type when using this from the outside, I'd move this outside below the
TCPConnection
class.