Created
May 28, 2017 20:21
-
-
Save GorNishanov/65195f6e5620f70721597caf920d4dcc to your computer and use it in GitHub Desktop.
An example of an await adapter for async_read_until
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename SyncReadStream, typename DynamicBuffer> | |
auto async_read_until(SyncReadStream &s, DynamicBuffer &&buffers, | |
string delim) { | |
struct Awaiter { | |
SyncReadStream &s; | |
DynamicBuffer &&buffers; | |
string delim; | |
std::error_code ec; | |
size_t sz; | |
bool await_ready() { return false; } | |
auto await_resume() { | |
if (ec) | |
throw std::system_error(ec); | |
return sz; | |
} | |
void await_suspend(std::experimental::coroutine_handle<> coro) { | |
net::async_read_until(s, std::move(buffers), delim, | |
[this, coro](auto ec, auto sz) { | |
this->ec = ec; | |
this->sz = sz; | |
coro.resume(); | |
}); | |
} | |
}; | |
return Awaiter{ s, std::forward<DynamicBuffer>(buffers), delim }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using this snippet I get an error converting
boost::system::error_code
tostd:error_code
at line 21. My variant of the adapter was using boost'serror_code
so I never paid much attention, but I was wondering whether you had a solution to do the conversion. There is a lengthy article about unifying the two here that is relatively recent (June 2016), but before I go down that path I wanted to check whether it was just a type-o or whether you are using some simpler approach.