5 ACE Acceptor Connector proactor asynchronous framework

Source: Internet
Author: User

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8C/94/wKioL1hwl4PSyKevAAIh0TXOajY795.png "title=" 02 Service management. PNG "alt=" Wkiol1hwl4psykevaaih0txoajy795.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/8C/97/wKiom1hwl4TR7kPaAALorbcWxjk660.png "title=" 03 Service off. png "alt=" Wkiom1hwl4tr7kpaaalorbcwxjk660.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8C/97/wKiom1hwl4fjABCkAAQfvULlylc550.png "title=" Proactor participant. png "alt=" Wkiom1hwl4fjabckaaqfvullylc550.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8C/94/wKioL1hwl4fw3FpQAAE7PWtr3w4256.png "title=" Framework class. png "alt=" Wkiol1hwl4fw3fpqaae7pwtr3w4256.png "/>




Ace_acceptor_connector Framework completes Accpet operation


[email protected]:~/ace/aceacceptorconnector$ cat echo_server.cpp  #include  <ace/ svc_handler.h> #include  <ace/SOCK_Stream.h> #include  <ace/SOCK_Acceptor.h> #include  <ace/acceptor.h>class acceptorhandler: public ace_svc_handler<ace_sock_stream,  ACE_NULL_SYNCH> {public:typedef ACE_Svc_Handler<ACE_SOCK_Stream, ACE_NULL_SYNCH>  parent;enum {buf_sime = 512};virtual int handle_input (ACE_HANDLE h)  { Ace_os::sleep (5); Ssize_t n = peer (). recv (Buf, buf_sime);if  (n <= 0) ACE _error_return ((lm_error,  "%p\n",  "Peer () recv ()"),  -1);if  (Peer (). Send (Buf, n)  = = -1) Ace_error_return ((lm_error,  "%p\n",  "peer (). Send ()"),  -1); return 0;} private:char buf[buf_sime];}; Typedef ace_acceptor<acceptorhandler, ace_sock_acceptor> myacceptor;int main () &NBSp {ace_inet_addr addr (8868); Myacceptor acceptor (Addr, ace_reactor::instance ()); Ace_reactor::instance ()->run_reactor_event_loop ();} [email protected]:~/ace/aceacceptorconnector$  compile run: [email protected]:~/ace/aceacceptorconnector $ g++ echo_server.cpp -lace -lpthread && ./a.out [email  protected]:~/ace/acetask$ nc localhost 8868121121[email protected]:~$  nc  localhost 8868321332133213132131




Ace_acceptor_connector Framework completes connector operation

[email protected]:~/ace/aceacceptorconnector$ cat echo_client.cpp  #include  < iostream> #include  <ace/Svc_Handler.h> #include  <ace/SOCK_Stream.h> #include  < Ace/sock_connector.h> #include  <ace/connector.h>class inputhandler: public ace_svc _handler<ace_sock_stream, ace_null_synch> {public:typedef ace_svc_handler<ace_sock_ Stream, ace_null_synch> parent;enum {buf_sime = 512};virtual int open ( Void* a)  {if  (Parent::open (a)  == -1) return -1;return this->activate (THR_ new_lwp | thr_detached);} Virtual int handle_input (ace_handle)  //output received data {Ssize_t n = peer (). recv (BUF,  buf_sime);if  (n <= 0) Ace_error_return ((lm_error,  "%p\n", "Peer () recv ()"),  -1 );buf[n] = 0; Ace_debug ((lm_debug,  "%s\n",  buf)); return 0;} Virtual int&nbsP;SVC ()  //reads data from the keyboard {char inbuf[buf_sime] =  "";while  (Std::cin.getline (inBuf, BUF_ SIME)  {if  (Peer (). Send (Inbuf, strlen (INBUF))  == -1)  {ace_error ((lm_error,  "%p\n",  "peer (). Send ()"); break;}} return 0;} private:char buf[buf_sime];}; Typedef ace_connector<inputhandler, ace_sock_connector> myconnector;int main ()   {ace_inet_addr addr (8868,  "127.0.0.1"); myconnector connector;inputhandler* p = 0;//created by connector   InputHandlerif  ( Connector.connect (P,&NBSP;ADDR)  == -1) Ace_error_return ((lm_error,  "%p\n",  "Connect ()"), &NBSP;-1); Ace_reactor::instance ()->run_reactor_event_loop ();} [email protected]:~/ace/aceacceptorconnector$  compile run: [email protected]:~/ace/aceacceptorconnector $ g++ echo_client.cpp -lace -lpthread && ./a.out  1234567890QAZWSX Shanghai, China! Hello ace ^c[email protected]:~/ace/aceacceptorconnector$  simulating a server:[email protected]:~/ace$ nc -l  LOCALHOST&NBSP;88681234567890QAZWSX Shanghai, China! hello ace [email protected]:~/ace$



Proactor Asynchronous Echo Server

[email protected]:~/ace/aceproactor$ cat proactor_echo_server.cpp  #include   "ace/ Asynch_io.h "#include   ace/asynch_acceptor.h" #include   "ace/inet_addr.h" #include   "Ace/proactor.h" Class echoservice: public ace_service_handler {public:~echoservice ()  {if  (this- >handle ()  != ace_invalid_handle) Ace_os::closesocket (This->handle ());} Virtual void open (ace_handle h, ace_message_block&)  {handle (h);if  (this-> Reader_.open (*this)  != 0 | |  this->writer_.open (*this)  != 0)  {ace_error ((lm_error,  "%p\n",  "open ()")); Delete this;return;} ace_message_block* mb; Ace_new_noreturn (Mb, ace_message_block (+));if  (This->reader_.read (*mb, mb->space ())  != 0)  {ace_error ((lm_error,  "%p\n",  "read ()")); Mb->release ();d elete this; return;}} Virtual void handle_read_stream (const ace_aSynch_read_stream::result& result)  {ace_message_block &mb = result.message_ Block ();if  (!result.success ()  | |  result.bytes_transferred ()  == 0)  {mb.release ();d elete this;}  else {if  (This->writer_.write (Mb, mb.length ())  != 0)  {ace_error ((lm_ error,  "%p\n",  "Write ()"); Mb.release ();}  else {ACE_Message_Block* mblk; Ace_new_noreturn (Mblk, ace_message_block); This->reader_.read (*mblk, mblk->space ());}}} Virtual void handle_write_stream (Const ace_asynch_write_stream::result& result)  { Result.message_block (). Release ();} private:ace_asynch_read_stream reader_; ace_asynch_write_stream writer_;}; Int main ()  {ace_inet_addr listen_addr (8868); ace_asynch_acceptor<echoservice> aio_acceptor;if  (0 != aio_acceptor.open (listen_addr ,  0, // bytes_to_read0, // pass_ADDRESSESACE_DEFAULT_BACKLOG,&NBSP;1,&NBSP;//&NBSP;REUSE_ADDR0,&NBSP;//&NBSP;PROACTOR0))  // validate _new_connectionace_error_return ((lm_error,  "%p\n",  "Write ()"),  1); Ace_proactor::instance ()->proactor_run_event_loop ();} [email protected]:~/ace/aceproactor$  Compiling run:[email protected]:~/ace/aceproactor$ g++  proactor_echo_server.cpp -lace && ./a.out  (2710 | 140613802981248)   ace_posix_aiocb_proactor::max number of aios=1024^c[email protected]:~/ace/aceproactor$   Client Test:[email protected]:~/ace$ nc  localhost 88681231233456734567^c[email  protected]:~/ace$






This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1889998

5 ACE Acceptor Connector proactor asynchronous framework

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.