Update timer-related members on socket iostream classes.
diff --git a/asio/include/asio/basic_socket_iostream.hpp b/asio/include/asio/basic_socket_iostream.hpp index f7fce67..8ca6b77 100644 --- a/asio/include/asio/basic_socket_iostream.hpp +++ b/asio/include/asio/basic_socket_iostream.hpp
@@ -125,14 +125,24 @@ typedef typename Protocol::endpoint endpoint_type; #if defined(GENERATING_DOCUMENTATION) - /// The time type. + /// (Deprecated: Use time_point.) The time type. typedef typename TimeTraits::time_type time_type; - /// The duration type. + /// The time type. + typedef typename TimeTraits::time_point time_point; + + /// (Deprecated: Use duration.) The duration type. typedef typename TimeTraits::duration_type duration_type; + + /// The duration type. + typedef typename TimeTraits::duration duration; #else +# if !defined(ASIO_NO_DEPRECATED) typedef typename traits_helper::time_type time_type; typedef typename traits_helper::duration_type duration_type; +# endif // !defined(ASIO_NO_DEPRECATED) + typedef typename traits_helper::time_type time_point; + typedef typename traits_helper::duration_type duration; #endif /// Construct a basic_socket_iostream without establishing a connection. @@ -225,13 +235,25 @@ return rdbuf()->puberror(); } +#if !defined(ASIO_NO_DEPRECATED) + /// (Deprecated: Use expiry().) Get the stream's expiry time as an absolute + /// time. + /** + * @return An absolute time value representing the stream's expiry time. + */ + time_point expires_at() const + { + return rdbuf()->expires_at(); + } +#endif // !defined(ASIO_NO_DEPRECATED) + /// Get the stream's expiry time as an absolute time. /** * @return An absolute time value representing the stream's expiry time. */ - time_type expires_at() const + time_point expiry() const { - return rdbuf()->expires_at(); + return rdbuf()->expiry(); } /// Set the stream's expiry time as an absolute time. @@ -243,20 +265,11 @@ * * @param expiry_time The expiry time to be used for the stream. */ - void expires_at(const time_type& expiry_time) + void expires_at(const time_point& expiry_time) { rdbuf()->expires_at(expiry_time); } - /// Get the timer's expiry time relative to now. - /** - * @return A relative time value representing the stream's expiry time. - */ - duration_type expires_from_now() const - { - return rdbuf()->expires_from_now(); - } - /// Set the stream's expiry time relative to now. /** * This function sets the expiry time associated with the stream. Stream @@ -266,10 +279,36 @@ * * @param expiry_time The expiry time to be used for the timer. */ - void expires_from_now(const duration_type& expiry_time) + void expires_after(const duration& expiry_time) + { + rdbuf()->expires_after(expiry_time); + } + +#if !defined(ASIO_NO_DEPRECATED) + /// (Deprecated: Use expiry().) Get the stream's expiry time relative to now. + /** + * @return A relative time value representing the stream's expiry time. + */ + duration expires_from_now() const + { + return rdbuf()->expires_from_now(); + } + + /// (Deprecated: Use expires_after().) Set the stream's expiry time relative + /// to now. + /** + * This function sets the expiry time associated with the stream. Stream + * operations performed after this time (where the operations cannot be + * completed using the internal buffers) will fail with the error + * asio::error::operation_aborted. + * + * @param expiry_time The expiry time to be used for the timer. + */ + void expires_from_now(const duration& expiry_time) { rdbuf()->expires_from_now(expiry_time); } +#endif // !defined(ASIO_NO_DEPRECATED) }; } // namespace asio
diff --git a/asio/include/asio/basic_socket_streambuf.hpp b/asio/include/asio/basic_socket_streambuf.hpp index 2dbe033..3d6690e 100644 --- a/asio/include/asio/basic_socket_streambuf.hpp +++ b/asio/include/asio/basic_socket_streambuf.hpp
@@ -118,14 +118,24 @@ typedef typename Protocol::endpoint endpoint_type; #if defined(GENERATING_DOCUMENTATION) - /// The time type. + /// (Deprecated: Use time_point.) The time type. typedef typename TimeTraits::time_type time_type; - /// The duration type. + /// The time type. + typedef typename TimeTraits::time_point time_point; + + /// (Deprecated: Use duration.) The duration type. typedef typename TimeTraits::duration_type duration_type; + + /// The duration type. + typedef typename TimeTraits::duration duration; #else +# if !defined(ASIO_NO_DEPRECATED) typedef typename traits_helper::time_type time_type; typedef typename traits_helper::duration_type duration_type; +# endif // !defined(ASIO_NO_DEPRECATED) + typedef typename traits_helper::time_type time_point; + typedef typename traits_helper::duration_type duration; #endif /// Construct a basic_socket_streambuf without establishing a connection. @@ -236,16 +246,35 @@ return error(); } +#if !defined(ASIO_NO_DEPRECATED) + /// (Deprecated: Use expiry().) Get the stream buffer's expiry time as an + /// absolute time. + /** + * @return An absolute time value representing the stream buffer's expiry + * time. + */ + time_point expires_at() const + { + return timer_service_ + ? timer_service_->expires_at(timer_implementation_) + : time_point(); + } +#endif // !defined(ASIO_NO_DEPRECATED) + /// Get the stream buffer's expiry time as an absolute time. /** * @return An absolute time value representing the stream buffer's expiry * time. */ - time_type expires_at() const + time_point expiry() const { return timer_service_ +#if defined(ASIO_HAS_BOOST_DATE_TIME) ? timer_service_->expires_at(timer_implementation_) - : time_type(); +#else // defined(ASIO_HAS_BOOST_DATE_TIME) + ? timer_service_->expiry(timer_implementation_) +#endif // defined(ASIO_HAS_BOOST_DATE_TIME) + : time_point(); } /// Set the stream buffer's expiry time as an absolute time. @@ -257,7 +286,7 @@ * * @param expiry_time The expiry time to be used for the stream. */ - void expires_at(const time_type& expiry_time) + void expires_at(const time_point& expiry_time) { construct_timer(); @@ -268,15 +297,6 @@ start_timer(); } - /// Get the stream buffer's expiry time relative to now. - /** - * @return A relative time value representing the stream buffer's expiry time. - */ - duration_type expires_from_now() const - { - return traits_helper::subtract(expires_at(), traits_helper::now()); - } - /// Set the stream buffer's expiry time relative to now. /** * This function sets the expiry time associated with the stream. Stream @@ -286,7 +306,7 @@ * * @param expiry_time The expiry time to be used for the timer. */ - void expires_from_now(const duration_type& expiry_time) + void expires_at(const duration& expiry_time) { construct_timer(); @@ -297,6 +317,63 @@ start_timer(); } + /// Set the stream buffer's expiry time relative to now. + /** + * This function sets the expiry time associated with the stream. Stream + * operations performed after this time (where the operations cannot be + * completed using the internal buffers) will fail with the error + * asio::error::operation_aborted. + * + * @param expiry_time The expiry time to be used for the timer. + */ + void expires_after(const duration& expiry_time) + { + construct_timer(); + + asio::error_code ec; +#if defined(ASIO_HAS_BOOST_DATE_TIME) + timer_service_->expires_from_now(timer_implementation_, expiry_time, ec); +#else // defined(ASIO_HAS_BOOST_DATE_TIME) + timer_service_->expires_after(timer_implementation_, expiry_time, ec); +#endif // defined(ASIO_HAS_BOOST_DATE_TIME) + asio::detail::throw_error(ec, "after"); + + start_timer(); + } + +#if !defined(ASIO_NO_DEPRECATED) + /// (Deprecated: Use expiry().) Get the stream buffer's expiry time relative + /// to now. + /** + * @return A relative time value representing the stream buffer's expiry time. + */ + duration expires_from_now() const + { + return traits_helper::subtract(expires_at(), traits_helper::now()); + } + + /// (Deprecated: Use expires_after().) Set the stream buffer's expiry time + /// relative to now. + /** + * This function sets the expiry time associated with the stream. Stream + * operations performed after this time (where the operations cannot be + * completed using the internal buffers) will fail with the error + * asio::error::operation_aborted. + * + * @param expiry_time The expiry time to be used for the timer. + */ + void expires_from_now(const duration& expiry_time) + { + construct_timer(); + + asio::error_code ec; + timer_service_->expires_from_now(timer_implementation_, expiry_time, ec); + asio::detail::throw_error(ec, "expires_from_now"); + + start_timer(); + } +#endif // !defined(ASIO_NO_DEPRECATED) + protected: int_type underflow() { @@ -498,9 +575,9 @@ void operator()(const asio::error_code&) { - time_type now = traits_helper::now(); + time_point now = traits_helper::now(); - time_type expiry_time = this_->timer_service_->expires_at( + time_point expiry_time = this_->timer_service_->expires_at( this_->timer_implementation_); if (traits_helper::less_than(now, expiry_time))
diff --git a/asio/include/asio/basic_waitable_timer.hpp b/asio/include/asio/basic_waitable_timer.hpp index 81819c2..0a9438b 100644 --- a/asio/include/asio/basic_waitable_timer.hpp +++ b/asio/include/asio/basic_waitable_timer.hpp
@@ -346,7 +346,7 @@ return this->get_service().cancel_one(this->get_implementation(), ec); } -#if defined(ASIO_NO_DEPRECATED) +#if !defined(ASIO_NO_DEPRECATED) /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute /// time. /**
diff --git a/asio/src/examples/cpp03/iostreams/http_client.cpp b/asio/src/examples/cpp03/iostreams/http_client.cpp index a7b6a55..404b354 100644 --- a/asio/src/examples/cpp03/iostreams/http_client.cpp +++ b/asio/src/examples/cpp03/iostreams/http_client.cpp
@@ -1,5 +1,5 @@ // -// sync_client.cpp +// http_client.cpp // ~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com) @@ -33,7 +33,7 @@ // The entire sequence of I/O operations must complete within 60 seconds. // If an expiry occurs, the socket is automatically closed and the stream // becomes bad. - s.expires_from_now(boost::posix_time::seconds(60)); + s.expires_after(boost::posix_time::seconds(60)); // Establish a connection to the server. s.connect(argv[1], "http");
diff --git a/asio/src/examples/cpp11/Makefile.am b/asio/src/examples/cpp11/Makefile.am index 8bfec1d..7e2c200 100644 --- a/asio/src/examples/cpp11/Makefile.am +++ b/asio/src/examples/cpp11/Makefile.am
@@ -27,7 +27,8 @@ executors/pipeline \ executors/priority_scheduler \ futures/daytime_client \ - http/server/http_server + http/server/http_server \ + iostreams/http_client if HAVE_BOOST_COROUTINE noinst_PROGRAMS += \ @@ -66,6 +67,7 @@ http/server/request_handler.cpp \ http/server/request_parser.cpp \ http/server/server.cpp +iostreams_http_client_SOURCES = iostreams/http_client.cpp if HAVE_BOOST_COROUTINE spawn_echo_server_SOURCES = spawn/echo_server.cpp
diff --git a/asio/src/examples/cpp11/iostreams/.gitignore b/asio/src/examples/cpp11/iostreams/.gitignore new file mode 100644 index 0000000..bc2b4f8 --- /dev/null +++ b/asio/src/examples/cpp11/iostreams/.gitignore
@@ -0,0 +1,11 @@ +.deps +.dirstamp +*.o +*.obj +*.exe +*_client +*_server +*.ilk +*.manifest +*.pdb +*.tds
diff --git a/asio/src/examples/cpp11/iostreams/http_client.cpp b/asio/src/examples/cpp11/iostreams/http_client.cpp new file mode 100644 index 0000000..66dd865 --- /dev/null +++ b/asio/src/examples/cpp11/iostreams/http_client.cpp
@@ -0,0 +1,91 @@ +// +// http_client.cpp +// ~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2014 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 <iostream> +#include <istream> +#include <ostream> +#include <string> +#include <asio/ip/tcp.hpp> + +using asio::ip::tcp; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cout << "Usage: http_client <server> <path>\n"; + std::cout << "Example:\n"; + std::cout << " http_client www.boost.org /LICENSE_1_0.txt\n"; + return 1; + } + + asio::ip::tcp::iostream s; + + // The entire sequence of I/O operations must complete within 60 seconds. + // If an expiry occurs, the socket is automatically closed and the stream + // becomes bad. + s.expires_after(std::chrono::seconds(60)); + + // Establish a connection to the server. + s.connect(argv[1], "http"); + if (!s) + { + std::cout << "Unable to connect: " << s.error().message() << "\n"; + return 1; + } + + // Send the request. We specify the "Connection: close" header so that the + // server will close the socket after transmitting the response. This will + // allow us to treat all data up until the EOF as the content. + s << "GET " << argv[2] << " HTTP/1.0\r\n"; + s << "Host: " << argv[1] << "\r\n"; + s << "Accept: */*\r\n"; + s << "Connection: close\r\n\r\n"; + + // By default, the stream is tied with itself. This means that the stream + // automatically flush the buffered output before attempting a read. It is + // not necessary not explicitly flush the stream at this point. + + // Check that response is OK. + std::string http_version; + s >> http_version; + unsigned int status_code; + s >> status_code; + std::string status_message; + std::getline(s, status_message); + if (!s || http_version.substr(0, 5) != "HTTP/") + { + std::cout << "Invalid response\n"; + return 1; + } + if (status_code != 200) + { + std::cout << "Response returned with status code " << status_code << "\n"; + return 1; + } + + // Process the response headers, which are terminated by a blank line. + std::string header; + while (std::getline(s, header) && header != "\r") + std::cout << header << "\n"; + std::cout << "\n"; + + // Write the remaining data to output. + std::cout << s.rdbuf(); + } + catch (std::exception& e) + { + std::cout << "Exception: " << e.what() << "\n"; + } + + return 0; +}