137 lines
4.3 KiB
C++
137 lines
4.3 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 <chrono>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
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);
|
|
|
|
// The client's effective network address: the X-Echoion-Client-Ip
|
|
// header set by the web server's /rpc bridge (trusted only from
|
|
// loopback peers), else the socket's remote address. Used for
|
|
// address-based autologin.
|
|
[[nodiscard]] std::optional<std::string>
|
|
getClientAddress(uint64_t clientId) const;
|
|
|
|
// Server introspection (for GetServerInfo / GetAuditLog).
|
|
struct AuditEntry {
|
|
uint64_t clientId = 0;
|
|
int method = 0;
|
|
std::chrono::system_clock::time_point timestamp;
|
|
};
|
|
[[nodiscard]] std::vector<AuditEntry> getAuditLog() const;
|
|
[[nodiscard]] size_t getClientCount() const;
|
|
[[nodiscard]] std::chrono::seconds getUptime() const;
|
|
|
|
// 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 doAccept();
|
|
void onAccept(asio::ip::tcp::socket socket);
|
|
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_;
|
|
// clientId -> effective network address (see getClientAddress).
|
|
std::unordered_map<uint64_t, std::string> clientAddresses_;
|
|
mutable 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_;
|
|
|
|
// Audit ring + uptime tracking
|
|
mutable std::mutex auditMutex_;
|
|
std::deque<AuditEntry> auditLog_;
|
|
static constexpr size_t MAX_AUDIT_ENTRIES = 500;
|
|
std::chrono::steady_clock::time_point startTime_{
|
|
std::chrono::steady_clock::now()};
|
|
|
|
// Command handlers
|
|
std::unordered_map<std::string, CommandHandler> handlers_;
|
|
std::mutex handlersMutex_;
|
|
};
|
|
|
|
} // namespace Echoion::Core
|