Skip to content

Instantly share code, notes, and snippets.

@GorNishanov
Created May 28, 2017 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GorNishanov/65195f6e5620f70721597caf920d4dcc to your computer and use it in GitHub Desktop.
Save GorNishanov/65195f6e5620f70721597caf920d4dcc to your computer and use it in GitHub Desktop.
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
Copy link

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