1 Star 1 Fork 1

苏小逝 / CppNet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

cppnet logo

Build Status Licenses

查看更多细节 Wiki

简介

CppNet是一个封装在Tcp协议上的Proactor模式multi-thread C++11网络库,目前支持在windows和linux上编译使用。
简单:对外只导出了少量的调用接口,所有的网络IO都封装为异步回调的形式,且接口声明都尽可能的像是调用系统socket API,对客户端而言,只多了一个新增的buffer对象类型。
快速:分别采用epoll和IOCP做底层事件驱动,其中epoll多线程惊群通过端口复用交由Linux内核处理。参照SGI STL和Nginx实现了内存池,每个建立连接的socket都独享一个内存池对象,所有从内存池中申请的内存都由智能指针管理。
明了:结构上分为三层:事件驱动层,会话管理层,接口层,各层之间通过回调向上通知。各个模块之间职责分工明确,上帝的事儿归上帝管,凯撒的事儿归凯撒管。最大的类不超过500行代码。

接口

所有的接口文件都在 include 中,其中关于库初始化和定时器的接口定义在 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);
    };

因为所有的网络IO接口都被定义为回调通知的模式,所以初始化库的时候需要设置各个调用的回调函数。
这里通过设置回调而不是提供虚函数继承的方式,是希望尽量的简单,减少类的继承关系,增加回调的灵活性,你可以将回调设置为任意一个函数。
关于网络IO的接口定义在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();
    };

接口的作用通过声明和注释即可明了。需要关注的是接口返回的错误码,与回调函数的声明一起定义在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.
    };

每个接口在采取下一步动作时应先检测一下当前返回的错误码,以获知当前连接是否正常。

示例

所有示例都在 test 目录下:
simple是一个简单的使用示例。
echo实现了10000连接量的echo的测试程序。
http参照muduo实现了一个简单的http服务器。
sendfile是一个文件发送和接收示例。
pingpong是一个pingpong测试程序。
rpc是一个简单的rpc示例。

效率

目前只用ab做了http echo测试,与muduo做了对比,执行的命令为:ab -kc[1-2000] -n100000 http://127.0.0.1:8000/hello.

mudo vs cppnet

编译(Windows)

你可以使用vs2017来编译CppNet库和示例。

编译(Linux)

只需要在源码目录下执行make即可编译CppNet库和示例。
其他示例则需要在编译完静态库之后,分别在本地目录里执行make。

$ make -j4

协议

CppNet使用BSD 3-Clause使用条款,详情请看https://opensource.org/licenses/BSD-3-Clause

<p align="left"><img width="500" src="./doc/image/logo.png" alt="cppnet logo"></p> <p align="left"> <a href="https://travis-ci.org/caozhiyi/CppNet"><img src="https://travis-ci.org/caozhiyi/CppNet.svg?branch=master" alt="Build Status"></a> <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-bsd-orange.svg" alt="Licenses"></a> </p> See [chinese](/README_cn.md) See the details in chinese [Wiki](https://github.com/caozhiyi/CppNet/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](/include). The interface definitions for library initialization and timer are in [CppNet](/include/CppNet.h): ```c++ 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](/include/Socket.h): ```c++ 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](/include/CppDefine.h): ```c++ 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](/test): [simple](/test/simple): A most simple example. [echo](/test/echo): A test program of echo with 10000 connection. [http](/test/http): A simple HTTP server is implemented with reference to muduo. [sendfile](/test/sendfile): An example of sending and receiving files. [pingpong](/test/pingpong): A pingpong test program. [rpc](/test/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. <p align="left"><img width="896" src="./doc/image/muduo_vs_cppnet.png" alt="mudo vs cppnet"></p> ## 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](https://opensource.org/licenses/BSD-3-Clause).

简介

暂无描述 展开 收起
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kimbeaur/CppNet.git
git@gitee.com:kimbeaur/CppNet.git
kimbeaur
CppNet
CppNet
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891