Initial commit

This commit is contained in:
sladro 2026-01-29 16:25:14 +08:00
parent 1063d3eb5c
commit 97a311fdc5
5 changed files with 32 additions and 33 deletions

View File

@ -84,17 +84,35 @@ find_package(Boost 1.69.0 REQUIRED COMPONENTS
# libcurl # libcurl
find_package(CURL REQUIRED) find_package(CURL REQUIRED)
# JSON 使 fallback FetchContentCentOS 7 nlohmann-json-devel # JSON 使线 FetchContent
#
# -DLOCAL_NLOHMANN_JSON_SOURCE_DIR=/path/to/json (包含 CMakeLists.txt)
# nlohmann_json::nlohmann_json
set(LOCAL_NLOHMANN_JSON_SOURCE_DIR "" CACHE PATH "Local nlohmann/json source dir (offline fallback). Should contain CMakeLists.txt")
find_package(nlohmann_json QUIET) find_package(nlohmann_json QUIET)
if(NOT TARGET nlohmann_json::nlohmann_json) if(NOT TARGET nlohmann_json::nlohmann_json)
FetchContent_Declare( if(LOCAL_NLOHMANN_JSON_SOURCE_DIR)
nlohmann_json if(EXISTS "${LOCAL_NLOHMANN_JSON_SOURCE_DIR}/CMakeLists.txt")
GIT_REPOSITORY https://github.com/nlohmann/json.git message(STATUS "Using local nlohmann/json from: ${LOCAL_NLOHMANN_JSON_SOURCE_DIR}")
GIT_TAG v3.11.3 add_subdirectory("${LOCAL_NLOHMANN_JSON_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/_deps/nlohmann_json-local")
GIT_SHALLOW TRUE else()
) message(FATAL_ERROR "LOCAL_NLOHMANN_JSON_SOURCE_DIR is set but CMakeLists.txt not found under: ${LOCAL_NLOHMANN_JSON_SOURCE_DIR}")
FetchContent_MakeAvailable(nlohmann_json) endif()
else()
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(nlohmann_json)
endif()
endif()
if(NOT TARGET nlohmann_json::nlohmann_json)
message(FATAL_ERROR "nlohmann_json::nlohmann_json not available. Install a system package providing it or set -DLOCAL_NLOHMANN_JSON_SOURCE_DIR=<path-to-nlohmann-json-source>.")
endif() endif()
# #

1
json Submodule

@ -0,0 +1 @@
Subproject commit 9cca280a4d0ccf0c08f47a99aa71d1b0e52f8d03

View File

@ -4,7 +4,6 @@
#include <boost/asio/ip/tcp.hpp> #include <boost/asio/ip/tcp.hpp>
#include <boost/asio/dispatch.hpp> #include <boost/asio/dispatch.hpp>
#include <algorithm> #include <algorithm>
#include <future>
#include "network/WebSocketServer.h" #include "network/WebSocketServer.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -72,16 +71,10 @@ namespace network {
Logger::debug("广播消息完成: ", successCount, " 个成功, ", failCount, " 个失败, 当前共 ", sessions_.size(), " 个连接"); Logger::debug("广播消息完成: ", successCount, " 个成功, ", failCount, " 个失败, 当前共 ", sessions_.size(), " 个连接");
}; };
// 保持原语义broadcast 返回前完成实际发送,但确保所有 websocket 操作在 io_context 线程执行 // 异步投递:避免 broadcast 阻塞调用方线程(例如碰撞处理主循环)
auto done = std::make_shared<std::promise<void>>(); boost::asio::dispatch(strand_, [do_broadcast]() mutable {
auto fut = done->get_future();
// dispatch: 若已在 strand 线程内会直接执行,避免等待导致自锁;否则投递到 io_context 线程执行
boost::asio::dispatch(strand_, [do_broadcast, done]() mutable {
do_broadcast(); do_broadcast();
done->set_value();
}); });
fut.wait();
} }
void WebSocketServer::handleAccept() { void WebSocketServer::handleAccept() {

View File

@ -4,27 +4,15 @@
#include <mutex> #include <mutex>
#include "config/SystemConfig.h" #include "config/SystemConfig.h"
// 定义静态成员变量
ControllableVehicles* ControllableVehicles::instance_ = nullptr;
ControllableVehicles& ControllableVehicles::getInstance() { ControllableVehicles& ControllableVehicles::getInstance() {
if (instance_ == nullptr) { static ControllableVehicles instance;
instance_ = new ControllableVehicles(); return instance;
}
return *instance_;
} }
ControllableVehicles::ControllableVehicles() ControllableVehicles::ControllableVehicles()
: http_client_(std::make_unique<HTTPClient>()) { : http_client_(std::make_unique<HTTPClient>()) {
} }
ControllableVehicles::~ControllableVehicles() {
if (instance_ != nullptr) {
delete instance_;
instance_ = nullptr;
}
}
void ControllableVehicles::updateRegistry(const std::vector<VehicleRegistryEntry>& entries) { void ControllableVehicles::updateRegistry(const std::vector<VehicleRegistryEntry>& entries) {
if (entries.empty()) { if (entries.empty()) {
return; return;

View File

@ -16,11 +16,10 @@ struct VehicleRegistryEntry {
class ControllableVehicles { class ControllableVehicles {
private: private:
static ControllableVehicles* instance_;
ControllableVehicles(); ControllableVehicles();
ControllableVehicles(const ControllableVehicles&) = delete; ControllableVehicles(const ControllableVehicles&) = delete;
ControllableVehicles& operator=(const ControllableVehicles&) = delete; ControllableVehicles& operator=(const ControllableVehicles&) = delete;
~ControllableVehicles(); ~ControllableVehicles() = default;
// 运行时车辆类型注册表(由前端接口增量更新) // 运行时车辆类型注册表(由前端接口增量更新)
mutable std::shared_mutex mutex_; mutable std::shared_mutex mutex_;