Boost :: asio socket - how to make read_some throw in timeout?

So we are doing something like this socket.read_some(boost::asio::buffer(buffer, buffer_size)); but how to make it throw an exeption in case the reading does not start for some time longer than 333 seconds?

+4
source share
1 answer

You should use async_read_some instead of read_some , as it allows you to start a new background timer while reading. Then, to create a new timer for each new socket:

 boost::asio::io_service io_service; time_t_timer timer(io_service); timer.expires_from_now(333); std::cout << "Starting asynchronous wait\n"; timer.async_wait(&handle_timeout); io_service.run(); 

You will have two asynchronous calls waiting on the background.

Whenever you get some timer data, you can reset the countdown with cancel and expires_from_now , and when the timer expires, you can close the socket or perform another action.

+3
source

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


All Articles