[Boost] Detailed description of the boost library ASIO 6 -- boost: ASIO: Error Usage Analysis

Source: Internet
Author: User
1. Overview

Generally, most of the types we create for receiving errors are declared as follows:

boost::system::error_code error

We use this type to accept Errors generated in functions, such:

socket.connect(endpoint, error);

If the connection fails, the error type will be saved to error. For example, if the connection to the host fails, this error may be returned.

boost::asio::error::host_not_found;

If (error) throws an exception after detecting the error

throw boost::system::system_error(error);

Note that our error is converted to system_error.
An error is displayed. STD: cout <E. What ()
2. asynchronous call

The passing of the Exception error is a problem, because asynchronous will return immediately and local variables will be destroyed. However, boost: ASIO: placeholders: error will save the abnormal state, so that we can use the asynchronous call as shown in Figure
When using Socket: async_write_some, you do not need to create boost: System: error_code error. You can directly use boost: ASIO: placeholders: error as the parameter,
Similarly, sync_write_some needs to return the size of read/write data. The good news is that boost: ASIO: placeholders: bytes_transferred can be directly used as a parameter to save the data size.
Example:

boost::asio::async_write(socket_,                         boost::asio::buffer(message_),                         boost::bind(&tcp_connection::handle_write, shared_from_this(),                         boost::asio::placeholders::error,                         boost::asio::placeholders::bytes_transferred));

The reference manual clearly states that the following two classes are designed for asynchronous calls using bind.

boost::asio::placeholders::errorboost::asio::placeholders::bytes_transferred
3. synchronous call

Of course, boost: System: error_code error is also useful. We use it as a parameter during synchronous calls. For example:

boost::system::error_code error;size_t len = socket.read_some(boost::asio::buffer(buf), error);

It is also used as a parameter in the callback handle of asynchronous calls, as shown in figure

void handle_write(const boost::system::error_code& /*error*/,                  size_t /*bytes_transferred*/){}
4. Summary

Asynchronous mode uses boost: ASIO: placeholders: error, boost: ASIO: placeholders: bytes_transferred.
Boost: System: error_code is used for synchronization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.