Home > OS >  How to check return type of boost asio async_read_some, async_write_some
How to check return type of boost asio async_read_some, async_write_some

Time:10-21

I want to check if async_read_some, async_write_some or any other async function is a success. I am aware of the boost::asio::placeholders::error param available in the handler. But does the async call, as such, not have a return type? I read about completion_condition, but am not aware of how to use it. Any simple code sample would be helpful.

                mSock.async_read_some(
                        boost::asio::buffer(mI8Data, MAX_BUFFER_LENGTH),
                        boost::bind(&ConnectionHandler::HandleRead,
                            shared_from_this(), 
                            boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));

I basically want something that tells me if the async_read_some call is facing any issue.

CodePudding user response:

Async operations typically do not fail: they're initiation functions. There is no result code to check.

However, all of the Asio IO objects and free initiation functions have the generalized completion-token interface. The return type actually depends on that token: you will see that the return type is some "complicated" calculation based on async_result<>.

This means that you can use other means of asynchrony and the result type can change. E.g:

size_t s.async_read_some(buf, boost::asio::use_future).get();

Will throw the system_error for the error_code if applicable.

  • Related