Migrate core infrastructure to VoidLibrary
This commit is contained in:
1 parent
53801f2b53
commit
2bdb63766d
65 files changed
+16109
-12011
No files matched your search
@@ -1,81 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api.hpp>
|
||||
#include <PluginTypes.hpp>
|
||||
#include <RpcTypes.hpp>
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Echoion::Core {
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace beast = boost::beast;
|
||||
namespace websocket = beast::websocket;
|
||||
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)>;
|
||||
class RpcServer : public std::enable_shared_from_this<RpcServer>
|
||||
{
|
||||
public:
|
||||
|
||||
RpcServer();
|
||||
~RpcServer();
|
||||
using CommandHandler = std::function<Api::Result<std::string>(
|
||||
const std::string& params, uint64_t clientId)>;
|
||||
|
||||
// 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_; }
|
||||
RpcServer();
|
||||
~RpcServer();
|
||||
|
||||
// Register command handlers
|
||||
void registerHandler(const std::string& method, CommandHandler handler);
|
||||
void unregisterHandler(const std::string& method);
|
||||
// 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_; }
|
||||
|
||||
// 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);
|
||||
// Register command handlers
|
||||
void registerHandler(const std::string& method,
|
||||
CommandHandler handler);
|
||||
void unregisterHandler(const std::string& method);
|
||||
|
||||
// Broadcast events to all connected clients
|
||||
void broadcastEvent(const std::string& event, const std::string& data);
|
||||
// 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);
|
||||
|
||||
// Send event to specific client
|
||||
void sendEvent(uint64_t clientId, const std::string& event, const std::string& data);
|
||||
// Broadcast events to all connected clients
|
||||
void broadcastEvent(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);
|
||||
// Send event to specific client
|
||||
void sendEvent(uint64_t clientId, const std::string& event,
|
||||
const std::string& data);
|
||||
|
||||
asio::io_context ioContext_;
|
||||
std::optional<asio::ip::tcp::acceptor> acceptor_;
|
||||
std::thread serverThread_;
|
||||
std::atomic<bool> running_{false};
|
||||
uint16_t port_ = 8765;
|
||||
private:
|
||||
|
||||
// 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};
|
||||
|
||||
// Client threads (for proper cleanup)
|
||||
std::vector<std::thread> clientThreads_;
|
||||
std::mutex clientThreadsMutex_;
|
||||
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);
|
||||
|
||||
// Authenticated clients
|
||||
std::set<uint64_t> authenticatedClients_;
|
||||
std::mutex authMutex_;
|
||||
std::string authToken_;
|
||||
asio::io_context ioContext_;
|
||||
std::optional<asio::ip::tcp::acceptor> acceptor_;
|
||||
std::thread serverThread_;
|
||||
std::atomic<bool> running_{false};
|
||||
uint16_t port_ = 8765;
|
||||
|
||||
// Command handlers
|
||||
std::unordered_map<std::string, CommandHandler> handlers_;
|
||||
std::mutex handlersMutex_;
|
||||
};
|
||||
// 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};
|
||||
|
||||
// Client threads (for proper cleanup)
|
||||
std::vector<std::thread> clientThreads_;
|
||||
std::mutex clientThreadsMutex_;
|
||||
|
||||
// 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
|
||||
Reference in new issue
Block a user