React   发布时间:2019-10-13  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ACE Reactor框架处理事件及多个I/O流大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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 &current_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(); //还有其它的几个循环函数,还没有具体研究@R_801_10333@,我们用的是一个 ACE_Reactor 单件,由 ACE_Object_Manager 管理它。

大佬总结

以上是大佬教程为你收集整理的ACE Reactor框架处理事件及多个I/O流全部内容,希望文章能够帮你解决ACE Reactor框架处理事件及多个I/O流所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。