CppServer
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Install / Use
/learn @chronoxor/CppServerREADME
CppServer
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution.
Has integration with high-level message protocol based on Fast Binary Encoding
Contents
- Features
- Requirements
- How to build?
- Examples
- Example: Asio service
- Example: Asio timer
- Example: TCP chat server
- Example: TCP chat client
- Example: SSL chat server
- Example: SSL chat client
- Example: UDP echo server
- Example: UDP echo client
- Example: UDP multicast server
- Example: UDP multicast client
- Example: Simple protocol
- Example: Simple protocol server
- Example: Simple protocol client
- Example: HTTP server
- Example: HTTP client
- Example: HTTPS server
- Example: HTTPS client
- Example: WebSocket chat server
- Example: WebSocket chat client
- Example: WebSocket secure chat server
- Example: WebSocket secure chat client
- Performance
- OpenSSL certificates
Features
- Cross platform (Linux, MacOS, Windows)
- Asynchronous communication
- Supported CPU scalability designs: IO service per thread, thread pool
- Supported transport protocols: TCP, SSL, UDP, UDP multicast
- Supported Web protocols: HTTP, HTTPS, WebSocket, WebSocket secure
- Supported Swagger OpenAPI iterative documentation
- Supported message protocol based on Fast Binary Encoding
Requirements
Optional:
How to build?
Linux: install required packages
sudo apt-get install -y binutils-dev uuid-dev libssl-dev
Install gil (git links) tool
pip3 install gil
Setup repository
git clone https://github.com/chronoxor/CppServer.git
cd CppServer
gil update
Linux
cd build
./unix.sh
MacOS
cd build
./unix.sh
Windows (MSYS2)
cd build
unix.bat
Windows (MinGW)
cd build
mingw.bat
Windows (Visual Studio)
cd build
vs.bat
Examples
Example: Asio service
Asio service is used to host all clients/servers based on Asio C++ library. It is implemented based on Asio C++ Library and use a separate thread to perform all asynchronous IO operations and communications.
The common usecase is to instantiate one Asio service, start the service and attach TCP/UDP/WebSocket servers or/and clients to it. One Asio service can handle several servers and clients asynchronously at the same time in one I/O thread. If you want to scale your servers or clients it is possible to create and use more than one Asio services to handle your servers/clients in balance.
Also it is possible to dispatch or post your custom handler into I/O thread. Dispatch will execute the handler immediately if the current thread is I/O one. Otherwise the handler will be enqueued to the I/O queue. In opposite the post method will always enqueue the handler into the I/O queue.
Here comes an example of using custom Asio service with dispatch/post methods:
#include "server/asio/service.h"
#include "threads/thread.h"
#include <iostream>
int main(int argc, char** argv)
{
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Dispatch
std::cout << "1 - Dispatch from the main thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "1.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
std::cout << "1.2 - Dispatch from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "1.2.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
std::cout << "1.3 - Post from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "1.3.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
});
// Post
std::cout << "2 - Post from the main thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "2.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
std::cout << "2.2 - Dispatch from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "2.2.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
std::cout << "2.3 - Post from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "2.3.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
});
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Output of the above example is the following:
Asio service started!
1 - Dispatch from the main thread with Id 16744
2 - Post from the main thread with Id 16744
1.1 - Dispatched in thread with Id 19920
1.2 - Dispatch from thread with Id 19920
1.2.1 - Dispatched in thread with Id 19920
1.3 - Post from
