ACE_Event_Handler:事件响应入口
Posted 炽离
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ACE_Event_Handler:事件响应入口相关的知识,希望对你有一定的参考价值。
1:ACE_Event_Handler类
在ACE Reactor框架中,ACE_Event_Handler是所有事件处理器的基类。ACE_Event_Handler提供了一组事件处理的挂钩方法,理解和掌握这些挂钩方法的触发条件和使用方法,是ACE Reactor编程装B道路的重点。先看一下ACE_Event_Handler提供的关键方法:
2:举个栗子
1:A从B那里下了订单,要求B每天送一份货物至A提供的地址。该过程类似于在ACE_Reactor::register_handler()上注册一个事件,注册的事件类型相当于收货地址(Reactor会根据注册的事件类型调用对应的挂钩方法),register_handler的第一个参数是ACE_Event_Handler指针,第二个参数是注册的事件类型,见下图:
register_handler注册了哪个事件,当该类型事件发生时,ACE_Reactor就会调用对应的挂钩方法,比如READ_MASK对应handle_input方法。没有注册的事件类型是不会触发对应方法的。PS:ACE_Event_Handler::RWE_MASK等价于READ+WRITE+EXCEPT。
ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::RWE_MASK);
2:仅有收货地址是不够的,还需要指定收货人。由于ACE_Event_Handler是虚基类,我们需要在子类中提供收货人实体。假设需要接收的物品为数据流,可以定义实体ACE_SOCK_Stream m_Peer。将ACE_SOCK_Stream收货人信息通知给某宝的方法是get_handle,使用方法如下:
ACE_HANDLE CClass::get_handle (void) const { return m_Peer.get_handle (); };
3:某君A收到了货物,试用了后觉得还不错,B继续送;或者通知B不要再送该类物品。
对于情况1,在ACE_Event_Handler的handle_*()方法中返回大于等于0的int值。对于情况2,在handle_*()方法中返回-1,ACE_Reactor将会通知handle_close()方法进行后续处理,并清除掉步骤1中register_handler对应的mask值。handle_*()各类方法的原型函数如下:
/// 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. @a current_time represents the current * time that the Event_Handler was selected for timeout * dispatching and @a 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 /// @a 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);
ps1:handle_close()的返回值将会被忽略。
ps2:handle_close()在两种情况下被触发:1,handle_*()方法返回-1;2,调用ACE_Reactor::remove_handler()。
4:A从B那里解除合约订单,调用ACE_Reactor::register_handler()相反的操作ACE_Reactor::remove_handler()。如果不希望remove_handler会触发handle_close()方法,可以在mask值加上DONT_CALL标识位:
ACE_Reactor::instance()->remove_handler(this,ACE_Event_Handler::RWE_MASK | ACE_Event_Handler::DONT_CALL);
读书笔记:C++ Network Programming Volume2 Systematic Reuse with ACE and Frameworks
以上是关于ACE_Event_Handler:事件响应入口的主要内容,如果未能解决你的问题,请参考以下文章