Related to the C ++ Boost Asio

Boost Asio related

 

Boost C ++ is a portable library, is to expand the standard library reserve, it is one of the engines developed C ++ standardization process.

 

Boost Boost asio 'is part of a library, Asio full name abbreviations Asynchronous input and output (input Asynchronous output). Combined with the characteristics of Boost, Asio provides asynchronous data processing capability and a set of platform-independent, of course, it also supports synchronous data processing.

 

Use Asio only need to introduce a header file.

#include<boost/asio.hpp>

 

For all programs using Asio, must contain at least contain a io_service object. Boost Asio For this library, which abstract concepts such as a network, serial communication and the like, and unified regulation of IO operation, so io_service This class provides access to the I / O functions, and therefore when using Asio, you must be defined:

boost :: asio :: io_service I;

 

HTTP connections

 

Since network-related concept has been abstracted into IO, we only need connections to get the message from IO stream, so we essentially get the message or IO stream is performed, so we essentially still performing IO operations, but these operations need to have some basic networking concepts.

 

HTTP and HTTPS bottom is actually used by TCP reliable connection, through the Socket communication technology, a Socket constituted by the IP address and port. Without exception, Asio also need to create a socket and related objects, that is, boost :: asio :: ip :: tcp :: socket. One can imagine, Socket since it is the basis for network communication, so our natural IO operations to be carried out will be done here, therefore, we define the boost :: asio :: ip :: tcp :: socket object, you must io_service performed by a structure in which:

 

boost::asio::ip::tcp::socket socket(io)

 

With socket object is not enough. In network communications, network IO to the inlet port, as is done in streaming mode. So this socket object can only be used to make a necessary attributes of our future conduct IO operations.

 

Not difficult to see, a normal boost :: asio :: ip :: tcp :: socket object is actually a HTTP Socket connection, so we write code at a later date, you can even use this type typedef directly defined as HTTP:

 

typedef boost::asio::ip::tcp::socket HTTP;

 

However, as a server, we could build a lot of concurrent connections in response, so when we need to establish a connection when you need to use an object called the acceptor.

 

The boost :: asio :: ip :: tcp :: acceptor can be seen from the name, this object should be used to establish a connection. In Boost, we need to initialize a acceptor object, you must provide a io_service object and an endpoint object.

 

So what is the endpoint? In fact, a socket is connected end to end, so-called endpoint is an endpoint server socket is located, we know, is the socket by an IP address and port number, and then when we need for the establishment of an IPv4 network, you can establish first a boost :: asio :: ip :: tcp :: endpoint objects:

 

unsigned short port = 8080;

boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port);

 

Wherein the boost :: asio :: ip :: tcp :: v4 () is used to initialize an IPv4 network. Finally, using the endpoint object to initialize acceptor:

 

boost::asio::ip::tcp::acceptor acceptor(io, endpoint);

 

So far, we have discussed how to use Asio establish a common network operating target acceptor, and the need for network operation during normal HTTP socket object.

 

HTTPS connection

 

Asio in discussions over an HTTP connection, we look at how Asio the HTTPS connection is established. Asio is an open source library, it is also inevitable in the process need to add a dependency on another logical framework is not good at. Asio operation of SSL HTTPS-related, it is dependent on the OpenSSL library.

 

To use SSL-related operations, but also additional introduction of a header file:

#include <boost/asio/ssl.hpp>

 

We discussed boost :: Socket objects asio :: ip :: tcp :: socket is actually produced by an ordinary HTTP object in the previous section. For HTTPS, the socket is actually on the channel generated by a layer of encapsulation and encryption. In Boost Asio, the encrypted socket way is to use boost :: asio :: ssl :: stream, and boost :: asio :: ip :: tcp :: socket as a template parameter passed to this object, namely:

typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;

 

And when we want to construct a socket object for HTTPS, Boost Asio requires the cultivation of a boost :: asio :: ssl :: context object to this socket. While a context can have many different types, the most common is to boost :: asio :: ssl :: context :: sslv23. Well context objects constructed after that was not enough, because of the need to provide a https server certificate file and secret key file, so it requires the use of use_certificate_chain_file () and use_private_key_file () These two methods for further configuration:

 

context.use_certificate_chain_file(cert_file);

context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);

 

The boost :: asio :: ssl :: context :: pem is specified certificate type. Therefore, compared to the terms of HTTP, HTTPS establishment is actually increasing the configuration of the certificate, and socket encrypted link, the following comparison:

// http

boost::asio::ip::tcp::socket http_socket(io);

// https

boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);

context.use_certificate_chain_file(cert_file);

context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);

boost::asio::ssl::stream<boost::asio::ip::tcp::socket> https_socket(io, context);

 

 

I / O operation

Above we discussed how to establish a connection, and now we look at how the IO operation.

 

When we have a socket object, you can read the network flow data from the inside. When reading data, we need to define a stream buffer boost :: asio :: streambuf objects, data read line by line for the socket:

 

boost::asio::streambuf read_buffer;

 

Further, many network protocols are actually implemented on the line, that is, they are confined by a protocol element \ r \ n symbols, HTTP is no exception, so Boost Asio, the read delimiters used protocol, may be used async_read_untile () method:

 

boost::asio::async_read_until(socket, readbuffer, "\r\n\r\n", read_handler);

 

Wherein the socket is our socket connection, while readbuffer delimiter is read in accordance with the data line, "\ r \ n \ r \ n" is a delimiter, and for read_handler we still need further discussion.

 

read_handler is a non-return type of the function object that accepts two parameters, a boost :: system :: error_code, and the other is size_t (bytes_transferred):

 
void read_handler(
    const boost::system::error_code& ec,
    std::size_t bytes_transferred)

{
  ...
}

 

 

boost :: system :: error_code used to describe the operation was successful, and size_t bytes_transferred is received to determine the number of bytes, usually, we can bind with a parameter to our std :: bind incoming function, but in fact we have a better way, and that is the lambda expression, because Lambda expressions also has another function, that is, a value capture, for this, we realize the framework of time and then later Discussed in detail.

 

In this read_handler, we actually continue to read the contents inside the socket, so we also need to use boost :: asio :: async_read on the back of the contents for further reading, and its usage and boost :: asio :: async_read_until almost the same, the only difference is that prior to this parameter read_handler, specify read length, typically we can use boost :: asio :: transfer_exactly specified, it will not be repeated here in detail, later in our implementation framework when discussed in detail again.

 

Finally, we have completed the read operation, it is only the last step, that is, the server responds to the request, the write request back to the resource supply the client, this time we need to use another method: boost :: asio :: async_write . As can be seen from the name, this method and boost :: asio :: async_read belong to the same family of methods, one can imagine the usage is totally similar, we leave behind the actual code further discussion.

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/12242539.html