boost::asio中的implementation_type介绍

 

 

 每个xxx_service都有一个implementation_type如上图所示,

./asio/detail/winrt_ssocket_service.hpp:50: struct implementation_type : base_implementation_type
./asio/detail/win_iocp_socket_service.hpp:109: struct implementation_type :
./asio/detail/reactive_socket_service.hpp:62: struct implementation_type :
./asio/detail/null_socket_service.hpp:48: struct implementation_type
./asio/detail/deadline_timer_service.hpp:56: struct implementation_type


这里主要说reactive_socket_service和deadline_timer_service下的implementation_type,

1)reactive_socket_service::implementation_type 信息如下:

 1 struct implementation_type :
 2     reactive_socket_service_base::base_implementation_type
 3   {
 4     // Default constructor.
 5     implementation_type()
 6       : protocol_(endpoint_type().protocol())
 7     {
 8     }
 9 
10     // The protocol associated with the socket.
11     protocol_type protocol_;
12   };

其父类base_implementation_type信息如下:

 1  struct base_implementation_type
 2   {
 3     // The native socket representation.
 4     socket_type socket_;
 5 
 6     // The current state of the socket.
 7     socket_ops::state_type state_;
 8 
 9     // Per-descriptor data used by the reactor.
10     reactor::per_descriptor_data reactor_data_;
11   };

其中per_descriptor_data信息如下:

 1 // Per-descriptor queues.
 2   class descriptor_state : operation
 3   {
 4     friend class epoll_reactor;
 5     friend class object_pool_access;
 6 
 7     descriptor_state* next_;
 8     descriptor_state* prev_;
 9 
10     mutex mutex_;
11     epoll_reactor* reactor_;
12     int descriptor_;
13     uint32_t registered_events_;
14     op_queue<reactor_op> op_queue_[max_ops];
15     bool shutdown_;
16 
17     BOOST_ASIO_DECL descriptor_state();
18     void set_ready_events(uint32_t events) { task_result_ = events; }
19     BOOST_ASIO_DECL operation* perform_io(uint32_t events);
20     BOOST_ASIO_DECL static void do_complete(
21         io_service_impl* owner, operation* base,
22         const boost::system::error_code& ec, std::size_t bytes_transferred);
23   };

// Per-descriptor data.
typedef descriptor_state* per_descriptor_data;

主要关心的是epoll_wait回调函数do_complete和io任务队列op_queue[max_ops],队列中封装了相关的read,write回调函数操作;

 

2)deadline_timer_service::struct implementation_type信息如下:

1 struct implementation_type
2     : private boost::asio::detail::noncopyable
3   {
4     time_type expiry;
5     bool might_have_pending_waits;
6     typename timer_queue<Time_Traits>::per_timer_data timer_data;
7   };
View Code

其中per_timer_data的结构体如下:

 1  class per_timer_data
 2   {
 3   public:
 4     per_timer_data() : next_(0), prev_(0) {}
 5 
 6   private:
 7     friend class timer_queue;
 8 
 9     // The operations waiting on the timer.
10     op_queue<wait_op> op_queue_;
11 
12     // The index of the timer in the heap.
13     std::size_t heap_index_;
14 
15     // Pointers to adjacent timers in a linked list.
16     per_timer_data* next_;
17     per_timer_data* prev_;
18   };

 

通过index我们可以从io_service中区分出不同的deadline_timer;

 

 

epoll_event中的event.data.ptr保存的就是implementation中per_xx_data的指针

 

posted @ 2018-09-14 17:40  郭流水  阅读(350)  评论(0编辑  收藏  举报