The goal of the ACE reactor framework is to implement a flexible event processing mechanism so that applications do not need to write central Code related to the platform to meet the needs of event processing. With the reactor framework, applications only need to do three things to implement event processing.
One:One or more classes are derived from ace_event_handler, and the unique event processing behavior of the application is added to each virtual callback method.
# 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 /// <Remove_handler> method is called on an ace_reactor.
/// <Close_mask> indicates which event has triggered
/// <Handle_close> method callback on a participant @ 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;
// Optional reload Based on the Application
}
Two:Register the application event processing object with the ace_reactor class, and associate each event processing object with the event it is interested in.
First, let's look at the Declaration of register_handler, a member of ace_reactor (<ACE/reactor. h>) (there are several other parameters to pass ):
/**
* 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 susponded, it will remain
* Suincluded. When the handler is resumed, it will have
* 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 );
Ace_reactor_mask is declared in <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,
Effect_mask = pollpri,
# Else/* use select */
Read_mask = (1 <0 ),
Write_mask = (1 <1 ),
Effect_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 |
Effect_mask |
Accept_mask |
Connect_mask |
Timer_mask |
Qos_mask |
Group_qos_mask |
Signal_mask,
Rwe_mask = read_mask |
Write_mask |
Effect_mask,
Dont_call = (1 <9)
};
In the service class, you can associate each event processing object with the event it is interested in:
This-> Reactor ()-> register_handler (this, ace_event_handler: read_mask); // associate virtual int handle_input (ace_handle FD = ace_invalid_handle); callback function.
Register the application event handling object with the ace_reactor class:
Service;
Service. Reactor (ace_reactor: instance ());
Three:Run the ace_reactor event loop.
Ace_reactor: instance ()-> run_reactor_event_loop ();
// There are several other cyclic functions that have not been studied.
We can see that we use an ace_reactor single piece, which is managed by ace_object_manager.