Files
EchoionCore/include/echoion/core/RpcServer.hpp
T
faumarayandClaude Opus 4.8 3af9aecbec Phase C: run RPC per-client handlers on a Void ThreadPool
Replace the one-detached-std::thread-per-client model and its manual
bookkeeping (the clientThreads_ vector, finished-thread sweep, and
detach-after-timeout shutdown) with a Void::Concurrency::ThreadPool:

- start() builds clientPool_ (CLIENT_POOL_SIZE workers); the accept loop
  submits each accepted connection's handleClient + cleanup as a task.
- stop() closes the client sockets (so each handler's read() returns) then
  resets the pool, whose destructor drains and joins every handler.

The accept loop stays on its own dedicated thread — a long-lived listen
loop is not pool work, and its cancellable/timeout-guarded shutdown is
unchanged. CLIENT_POOL_SIZE (64) caps concurrent long-lived client handlers
far above any realistic count for this bot's RPC surface.

Build green. Runtime behavior (live RPC sessions) is unverified here.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 13:26:16 +03:00

105 lines
3.1 KiB
C++

#pragma once
#include <PluginTypes.hpp>
#include <RpcTypes.hpp>
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
#include <unordered_map>
namespace Echoion::Core
{
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace websocket = beast::websocket;
class RpcServer : public std::enable_shared_from_this<RpcServer>
{
public:
using CommandHandler = std::function<Api::Result<std::string>(
const std::string& params, uint64_t clientId)>;
RpcServer();
~RpcServer();
// Initialize and start the server
Api::Result<void> start(uint16_t port = 8765);
void stop();
[[nodiscard]] bool isRunning() const { return running_; }
[[nodiscard]] uint16_t getPort() const { return port_; }
// Register command handlers
void registerHandler(const std::string& method,
CommandHandler handler);
void unregisterHandler(const std::string& method);
// Authentication
void setAuthToken(const std::string& token);
[[nodiscard]] bool authenticate(const std::string& token);
void addAuthenticatedClient(uint64_t clientId);
void removeAuthenticatedClient(uint64_t clientId);
[[nodiscard]] bool isClientAuthenticated(uint64_t clientId);
// Broadcast events to all connected clients
void broadcastEvent(const std::string& event,
const std::string& data);
// Send event to specific client
void sendEvent(uint64_t clientId, const std::string& event,
const std::string& data);
private:
void acceptLoop();
void handleClient(
std::shared_ptr<websocket::stream<asio::ip::tcp::socket>> ws);
void processMessage(uint64_t clientId, const std::string& message);
void sendResponse(uint64_t clientId,
const Api::RpcResponse& response);
void sendNotification(uint64_t clientId,
const Api::RpcNotification& notification);
asio::io_context ioContext_;
std::optional<asio::ip::tcp::acceptor> acceptor_;
std::thread serverThread_;
std::atomic<bool> running_{false};
uint16_t port_ = 8765;
// Connected clients
std::unordered_map<
uint64_t,
std::shared_ptr<websocket::stream<asio::ip::tcp::socket>>>
clients_;
std::mutex clientsMutex_;
std::atomic<uint64_t> nextClientId_{1};
// Per-client handlers run on a Void worker pool instead of one
// detached std::thread each; ~ThreadPool drains and joins them on
// stop. Sized well above any realistic concurrent-client count
// (each long-lived handler holds a worker for the connection's
// lifetime).
static constexpr std::size_t CLIENT_POOL_SIZE = 64;
std::unique_ptr<Void::Concurrency::ThreadPool> clientPool_;
// Authenticated clients
std::set<uint64_t> authenticatedClients_;
std::mutex authMutex_;
std::string authToken_;
// Command handlers
std::unordered_map<std::string, CommandHandler> handlers_;
std::mutex handlersMutex_;
};
} // namespace Echoion::Core