使用boost pfr 遍历结构体
This commit is contained in:
@@ -1,470 +1,419 @@
|
||||
#pragma once
|
||||
|
||||
#include "Data_Source_Handler.h"
|
||||
|
||||
namespace Psc {
|
||||
class SM_RingBuffer;
|
||||
}
|
||||
|
||||
class Data_Source;
|
||||
std::shared_ptr<Data_Source> create_from_json(const Psc::JSON *that_json);
|
||||
|
||||
std::shared_ptr<Data_Source> create_from_json(const Psc::JSON* that_json);
|
||||
class Input_Format {
|
||||
public:
|
||||
Input_Format() {
|
||||
test_v.resize(static_cast<size_t>(SSR::Downlink_Format::Unknown));
|
||||
}
|
||||
|
||||
Psc::JSON to_json() {
|
||||
std::lock_guard g(mtx);
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
for (auto e : magic_enum::enum_values<SSR::Downlink_Format>()) {
|
||||
std::string_view value_view = magic_enum::enum_name(e);
|
||||
std::string value(value_view.begin(), value_view.end());
|
||||
if (value == Psc::to_string(SSR::Downlink_Format::Unknown))
|
||||
continue;
|
||||
auto ev = static_cast<std::uint8_t>(e);
|
||||
bool it = test_v[ev];
|
||||
ret.append({value, it});
|
||||
Input_Format() {
|
||||
test_v.resize(static_cast<size_t>(SSR::Downlink_Format::Unknown));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void from_json(const Psc::JSON *json) {
|
||||
std::lock_guard g(mtx);
|
||||
for (auto e : magic_enum::enum_values<SSR::Downlink_Format>()) {
|
||||
std::string_view value_view = magic_enum::enum_name(e);
|
||||
std::string value(value_view.begin(), value_view.end());
|
||||
if (value == Psc::to_string(SSR::Downlink_Format::Unknown))
|
||||
continue;
|
||||
auto use = json->get_bool(value);
|
||||
auto ev = static_cast<std::uint8_t>(e);
|
||||
test_v[ev] = use;
|
||||
Psc::JSON to_json() {
|
||||
std::lock_guard g(mtx);
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
for (auto e : magic_enum::enum_values<SSR::Downlink_Format>()) {
|
||||
std::string_view value_view = magic_enum::enum_name(e);
|
||||
std::string value(value_view.begin(), value_view.end());
|
||||
if (value == Psc::to_string(SSR::Downlink_Format::Unknown))
|
||||
continue;
|
||||
auto ev = static_cast<std::uint8_t>(e);
|
||||
bool it = test_v[ev];
|
||||
ret.append({value, it});
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void from_json(const Psc::JSON* json) {
|
||||
std::lock_guard g(mtx);
|
||||
for (auto e : magic_enum::enum_values<SSR::Downlink_Format>()) {
|
||||
std::string_view value_view = magic_enum::enum_name(e);
|
||||
std::string value(value_view.begin(), value_view.end());
|
||||
if (value == Psc::to_string(SSR::Downlink_Format::Unknown))
|
||||
continue;
|
||||
auto use = json->get_bool(value);
|
||||
auto ev = static_cast<std::uint8_t>(e);
|
||||
test_v[ev] = use;
|
||||
}
|
||||
}
|
||||
bool test(SSR::Downlink_Format df) {
|
||||
std::lock_guard g(mtx);
|
||||
auto dfv = static_cast<std::uint8_t>(df);
|
||||
if (dfv >= static_cast<std::uint8_t>(SSR::Downlink_Format::Unknown))
|
||||
return false;
|
||||
return test_v[dfv];
|
||||
}
|
||||
bool test(std::uint8_t dfv) {
|
||||
std::lock_guard g(mtx);
|
||||
if (dfv >= static_cast<std::uint8_t>(SSR::Downlink_Format::Unknown))
|
||||
return false;
|
||||
return test_v[dfv];
|
||||
}
|
||||
}
|
||||
|
||||
bool test(SSR::Downlink_Format df) {
|
||||
std::lock_guard g(mtx);
|
||||
auto dfv = static_cast<std::uint8_t>(df);
|
||||
if (dfv >= static_cast<std::uint8_t>(SSR::Downlink_Format::Unknown))
|
||||
return false;
|
||||
return test_v[dfv];
|
||||
}
|
||||
|
||||
bool test(std::uint8_t dfv) {
|
||||
std::lock_guard g(mtx);
|
||||
if (dfv >= static_cast<std::uint8_t>(SSR::Downlink_Format::Unknown))
|
||||
return false;
|
||||
return test_v[dfv];
|
||||
}
|
||||
|
||||
protected:
|
||||
std::mutex mtx;
|
||||
std::vector<bool> test_v;
|
||||
std::mutex mtx;
|
||||
std::vector<bool> test_v;
|
||||
};
|
||||
class Data_Source_Data {
|
||||
public:
|
||||
Psc::Copyable_Atomic<bool> base_station_show{};
|
||||
Psc::Copyable_Atomic<bool> aircraft_show{};
|
||||
std::string color = "#1677ff";
|
||||
int aircraft_pixel_size{};
|
||||
double lat{};
|
||||
double lon{};
|
||||
double alt{};
|
||||
Psc::Copyable_Atomic<bool> update_form_gps{};
|
||||
Psc::Copyable_Atomic<bool> show_icao = true;
|
||||
Psc::Copyable_Atomic<bool> show_call_sign = false;
|
||||
Psc::Copyable_Atomic<bool> show_fly_status = false;
|
||||
Psc::Copyable_Atomic<bool> keep_mode = true;
|
||||
PSC_USE_JSON
|
||||
};
|
||||
|
||||
class Data_Source : public std::enable_shared_from_this<Data_Source>,
|
||||
public Data_Source_Handler {
|
||||
public Data_Source_Handler, public Data_Source_Data {
|
||||
public:
|
||||
std::shared_ptr<Data_Source> that();
|
||||
virtual concurrencpp::result<std::string> read_coro() { co_return ""; };
|
||||
bool registered() const;
|
||||
virtual Psc::JSON get_custom_state_json() = 0;
|
||||
Psc::JSON get_state();
|
||||
std::string last_char; // 用于处理奇数字节
|
||||
Data_Source();
|
||||
concurrencpp::result<void> loop_coro(
|
||||
std::shared_ptr<concurrencpp::worker_thread_executor> executor) override;
|
||||
std::shared_ptr<SSR::Mode_Msg> last_prase_msg = nullptr;
|
||||
virtual concurrencpp::result<void> handle_in_loop_coro() { co_return; }
|
||||
std::string type;
|
||||
std::atomic<bool> base_station_show{};
|
||||
std::atomic<bool> aircraft_show{};
|
||||
std::atomic<bool> show_icao = true;
|
||||
std::atomic<bool> show_call_sign = false;
|
||||
std::atomic<bool> show_fly_status = false;
|
||||
std::atomic<bool> keep_mode = true;
|
||||
double lat{};
|
||||
double lon{};
|
||||
double alt{};
|
||||
std::atomic<bool> update_form_gps{};
|
||||
std::string color = "#1677ff";
|
||||
int aircraft_pixel_size{};
|
||||
Frequency_Limit statistic_fl;
|
||||
std::string thread_key() const;
|
||||
Psc::JSON statistic_json() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
Ret_J(enable);
|
||||
Ret_J(type);
|
||||
ret.append({"aircraft_total", get_aircraft_num()});
|
||||
ret.append({"aircraft_with_position", have_pos_aircraft_num});
|
||||
ret.append({"mode_ac_statistic", mode_ac_statistic.to_json()});
|
||||
ret.append({"mode_s_statistic", mode_s_statistic.to_json()});
|
||||
ret.append({"all_connect_feed", get_all_connect_feed_status()});
|
||||
return ret;
|
||||
}
|
||||
virtual Psc::JSON to_json() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
Ret_J(key);
|
||||
Ret_J(enable);
|
||||
Ret_J(base_station_show);
|
||||
Ret_J(aircraft_show);
|
||||
Ret_J(color);
|
||||
Ret_J(aircraft_pixel_size);
|
||||
Ret_J(type);
|
||||
Ret_J(lat);
|
||||
Ret_J(lon);
|
||||
Ret_J(alt);
|
||||
Ret_J(update_form_gps);
|
||||
Ret_J(show_icao);
|
||||
Ret_J(show_call_sign);
|
||||
Ret_J(show_fly_status);
|
||||
Ret_J(keep_mode);
|
||||
// ret.append({"parse_format", parse_format->to_json()});
|
||||
return ret;
|
||||
}
|
||||
virtual void from_json(const Psc::JSON *that_json) {
|
||||
Get_J(key);
|
||||
Get_J(enable);
|
||||
Get_J(base_station_show);
|
||||
Get_J(aircraft_show);
|
||||
Get_J(color);
|
||||
Get_J(aircraft_pixel_size);
|
||||
Get_J(type);
|
||||
Get_J(lat);
|
||||
Get_J(lon);
|
||||
Get_J(alt);
|
||||
Get_J(update_form_gps);
|
||||
Get_J(show_icao);
|
||||
Get_J(show_call_sign);
|
||||
Get_J(show_fly_status);
|
||||
Get_J(keep_mode);
|
||||
// parse_format->from_json(that_json->get("parse_format"));
|
||||
}
|
||||
std::string buffer;
|
||||
std::shared_ptr<Data_Source> that();
|
||||
virtual concurrencpp::result<std::string> read_coro() {
|
||||
co_return "";
|
||||
};
|
||||
bool registered() const;
|
||||
virtual Psc::JSON get_custom_state_json() = 0;
|
||||
Psc::JSON get_state();
|
||||
std::string last_char; // 用于处理奇数字节
|
||||
Data_Source();
|
||||
concurrencpp::result<void> loop_coro(
|
||||
std::shared_ptr<concurrencpp::worker_thread_executor> executor) override;
|
||||
std::shared_ptr<SSR::Mode_Msg> last_prase_msg = nullptr;
|
||||
virtual concurrencpp::result<void> handle_in_loop_coro() {
|
||||
co_return;
|
||||
}
|
||||
Frequency_Limit statistic_fl;
|
||||
std::string thread_key() const;
|
||||
Psc::JSON statistic_json() {
|
||||
Psc::JSON ret = With_Loop_Coro::to_base_json();
|
||||
ret.append({"aircraft_total", get_aircraft_num()});
|
||||
ret.append({"aircraft_with_position", have_pos_aircraft_num});
|
||||
ret.append({"mode_ac_statistic", mode_ac_statistic.to_json()});
|
||||
ret.append({"mode_s_statistic", mode_s_statistic.to_json()});
|
||||
ret.append({"all_connect_feed", get_all_connect_feed_status()});
|
||||
return ret;
|
||||
}
|
||||
virtual Psc::JSON to_json() {
|
||||
return With_Loop_Coro::to_base_json() += Data_Source_Data::to_base_json();
|
||||
}
|
||||
virtual void from_json(const Psc::JSON* that_json) {
|
||||
With_Loop_Coro::from_base_json(that_json);
|
||||
Data_Source_Data::from_base_json(that_json);
|
||||
}
|
||||
std::string buffer;
|
||||
};
|
||||
|
||||
class TCP_Client_Data_Source : public Data_Source {
|
||||
class TCP_Client_Data_Source_Data {
|
||||
public:
|
||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
||||
co_await cli.tick_coro();
|
||||
co_return;
|
||||
}
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto &state = cli.state;
|
||||
return VAR_JSON_1(state);
|
||||
}
|
||||
Psc::asio_socket::TCP_Client_Coro cli;
|
||||
std::string ip;
|
||||
std::uint16_t port{};
|
||||
|
||||
~TCP_Client_Data_Source() override = default;
|
||||
TCP_Client_Data_Source() { type = "TCP_Client_Data_Source"; }
|
||||
concurrencpp::result<void> _open() override {
|
||||
Psc::asio_socket::Sockaddr_In addr;
|
||||
addr.ip = ip;
|
||||
addr.port = port;
|
||||
cli.set_dest_address(addr);
|
||||
cli.create();
|
||||
co_await cli.connect_coro();
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
co_await cli.close_coro();
|
||||
co_return;
|
||||
}
|
||||
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
Ret_J(ip);
|
||||
Ret_J(port);
|
||||
return ret;
|
||||
}
|
||||
void from_json(const Psc::JSON *that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Get_J(ip);
|
||||
Get_J(port);
|
||||
}
|
||||
|
||||
concurrencpp::result<std::string> read_coro() override {
|
||||
co_return co_await cli.read_coro();
|
||||
}
|
||||
std::string ip;
|
||||
std::uint16_t port{};
|
||||
PSC_USE_JSON
|
||||
};
|
||||
class Serial_Data_Source : public Data_Source {
|
||||
class TCP_Client_Data_Source : public Data_Source, public TCP_Client_Data_Source_Data {
|
||||
public:
|
||||
std::string port_name;
|
||||
Baud_Rate_Type baud_rate{};
|
||||
Psc::JSON get_custom_state_json() override { return Psc::JSON::object(); }
|
||||
Serial_Data_Source() { this->type = "Serial_Data_Source"; }
|
||||
concurrencpp::result<void> _open() override {
|
||||
serial = std::make_unique<Psc::serial::Serial_Coro>();
|
||||
serial->set_serial_name(port_name);
|
||||
serial->set_baud_rate(baud_rate);
|
||||
serial->set_parity(Psc::serial::Parity::NoParity);
|
||||
serial->set_data_bits(Psc::serial::DataBits::Data8);
|
||||
serial->set_stop_bits(Psc::serial::StopBits::OneStop);
|
||||
serial->set_flow_control(Psc::serial::FlowControl::HardwareControl);
|
||||
serial->set_buffer_byte_size(10 * 1024);
|
||||
bool ok = serial->open();
|
||||
if (!ok) {
|
||||
std::cerr << "createSerial " + port_name + ":" +
|
||||
std::to_string(baud_rate) + " 打开串口失败!\n";
|
||||
} else {
|
||||
// std::cerr << "createSerial " + serial_name + ":" +
|
||||
// std::to_string(baud_rate) + " 打开串口成功!\n";
|
||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
||||
co_await cli.tick_coro();
|
||||
co_return;
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
if (serial)
|
||||
serial->close();
|
||||
co_return;
|
||||
}
|
||||
|
||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
||||
if (serial) {
|
||||
co_await serial->tick_coro();
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto& state = cli.state;
|
||||
return VAR_JSON_1(state);
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<std::string> read_coro() override {
|
||||
if (!serial) {
|
||||
co_return "";
|
||||
Psc::asio_socket::TCP_Client_Coro cli;
|
||||
~TCP_Client_Data_Source() override = default;
|
||||
TCP_Client_Data_Source() {
|
||||
type = "TCP_Client_Data_Source";
|
||||
}
|
||||
co_return co_await serial->read_coro();
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
Ret_J(port_name);
|
||||
Ret_J(baud_rate);
|
||||
return ret;
|
||||
}
|
||||
void from_json(const Psc::JSON *that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Get_J(port_name) Get_J(baud_rate);
|
||||
}
|
||||
std::unique_ptr<Psc::serial::Serial_Coro> serial{};
|
||||
~Serial_Data_Source() override {
|
||||
if (serial) {
|
||||
serial->close();
|
||||
concurrencpp::result<void> _open() override {
|
||||
Psc::asio_socket::Sockaddr_In addr;
|
||||
addr.ip = ip;
|
||||
addr.port = port;
|
||||
cli.set_dest_address(addr);
|
||||
cli.create();
|
||||
co_await cli.connect_coro();
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
co_await cli.close_coro();
|
||||
co_return;
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
return Data_Source::to_json() += TCP_Client_Data_Source_Data::to_base_json();
|
||||
}
|
||||
void from_json(const Psc::JSON* that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
TCP_Client_Data_Source_Data::from_base_json(that_json);
|
||||
}
|
||||
concurrencpp::result<std::string> read_coro() override {
|
||||
co_return co_await cli.read_coro();
|
||||
}
|
||||
};
|
||||
class Serial_Data_Source_Data {
|
||||
public:
|
||||
std::string port_name;
|
||||
Baud_Rate_Type baud_rate{};
|
||||
PSC_USE_JSON
|
||||
};
|
||||
class Serial_Data_Source : public Data_Source, public Serial_Data_Source_Data {
|
||||
public:
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
return Psc::JSON::object();
|
||||
}
|
||||
Serial_Data_Source() {
|
||||
this->type = "Serial_Data_Source";
|
||||
}
|
||||
concurrencpp::result<void> _open() override {
|
||||
serial = std::make_unique<Psc::serial::Serial_Coro>();
|
||||
serial->set_serial_name(port_name);
|
||||
serial->set_baud_rate(baud_rate);
|
||||
serial->set_parity(Psc::serial::Parity::NoParity);
|
||||
serial->set_data_bits(Psc::serial::DataBits::Data8);
|
||||
serial->set_stop_bits(Psc::serial::StopBits::OneStop);
|
||||
serial->set_flow_control(Psc::serial::FlowControl::HardwareControl);
|
||||
serial->set_buffer_byte_size(10 * 1024);
|
||||
bool ok = serial->open();
|
||||
if (!ok) {
|
||||
std::cerr << "createSerial " + port_name + ":" +
|
||||
std::to_string(baud_rate) + " 打开串口失败!\n";
|
||||
}
|
||||
else {
|
||||
// std::cerr << "createSerial " + serial_name + ":" +
|
||||
// std::to_string(baud_rate) + " 打开串口成功!\n";
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
if (serial)
|
||||
serial->close();
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
||||
if (serial) {
|
||||
co_await serial->tick_coro();
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<std::string> read_coro() override {
|
||||
if (!serial) {
|
||||
co_return "";
|
||||
}
|
||||
co_return co_await serial->read_coro();
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
return Data_Source::to_json() += Serial_Data_Source_Data::to_base_json();
|
||||
}
|
||||
void from_json(const Psc::JSON* that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Serial_Data_Source_Data::from_base_json(that_json);
|
||||
}
|
||||
std::unique_ptr<Psc::serial::Serial_Coro> serial{};
|
||||
~Serial_Data_Source() override {
|
||||
if (serial) {
|
||||
serial->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
enum class File_Data_Type {
|
||||
SIMPLE_BIN_Blank, // 没有1a转义�?
|
||||
BIN_Blank_Text,
|
||||
AVR,
|
||||
BIN_Text,
|
||||
BIN_Blank_One_Line_With_Escape,
|
||||
BIN_Blank_One_Line_No_Escape,
|
||||
BIN,
|
||||
Unknown
|
||||
// 纯粹的二进制
|
||||
SIMPLE_BIN_Blank, // 没有1a转义�?
|
||||
BIN_Blank_Text,
|
||||
AVR,
|
||||
BIN_Text,
|
||||
BIN_Blank_One_Line_With_Escape,
|
||||
BIN_Blank_One_Line_No_Escape,
|
||||
BIN,
|
||||
Unknown
|
||||
// 纯粹的二进制
|
||||
};
|
||||
enum Play_Mode { one, loop, analysis, time_batch };
|
||||
|
||||
class File_Data_Source : public Data_Source {
|
||||
class File_Data_Source_Data {
|
||||
public:
|
||||
std::string file_path;
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
Play_Mode play_mode = Play_Mode::one;
|
||||
bool have_report_play_back_all_success = false;
|
||||
std::string state = "null";
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto cur_virtual_time = player_clock.get_cur_time_point();
|
||||
auto playback_speed_rate = player_clock.playback_speed_rate;
|
||||
return VAR_JSON_5(cur_virtual_time, playback_speed_rate, state, index,
|
||||
part_infos.size());
|
||||
}
|
||||
File_Data_Source() { this->type = "File_Data_Source"; }
|
||||
~File_Data_Source() override = default;
|
||||
void before_handle_msg(std::shared_ptr<SSR::Mode_Msg> &msg) override;
|
||||
|
||||
struct Cache_Msg {
|
||||
explicit Cache_Msg(std::shared_ptr<SSR::Mode_S_Msg> msg)
|
||||
: msg(std::move(msg)) {}
|
||||
SSR::Play_Back_Time_Point time() const {
|
||||
return SSR::Play_Back_Time_Point{day_num, msg->mlat_timestamp.daysec};
|
||||
std::string file_path;
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
Play_Mode play_mode = Play_Mode::one;
|
||||
PSC_USE_JSON
|
||||
};
|
||||
class File_Data_Source : public Data_Source, public File_Data_Source_Data {
|
||||
public:
|
||||
bool have_report_play_back_all_success = false;
|
||||
std::string state = "null";
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto cur_virtual_time = player_clock.get_cur_time_point();
|
||||
auto playback_speed_rate = player_clock.playback_speed_rate;
|
||||
return VAR_JSON_5(cur_virtual_time, playback_speed_rate, state, index,
|
||||
part_infos.size());
|
||||
}
|
||||
std::uint64_t day_num = 0;
|
||||
std::shared_ptr<SSR::Mode_S_Msg> msg;
|
||||
};
|
||||
Player_Clock player_clock;
|
||||
std::shared_ptr<Cache_Msg> pre_cache = nullptr;
|
||||
std::deque<std::shared_ptr<Cache_Msg>> cache_list;
|
||||
|
||||
std::string get_true_file_path() const {
|
||||
return Psc::get_abs_path(file_path);
|
||||
}
|
||||
concurrencpp::result<void> _close() override;
|
||||
concurrencpp::result<void> _open() override;
|
||||
std::vector<std::string> readBinaryFileAsString(const std::string &filepath,
|
||||
size_t part_size);
|
||||
void from_json(const Psc::JSON *that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Get_J(file_path);
|
||||
Get_J(data_type);
|
||||
Get_J(play_mode);
|
||||
}
|
||||
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
Ret_J(file_path);
|
||||
Ret_J(data_type);
|
||||
Ret_J(play_mode);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void origin_data_transform_mode_data(std::string &data) override;
|
||||
|
||||
void handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) override;
|
||||
|
||||
File_Data_Source() {
|
||||
this->type = "File_Data_Source";
|
||||
}
|
||||
~File_Data_Source() override = default;
|
||||
void before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) override;
|
||||
struct Cache_Msg {
|
||||
explicit Cache_Msg(std::shared_ptr<SSR::Mode_S_Msg> msg)
|
||||
: msg(std::move(msg)) {}
|
||||
SSR::Play_Back_Time_Point time() const {
|
||||
return SSR::Play_Back_Time_Point{day_num, msg->mlat_timestamp.daysec};
|
||||
}
|
||||
std::uint64_t day_num = 0;
|
||||
std::shared_ptr<SSR::Mode_S_Msg> msg;
|
||||
};
|
||||
Player_Clock player_clock;
|
||||
std::shared_ptr<Cache_Msg> pre_cache = nullptr;
|
||||
std::deque<std::shared_ptr<Cache_Msg>> cache_list;
|
||||
std::string get_true_file_path() const {
|
||||
return Psc::get_abs_path(file_path);
|
||||
}
|
||||
concurrencpp::result<void> _close() override;
|
||||
concurrencpp::result<void> _open() override;
|
||||
std::vector<std::string> readBinaryFileAsString(const std::string& filepath,
|
||||
size_t part_size);
|
||||
void from_json(const Psc::JSON* that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
File_Data_Source_Data::from_base_json(that_json);
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
return Data_Source::to_json() += File_Data_Source_Data::to_base_json();
|
||||
}
|
||||
void origin_data_transform_mode_data(std::string& data) override;
|
||||
void handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) override;
|
||||
protected:
|
||||
std::optional<std::string> get_raw_line(int &ret_index);
|
||||
long long index = 0;
|
||||
std::vector<std::string> part_infos;
|
||||
std::mutex mtx;
|
||||
std::optional<std::string> get_raw_line(int& ret_index);
|
||||
long long index = 0;
|
||||
std::vector<std::string> part_infos;
|
||||
std::mutex mtx;
|
||||
};
|
||||
class Dll_Data_Source : public Data_Source {
|
||||
class Dll_Data_Source_Data {
|
||||
public:
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto ret = VAR_JSON_1(state);
|
||||
ret.append({"read_vaild_len", vs.to_string()});
|
||||
return ret;
|
||||
}
|
||||
Psc::Value_Statistics vs;
|
||||
std::string library_path;
|
||||
std::string function_name;
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
Dll_Data_Source() { this->type = "Dll_Data_Source"; }
|
||||
~Dll_Data_Source() override = default;
|
||||
std::size_t buffer_size{};
|
||||
std::vector<std::uint8_t> buffer;
|
||||
void from_json(const Psc::JSON *that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Get_J(library_path);
|
||||
Get_J(function_name);
|
||||
Get_J(data_type);
|
||||
Get_J(buffer_size);
|
||||
buffer.resize(buffer_size);
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
Ret_J(library_path);
|
||||
Ret_J(function_name);
|
||||
Ret_J(data_type);
|
||||
Ret_J(buffer_size);
|
||||
return ret;
|
||||
}
|
||||
void *lib{};
|
||||
|
||||
using Func_Type = size_t (*)(char *buf, std::size_t max_len);
|
||||
// using Func_Type = std::uint16_t (*)(char *buf, std::uint16_t max_len);
|
||||
Func_Type read_func_ptr{};
|
||||
|
||||
using Call_Back = void (*)(char *buf, std::size_t len);
|
||||
using set_Call_back = void (*)(Call_Back);
|
||||
|
||||
std::string state;
|
||||
concurrencpp::result<void> _close() override {
|
||||
Psc::free_library(lib);
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _open() override {
|
||||
{
|
||||
auto r = Psc::try_load_library(Psc::get_abs_path(library_path));
|
||||
if (!r) {
|
||||
state = r.error().message();
|
||||
co_return;
|
||||
}
|
||||
lib = r.value();
|
||||
}
|
||||
{
|
||||
auto r = Psc::try_load_function(lib, function_name);
|
||||
if (!r) {
|
||||
state = r.error().message();
|
||||
std::cout << LOG_POS << " [" << function_name << "] 函数指针加载失败!"
|
||||
<< std::endl;
|
||||
co_return;
|
||||
}
|
||||
read_func_ptr = (Func_Type)r.value();
|
||||
}
|
||||
state = "加载成功";
|
||||
co_return;
|
||||
}
|
||||
void origin_data_transform_mode_data(std::string &data) override;
|
||||
std::string library_path;
|
||||
std::string function_name;
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
std::size_t buffer_size{};
|
||||
PSC_USE_JSON
|
||||
};
|
||||
|
||||
class Shared_Memory_Data_Source : public Data_Source {
|
||||
class Dll_Data_Source : public Data_Source, public Dll_Data_Source_Data {
|
||||
public:
|
||||
Psc::JSON get_custom_state_json() override { return Psc::JSON::object(); }
|
||||
void from_json(const Psc::JSON *that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Get_J(shared_memory_name);
|
||||
Get_J(shared_memory_size);
|
||||
Get_J(data_type);
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
Ret_J(shared_memory_name);
|
||||
Ret_J(shared_memory_size);
|
||||
Ret_J(data_type);
|
||||
return ret;
|
||||
}
|
||||
concurrencpp::result<void> _open() override;
|
||||
concurrencpp::result<void> _close() override;
|
||||
|
||||
void origin_data_transform_mode_data(std::string &data) override;
|
||||
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
auto ret = VAR_JSON_1(state);
|
||||
ret.append({"read_vaild_len", vs.to_string()});
|
||||
return ret;
|
||||
}
|
||||
Psc::Value_Statistics vs;
|
||||
Dll_Data_Source() {
|
||||
this->type = "Dll_Data_Source";
|
||||
}
|
||||
~Dll_Data_Source() override = default;
|
||||
std::vector<std::uint8_t> buffer;
|
||||
void from_json(const Psc::JSON* that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Dll_Data_Source_Data::from_base_json(that_json);
|
||||
buffer.resize(buffer_size);
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
return Data_Source::to_json() += Dll_Data_Source_Data::to_base_json();
|
||||
}
|
||||
void* lib{};
|
||||
using Func_Type = size_t (*)(char* buf, std::size_t max_len);
|
||||
// using Func_Type = std::uint16_t (*)(char *buf, std::uint16_t max_len);
|
||||
Func_Type read_func_ptr{};
|
||||
using Call_Back = void (*)(char* buf, std::size_t len);
|
||||
using set_Call_back = void (*)(Call_Back);
|
||||
std::string state;
|
||||
concurrencpp::result<void> _close() override {
|
||||
Psc::free_library(lib);
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _open() override {
|
||||
{
|
||||
auto r = Psc::try_load_library(Psc::get_abs_path(library_path));
|
||||
if (!r) {
|
||||
state = r.error().message();
|
||||
co_return;
|
||||
}
|
||||
lib = r.value();
|
||||
}
|
||||
{
|
||||
auto r = Psc::try_load_function(lib, function_name);
|
||||
if (!r) {
|
||||
state = r.error().message();
|
||||
std::cout << LOG_POS << " [" << function_name << "] 函数指针加载失败!"
|
||||
<< std::endl;
|
||||
co_return;
|
||||
}
|
||||
read_func_ptr = (Func_Type)r.value();
|
||||
}
|
||||
state = "加载成功";
|
||||
co_return;
|
||||
}
|
||||
void origin_data_transform_mode_data(std::string& data) override;
|
||||
};
|
||||
class Shared_Memory_Data_Source_Data {
|
||||
public:
|
||||
std::string shared_memory_name;
|
||||
std::uint64_t shared_memory_size{};
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
PSC_USE_JSON
|
||||
};
|
||||
class Shared_Memory_Data_Source : public Data_Source, public Shared_Memory_Data_Source_Data {
|
||||
public:
|
||||
Psc::JSON get_custom_state_json() override {
|
||||
return Psc::JSON::object();
|
||||
}
|
||||
void from_json(const Psc::JSON* that_json) override {
|
||||
Data_Source::from_json(that_json);
|
||||
Shared_Memory_Data_Source_Data::from_base_json(that_json);
|
||||
}
|
||||
Psc::JSON to_json() override {
|
||||
return Data_Source::to_json() += Shared_Memory_Data_Source_Data::to_base_json();
|
||||
}
|
||||
concurrencpp::result<void> _open() override;
|
||||
concurrencpp::result<void> _close() override;
|
||||
void origin_data_transform_mode_data(std::string& data) override;
|
||||
protected:
|
||||
std::unique_ptr<Psc::SM_RingBuffer> sm;
|
||||
std::string shared_memory_name;
|
||||
std::uint64_t shared_memory_size{};
|
||||
File_Data_Type data_type = File_Data_Type::BIN_Blank_Text;
|
||||
std::unique_ptr<Psc::SM_RingBuffer> sm;
|
||||
};
|
||||
|
||||
inline std::shared_ptr<Data_Source>
|
||||
create_data_source_from_type(const std::string &t) {
|
||||
std::shared_ptr<Data_Source> ret{};
|
||||
if (t == "Serial_Data_Source")
|
||||
ret = std::make_shared<Serial_Data_Source>();
|
||||
else if (t == "TCP_Client_Data_Source")
|
||||
ret = std::make_shared<TCP_Client_Data_Source>();
|
||||
else if (t == "File_Data_Source")
|
||||
ret = std::make_shared<File_Data_Source>();
|
||||
else if (t == "Dll_Data_Source")
|
||||
ret = std::make_shared<Dll_Data_Source>();
|
||||
else if (t == "Shared_Memory_Data_Source")
|
||||
ret = std::make_shared<Shared_Memory_Data_Source>();
|
||||
else {
|
||||
std::cout << "未知�?Data_Source type类型!" << std::endl;
|
||||
throw std::invalid_argument("unknown Data_Source type: " + t);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct Data_Source_Config {
|
||||
Ordered_Map<std::string, std::shared_ptr<Data_Source>> map;
|
||||
Data_Source_Config() = default;
|
||||
void init(const Psc::JSON *that_json) {
|
||||
auto list = that_json->get("list");
|
||||
for (auto &it : list->children) {
|
||||
auto ds = create_from_json(&it);
|
||||
map.push_back(ds);
|
||||
create_data_source_from_type(const std::string& t) {
|
||||
std::shared_ptr<Data_Source> ret{};
|
||||
if (t == "Serial_Data_Source")
|
||||
ret = std::make_shared<Serial_Data_Source>();
|
||||
else if (t == "TCP_Client_Data_Source")
|
||||
ret = std::make_shared<TCP_Client_Data_Source>();
|
||||
else if (t == "File_Data_Source")
|
||||
ret = std::make_shared<File_Data_Source>();
|
||||
else if (t == "Dll_Data_Source")
|
||||
ret = std::make_shared<Dll_Data_Source>();
|
||||
else if (t == "Shared_Memory_Data_Source")
|
||||
ret = std::make_shared<Shared_Memory_Data_Source>();
|
||||
else {
|
||||
std::cout << "未知�?Data_Source type类型!" << std::endl;
|
||||
throw std::invalid_argument("unknown Data_Source type: " + t);
|
||||
}
|
||||
}
|
||||
Psc::JSON list() {
|
||||
auto connect_json = Psc::JSON::array();
|
||||
for (auto &it : map.list()) {
|
||||
connect_json.children.emplace_back(it->to_json());
|
||||
}
|
||||
return connect_json;
|
||||
}
|
||||
Psc::JSON to_json() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
ret.append({"list", list()});
|
||||
return ret;
|
||||
}
|
||||
void server(Global *g);
|
||||
}
|
||||
struct Data_Source_Config {
|
||||
Ordered_Map<std::string, std::shared_ptr<Data_Source>> map;
|
||||
Data_Source_Config() = default;
|
||||
void init(const Psc::JSON* that_json) {
|
||||
auto list = that_json->get("list");
|
||||
for (auto& it : list->children) {
|
||||
auto ds = create_from_json(&it);
|
||||
map.push_back(ds);
|
||||
}
|
||||
}
|
||||
Psc::JSON list() {
|
||||
auto connect_json = Psc::JSON::array();
|
||||
for (auto& it : map.list()) {
|
||||
connect_json.children.emplace_back(it->to_json());
|
||||
}
|
||||
return connect_json;
|
||||
}
|
||||
Psc::JSON to_json() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
ret.append({"list", list()});
|
||||
return ret;
|
||||
}
|
||||
void server(Global* g);
|
||||
};
|
||||
|
||||
@@ -4,13 +4,11 @@
|
||||
#include "../External_Database/export.h"
|
||||
#include "BaseStation.h"
|
||||
#include "Local_Server/server/With_Loop_Coro.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
template <
|
||||
typename Key_Type,
|
||||
typename Value_Type,
|
||||
@@ -18,94 +16,68 @@ template <
|
||||
>
|
||||
struct Data_Map {
|
||||
using Ptr = std::shared_ptr<Value_Type>;
|
||||
|
||||
size_t size() const {
|
||||
size_t ret = 0;
|
||||
|
||||
for (const auto& shard : shards) {
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
ret += shard.map.size();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ptr get(const Key_Type& key) const {
|
||||
const auto& shard = get_shard(key);
|
||||
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
auto iter = shard.map.find(key);
|
||||
if (iter == shard.map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
Ptr create(const Key_Type& key) {
|
||||
auto& shard = get_shard(key);
|
||||
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
auto iter = shard.map.find(key);
|
||||
if (iter != shard.map.end()) {
|
||||
return iter->second;
|
||||
}
|
||||
}
|
||||
|
||||
// 构造对象放锁外,避免构造期间占锁
|
||||
auto new_value = std::make_shared<Value_Type>(key);
|
||||
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
auto [iter, inserted] = shard.map.emplace(key, new_value);
|
||||
|
||||
// 如果别的线程已经创建了,返回已有对象
|
||||
return iter->second;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(const Key_Type& key) {
|
||||
auto& shard = get_shard(key);
|
||||
|
||||
std::unique_lock<std::shared_mutex> g(shard.mtx);
|
||||
shard.map.erase(key);
|
||||
}
|
||||
|
||||
std::vector<Ptr> values() const {
|
||||
std::vector<Ptr> ret;
|
||||
|
||||
for (const auto& shard : shards) {
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
for (const auto& [key, val] : shard.map) {
|
||||
ret.push_back(val);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Cond>
|
||||
std::vector<Ptr> get_cond(Cond&& cond) const {
|
||||
std::vector<Ptr> ret;
|
||||
|
||||
for (const auto& shard : shards) {
|
||||
std::vector<Ptr> snapshot;
|
||||
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
snapshot.reserve(shard.map.size());
|
||||
|
||||
for (const auto& [key, val] : shard.map) {
|
||||
snapshot.push_back(val);
|
||||
}
|
||||
}
|
||||
|
||||
// cond 放锁外执行
|
||||
for (const auto& val : snapshot) {
|
||||
if (cond(val)) {
|
||||
@@ -113,47 +85,36 @@ struct Data_Map {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Cond>
|
||||
void remove_cond(Cond&& cond) {
|
||||
for (auto& shard : shards) {
|
||||
std::vector<std::pair<Key_Type, Ptr>> snapshot;
|
||||
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
snapshot.reserve(shard.map.size());
|
||||
|
||||
for (const auto& [key, val] : shard.map) {
|
||||
snapshot.emplace_back(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::pair<Key_Type, Ptr>> need_remove;
|
||||
|
||||
// cond 放锁外执行
|
||||
for (const auto& [key, val] : snapshot) {
|
||||
if (cond(val)) {
|
||||
need_remove.emplace_back(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
if (need_remove.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> g(shard.mtx);
|
||||
|
||||
for (const auto& [key, old_val] : need_remove) {
|
||||
auto iter = shard.map.find(key);
|
||||
if (iter == shard.map.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 防止同 key 已经被换成新对象,误删
|
||||
if (iter->second == old_val) {
|
||||
shard.map.erase(iter);
|
||||
@@ -162,36 +123,29 @@ struct Data_Map {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void delete_all() {
|
||||
for (auto& shard : shards) {
|
||||
std::unique_lock<std::shared_mutex> g(shard.mtx);
|
||||
shard.map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
struct Shard {
|
||||
mutable std::shared_mutex mtx;
|
||||
std::unordered_map<Key_Type, Ptr> map;
|
||||
};
|
||||
|
||||
Shard& get_shard(const Key_Type& key) {
|
||||
const size_t index = hasher(key) % Shard_Count;
|
||||
return shards[index];
|
||||
}
|
||||
|
||||
const Shard& get_shard(const Key_Type& key) const {
|
||||
const size_t index = hasher(key) % Shard_Count;
|
||||
return shards[index];
|
||||
}
|
||||
|
||||
protected:
|
||||
std::hash<Key_Type> hasher;
|
||||
Shard shards[Shard_Count];
|
||||
};
|
||||
|
||||
|
||||
class DataBase : public SSR::Data_Source_Interface, public With_Loop_Coro {
|
||||
public:
|
||||
std::shared_ptr<SSR::Aircraft_Info> get_aircraft(const std::string& icao) override;
|
||||
@@ -203,7 +157,6 @@ public:
|
||||
std::string get_key() override {
|
||||
return key;
|
||||
}
|
||||
|
||||
std::atomic<size_t> have_pos_aircraft_num{};
|
||||
Psc::JSON get_all_aircraft_json();
|
||||
Psc::JSON get_aircraft_list_after(time_t timestamp);
|
||||
@@ -211,18 +164,14 @@ public:
|
||||
Psc::JSON get_limit_mode_s_msg_info();
|
||||
#endif
|
||||
size_t get_aircraft_num();
|
||||
void clear_aircraft() { aircraft_map.delete_all(); }
|
||||
void clear_aircraft() {
|
||||
aircraft_map.delete_all();
|
||||
}
|
||||
~DataBase() override {
|
||||
aircraft_map.delete_all();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
Data_Map<std::string, Aircraft> aircraft_map;
|
||||
};
|
||||
|
||||
|
||||
struct Mode_S_Msg;
|
||||
void database_server(Global* g);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user