3#include <spdlog/spdlog.h>
8 : transport_(std::move(transport)) {
9 dispatcher_ = std::make_unique<Dispatcher>();
10 spdlog::info(
"Server initialized with transport");
14 spdlog::info(
"Server starting");
20 spdlog::info(
"Server stopping");
21 running_.store(
false);
25 return running_.load();
28void Server::Listen() {
30 spdlog::error(
"Dispatcher is not set.");
34 spdlog::error(
"Transport is not set.");
39 std::string request = transport_->ReceiveMessage();
40 if (request.empty()) {
43 std::optional<std::string> response = dispatcher_->DispatchRequest(request);
44 if (response.has_value()) {
45 transport_->SendMessage(response.value());
52 dispatcher_->RegisterMethodCall(method, handler);
57 dispatcher_->RegisterNotification(method, handler);
void Start()
Starts the server to handle incoming JSON-RPC requests.
auto IsRunning() const -> bool
Checks if the server is currently running.
void RegisterMethodCall(const std::string &method, const MethodCallHandler &handler)
Registers an RPC method handler with the dispatcher.
void RegisterNotification(const std::string &method, const NotificationHandler &handler)
Registers an RPC notification handler with the dispatcher.
Server(std::unique_ptr< transport::Transport > transport)
Constructs a Server with the specified transport.
void Stop()
Stops the server from handling requests.
std::function< void(const std::optional< nlohmann::json > &)> NotificationHandler
Type alias for notification handler functions.
std::function< nlohmann::json(const std::optional< nlohmann::json > &)> MethodCallHandler
Type alias for method call handler functions.