📜 ⬆️ ⬇️

"Boost.Asio C ++ Network Programming". Chapter 7: Boost.Asio - additional topics

Hello!
I continue to translate the book of John Torjo "Boost.Asio C ++ Network Programming".

Content:


This chapter covers some additional Boost.Asio topics. It is unlikely that you will use it every day, but it will certainly not be out of place to know:

')


Asio vs Boost.Asio


Boost.Asio also provided support for Asio. You can think of it as Asio, since this library comes in two versions, Asio (not Boost) and Boost.Asio. The authors claim that updates will first appear in Asio, and will be added periodically to the Boost distribution.
In a nutshell, the differences are as follows:

You can get more information on Asio here .
You must decide for yourself which option you prefer; I personally prefer Boost.Asio. Here are a few things you should pay attention to when making your choice:

You can use Asio and Boost.Asio in the same application, although I do not recommend you do this. This may not work out intentionally, in this case everything will be fine, for example, if you use Asio, and some third-party libraries use Boost.Asio and vice versa.

Debugging


Debugging a synchronous application is usually simpler than an asynchronous application. For a synchronous application, if it is blocked, you will simply enter debugging and get a picture of where it happened (synchronous means consecutive). However, when you program asynchronously, events do not occur sequentially, so if an error occurs, it will be really difficult to catch it.
To avoid this, first, you must understand coroutines very well. If the program is implemented correctly, then you practically will have no problems at all.
Only when it comes to asynchronous programming, Boost.Asio will give you a helping hand; Boost.Asio allows you to track handlers if the BOOST_ASIO_ENABLE_HANDLER_TRACKING macro is BOOST_ASIO_ENABLE_HANDLER_TRACKING . If this is the case, then Boost.Asio contributes to the output of information to the standard error output stream, recording the time, asynchronous operation and, related to it, the final handler.

Handler tracking information


Information is not so easy to understand, but nevertheless it is very useful. The output of Boost.Asio gives the following:

 @asio|<timestamp>|<action>|<description>. 

The first tag is always @asio , you can use it to easily filter messages coming from Boost.Asio in case other sources write to the standard error stream (equivalent to std::cerr ). The timestamp instance is counted in seconds and microseconds, starting January 1, 1970 UTC. An action instance can be one of the following:

Whenever n = 0, then all the handlers are executed from the outside (asynchronously), usually you see when the first operation (operations) is performed or if you work with signals and the signal is triggered.
You should pay attention to the messages like !n and ~n , which occur when there are errors in the code. In the first case, the asynchronous function did not throw an exception, so the exception must be generated by you; you should not allow exceptions when exiting your final handler. In the latter case, you probably destroyed the io_service instance too early, before all the called handlers were completed.

Example


In order to show you an example of supporting information, let me change the example from Chapter 6. All you need to do is add an additional #define before enabling boost/asio.hpp :

 #define BOOST_ASIO_ENABLE_HANDLER_TRACKING #include <boost/asio.hpp> ... 

We will also dump the console to the console when the user logs in and receives the first client list. The output will be as follows:

 @asio|1355603116.602867|0*1|socket@008D4EF8.async_connect @asio|1355603116.604867|>1|ec=system:0 @asio|1355603116.604867|1*2|socket@008D4EF8.async_send @asio|1355603116.604867|<1| @asio|1355603116.604867|>2|ec=system:0,bytes_transferred=11 @asio|1355603116.604867|2*3|socket@008D4EF8.async_receive @asio|1355603116.604867|<2| @asio|1355603116.605867|>3|ec=system:0,bytes_transferred=9 @asio|1355603116.605867|3*4|io_service@008D4BC8.post @asio|1355603116.605867|<3| @asio|1355603116.605867|>4| John logged in @asio|1355603116.606867|4*5|io_service@008D4BC8.post @asio|1355603116.606867|<4| @asio|1355603116.606867|>5| @asio|1355603116.606867|5*6|socket@008D4EF8.async_send @asio|1355603116.606867|<5| @asio|1355603116.606867|>6|ec=system:0,bytes_transferred=12 @asio|1355603116.606867|6*7|socket@008D4EF8.async_receive @asio|1355603116.606867|<6| @asio|1355603116.606867|>7|ec=system:0,bytes_transferred=14 @asio|1355603116.606867|7*8|io_service@008D4BC8.post @asio|1355603116.607867|<7| @asio|1355603116.607867|>8| John, new client list: John 

Let me analyze each line:

It will take some time to get used to, but once you understand this, you can isolate the output that contains the problem and find the actual part of the code that needs to be fixed.

Writing handler tracking information to a file


By default, the tracking information for handlers is output to the standard error stream (equivalent to std::cerr ). It is very likely that you will want to redirect this output to another location. On the one hand, by default, for console applications, the output and reset of errors occurs in one place, that is, in the console. But for Windows (non-console) applications, the default error flow is empty.
You can redirect error output using the command line, for example:

 some_application 2>err.txt 

If you are not lazy, you can do it programmatically, as shown in the following code snippet:

 // for Windows HANDLE h = CreateFile("err.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL , 0); SetStdHandle(STD_ERROR_HANDLE, h); // for Unix int err_file = open("err.txt", O_WRONLY); dup2(err_file, STDERR_FILENO); 


SSL


Boost.Asio provides classes for supporting some basic SSL features. Inside she uses OpenSSL . So if you want to use SSL, first download and build OpenSSL . It should be noted that, as a rule, building OpenSSL is no easy task, especially if you do not have popular compilers, such as Visual Studio.
If you have successfully compiled OpenSSL , then Boost.Asio has some add-on classes over it:

Source: https://habr.com/ru/post/197392/


All Articles