currynihao / CppNet

Cross platform network library with C++11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cppnet logo

Build Status Licenses

See chinese
See the details in chinese Wiki

Introduction

CppNet is a proactor mode and multithreaded network with C++11 on tcp.
Simple: only export a little interfaces, all net ios insterface are asynchronous callbacks, as much as possible like calling the socket API of the system. There is only one additional buffer object type for the client.
Fast: epoll and IOCP are used, in which epoll multithreaded threads are handled by the Linux kernel through port reuse. Each socket has a single memory pool object. All memory requested from the memory pool is managed by an intelligent pointer.
Clear:three layers: event-driven layer, session management layer and interface layer, upward notification through callbacks between layers. Clear division of responsibilities among modules, pay to Caesar what belongs to Caesar and God what belongs to God. The largest class does not exceed 500 lines of code.

Interface

All the interface files are in include. The interface definitions for library initialization and timer are in CppNet:

    class CCppNet {
    public:
        // common
        // init cppnet library.
        // thread_num : the number of running threads.
        void Init(int32_t thread_num);

        // thread join
        void Join();

        // must set callback before listen
        void SetReadCallback(const read_call_back& func);
        void SetWriteCallback(const write_call_back& func);
        void SetDisconnectionCallback(const connection_call_back& func);

        //timer
        uint64_t SetTimer(int32_t interval, const timer_call_back& func, void* param = nullptr, bool always = false);
        void RemoveTimer(uint64_t timer_id);

        //server
        void SetAcceptCallback(const connection_call_back& func);
        bool ListenAndAccept(const std::string& ip, int16_t port);

        //client
        void SetConnectionCallback(const connection_call_back& func);

#ifndef __linux__
        // sync connection. 
        bool Connection(const std::string& ip, int16_t port, const char* buf, int32_t buf_len);
#endif
        bool Connection(const std::string& ip, int16_t port);
    };

Since all network IO interfaces are defined as callback notification modes, callback functions for each call need to be set when initializing the library.
By setting callbacks instead of providing virtual function inheritance, we hope to be as simple as possible, reduce the inheritance relationship of classes, and increase the flexibility of callbacks. You can set callbacks to any function.
The interface definition for network IO are in Socket:

    class CNSocket {
    public:
        // get socket ip and adress
        int16_t GetAddress(std::string& ip, uint16_t& port);
        // post sync write event.
        int16_t Write(const char* src, int32_t len);
        // close the connect
        int16_t Close();
    };

The function of the interface is evident through declarations and annotations. Attention should be paid to the error code returned by the interface, defined in CppDefine:

    enum CPPNET_ERROR_CODE {
        CEC_SUCCESS                = 1,    // success.
        CEC_TIMEOUT                = 2,    // the event time out call back.
        CEC_CLOSED                 = 3,    // remote close the socket.
        CEC_INVALID_HANDLE         = 4,    // invalid cppnet handle, can't find in socket manager.
        CEC_FAILED                 = 5,    // call function failed.
        CEC_CONNECT_BREAK          = 6,    // connect break.
        CEC_CONNECT_REFUSE         = 7     // remote refuse connect or server not exist.
    };

When each interface takes the next action, you should first check the error code returned at present to know whether the current connection is normal.

Example

All simples are in test:
simple: A most simple example.
echo: A test program of echo with 10000 connection.
http: A simple HTTP server is implemented with reference to muduo.
sendfile: An example of sending and receiving files.
pingpong: A pingpong test program.
rpc: A interesting rpc program.

Efficiency

Only use apache ab test HTTP echo,comparison with Muduo. The command executed is:ab -kc[1-2000] -n100000 http://127.0.0.1:8000/hello.

mudo vs cppnet

Build(Windows)

You can compile CppNet library and example with vs2017.

Build(Linux)

The CppNet library and examples can be compiled simply by executing make in the source directory.
Other examples need to make in local directories after compiling static libraries.

$ make -j4

Licenses

This program is under the terms of the BSD 3-Clause License. See https://opensource.org/licenses/BSD-3-Clause.

About

Cross platform network library with C++11


Languages

Language:C++ 95.7%Language:CMake 2.3%Language:Makefile 1.5%Language:C 0.6%Language:Shell 0.0%