Ace self-adaption communication environment Chinese technical documents (Part 1: Ace programmer tutorial) reactor instance

Source: Internet
Author: User

This is an example in Chapter 6 of the ACE learning tutorial mentioned in the title.

# Include "ACE/reactor. H "# include" ACE/handler "# define port_no 19998 typedef ace_sock_acceptor acceptor; // Forward Declaration class my_accept_handler; // data processor class my_input_handler: Public ace_event_handler {public: // constructor my_input_handler () {ace_debug (lm_debug, "constructor \ n");} // calls back any data from the handle int handle_input (ace_handle) {// obtain the data peer_ I (). recv_n (data, 12); ace_debug (lm_debug, "% s \ n", data); // return 0 in the event processing area;} // required, ace_handle get_handle (void) {return this-> peer_ I (). get_handle ();} ace_sock_stream & peer_ I () {return this-> peer _;} PRIVATE: ace_sock_stream peer _; // event processing I/O entry, buffer char data [12] ;}; // accept the processor class my_accept_handler: Public ace_event_handler {public: // construct my_accept_handler (ace_addr & ADDR) {This-> open (ADDR );} // start listening // accept the request int open (ace_addr & ADDR) {peer_acceptor.open (ADDR); Return 0 ;}// reload the I/O processing area from the client, client to process int handle_input (ace_handle handle) {// a client has been created to create a data processor instance to process my_input_handler * Eh = new my_input_handler (); // accept the connection "into" the event handler if (this-> peer_acceptor.accept (Eh-> peer_ I (), // Stream object 0, // remote address 0, // timeout 1) =-1) // restart if interrupted ace_debug (lm_error, "error in connection \ n"); ace_debug (lm_debug, "Connection established \ n"); // bind the processor to the ace_reactor: instance ()-> register_handler (EH, ace_event_handler: read_mask ); // if you want to process more clients, return-1. If you want to permanently multiple clients, return 0 return 0;} // required, ace_handle get_handle (void) const {return this-> peer_acceptor.get_handle ();} PRIVATE: acceptor peer_acceptor;}; int main (INT argc, char * argv []) {incluaddr (port_no); inclu* Eh = new my_accept_handler (ADDR); ace_reactor: instance ()-> register_handler (EH, ace_event_handler: accept_mask); While (1) ace_reactor: instance ()-> handle_events ();}

Post: http://raojl.iteye.com/blog/337952

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.