Boost logo

Boost-Commit :

From: chris_at_[hidden]
Date: 2008-04-20 21:36:28


Author: chris_kohlhoff
Date: 2008-04-20 21:36:28 EDT (Sun, 20 Apr 2008)
New Revision: 44665
URL: http://svn.boost.org/trac/boost/changeset/44665

Log:
Enhance example to make it clear that invocation hooking can be used with
asynchronous operations.

Text files modified:
   trunk/libs/asio/example/invocation/prioritised_handlers.cpp | 23 +++++++++++++++++++----
   1 files changed, 19 insertions(+), 4 deletions(-)

Modified: trunk/libs/asio/example/invocation/prioritised_handlers.cpp
==============================================================================
--- trunk/libs/asio/example/invocation/prioritised_handlers.cpp (original)
+++ trunk/libs/asio/example/invocation/prioritised_handlers.cpp 2008-04-20 21:36:28 EDT (Sun, 20 Apr 2008)
@@ -13,6 +13,8 @@
 #include <iostream>
 #include <queue>
 
+using boost::asio::ip::tcp;
+
 class handler_priority_queue
 {
 public:
@@ -108,12 +110,12 @@
 
 //----------------------------------------------------------------------
 
-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 @@
 
   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())
   {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk