/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Core/Network/Router.cpp
107 строк
4 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "Router.h" #include "ResponseContext.h" #include "Core/Infrastructure/TaskQueue.h" #include "Core/Utils/Compression.h" #include <iostream> namespace Core::Network { Router::Router(Infrastructure::TaskQueue& taskQueue) : m_taskQueue(taskQueue) {} void Router::registerJsonHandler(const std::string& command, JsonHandler handler) { m_jsonHandler[command] = std::move(handler); } void Router::registerFileHandler(const std::string& command, FileHandler handler) { m_fileHandler[command] = std::move(handler); } void Router::routeJson(const std::vector<std::byte>& compressedJson, std::shared_ptr<TcpSession> session) { ResponseContext responseCtx(session); std::weak_ptr<TcpSession> weakSession = session; m_taskQueue.post([compressedJson, responseCtx = std::move(responseCtx), weakSession, this]() mutable { try { auto jsonStr = Utils::decompress(compressedJson); auto json = nlohmann::json::parse(jsonStr); if(!json.contains("command")) { std::cerr << "[Router] JSON without 'command': " << json.dump() << std::endl; return; } std::string cmd = json["command"]; std::cout << cmd << std::endl; auto it = m_jsonHandler.find(cmd); if(it != m_jsonHandler.end()) { if(weakSession.lock()) { it->second(json, responseCtx); } } else { std::cerr << "[Router] No handler for command: " << cmd << std::endl; } } catch(std::exception& ex) { std::cerr << "[Router] JSON processing error: " << ex.what() << std::endl; } }); } void Router::routeFile(const std::vector<std::byte>& compressedPayload, const std::vector<std::byte>& compressedRawData, std::shared_ptr<TcpSession> session) { ResponseContext responseCtx(session); std::weak_ptr<TcpSession> weakSession = session; m_taskQueue.post([compressedPayload, compressedRawData = std::move(compressedRawData), responseCtx = std::move(responseCtx), weakSession, this]() mutable { try { auto jsonStr = Utils::decompress(compressedPayload); auto json = nlohmann::json::parse(jsonStr); if(!json.contains("command")) { std::cerr << "[Router] FILE payload without 'command': " << json.dump() << std::endl; return; } std::vector<std::byte> rawData; { auto decompressed = Utils::decompress(compressedRawData); rawData.assign(reinterpret_cast<const std::byte*>(decompressed.data()), reinterpret_cast<const std::byte*>(decompressed.data() + decompressed.size())); } std::string cmd = json["command"]; auto it = m_fileHandler.find(cmd); if(it != m_fileHandler.end()) { if(weakSession.lock()) { it->second(json, rawData, responseCtx); } } else { std::cerr << "[Router] No file handler for command: " << cmd << std::endl; } } catch(std::exception& ex) { std::cerr << "[Router] FILE processing error: " << ex.what() << std::endl; } }); } } // namespace Core::Network