knet 中文版
Linux Build Status | Windows Build Status | Coverity Scan Build Status | License |
---|---|---|---|
A cross platform lock-free and timer-supported(heartbeat etc.) C++11 network library.
- Support Windows, Linux, MacOS, FreeBSD, OpenBSD
- 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.
- CMake 3.1 or higher
- Clang 3.3 or higher (If you build with Clang)
- Visual Studio 2015 or higher(Windows)
- Gcc 4.9 or higher(Linux)
- Xcode 8.0 or higher(MacOS)
below command working on Windows and Linux
# enter source code root directory
cd knet
# generate project
cmake . -B build
# build RELASE
cmake --build build --config Release
# enter build directory(under source code root directory)
cd build
# test
ctest -C Release
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:
producer consumer
┌───────────┐ ┌──────────────┐
│ connector │ ——————————> │ worker │
│ │ socket │ │
│ acceptor │ ——————————> │ async_worker │
└───────────┘ └──────────────┘
- connector connect to server
- acceptor accept connection from client
- worker synchronous processing connections
- async_worker asynchronous processing connections
example provides echo server and echo client.
┌─────────────────────────────┬──────┐
│ total package size(4 bytes) │ data │
└─────────────────────────────┴──────┘
4 bytes package header and data follow.
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:
- Synchronous Echo Serverexamples/server/sync_server.cpp
- Asynchronous Echo Serverexamples/server/async_server.cpp
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:
- Synchronous Echo Clientexamples/client/sync_client.cpp
- Asynchronous Echo Clientexamples/client/async_client.cpp