在ACE中使用epoll的ET模式


用ace做了一个linux下的服务端程序,使用了ACE_Dev_Poll_Reactor。

运行后发现,当注册了socket的读写事件(ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK)后,handle_output就会不停的被调用,同时cpu达到了100%。

查找原因,原来是ACE_Dev_Poll_Reactor中使用了epoll的LT模式,同时搜索代码,也没有发现ET相关的关键字。

最初的想法是修改ACE_Dev_Poll_Reactor的代码,增加ET的处理。但是我不想修改ace库内部的代码,所以使用了派生的方法,同时感觉到ace结构设计的完美。

  1. class CMyReactor : public ACE_Dev_Poll_Reactor  
  2.   
  3. {  
  4.   
  5. ....  
  6.   
  7. //重载内部注册回调的接口   
  8.   
  9. int register_handler_i (ACE_HANDLE handle,ACE_Event_Handler *eh,ACE_Reactor_Mask mask)  
  10.   
  11. {  
  12.   
  13.  ACE_TRACE ("ACE_Dev_Poll_Reactor::register_handler_i");  
  14.   
  15.  if (handle == ACE_INVALID_HANDLE  
  16.   || mask == ACE_Event_Handler::NULL_MASK)  
  17.  {  
  18.   errno = EINVAL;  
  19.   return -1;  
  20.  }  
  21.   
  22.  if (this->handler_rep_.find (handle) == 0)  
  23.  {  
  24.   // Handler not present in the repository.  Bind it.   
  25.   if (this->handler_rep_.bind (handle, event_handler, mask) != 0)  
  26.    return -1;  
  27.   
  28.   struct epoll_event epev;  
  29.   ACE_OS::memset (&epev, 0, sizeof (epev));  
  30.   static const int op = EPOLL_CTL_ADD;  
  31.   
  32.   epev.events  = this->reactor_mask_to_poll_event (mask);  
  33.   epev.data.fd = handle;  
  34.   
  35.   if( NET_SETTING.ET_MODE )  
  36.   {  
  37.    epev.events |= EPOLLET;  // 关键代码,增加ET标识 [1/13/2011 yao]   
  38.   }  
  39.   
  40.   if (::epoll_ctl (this->poll_fd_, op, handle, &epev) == -1)  
  41.   {  
  42.    ACE_ERROR ((LM_ERROR, "%p\n""epoll_ctl"));  
  43.    (voidthis->handler_rep_.unbind (handle);  
  44.    return -1;  
  45.   }  
  46.  }  
  47.  else  
  48.  {  
  49.   // Handler is already present in the repository, so register it   
  50.   // again, possibly for different event.  Add new mask to the   
  51.   // current one.   
  52.   if (this->mask_ops_i (handle, mask, ACE_Reactor::ADD_MASK) == -1)  
  53.    ACE_ERROR_RETURN ((LM_ERROR, "%p\n""mask_ops_i"), -1);  
  54.  }  
  55.   
  56.  // Note the fact that we've changed the state of the wait_set_,   
  57.  // which is used by the dispatching loop to determine whether it can   
  58.  // keep going or if it needs to reconsult select().   
  59.  // this->state_changed_ = 1;   
  60.   
  61.  return 0;  
  62.   
  63.   
  64.   
  65.   
  66. }  
  67.   
  68. ....  
  69.   
  70. }  

只要在epoll_ctl 之前增加EPOLLET就可以了搞定了。

如果想要执行,还需要重载几个其他的函数,因为register_handler_i并不是虚函数,所以要从调用register_handler_i的函数开始重载。这里有没有更好的做法,我还不清楚。哪位朋友有更好的做法,欢迎一起研究一下大笑

相关内容