ACE_RAACTOR Reflective Mechanism Use ACE_REACTOR to implement Socket event Processing All Ace_Event_Handler classes that require event processing, and implement the handle_signal method, if necessary, the event trigger object is when the Handle_Signal method returns -1, will call Handle_Close the method of the event to be processed by register_handler method ACE_Reactor with the event triggering object (ACE_Event) register_handler method has the following three common call mode 1, virtual int register_handler (ACE_Event_Handler * event_handler, ACE_Reactor_Mask mask) associates; table for the I / O event registration event notice I / O event triggered by the sources which get_handle () method calls ACE_Event_Handler :: add_reference () method, if the current event registration notification is not registered 2, virtual int register_handler (ACE_HANDLE io_handle, ACE_Event_Handler * event_handler, ACE_Reactor_Mask mask) ; method 2 is similar to the same method, but without providing get_handle () method 3, virtual int register_handler (ACE_Event_Handler * event_handler, ACE_HANDLE event_handle = ACE_INVALID_HANDLE); only in WIN platform (ACE_Event the handle () returns the event handler ACE_HANDLE) to Make the trigger event normally, to perform an event processing method for ACE_REAAAAAACTOR Handle_Events (Time), Do not add a TIME parameter, there will be no timeout exit, unless the CLOSE method of the ACE_RAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC can also use the ACE_Reactor's resume_handler suspend_handler method to hang and recover event notifications. Of course, you can also use remove_handler to completely delete event notifications.
By following, by example, a slightly changed example, a slight change) How to use the ACE event mechanism to process the Socket I / O request to implement a simple Socket Server involves the following primary system class ACE_INET_ADDR: Network address classes have multiple constructor common structure as follows: ACE_INET_Addr (u_short port_number, ACE_UINT32 ip_addr = INADDR_ANY); ACE_INET_Addr (u_short port_number, const char host_name [], int address_family = AF_UNSPEC); ACE_EXPLICIT ACE_INET_Addr (const char address []); ACE_SOCK_Acceptor SOCKET receiver class, used configuration: ACE_SOCK_Acceptor (const ACE_Addr & local_sap, int reuse_addr = 0, int protocol_family = PF_UNSPEC, int backlog = ACE_DEFAULT_BACKLOG, int protocol = 0); parameter reuse_addr = 1 table address reuse which mainly has the following methods: open method accept method ACE_SOCK_Stream SOCKET stream processing Class, the class can be given by the ACCE_Sock_Acceptor assigned to the real content provided with RECV, Send, Close, etc. ACE_EVENT_HANDLER event processing class provides some way to define some methods of callback when trigger: Set the event trigger mechanism and Setting the event trigger mechanism source code on the received data read in: #include "ace / reactor.h" #include "ace / wfmo_reactor.h" #include "ace / inet_addr.h" #include "ACE / SOCK_STREAM.H "#include "ACE / SOCK_ACCEPTOR.H" #include "ace / os_main.h"
ACE_RCSID (wfmo_reactor, network_events, "netWork_events.cpp, v 4.3 2003/11/05 09:36:08 jwillemsen exp")
// data read in the event handler class class Network_Handler: public ACE_Event_Handler {public: virtual int handle_input (ACE_HANDLE handle); virtual int handle_close (ACE_HANDLE handle, ACE_Reactor_Mask close_mask); virtual ACE_HANDLE get_handle (void) const; // data read Event trigger object ACE_SOCK_STREAM STREAM_;
}
ACE_HANDLENETWORK_HANDLER :: GET_HANDLE (VOID) const {return this-> stream_.get_handle ();
intNetwork_Handler :: handle_input (ACE_HANDLE handle) {ACE_DEBUG ((LM_DEBUG, "Network_Handler :: handle_input handle =% d / n", handle)); while (1) {char message [BUFSIZ]; int result = this-> stream_. Recv (Message, SizeOf Message); if (Result> 0) {Message [Result] = 0; ACE_DEBUG ((LM_Debug, "Remote Message:% S / N", Message));} Else IF (Result == 0) {ACE_DEBUG ((LM_Debug, "Connection CloseD / N")); // Note that -1 is to return -1 so that Handle_Close executes return -1;} else if (errno == ewouldblock) {return 0;} else {ACE_DEBUG LM_Debug, "problems in receiving data, result =% d", result); return -1;}}}
INTNETWORK_HANDLER :: Handle_close (ACE_HANDLE HANDLE, ACE_REACTOR_MASK) {ACE_DEBUG ((LM_Debug, "Network_handler :: Handle_Close Handle =% D / N", Handle)); // Note: Be sure to call the Close method, otherwise you will find it in connection When more than 4,000 customers, the program will become more and more slower (); Return 0;}
// Customer Connection Event Handling Class Class Network_Listener: Public Ace_Event_Handler {Public: Network_Listener (Void); // Default Constructionor ~ Network_Listener (Void); // Default Constructionor; // DEFAULT CONSTRUCTOR
Virtual Int Handle_Input (ACE_HANDLE HANDLE); Virtual Int Handle_close (ACE_HANDLE HANDLE, ACE_REAACTOR_MASK CLOSE_MASK); ACE_HANDLE GET_HANDLE (VOID) Const;
ACE_INET_ADDR LOCAL_ADDRESS_; // Event Trigger Object ACE_SOCK_ACCEPTOR ACCEPTOR_; NETWORK_HANDLER * HANDLER;
Network_Listener :: Network_Listener (void): local_address_ (ACE_DEFAULT_SERVER_PORT), acceptor_ (local_address_, 1) {this-> reactor (ACE_Reactor :: instance ()); // int result register connected to the reactor event = this-> reactor () - > register_handler (this, ACE_EVENT_HANDLER :: Accept_Mask); ACE_ASSERT (Result == 0); handler = new network_handler ();} network_listener :: ~ network_listener (void) {}
ACE_HANDLENETWORK_LISTENER :: GET_HANDLE (VOID) const {return this-> acceptor_.get_handle ();}
INTNETWORK_LISTENER :: Handle_Input (ACE_HANDLE HANDLE) {ACE_DEBUG ((LM_Debug, "Network_Listener :: Handle_Input Handle =% D / N", Handle);
ACE_INET_ADDR Remote_Address; ACE_SOCK_STREAM Stream;
// Try re-association Event int RET_NEW_HANDE = this-> Reactor () -> Uses_Event_associations ();
Int result = this-> acceptor_.accept (stream, // stream & remote_address, // remote address 0, // timeout setting 1, // Reuse address reset_new_handle); // reset new handler ace_assert (result == 0);
ACE_DEBUG ((LM_DEBUG, "Remote connection from:")); remote_address.dump (); handler-> stream_ = stream; result = this-> reactor () -> register_handler (handler, READ_MASK); ACE_ASSERT (result == 0 RETURN 0;}
INTNETWORK_LISTENER :: Handle_Close (ACE_HANDLE HANDLE, ACE_REACTOR_MASK) {ACE_DEBUG ((LM_Debug, "NetWork_Listener :: Handle_Close Handle =% D / N", Handle);
This-> acceptor_.close ();
DELETE THIS;
Return 0;}
INTACE_TMAIN (INT, ACE_TCHAR * []) {network_listener * listener = new network_listener; // Start loop event processing ace_reactor :: run_event_loop (); Return 0;};