Develop a multi-thread high concurrency network library based on the new c ++ 11 standard

Source: Internet
Author: User

Develop a multi-thread high concurrency network library based on the new c ++ 11 standard

Background

After the new c ++ 11 standard is released, the c ++ syntax has been expanded a lot, which is more flexible and efficient than ever before, improving the efficiency of program coding, it saves software developers a lot of time. I have also written about the ACE-Based Network Server framework before, But ACE is a little bloated after all, and the internal object relationships are complicated. It is easy to give people the illusion that trees do not see the forest. Therefore, we plan to use c ++ 11 to develop a concise, efficient, and highly concurrent network library.

Open Source

It took two or three weeks to finally complete the development of the basic structure, the Code is also open source on github, the URL is https://github.com/lichuan/fly welcome you to make suggestions.

Structure

The fly network library is mainly divided into the base module, task module, and net module. Base is a set of basic functions, including logs, id splitters, random numbers, and queues. tasks mainly encapsulate task abstraction and task scheduling and execution functions; net is mainly used to encapsulate the network layer and provide easy-to-use network interfaces for upper-layer users. This is also the core module of the fly library. It implements Parser encapsulation of the Resolution Protocol and monitors the network's Acceptor, poller encapsulation body responsible for network connection I/O functions further encapsulates Server classes used as servers and Client classes used as clients on the basis of Parser, Acceptor, and Poller, these two classes are the core classes of the fly library. Upper-layer applications can directly use Server objects and Client objects to create network servers. The Connection object, as the concept of network Connection, manages its lifecycle through std: shared_ptr. The shared_ptr built-in shared object function greatly simplifies the management of network Connection life periods.

Protocol

The network protocol of the fly library is based on the most common json format in the world, and its parsing relies on the third-party library of rapidjson.

The Protocol composition is as follows:

| Length field | {protocol type: type value, protocol command: command value, other json fields} |

The protocol type and protocol command form a second-level message word, and various protocols can be combined.

Application

The test directory of the fly Library provides a simple routine. The test_server.cpp code is as follows:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *                    _______    _                                     * *                   (  ____   (      |     /|                      *  *                   | (    /  | (     (    / )                      * *                   | (__      | |       (_) /                       * *                   |  __)     | |          /                        * *                   | (        | |        ) (                         * *                   | )        | (____/  | |                         * *                   |/         (_______/  _/                         * *                                                                     * *                                                                     * *     fly is an awesome c++11 network library.                        * *                                                                     * *   @author: lichuan                                                  * *   @qq: 308831759                                                    * *   @email: 308831759@qq.com                                          * *   @github: https://github.com/lichuan/fly                           * *   @date: 2015-06-10 13:34:21                                        * *                                                                     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */#include 
 
  #include 
  
   #include fly/init.hpp#include fly/net/server.hpp#include fly/base/logger.hppusing namespace std::placeholders;class Test_Server : public fly::base::Singleton
   
    {public:    bool allow(std::shared_ptr
    
      connection)    {        return true;    }        void init(std::shared_ptr
     
       connection)    {        std::lock_guard
      
        guard(m_mutex); m_connections[connection->id()] = connection; LOG_INFO(connection count: %u, m_connections.size()); } void dispatch(std::unique_ptr
       
         message) { std::shared_ptr
        
          connection = message->get_connection(); const fly::net::Addr &addr = connection->peer_addr(); LOG_INFO(recv message from %s:%d raw_data: %s, addr.m_host.c_str(), addr.m_port, message->raw_data().c_str()); } void close(std::shared_ptr
         
           connection) { LOG_INFO(close connection from %s:%d, connection->peer_addr().m_host.c_str(), connection->peer_addr().m_port); std::lock_guard
          
            guard(m_mutex); m_connections.erase(connection->id()); LOG_INFO(connection count: %u, m_connections.size()); } void be_closed(std::shared_ptr
           
             connection) { LOG_INFO(connection from %s:%d be closed, connection->peer_addr().m_host.c_str(), connection->peer_addr().m_port); std::lock_guard
            
              guard(m_mutex); m_connections.erase(connection->id()); LOG_INFO(connection count: %u, m_connections.size()); } void main() { //init library fly::init(); //init logger fly::base::Logger::instance()->init(fly::base::DEBUG, server, ./log/); //test tcp server std::unique_ptr
             
               server(new fly::net::Server(fly::net::Addr(127.0.0.1, 8899), std::bind(&Test_Server::allow, this, _1), std::bind(&Test_Server::init, this, _1), std::bind(&Test_Server::dispatch, this, _1), std::bind(&Test_Server::close, this, _1), std::bind(&Test_Server::be_closed, this, _1))); if(server->start()) { LOG_INFO(start server ok!); server->wait(); } else { LOG_ERROR(start server failed); } } private: std::unordered_map
              
               > m_connections; std::mutex m_mutex;};int main(){ Test_Server::instance()->main();}
              
             
            
           
          
         
        
       
      
     
    
   
  
 

 

When the Server Object is constructed, the listener address and callback function are required. When the Server Object start is started, the fly library underlying layer will create the corresponding Poller, Parser, and Acceptor objects, to implement multi-threaded Poller and Parser, You need to input the number of concurrent threads. The callback function is described as follows:

Allow_cb: called when a new connection exists to determine whether the connection registration is allowed.

Init_cb: it is called after the connection object is registered to a certain Poller and Parser for initialization.

Dispatch_cb: it is called to distribute messages when a message arrives.

Close_cb: called when the connection object is closed.

Be_closed_cb: it is called when the peer closes the connection object.

Test_client.cpp uses the Client object to connect to a Server. Similarly, the callback function must be passed in when the Client is constructed. The function is the same as the callback passed in when the Server is constructed.

 

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.