Development of a network library supporting multithreading and high concurrency based on the new C++11 standard

Source: Internet
Author: User

Background

The new c++11 standard, the C + + syntax has been a lot of expansion, more flexible and efficient than ever, improve the efficiency of program coding, for software developers to save a lot of time. Before I also wrote the network server framework based on ACE, but Ace is a bit bloated, internal object relationship is complex, easy to cause trees trees illusion. So we are going to use c++11 to develop a relatively concise, efficient, high concurrency support network library.

Open source

spent two or three weeks, finally the basic structure of the development completed, code also open source on GitHub, the site is https://github.com/lichuan/fly welcome you to make suggestions.

Structure

Fly Network library is mainly divided into Base module, Task module, net module. Base is mainly some of the most basic functions of the collection, including logs, ID allocator, random number, queue, etc. task mainly encapsulates the task abstraction and task scheduling execution functions, net is mainly to achieve the network level of encapsulation, for the upper users to provide a simple and easy to use network interface, which is the Fly Library core module, It realizes the parser encapsulation of the parsing protocol, and is responsible for the acceptor of the network connection IO function, the Poller package, in parser, acceptor, And Poller also further encapsulate the server classes used as servers and the client classes used as clients, these two classes are the core classes of the fly library, and upper-level applications can directly use the server object and client object to establish a network server. As the connection object of the concept of network connection is to manage its life cycle through std::shared_ptr, shared_ptr the function of built-in shared object greatly simplifies the management of the network connection life time.

Agreement

The network protocol of the Fly Library is based on the most common JSON format in the world, and its parsing relies on the Rapidson third-party library to complete.

The agreement consists of the following:

| Length Field | {protocol type: Type value, protocol command: command value, other JSON field}|

The Protocol type and protocol commands form a level two message word, which allows you to assemble the protocols of the various conventions.

Application

The test catalog of the Fly Library provides a simple routine. Where the Test_server.cpp code is as follows:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *                    _______    _                      * *                   (  ____ \  ( \     |\     /| *  *                   | (    \/  | (     ( \   / )                      * *                   |      (__      | |  \ (_) /                       * *                   |       __)     | | \   /                        * *                   |        (        | |        ) (                         * *                   | ) |                         (____/\  | |                                                                     * *                   |/         (_______/  \_/                         * * * * * * Fly is an awesome c+                        +11 Network Library.                                          * * * * @author: Lichuan        * * @qq: 308831759 * * @email: [email protected] * * @github: Https://github.com/lichuan/fly * * @date: 2                                                                     015-06-10 13:34:21 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include ordered_map> #include "fly/init.hpp" #include "fly/net/server.hpp" #include "fly/base/logger.hpp" using namespace std::p laceholders;class test_server:public fly::base::singleton<test_server>{public:bool Allow (std::shared_    Ptr<fly::net::connection> Connection) {return true; } void Init (std::shared_ptr<fly::net::connection> Connection) {std::lock_guard<std::mutex> g        Uard (M_mutex);        M_connections[connection->id ()] = connection; Log_info("Connection count:%u", m_connections.size ()); } void Dispatch (Std::unique_ptr<fly::net::message> Message) {std::shared_ptr<fly::net::connecti        on> connection = Message->get_connection ();        Const FLY::NET::ADDR &AMP;ADDR = CONNECTION-&GT;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<fly::net::connection> Connection) {log_info ("Close Connection from%s        :%d ", connection->peer_addr (). M_HOST.C_STR (), Connection->peer_addr (). M_port);        Std::lock_guard<std::mutex> Guard (M_mutex);        M_connections.erase (Connection->id ());    Log_info ("Connection count:%u", m_connections.size ()); } void Be_closed (Std::shared_ptr<fly::net::connection> Connection) {log_info ("Connection from%s:% D be closed ", connection->peer_addr (). M_HOST.C_STR (), Connection->peer_addr (). M_port);        Std::lock_guard<std::mutex> 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::D ebug, "Server", "./log/"); Test TCP Server Std::unique_ptr<fly::net::server> server (new Fly::net::server (Fly::net::addr ("127.0.0.1", 8                                                                      899), Std::bind (&test_server::allow, this, _1),                                                                      Std::bind (&test_server::init, this, _1),                                                                      Std::bind (&test_server::d ispatch, 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<uint64, std::shared_ptr<fly::net::connection>> m_connections; Std::mutex M_mutex;}; int main () {test_server::instance ()->main ();}

When the server object is constructed, the incoming listener address and callback function are required, and when the server object start starts, the Poller, Parser, and acceptor objects are created at the bottom of the fly Library. If you want to implement multithreaded Poller and parser, you need to pass in the number of concurrent threads, the callback function is described as follows:

ALLOW_CB: Called when a new connection arrives, to determine whether the connection is allowed to be registered.

INIT_CB: Called when the Connection object is registered with a poller and parser, and is initialized.

DISPATCH_CB: When a message arrives, it is called for message dispatch.

CLOSE_CB: Called when the Connection object is actively closed.

BE_CLOSED_CB: Called when a peer close connection object is detected.

Test_client.cpp primarily uses client objects to connect to a server, and the client constructs a callback function that is similar to the callback that is passed in when the server constructs.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Development of a network library supporting multithreading and high concurrency based on the new C++11 standard

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.