switch to asyncAwait

This commit is contained in:
Boris-Barboris 2017-11-11 15:45:44 +00:00 committed by Sönke Ludwig
parent 55e7866497
commit 6634cbc645
2 changed files with 23 additions and 13 deletions

View file

@ -195,18 +195,18 @@ TCPConnection connectTCP(NetworkAddress addr, NetworkAddress bind_address = anyA
scope uaddr = new RefAddress(addr.sockAddr, addr.sockAddrLen);
scope baddr = new RefAddress(bind_address.sockAddr, bind_address.sockAddrLen);
auto result = asyncAwaitUninterruptible!(ConnectCallback,
auto result = asyncAwait!(ConnectCallback,
cb => eventDriver.sockets.connectStream(uaddr, baddr, cb),
(ConnectCallback cb, StreamSocketFD sock_fd) {
eventDriver.sockets.cancelConnectStream(sock_fd);
}
)(timeout);
if (result[1] == ConnectStatus.cancelled)
result[1] = ConnectStatus.timeout;
enforce(result[1] == ConnectStatus.connected, "Failed to connect to "~addr.toString()~": "~result[1].to!string);
enforce(result.completed, "Failed to connect to " ~ addr.toString() ~
": timeout");
enforce(result.results[1] == ConnectStatus.connected, "Failed to connect to "~addr.toString()~": "~result.results[1].to!string);
return TCPConnection(result[0], uaddr);
return TCPConnection(result.results[0], uaddr);
} ();
}

View file

@ -1,6 +1,6 @@
module vibe.internal.async;
import std.traits : ParameterTypeTuple;
import std.traits : ParameterTypeTuple, ReturnType;
import std.typecons : tuple;
import vibe.core.core : hibernate, switchToTask;
import vibe.core.task : InterruptException, Task;
@ -20,14 +20,23 @@ auto asyncAwait(Callback, alias action, alias cancel)(Duration timeout, string f
{
static struct R {
bool completed = true;
typeof(waitable.results) results;
ParameterTypeTuple!Callback results;
}
R ret;
static if (is(ReturnType!action == void)) {
alias waitable = Waitable!(Callback,
action,
(cb) { ret.completed = false; cancel(cb); },
(ParameterTypeTuple!Callback r) { ret.results = r; }
);
}
else {
alias waitable = Waitable!(Callback,
action,
(cb, waitres) { ret.completed = false; cancel(cb, waitres); },
(ParameterTypeTuple!Callback r) { ret.results = r; }
);
}
asyncAwaitAny!(true, waitable)(timeout, func);
return ret;
}
@ -60,7 +69,8 @@ template Waitable(CB, alias WAIT, alias CANCEL, alias DONE)
"CANCEL must be callable with a parameter of type "~CB.stringof);
else
static assert(is(typeof(CANCEL(CB.init, typeof(WAIT(CB.init)).init))),
"CANCEL must be callable with parameters ("~CB.stringof~", "~typeof(WAIT(CB.init)).stringof~")");
"CANCEL must be callable with parameters "~CB.stringof~
" and "~typeof(WAIT(CB.init)).stringof);
static assert(is(typeof(DONE(ParameterTypeTuple!CB.init))),
"DONE must be callable with types "~ParameterTypeTuple!CB.stringof);