Browse by type
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
Optional: * clang * CLion * MSYS2 * MinGW * Visual Studio
sudo apt-get install -y binutils-dev uuid-dev libssl-dev
pip3 install gil
git clone https://github.com/chronoxor/CppServer.git
cd CppServer
gil update
cd build
./unix.sh
cd build
./unix.sh
cd build
unix.bat
cd build
mingw.bat
cd build
vs.bat
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 thread with Id 19920
2.1 - Posted in thread with Id 19920
2.2 - Dispatch from thread with Id 19920
2.2.1 - Dispatched in thread with Id 19920
2.3 - Post from thread with Id 19920
1.3.1 - Posted in thread with Id 19920
2.3.1 - Posted in thread with Id 19920
Asio service stopped!
Here comes the example of Asio timer. It can be used to wait for some action in future with providing absolute time or relative time span. Asio timer can be used in synchronous or asynchronous modes.
#include "server/asio/timer.h"
#include "threads/thread.h"
#include <iostream>
class AsioTimer : public CppServer::Asio::Timer
{
public:
using CppServer::Asio::Timer::Timer;
protected:
void onTimer(bool canceled) override
{
std::cout << "Asio timer " << (canceled ? "canceled" : "expired") << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Asio timer caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
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;
// Create a new Asio timer
auto timer = std::make_shared<AsioTimer>(service);
// Setup and synchronously wait for the timer
timer->Setup(CppCommon::UtcTime() + CppCommon::Timespan::seconds(1));
timer->WaitSync();
// Setup and asynchronously wait for the timer
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
// Wait for a while...
CppCommon::Thread::Sleep(2000);
// Setup and asynchronously wait for the timer
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
// Wait for a while...
CppCommon::Thread::Sleep(500);
// Cancel the timer
timer->Cancel();
// Wait for a while...
CppCommon::Thread::Sleep(500);
// 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 starting...Done!
Timer was expired
Timer was canceled
Asio service stopping...Done!
Here comes the example of the TCP chat server. It handles multiple TCP client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
```c++
class ChatSession : public CppServer::Asio::TCPSession { public: using CppServer::Asio::TCPSession::TCPSession;
protected: void onConnected() override { std::cout << "Chat TCP session with Id " << id() << " connected!" << std::endl;
// Send invite message
std::string message("Hello from TCP chat! Please send a message or '!' to disconnect the client!");
SendAsync(message);
}
void onDisconnected() override
{
std::cout << "Chat TCP session with Id " << id() << " disconnected!" << std::endl;
}
void onReceived(const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Multicast message to all connected sessions
server()->Multicast(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
DisconnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat TCP session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class ChatServer : publ
$ claude mcp add CppServer \
-- python -m otcore.mcp_server <graph>