diff options
Diffstat (limited to 'doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp')
-rw-r--r-- | doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp b/doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp index 2134e6395d..f295ed5fb2 100644 --- a/doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp +++ b/doc/html/boost_asio/example/cpp03/timeouts/async_tcp_client.cpp @@ -2,22 +2,23 @@ // async_tcp_client.cpp // ~~~~~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include <boost/asio/deadline_timer.hpp> +#include <boost/asio/buffer.hpp> #include <boost/asio/io_context.hpp> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/read_until.hpp> -#include <boost/asio/streambuf.hpp> +#include <boost/asio/steady_timer.hpp> #include <boost/asio/write.hpp> #include <boost/bind.hpp> #include <iostream> +#include <string> -using boost::asio::deadline_timer; +using boost::asio::steady_timer; using boost::asio::ip::tcp; // @@ -78,7 +79,7 @@ using boost::asio::ip::tcp; // // The heartbeat actor sends a heartbeat (a message that consists of a single // newline character) every 10 seconds. In this example, no deadline is applied -// message sending. +// to message sending. // class client { @@ -125,7 +126,7 @@ private: std::cout << "Trying " << endpoint_iter->endpoint() << "...\n"; // Set a deadline for the connect operation. - deadline_.expires_from_now(boost::posix_time::seconds(60)); + deadline_.expires_after(boost::asio::chrono::seconds(60)); // Start the asynchronous connect operation. socket_.async_connect(endpoint_iter->endpoint(), @@ -185,14 +186,15 @@ private: void start_read() { // Set a deadline for the read operation. - deadline_.expires_from_now(boost::posix_time::seconds(30)); + deadline_.expires_after(boost::asio::chrono::seconds(30)); // Start an asynchronous operation to read a newline-delimited message. - boost::asio::async_read_until(socket_, input_buffer_, '\n', - boost::bind(&client::handle_read, this, _1)); + boost::asio::async_read_until(socket_, + boost::asio::dynamic_buffer(input_buffer_), '\n', + boost::bind(&client::handle_read, this, _1, _2)); } - void handle_read(const boost::system::error_code& ec) + void handle_read(const boost::system::error_code& ec, std::size_t n) { if (stopped_) return; @@ -200,9 +202,8 @@ private: if (!ec) { // Extract the newline-delimited message from the buffer. - std::string line; - std::istream is(&input_buffer_); - std::getline(is, line); + std::string line(input_buffer_.substr(0, n - 1)); + input_buffer_.erase(0, n); // Empty messages are heartbeats and so ignored. if (!line.empty()) @@ -238,7 +239,7 @@ private: if (!ec) { // Wait 10 seconds before sending the next heartbeat. - heartbeat_timer_.expires_from_now(boost::posix_time::seconds(10)); + heartbeat_timer_.expires_after(boost::asio::chrono::seconds(10)); heartbeat_timer_.async_wait(boost::bind(&client::start_write, this)); } else @@ -257,15 +258,16 @@ private: // Check whether the deadline has passed. We compare the deadline against // the current time since a new asynchronous operation may have moved the // deadline before this actor had a chance to run. - if (deadline_.expires_at() <= deadline_timer::traits_type::now()) + if (deadline_.expiry() <= steady_timer::clock_type::now()) { // The deadline has passed. The socket is closed so that any outstanding // asynchronous operations are cancelled. socket_.close(); - // There is no longer an active deadline. The expiry is set to positive - // infinity so that the actor takes no action until a new deadline is set. - deadline_.expires_at(boost::posix_time::pos_infin); + // There is no longer an active deadline. The expiry is set to the + // maximum time point so that the actor takes no action until a new + // deadline is set. + deadline_.expires_at(steady_timer::time_point::max()); } // Put the actor back to sleep. @@ -276,9 +278,9 @@ private: bool stopped_; tcp::resolver::results_type endpoints_; tcp::socket socket_; - boost::asio::streambuf input_buffer_; - deadline_timer deadline_; - deadline_timer heartbeat_timer_; + std::string input_buffer_; + steady_timer deadline_; + steady_timer heartbeat_timer_; }; int main(int argc, char* argv[]) |