项目作者: KibaAmor

项目描述 :
A cross platform lock-free and timer-supported(heartbeat etc.) C++11 network library.一个跨平台的无锁且支持定时器(如:心跳)的C++11网络库。
高级语言: C++
项目地址: git://github.com/KibaAmor/knet.git


knet

中文版

A cross platform lock-free and timer-supported C++11 network library.

AppVeyor
License
Standard
FOSSA Status

Table of Contents


Highlights

  • Support Windows, Linux, MacOS, FreeBSD
  • Support Synchronous and Asynchronous processing connections
  • Support network package defragment
  • Less code and no third party dependency
  • Lockfree, connection work on same fixed thread
  • Timer support, such as: hearbeat check etc.

Environment

  • CMake 3.15 or higher
  • Clang 3.8 or higher (If you build with Clang)
  • Visual Studio 2015 or higher(Windows)
  • G++ 5 or higher(Linux)
  • Xcode 9.4 or higher(MacOS)

How To Use

vcpkg

  1. vcpkg install knet

Build from source

  1. # clone source code
  2. git clone https://github.com/KibaAmor/knet.git # or https://gitee.com/kibaamor/knet.git
  3. # enter source code root directory
  4. cd knet
  5. # generate project
  6. cmake . -B build
  7. # build Release
  8. cmake --build build --config Release
  9. # run tests
  10. (cd build && ctest --output-on-failure)
  11. # install
  12. (cd build && sudo make install)

Core Concept

the core concept of knet is: produce socket-consume socket.

In fact, both connecting to server and accepting connection from client are creating readable and writable socket. After the socket is created, both the client and the server do the same to send and accept messages. This process can then be seen as a process of generating sockets and consuming sockets. Here’s the diagram:

  1. producer consumer
  2. ┌───────────┐ ┌──────────────┐
  3. connector ——————————> worker
  4. socket
  5. acceptor ——————————> async_worker
  6. └───────────┘ └──────────────┘

Examples

Echo Server and Client

example provides echo server and echo client.

Protocol

  1. ┌────────────────────┬──────┐
  2. data size(4 bytes) data
  3. └───-────────────────┴──────┘

4 bytes package header and data follow.

Echo Server

The echo server sends the received data back to the client intact.

A timer is also set to check whether a client message is received within the specified time, and the connection to the client is closed if the client message is not received within the specified time.

The server provides two types:

Echo Client

The client actively connects to the server after starting (and automatically reconnects the server if the connection is not available), and when the connection is successful, it actively sends an incomplete network package to the server.

When the network package returned by the server is received, the data envelope is verified and disconnected if it fails.

The client provides two types:

License

FOSSA Status