首页 > 代码库 > ASIO例子中定制handler调用

ASIO例子中定制handler调用

//// prioritised_handlers.cpp// ~~~~~~~~~~~~~~~~~~~~~~~~//// Copyright (c) 2003-2013 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)// 该例子展示如何定制handler调用。completion handlers将会加入到一个优先级队列,而不是立即执行。#include <boost/asio.hpp>#include <boost/function.hpp>#include <iostream>#include <queue>using boost::asio::ip::tcp;class handler_priority_queue{public:  void add(int priority, boost::function<void()> function)  {    handlers_.push(queued_handler(priority, function));  }  void execute_all()  {    while (!handlers_.empty())    {      queued_handler handler = handlers_.top();      handler.execute();      //执行完毕后退出      handlers_.pop();    }  }  // A generic wrapper class for handlers to allow the invocation to be hooked.  //为handler包装一个类,允许被调用。包装调用多个参数  template <typename Handler>  class wrapped_handler  {  public:    wrapped_handler(handler_priority_queue& q, int p, Handler h)      : queue_(q), priority_(p), handler_(h)    {    }    void operator()()    {      handler_();    }    template <typename Arg1>    void operator()(Arg1 arg1)    {      handler_(arg1);    }    template <typename Arg1, typename Arg2>    void operator()(Arg1 arg1, Arg2 arg2)    {      handler_(arg1, arg2);    }  //private:    handler_priority_queue& queue_;    int priority_;    Handler handler_;  };  //返回包装好的handler  template <typename Handler>  wrapped_handler<Handler> wrap(int priority, Handler handler)  {    return wrapped_handler<Handler>(*this, priority, handler);  }private:    //内部类  一个入队的handler  class queued_handler  {  public:    queued_handler(int p, boost::function<void()> f)      : priority_(p), function_(f)    {    }    void execute()    {      function_();    }    friend bool operator<(const queued_handler& a,        const queued_handler& b)    {      return a.priority_ < b.priority_;    }  private:    int priority_;    boost::function<void()> function_;  };  //优先队列。按大小排列?  std::priority_queue<queued_handler> handlers_;};//关于asio_handler_invoke的//参考http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/reference/asio_handler_invoke.html//默认handlers的调用函数对象。被io_service调用与相应对象(socket或者deadline_timer)相关的异步完成调用操作可以在此处定制template <typename Function, typename Handler>void asio_handler_invoke(Function f,    handler_priority_queue::wrapped_handler<Handler>* h){  h->queue_.add(h->priority_, f);}//----------------------------------------------------------------------void high_priority_handler(const boost::system::error_code& /*ec*/){  std::cout << "High priority handler\n";}void middle_priority_handler(const boost::system::error_code& /*ec*/){  std::cout << "Middle priority handler\n";}void low_priority_handler(){  std::cout << "Low priority handler\n";}int main(){  boost::asio::io_service io_service;  handler_priority_queue pri_queue;  // Post a completion handler to be run immediately.  //先向io_service加入几个异步操作  io_service.post(pri_queue.wrap(0, low_priority_handler));  // Start an asynchronous accept that will complete immediately.  //加上socket异步操作  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));  //io_service对象启动事件处理循环完成至多一个handler  //返回执行过的handler数字。如果返回0则说明io_service对象已经停止了  while (io_service.run_one())  {    // The custom invocation hook adds the handlers to the priority queue    // rather than executing them from within the poll_one() call.      //io_service执行一个已经准备的队列。以下执行所有异步操作。也就是入队。    while (io_service.poll_one())      ;    //执行函数对象    pri_queue.execute_all();  }  return 0;}

 

ASIO例子中定制handler调用