ACE Reactor框架处理事件及多个IO流

    技术2022-05-20  27

    ACE Reactor框架设计的目标是,实现一种灵活的事件处理机制,使应用无需为了满足事件处理的需要而编写平台相关的中心代码。使用Reactor框架,应用要实现其事件处理只需要做三件事情。ONE:从 ACE_Event_Handler 派生一个或多个类,并给各个虚回调方法增加应用特有的事件处理行为。#include <ace/Event_Handler.h>//...class Service : public ACE_Event_Handler{/// Called when input events occur (e.g., connection or data).virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);/// Called when output events are possible (e.g., when flow control/// abates or non-blocking connection completes).virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);/// Called when an exceptional events occur (e.g., SIGURG).virtual int handle_exception (ACE_HANDLE fd = ACE_INVALID_HANDLE);/**   * Called when timer expires. <current_time> represents the current   * time that the <Event_Handler> was selected for timeout   * dispatching and <act> is the asynchronous completion token that   * was passed in when <schedule_timer> was invoked.   */virtual int handle_timeout (const ACE_Time_Value ¤t_time,                              const void *act = 0);/// Called when a process exits.virtual int handle_exit (ACE_Process *);/// Called when a <handle_*()> method returns -1 or when the/// <remove_handler> method is called on an ACE_Reactor. The/// <close_mask> indicates which event has triggered the/// <handle_close> method callback on a particular @a handle.virtual int handle_close (ACE_HANDLE handle,                            ACE_Reactor_Mask close_mask);/// Called when object is signaled by OS (either via UNIX signals or/// when a Win32 object becomes signaled).virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);virtual int handle_qos (ACE_HANDLE = ACE_INVALID_HANDLE);virtual int handle_group_qos (ACE_HANDLE = ACE_INVALID_HANDLE);/// Get the I/O handle.virtual ACE_HANDLE get_handle (void) const;//根据应用选择性的重载}TWO:向 ACE_Reactor 类登记应用的事件处理对象,把每个事件处理对象与它感兴趣的事件关联起来。先看ACE_Reactor(<ace/Reactor.h>)成员register_handler一个声明(还有其它几个传不同参数):/**   * Register handler for I/O events.   *   * A handler can be associated with multiple handles. A handle   * cannot be associated with multiple handlers.   *   * The handle will come from ACE_Event_Handler::get_handle().   *   * Reactor will call ACE_Event_Handler::add_reference() for a new   * handler/handle pair.   *   * If this handler/handle pair has already been registered, any new   * masks specified will be added. In this case,   * ACE_Event_Handler::add_reference() will not be called.   *   * If the registered handler is currently suspended, it will remain   * suspended. When the handler is resumed, it will have the   * existing masks plus any masks added through this call. Handlers   * do not have partial suspensions.   */virtual int register_handler (ACE_Event_Handler *event_handler,                                ACE_Reactor_Mask mask);ACE_Reactor_Mask声明于 <ace/Event_Handler.h> 中:enum{    LO_PRIORITY = 0,    HI_PRIORITY = 10,    NULL_MASK = 0,#if defined (ACE_USE_POLL)    READ_MASK = POLLIN,    WRITE_MASK = POLLOUT,    EXCEPT_MASK = POLLPRI,#else /* USE SELECT */    READ_MASK = (1 << 0),    WRITE_MASK = (1 << 1),    EXCEPT_MASK = (1 << 2),#endif /* ACE_USE_POLL */    ACCEPT_MASK = (1 << 3),    CONNECT_MASK = (1 << 4),    TIMER_MASK = (1 << 5),    QOS_MASK = (1 << 6),    GROUP_QOS_MASK = (1 << 7),    SIGNAL_MASK = (1 << 8),    ALL_EVENTS_MASK = READ_MASK |                      WRITE_MASK |                      EXCEPT_MASK |                      ACCEPT_MASK |                      CONNECT_MASK |                      TIMER_MASK |                      QOS_MASK |                      GROUP_QOS_MASK |                      SIGNAL_MASK,    RWE_MASK = READ_MASK |               WRITE_MASK |               EXCEPT_MASK,    DONT_CALL = (1 << 9)};你可在 Service 类中,把每个事件处理对象与它感兴趣的事件关联起来:    this->reactor()->register_handler(this, ACE_Event_Handler::READ_MASK);//关联virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);回调函数。向 ACE_Reactor 类登记应用的事件处理对象:    Service service;    service.reactor(ACE_Reactor::instance());THREE:运行 ACE_Reactor 事件循环。    ACE_Reactor::instance()->run_reactor_event_loop();    //还有其它的几个循环函数,还没有具体研究可以看出,我们用的是一个 ACE_Reactor 单件,由 ACE_Object_Manager 管理它。


    最新回复(0)