Initial commit
This commit is contained in:
parent
1063d3eb5c
commit
97a311fdc5
@ -84,17 +84,35 @@ find_package(Boost 1.69.0 REQUIRED COMPONENTS
|
||||
# 添加 libcurl
|
||||
find_package(CURL REQUIRED)
|
||||
|
||||
# JSON 库:优先使用系统包;不可用时 fallback 到 FetchContent(CentOS 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)
|
||||
|
||||
if(NOT TARGET nlohmann_json::nlohmann_json)
|
||||
FetchContent_Declare(
|
||||
nlohmann_json
|
||||
GIT_REPOSITORY https://github.com/nlohmann/json.git
|
||||
GIT_TAG v3.11.3
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(nlohmann_json)
|
||||
if(LOCAL_NLOHMANN_JSON_SOURCE_DIR)
|
||||
if(EXISTS "${LOCAL_NLOHMANN_JSON_SOURCE_DIR}/CMakeLists.txt")
|
||||
message(STATUS "Using local nlohmann/json from: ${LOCAL_NLOHMANN_JSON_SOURCE_DIR}")
|
||||
add_subdirectory("${LOCAL_NLOHMANN_JSON_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/_deps/nlohmann_json-local")
|
||||
else()
|
||||
message(FATAL_ERROR "LOCAL_NLOHMANN_JSON_SOURCE_DIR is set but CMakeLists.txt not found under: ${LOCAL_NLOHMANN_JSON_SOURCE_DIR}")
|
||||
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()
|
||||
|
||||
# 源文件列表
|
||||
|
||||
1
json
Submodule
1
json
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 9cca280a4d0ccf0c08f47a99aa71d1b0e52f8d03
|
||||
@ -4,7 +4,6 @@
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <algorithm>
|
||||
#include <future>
|
||||
#include "network/WebSocketServer.h"
|
||||
#include "utils/Logger.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
@ -72,16 +71,10 @@ namespace network {
|
||||
Logger::debug("广播消息完成: ", successCount, " 个成功, ", failCount, " 个失败, 当前共 ", sessions_.size(), " 个连接");
|
||||
};
|
||||
|
||||
// 保持原语义:broadcast 返回前完成实际发送,但确保所有 websocket 操作在 io_context 线程执行
|
||||
auto done = std::make_shared<std::promise<void>>();
|
||||
auto fut = done->get_future();
|
||||
|
||||
// dispatch: 若已在 strand 线程内会直接执行,避免等待导致自锁;否则投递到 io_context 线程执行
|
||||
boost::asio::dispatch(strand_, [do_broadcast, done]() mutable {
|
||||
// 异步投递:避免 broadcast 阻塞调用方线程(例如碰撞处理主循环)
|
||||
boost::asio::dispatch(strand_, [do_broadcast]() mutable {
|
||||
do_broadcast();
|
||||
done->set_value();
|
||||
});
|
||||
fut.wait();
|
||||
}
|
||||
|
||||
void WebSocketServer::handleAccept() {
|
||||
|
||||
@ -4,27 +4,15 @@
|
||||
#include <mutex>
|
||||
#include "config/SystemConfig.h"
|
||||
|
||||
// 定义静态成员变量
|
||||
ControllableVehicles* ControllableVehicles::instance_ = nullptr;
|
||||
|
||||
ControllableVehicles& ControllableVehicles::getInstance() {
|
||||
if (instance_ == nullptr) {
|
||||
instance_ = new ControllableVehicles();
|
||||
}
|
||||
return *instance_;
|
||||
static ControllableVehicles instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
ControllableVehicles::ControllableVehicles()
|
||||
: http_client_(std::make_unique<HTTPClient>()) {
|
||||
}
|
||||
|
||||
ControllableVehicles::~ControllableVehicles() {
|
||||
if (instance_ != nullptr) {
|
||||
delete instance_;
|
||||
instance_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void ControllableVehicles::updateRegistry(const std::vector<VehicleRegistryEntry>& entries) {
|
||||
if (entries.empty()) {
|
||||
return;
|
||||
|
||||
@ -16,11 +16,10 @@ struct VehicleRegistryEntry {
|
||||
|
||||
class ControllableVehicles {
|
||||
private:
|
||||
static ControllableVehicles* instance_;
|
||||
ControllableVehicles();
|
||||
ControllableVehicles(const ControllableVehicles&) = delete;
|
||||
ControllableVehicles& operator=(const ControllableVehicles&) = delete;
|
||||
~ControllableVehicles();
|
||||
~ControllableVehicles() = default;
|
||||
|
||||
// 运行时车辆类型注册表(由前端接口增量更新)
|
||||
mutable std::shared_mutex mutex_;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user