3

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 to ioc when making requests? What about the ioc.run()? - I need it to run constantly, should I open a dedicated thread for running ioc to avoid blocking?
  • How to handle session errors? I would like to use std::promise interface and put there an exception in case something goes wrong - basically replace all calls to fail() in the example with appropriate exceptions. But I can't just put an exception object into the promise, it expects std::exception_ptr - should I just throw, instantly catch it and put std::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)

Xeverous
  • 973
  • 1
  • 12
  • 25
  • "But I can't just put an exception object into the promise, it expects std::exception_ptr" Have you tried `std::make_exception_ptr`? Otherwise, everything you mentioned seems fine – KABoissonneault Mar 04 '19 at 13:51
  • @KABoissonneault ahh, so there is a standard library wrapper for it. Good. – Xeverous Mar 04 '19 at 14:14

0 Answers0