I have a project that will do some networking and would like to wrap underlying Boost Beast HTTP(S) requests implementation inside futures.
I copied the session
class from https://www.boost.org/doc/libs/1_69_0/libs/beast/example/http/client/async-ssl/http_client_async_ssl.cpp and want to change session::run()
return type to std::future<http::response<http::string_body>>
. Also, change the place // Write the message to standard out
to setting up the result in the promise instead of printing.
The questions are:
- where to put the promise that will be used to generate the future? Is the class member a right choice?
- where to put
boost::asio::io_context ioc
? Should it be in the core loop of my own application (assuming the application will fire multiple requests during it's lifetime) and then pass a reference toioc
when making requests? What about theioc.run()
? - I need it to run constantly, should I open a dedicated thread for runningioc
to avoid blocking? - How to handle
session
errors? I would like to usestd::promise
interface and put there an exception in case something goes wrong - basically replace all calls tofail()
in the example with appropriate exceptions. But I can't just put an exception object into the promise, it expectsstd::exception_ptr
- should I just throw, instantly catch it and putstd::current_exception()
? This seems like a bad hack to me. - Will this work if I don't explicitly create any threads in the program? If no, what would be a good way to query the
session
object that all tasks are complete?
Edit: added further question about ioc
(in bold)