ACE-code hierarchy and socket encapsulation

Source: Internet
Author: User
Tags getstream

The original text from Http://www.cnblogs.com/binchen-china, Forbidden reprint.

Ace source code about 100,000 lines, is a very large C + + network programming code library, including network Programming Edge corner. In practice, not all of the code can be used in your project, instead you just need a small part of it to be able to do what is actually needed.

The most recent study of its source code is the greatest feeling is the size of large, small data, logical jump. The online search of Ace books and materials, but also the fur on the roll, the concept of flying around, focusing on how to use its framework, the complex underlying code and implementation are not, rather than directly see the source directly. Ace Code directory structure is not very good, a lot of files under a path, very reading difficult to summarize the function of each file, the source of a large number of templates and import export, source insight to see the real shame.

The ACE code can be divided into three levels: OS layer, OO layer, and framework layer:

    • OS layer is mainly to be compatible with each platform, the network of the underlying API unified, this layer of users often do not care.
    • OO layer is the use of some commonly used data structures or methods OO encapsulation, convenient for the upper layer, including the socket method, processes, threads and their synchronization mechanism.
    • The framework layer implements some excellent network framework, which is good to use directly.

The most common use of the OO layer is the socket package, which includes the following:

    • Ace_sock_connector: Connector, actively establish a connection for the socket Client.
    • Ace_sock_acceptor: The receiver, passively establishes the connection for the socket Server.
    • Ace_sock_stream: A stream that transmits data for transferring data.
    • ACE_INET_ADDR: The address used to represent the communication endpoint.

The encapsulation of these four structures simplifies the socket programming code, avoids the error in code details, and enhances the application of portability and object-oriented thinking.

The previous article server programming can be regarded as a pure C language oriented process programming, from bind to listen, and so on, very cumbersome and complex parameters, call error, exception handling easily omitted, Ace of these four socket package to solve these problems. All the detail code is masked inside the ace.

Directly below the server code:

1#include <ace/INET_Addr.h>2#include <ace/SOCK_Acceptor.h>3#include <ace/SOCK_Stream.h>4#include <ace/Log_Msg.h>5 6 #defineMax_buff_size 10247 #defineListen_port 50108 #defineServer_ip Ace_localhost9 Ten classServer One { A  Public: -Server (intPortChar*IP); -~Server (); the     BOOLopen (); -     BOOLrun (); -     voidclose (); -ace_sock_stream& GetStream () {returnSvr_stream;} + Private: - ace_inet_addr svr_addr,cli_addr; + ace_sock_acceptor svr_aceept; A Ace_sock_stream Svr_stream; at }; -  -Server::server (intPortChar*IP): svr_addr (PORT,IP) - { -      - } in  -server::~Server () to { +      - } the  * BOOLServer::open () $ {Panax Notoginseng     if(-1= = Svr_aceept.open (svr_addr,1)) -     { theAce_debug ((Lm_error,ace_text ("failed to accept\n"))); + svr_aceept.close (); A         return false; the     } +     return true; - } $  $ BOOLServer::run () - { -     if(-1= = Svr_aceept.accept (svr_stream,&cli_addr)) the     { -Ace_debug ((Lm_error,ace_text ("failed to accept\n")));Wuyi svr_aceept.close (); the         return false; -     } Wu     return true; - } About  $ voidserver::close () - { - svr_aceept.close (); - } A  + intMain () the { -Server SVR (Listen_port, (Char*) server_ip); $     CharStrbuffer[max_buff_size]; the      the     if(!Svr.open ()) the     { the         return 0; -     } inAce_debug (Lm_info, Ace_text ("Open success!\n"))); the      the     if(!Svr.run ()) About     { the         return 0; the     } theAce_debug (Lm_info, Ace_text ("Run success!\n"))); +      -Ace_sock_stream Svr_data =Svr.getstream ();  the Bayi      while(1) the     {     the         int byte=svr_data.recv (strbuffer,max_buff_size); -         if(-1==byte) -         { theAce_debug (Lm_info, Ace_text ("Receive Data failed\n"))); the              Break; the         } the         Else if(0==byte) -         { theAce_debug (Lm_info, Ace_text ("Client closed!\n"))); the              Break; the         }94         Else the         { theAce_debug (Lm_info, Ace_text ("receive from client:%s\n") , Strbuffer)); theAce_os::memset (Strbuffer,0,sizeof(Strbuffer));98         } About     } - svr_data.close ();101 svr.close ();102     return 0;103}

Operation Result:

At this point, there is no difference between the server that ACE finishes and the code or functionality that was previously done with the Linux underlying API. For the time being, it's just a simple use of OO code familiarity with Ace, followed by the use of Ace's reactor framework for server development. Feel the benefits and convenience of ACE.

ACE-code hierarchy and socket encapsulation

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.