From 17ef45244b4791bc00a76094e07d98e7f1ebd3a2 Mon Sep 17 00:00:00 2001 From: Christopher Kohlhoff Date: Mon, 21 Apr 2008 01:36:28 +0000 Subject: [PATCH] Enhance example to make it clear that invocation hooking can be used with asynchronous operations. [SVN r44665] --- example/invocation/prioritised_handlers.cpp | 23 +++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/example/invocation/prioritised_handlers.cpp b/example/invocation/prioritised_handlers.cpp index 657eb5f6..82dc4a3e 100644 --- a/example/invocation/prioritised_handlers.cpp +++ b/example/invocation/prioritised_handlers.cpp @@ -13,6 +13,8 @@ #include #include +using boost::asio::ip::tcp; + class handler_priority_queue { public: @@ -108,12 +110,12 @@ void asio_handler_invoke(Function f, //---------------------------------------------------------------------- -void high_priority_handler() +void high_priority_handler(const boost::system::error_code& /*ec*/) { std::cout << "High priority handler\n"; } -void middle_priority_handler() +void middle_priority_handler(const boost::system::error_code& /*ec*/) { std::cout << "Middle priority handler\n"; } @@ -129,9 +131,22 @@ int main() handler_priority_queue pri_queue; + // Post a completion handler to be run immediately. io_service.post(pri_queue.wrap(0, low_priority_handler)); - io_service.post(pri_queue.wrap(100, high_priority_handler)); - io_service.post(pri_queue.wrap(42, middle_priority_handler)); + + // Start an asynchronous accept that will complete immediately. + tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 0); + tcp::acceptor acceptor(io_service, endpoint); + tcp::socket server_socket(io_service); + acceptor.async_accept(server_socket, + pri_queue.wrap(100, high_priority_handler)); + tcp::socket client_socket(io_service); + client_socket.connect(acceptor.local_endpoint()); + + // Set a deadline timer to expire immediately. + boost::asio::deadline_timer timer(io_service); + timer.expires_at(boost::posix_time::neg_infin); + timer.async_wait(pri_queue.wrap(42, middle_priority_handler)); while (io_service.run_one()) {