Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of an await adapter for async_read_until
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 };
}
@therealkenc

This comment has been minimized.

Show comment
Hide comment
@therealkenc

therealkenc Jun 11, 2017

Using this snippet I get an error converting boost::system::error_code to std:error_code at line 21. My variant of the adapter was using boost's error_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.

therealkenc commented Jun 11, 2017

Using this snippet I get an error converting boost::system::error_code to std:error_code at line 21. My variant of the adapter was using boost's error_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment