concurrencpp 换成asio协程
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
#include "Local_Server/Data_Feed/Data_Feed.h"
|
#include "Local_Server/Data_Feed/Data_Feed.h"
|
||||||
#include "../server/Global.h"
|
#include "../server/Global.h"
|
||||||
|
|
||||||
bool Data_Feed::registered() {
|
bool Data_Feed::registered() {
|
||||||
auto all_feed = Global::instance()->mode_acs.data_feed_config.map.list();
|
auto all_feed = Global::instance()->mode_acs.data_feed_config.map.list();
|
||||||
for (auto& item : all_feed) {
|
for (auto& item : all_feed) {
|
||||||
@@ -10,24 +9,20 @@ bool Data_Feed::registered() {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> Data_Feed_UDP_Server::handle_in_loop_coro() {
|
||||||
concurrencpp::result<void> Data_Feed_UDP_Server::handle_in_loop_coro() {
|
|
||||||
co_await svr.tick_coro();
|
co_await svr.tick_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> Data_Feed_UDP_Server::send_coro(const std::string& data) {
|
||||||
concurrencpp::result<void> Data_Feed_UDP_Server::send_coro(const std::string& data) {
|
|
||||||
co_await svr.write_to_all_clients_coro(data);
|
co_await svr.write_to_all_clients_coro(data);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> Data_Feed_UDP_Server::_open() {
|
||||||
concurrencpp::result<void> Data_Feed_UDP_Server::_open() {
|
|
||||||
svr.set_bind_address("0.0.0.0", port);
|
svr.set_bind_address("0.0.0.0", port);
|
||||||
svr.create();
|
svr.create();
|
||||||
server_logger->c_debug({}, {}, to_string() + "开启!");
|
server_logger->c_debug({}, {}, to_string() + "开启!");
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON Data_feed_Config::get_feed(const std::string& key) {
|
JSON Data_feed_Config::get_feed(const std::string& key) {
|
||||||
auto ret = map.get(key);
|
auto ret = map.get(key);
|
||||||
if (ret.has_value()) {
|
if (ret.has_value()) {
|
||||||
@@ -36,8 +31,7 @@ JSON Data_feed_Config::get_feed(const std::string& key) {
|
|||||||
}
|
}
|
||||||
return {nullptr};
|
return {nullptr};
|
||||||
}
|
}
|
||||||
|
void Data_feed_Config::server(Global *g) {
|
||||||
void Data_feed_Config::server(Global* g) {
|
|
||||||
auto& svr = g->svr;
|
auto& svr = g->svr;
|
||||||
auto& api = g->api;
|
auto& api = g->api;
|
||||||
std::string name = "data_feed";
|
std::string name = "data_feed";
|
||||||
@@ -86,13 +80,10 @@ void Data_feed_Config::server(Global* g) {
|
|||||||
svr.Post(api + svr.update + name, [g, this](HTTP_Param) {
|
svr.Post(api + svr.update + name, [g, this](HTTP_Param) {
|
||||||
CHECK_JSON_PARAM
|
CHECK_JSON_PARAM
|
||||||
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
|
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
|
||||||
HTTP_REQUIRE_VALUE(t, map.get(key))
|
HTTP_REQUIRE_VALUE(df, map.get(key))
|
||||||
// 直接关闭
|
// 直接关闭
|
||||||
// t->close();
|
// t->close();
|
||||||
t->from_json(¶ms);
|
df->from_json(¶ms);
|
||||||
// if (t->enable) {
|
|
||||||
// t->check_and_open();
|
|
||||||
// }
|
|
||||||
Global::save();
|
Global::save();
|
||||||
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
||||||
});
|
});
|
||||||
@@ -104,10 +95,6 @@ void Data_feed_Config::server(Global* g) {
|
|||||||
auto df = create_data_feed_from_type(t);
|
auto df = create_data_feed_from_type(t);
|
||||||
df->from_json(data);
|
df->from_json(data);
|
||||||
HTTP_REQUIRE_VALUE(enable, data->try_get_bool("enable"))
|
HTTP_REQUIRE_VALUE(enable, data->try_get_bool("enable"))
|
||||||
// if (enable)
|
|
||||||
// {
|
|
||||||
// df->check_and_open();
|
|
||||||
// }
|
|
||||||
HTTP_REQUIRE_TRUE(map.insert(index, df), "index")
|
HTTP_REQUIRE_TRUE(map.insert(index, df), "index")
|
||||||
res->setBody(warp(to_json()).to_json_string());
|
res->setBody(warp(to_json()).to_json_string());
|
||||||
Global::save();
|
Global::save();
|
||||||
@@ -140,7 +127,6 @@ void Data_feed_Config::server(Global* g) {
|
|||||||
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void BIN_Msg_Buffer::push(const std::string& msg) {
|
void BIN_Msg_Buffer::push(const std::string& msg) {
|
||||||
auto output_packet_size =
|
auto output_packet_size =
|
||||||
Global::instance()->mode_acs.data_feed_config.packet_byte_size.load();
|
Global::instance()->mode_acs.data_feed_config.packet_byte_size.load();
|
||||||
@@ -156,7 +142,6 @@ void BIN_Msg_Buffer::push(const std::string& msg) {
|
|||||||
msg_list_cache.back()->append(msg);
|
msg_list_cache.back()->append(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<std::string*>& BIN_Msg_Buffer::get_all() {
|
const std::vector<std::string*>& BIN_Msg_Buffer::get_all() {
|
||||||
std::lock_guard<std::mutex> g(mtx);
|
std::lock_guard<std::mutex> g(mtx);
|
||||||
std::swap(msg_list_cache, msg_list);
|
std::swap(msg_list_cache, msg_list);
|
||||||
@@ -165,12 +150,10 @@ const std::vector<std::string*>& BIN_Msg_Buffer::get_all() {
|
|||||||
mode_other_msg_num = 0;
|
mode_other_msg_num = 0;
|
||||||
return msg_list;
|
return msg_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
BIN_Msg_Buffer::BIN_Msg_Buffer() {
|
BIN_Msg_Buffer::BIN_Msg_Buffer() {
|
||||||
msg_list_cache.reserve(8 * 1024);
|
msg_list_cache.reserve(8 * 1024);
|
||||||
msg_list.reserve(8 * 1024);
|
msg_list.reserve(8 * 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
Psc::JSON BIN_Msg_Buffer::state_json() {
|
Psc::JSON BIN_Msg_Buffer::state_json() {
|
||||||
auto output_packet_size =
|
auto output_packet_size =
|
||||||
Global::instance()->mode_acs.data_feed_config.packet_byte_size.load();
|
Global::instance()->mode_acs.data_feed_config.packet_byte_size.load();
|
||||||
|
|||||||
@@ -17,15 +17,15 @@ public:
|
|||||||
BIN_Msg_Buffer msg_buffer{};
|
BIN_Msg_Buffer msg_buffer{};
|
||||||
Output_Format output_format;
|
Output_Format output_format;
|
||||||
Frequency_Limit_Multi sbs_flm{};
|
Frequency_Limit_Multi sbs_flm{};
|
||||||
concurrencpp::result<void> loop_coro() final;
|
asio::awaitable<void> loop_coro() final;
|
||||||
virtual void handle_in_loop() {}
|
virtual void handle_in_loop() {}
|
||||||
|
|
||||||
virtual concurrencpp::result<void> handle_in_loop_coro() {
|
virtual asio::awaitable<void> handle_in_loop_coro() {
|
||||||
handle_in_loop();
|
handle_in_loop();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual concurrencpp::result<void> send_coro(const std::string&) {
|
virtual asio::awaitable<void> send_coro(const std::string&) {
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,13 +83,13 @@ public:
|
|||||||
|
|
||||||
~Data_Feed_TCP_Server() override = default;
|
~Data_Feed_TCP_Server() override = default;
|
||||||
|
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
asio::awaitable<void> handle_in_loop_coro() override {
|
||||||
co_await svr.flush_clients_coro();
|
co_await svr.flush_clients_coro();
|
||||||
co_await svr.tick_coro();
|
co_await svr.tick_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> send_coro(const std::string& data) override {
|
asio::awaitable<void> send_coro(const std::string& data) override {
|
||||||
co_await svr.write_to_all_clients_coro(data);
|
co_await svr.write_to_all_clients_coro(data);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -105,12 +105,12 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
co_await svr.close_coro();
|
co_await svr.close_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
svr.set_connect_user_buffer_size(connect_user_buffer_size);
|
svr.set_connect_user_buffer_size(connect_user_buffer_size);
|
||||||
svr.set_connect_system_buffer_size(connect_system_buffer_size);
|
svr.set_connect_system_buffer_size(connect_system_buffer_size);
|
||||||
svr.set_tcp_no_delay(false);
|
svr.set_tcp_no_delay(false);
|
||||||
@@ -157,12 +157,12 @@ public:
|
|||||||
};
|
};
|
||||||
class Data_Feed_TCP_Client : public Data_Feed, public Data_Feed_TCP_Client_Data {
|
class Data_Feed_TCP_Client : public Data_Feed, public Data_Feed_TCP_Client_Data {
|
||||||
public:
|
public:
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
asio::awaitable<void> handle_in_loop_coro() override {
|
||||||
co_await cli.tick_coro();
|
co_await cli.tick_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> send_coro(const std::string& data) override {
|
asio::awaitable<void> send_coro(const std::string& data) override {
|
||||||
co_await cli.send_coro(data);
|
co_await cli.send_coro(data);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -178,12 +178,12 @@ public:
|
|||||||
Psc::asio_socket::TCP_Client_Coro cli;
|
Psc::asio_socket::TCP_Client_Coro cli;
|
||||||
~Data_Feed_TCP_Client() override = default;
|
~Data_Feed_TCP_Client() override = default;
|
||||||
|
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
co_await cli.close_coro();
|
co_await cli.close_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
Psc::asio_socket::Sockaddr_In sockaddr_in;
|
Psc::asio_socket::Sockaddr_In sockaddr_in;
|
||||||
sockaddr_in.ip = url;
|
sockaddr_in.ip = url;
|
||||||
sockaddr_in.port = port;
|
sockaddr_in.port = port;
|
||||||
@@ -216,8 +216,8 @@ public:
|
|||||||
|
|
||||||
Data_Feed_UDP_Server() = default;
|
Data_Feed_UDP_Server() = default;
|
||||||
~Data_Feed_UDP_Server() override = default;
|
~Data_Feed_UDP_Server() override = default;
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override;
|
asio::awaitable<void> handle_in_loop_coro() override;
|
||||||
concurrencpp::result<void> send_coro(const std::string& data) override;
|
asio::awaitable<void> send_coro(const std::string& data) override;
|
||||||
|
|
||||||
[[nodiscard]] Psc::JSON get_clients_json() const {
|
[[nodiscard]] Psc::JSON get_clients_json() const {
|
||||||
Psc::JSON ret = Psc::JSON::array();
|
Psc::JSON ret = Psc::JSON::array();
|
||||||
@@ -239,12 +239,12 @@ public:
|
|||||||
return Data_Feed::to_json() += Data_Feed_UDP_Server_Data::to_base_json();
|
return Data_Feed::to_json() += Data_Feed_UDP_Server_Data::to_base_json();
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
co_await svr.close_coro();
|
co_await svr.close_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _open() override;
|
asio::awaitable<void> _open() override;
|
||||||
Psc::asio_socket::UDP_Server_Coro svr;
|
Psc::asio_socket::UDP_Server_Coro svr;
|
||||||
};
|
};
|
||||||
class Data_Feed_UDP_Client_Data {
|
class Data_Feed_UDP_Client_Data {
|
||||||
@@ -263,12 +263,12 @@ public:
|
|||||||
return VAR_JSON_1(state);
|
return VAR_JSON_1(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
asio::awaitable<void> handle_in_loop_coro() override {
|
||||||
co_await cli.tick_coro();
|
co_await cli.tick_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> send_coro(const std::string& data) override {
|
asio::awaitable<void> send_coro(const std::string& data) override {
|
||||||
co_await cli.send_coro(data);
|
co_await cli.send_coro(data);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -284,14 +284,14 @@ public:
|
|||||||
return Data_Feed::to_json() += Data_Feed_UDP_Client_Data::to_base_json();
|
return Data_Feed::to_json() += Data_Feed_UDP_Client_Data::to_base_json();
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
std::cout << "udp client target address:" << cli.dest_address.to_string()
|
std::cout << "udp client target address:" << cli.dest_address.to_string()
|
||||||
<< " closed" << std::endl;
|
<< " closed" << std::endl;
|
||||||
co_await cli.close_coro();
|
co_await cli.close_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
cli.create();
|
cli.create();
|
||||||
cli.set_dest_address(url, port);
|
cli.set_dest_address(url, port);
|
||||||
co_await cli.connect_coro();
|
co_await cli.connect_coro();
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
#include "../server/Global.h"
|
#include "../server/Global.h"
|
||||||
#include "Database.h"
|
#include "Database.h"
|
||||||
|
|
||||||
Psc::serial::Serial* create_serial(const std::string& serial_name,
|
Psc::serial::Serial* create_serial(const std::string& serial_name,
|
||||||
Baud_Rate_Type baud_rate) {
|
Baud_Rate_Type baud_rate) {
|
||||||
auto serial = new Psc::serial::Serial;
|
auto serial = new Psc::serial::Serial;
|
||||||
@@ -25,8 +24,7 @@ Psc::serial::Serial* create_serial(const std::string& serial_name,
|
|||||||
}
|
}
|
||||||
return serial;
|
return serial;
|
||||||
}
|
}
|
||||||
|
std::shared_ptr<Data_Source> create_from_json(const JSON *that_json) {
|
||||||
std::shared_ptr<Data_Source> create_from_json(const JSON* that_json) {
|
|
||||||
std::string type = that_json->get_string("type");
|
std::string type = that_json->get_string("type");
|
||||||
std::shared_ptr<Data_Source> ret = create_data_source_from_type(type);
|
std::shared_ptr<Data_Source> ret = create_data_source_from_type(type);
|
||||||
ret->from_json(that_json);
|
ret->from_json(that_json);
|
||||||
@@ -36,11 +34,9 @@ std::shared_ptr<Data_Source> create_from_json(const JSON* that_json) {
|
|||||||
// }
|
// }
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Data_Source> Data_Source::that() {
|
std::shared_ptr<Data_Source> Data_Source::that() {
|
||||||
return shared_from_this();
|
return shared_from_this();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Data_Source::registered() const {
|
bool Data_Source::registered() const {
|
||||||
auto all_source = Global::instance()->mode_acs.data_source_config.map.list();
|
auto all_source = Global::instance()->mode_acs.data_source_config.map.list();
|
||||||
for (auto& item : all_source) {
|
for (auto& item : all_source) {
|
||||||
@@ -50,7 +46,6 @@ bool Data_Source::registered() const {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Psc::JSON Data_Source::get_state() {
|
Psc::JSON Data_Source::get_state() {
|
||||||
Psc::JSON ret = Psc::JSON::object();
|
Psc::JSON ret = Psc::JSON::object();
|
||||||
ret.append_list(get_custom_state_json().children);
|
ret.append_list(get_custom_state_json().children);
|
||||||
@@ -58,7 +53,6 @@ Psc::JSON Data_Source::get_state() {
|
|||||||
ret.append({"ds: value_statistics(byte)", value_statistics});
|
ret.append({"ds: value_statistics(byte)", value_statistics});
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data_Source::Data_Source() {
|
Data_Source::Data_Source() {
|
||||||
// parse_format = std::make_shared<Input_Format>();
|
// parse_format = std::make_shared<Input_Format>();
|
||||||
// 写入到配置文件是懒加载 其他保存时他跟着保存
|
// 写入到配置文件是懒加载 其他保存时他跟着保存
|
||||||
@@ -70,12 +64,10 @@ Data_Source::Data_Source() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Data_Source::thread_key() const {
|
std::string Data_Source::thread_key() const {
|
||||||
// return "Data_Source_Handle_Thread:[" + key + "]";
|
// return "Data_Source_Handle_Thread:[" + key + "]";
|
||||||
return key + "_DS_HT";
|
return key + "_DS_HT";
|
||||||
}
|
}
|
||||||
|
|
||||||
void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
|
void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
|
||||||
auto cur = std::dynamic_pointer_cast<SSR::Mode_Msg>(msg);
|
auto cur = std::dynamic_pointer_cast<SSR::Mode_Msg>(msg);
|
||||||
if (cur != nullptr) {
|
if (cur != nullptr) {
|
||||||
@@ -90,7 +82,6 @@ void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> readLines(const std::string& path) {
|
std::vector<std::string> readLines(const std::string& path) {
|
||||||
std::vector<std::string> lines;
|
std::vector<std::string> lines;
|
||||||
std::filesystem::path p =
|
std::filesystem::path p =
|
||||||
@@ -146,7 +137,6 @@ std::vector<std::string> readLines(const std::string& path) {
|
|||||||
std::cout << oss.str() << std::flush;
|
std::cout << oss.str() << std::flush;
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string extractID(const std::string& logLine) {
|
std::string extractID(const std::string& logLine) {
|
||||||
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
||||||
if (lastSpacePos != std::string::npos) {
|
if (lastSpacePos != std::string::npos) {
|
||||||
@@ -154,7 +144,6 @@ std::string extractID(const std::string& logLine) {
|
|||||||
}
|
}
|
||||||
return logLine;
|
return logLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t convert_to_timestamp(const std::string& str) {
|
time_t convert_to_timestamp(const std::string& str) {
|
||||||
// 创建一个结构体 tm 来存储解析后的时间
|
// 创建一个结构体 tm 来存储解析后的时间
|
||||||
std::tm timeStruct = {};
|
std::tm timeStruct = {};
|
||||||
@@ -172,7 +161,6 @@ time_t convert_to_timestamp(const std::string& str) {
|
|||||||
}
|
}
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<std::string, time_t> extractID2(const std::string& logLine) {
|
std::tuple<std::string, time_t> extractID2(const std::string& logLine) {
|
||||||
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
||||||
time_t t = convert_to_timestamp(logLine.substr(0, 19));
|
time_t t = convert_to_timestamp(logLine.substr(0, 19));
|
||||||
@@ -184,15 +172,13 @@ std::tuple<std::string, time_t> extractID2(const std::string& logLine) {
|
|||||||
}
|
}
|
||||||
return {logLine, t};
|
return {logLine, t};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string bin_format(const std::string& hex, time_t t) {
|
std::string bin_format(const std::string& hex, time_t t) {
|
||||||
std::tm* currentTime = std::localtime(&t);
|
std::tm *currentTime = std::localtime(&t);
|
||||||
auto sec = currentTime->tm_hour * 3600 + currentTime->tm_min * 60 +
|
auto sec = currentTime->tm_hour * 3600 + currentTime->tm_min * 60 +
|
||||||
currentTime->tm_sec;
|
currentTime->tm_sec;
|
||||||
SSR::MLAT_timestamp a(sec, 0);
|
SSR::MLAT_timestamp a(sec, 0);
|
||||||
return create_Binary_Format_memory(hex, 0, &a);
|
return create_Binary_Format_memory(hex, 0, &a);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
|
std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
|
||||||
if (index == 0 && part_infos.empty()) {
|
if (index == 0 && part_infos.empty()) {
|
||||||
ret_index = -1;
|
ret_index = -1;
|
||||||
@@ -220,9 +206,8 @@ std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
|
|||||||
ret_index = index + 1;
|
ret_index = index + 1;
|
||||||
return part_infos[index++];
|
return part_infos[index++];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1A 33 1A 1A F1 FB 87 73 7E 7F a8001d81a87543b0a80000
|
// 1A 33 1A 1A F1 FB 87 73 7E 7F a8001d81a87543b0a80000
|
||||||
std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type,
|
std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type,
|
||||||
int index, const std::string& log_info) {
|
int index, const std::string& log_info) {
|
||||||
std::string ret;
|
std::string ret;
|
||||||
// 2025-01-06 07:20:17 [info] dc2541e65001401a3119b2dd1c7af67132201
|
// 2025-01-06 07:20:17 [info] dc2541e65001401a3119b2dd1c7af67132201
|
||||||
@@ -326,15 +311,13 @@ std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type,
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> File_Data_Source::_close() {
|
||||||
concurrencpp::result<void> File_Data_Source::_close() {
|
|
||||||
std::lock_guard g(mtx);
|
std::lock_guard g(mtx);
|
||||||
index = 0;
|
index = 0;
|
||||||
part_infos.clear();
|
part_infos.clear();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> File_Data_Source::_open() {
|
||||||
concurrencpp::result<void> File_Data_Source::_open() {
|
|
||||||
std::lock_guard g(mtx);
|
std::lock_guard g(mtx);
|
||||||
auto path = get_true_file_path();
|
auto path = get_true_file_path();
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
@@ -351,7 +334,6 @@ concurrencpp::result<void> File_Data_Source::_open() {
|
|||||||
state = "已加载" + to_string(part_infos.size()) + "长度数据!";
|
state = "已加载" + to_string(part_infos.size()) + "长度数据!";
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> File_Data_Source::readBinaryFileAsString(const std::string& filepath,
|
std::vector<std::string> File_Data_Source::readBinaryFileAsString(const std::string& filepath,
|
||||||
size_t part_size) {
|
size_t part_size) {
|
||||||
// 打开文件(以二进制模式)
|
// 打开文件(以二进制模式)
|
||||||
@@ -385,7 +367,6 @@ std::vector<std::string> File_Data_Source::readBinaryFileAsString(const std::str
|
|||||||
file.close();
|
file.close();
|
||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
||||||
std::lock_guard g(mtx);
|
std::lock_guard g(mtx);
|
||||||
assert(mode_data.size() == 0);
|
assert(mode_data.size() == 0);
|
||||||
@@ -416,13 +397,10 @@ void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
|||||||
mode_data = ret;
|
mode_data = ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
|
void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
|
||||||
if (!msg)
|
if (!msg) return;
|
||||||
return;
|
|
||||||
cache_list.push_back(std::make_shared<Cache_Msg>(std::move(msg)));
|
cache_list.push_back(std::make_shared<Cache_Msg>(std::move(msg)));
|
||||||
if (cache_list.empty())
|
if (cache_list.empty()) return;
|
||||||
return;
|
|
||||||
if (pre_cache == nullptr) {
|
if (pre_cache == nullptr) {
|
||||||
pre_cache = cache_list.front();
|
pre_cache = cache_list.front();
|
||||||
cache_list.pop_front();
|
cache_list.pop_front();
|
||||||
@@ -432,8 +410,7 @@ void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
|
|||||||
}
|
}
|
||||||
auto sys_time = player_clock.get_cur_time_point();
|
auto sys_time = player_clock.get_cur_time_point();
|
||||||
while (true) {
|
while (true) {
|
||||||
if (cache_list.empty())
|
if (cache_list.empty()) break;
|
||||||
break;
|
|
||||||
auto pre = pre_cache;
|
auto pre = pre_cache;
|
||||||
auto cur = cache_list.front();
|
auto cur = cache_list.front();
|
||||||
cur->day_num = pre->day_num;
|
cur->day_num = pre->day_num;
|
||||||
@@ -448,11 +425,9 @@ void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dll_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
void Dll_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
||||||
auto start = std::chrono::steady_clock::now();
|
auto start = std::chrono::steady_clock::now();
|
||||||
if (read_func_ptr == nullptr)
|
if (read_func_ptr == nullptr) return;
|
||||||
return;
|
|
||||||
auto buf = reinterpret_cast<char*>(buffer.data());
|
auto buf = reinterpret_cast<char*>(buffer.data());
|
||||||
auto len = read_func_ptr(buf, buffer_size);
|
auto len = read_func_ptr(buf, buffer_size);
|
||||||
vs.update(len);
|
vs.update(len);
|
||||||
@@ -472,18 +447,15 @@ void Dll_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
|||||||
// std::cout << "Dll_Data_Source::read cost: " << cost << " us\n";
|
// std::cout << "Dll_Data_Source::read cost: " << cost << " us\n";
|
||||||
mode_data = ret;
|
mode_data = ret;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> Shared_Memory_Data_Source::_open() {
|
||||||
concurrencpp::result<void> Shared_Memory_Data_Source::_open() {
|
|
||||||
sm = std::make_unique<Psc::SM_RingBuffer>();
|
sm = std::make_unique<Psc::SM_RingBuffer>();
|
||||||
sm->init(shared_memory_name, shared_memory_size);
|
sm->init(shared_memory_name, shared_memory_size);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
asio::awaitable<void> Shared_Memory_Data_Source::_close() {
|
||||||
concurrencpp::result<void> Shared_Memory_Data_Source::_close() {
|
|
||||||
sm.reset();
|
sm.reset();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shared_Memory_Data_Source::origin_data_transform_mode_data(
|
void Shared_Memory_Data_Source::origin_data_transform_mode_data(
|
||||||
std::string& mode_data) {
|
std::string& mode_data) {
|
||||||
auto size = sm->shm.size();
|
auto size = sm->shm.size();
|
||||||
@@ -503,8 +475,7 @@ void Shared_Memory_Data_Source::origin_data_transform_mode_data(
|
|||||||
auto ret = get_true_from_raw_line(this, data_type, -1, data);
|
auto ret = get_true_from_raw_line(this, data_type, -1, data);
|
||||||
mode_data = ret;
|
mode_data = ret;
|
||||||
}
|
}
|
||||||
|
void Data_Source_Config::server(Global *g) {
|
||||||
void Data_Source_Config::server(Global* g) {
|
|
||||||
auto& svr = g->svr;
|
auto& svr = g->svr;
|
||||||
auto& api = g->api;
|
auto& api = g->api;
|
||||||
std::string name = "data_source";
|
std::string name = "data_source";
|
||||||
@@ -536,9 +507,6 @@ void Data_Source_Config::server(Global* g) {
|
|||||||
CHECK_JSON_PARAM
|
CHECK_JSON_PARAM
|
||||||
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
|
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
|
||||||
HTTP_REQUIRE_VALUE(ds, map.get(key))
|
HTTP_REQUIRE_VALUE(ds, map.get(key))
|
||||||
// 直接关闭
|
|
||||||
// ds->enable = false;
|
|
||||||
// ds->close();
|
|
||||||
ds->from_json(¶ms);
|
ds->from_json(¶ms);
|
||||||
g->save();
|
g->save();
|
||||||
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
||||||
|
|||||||
@@ -73,15 +73,15 @@ class Data_Source : public std::enable_shared_from_this<Data_Source>,
|
|||||||
public Data_Source_Handler, public Data_Source_Data {
|
public Data_Source_Handler, public Data_Source_Data {
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<Data_Source> that();
|
std::shared_ptr<Data_Source> that();
|
||||||
virtual concurrencpp::result<std::string> read_coro() {co_return "";}
|
virtual asio::awaitable<std::string> read_coro() {co_return "";}
|
||||||
bool registered() const;
|
bool registered() const;
|
||||||
virtual Psc::JSON get_custom_state_json() = 0;
|
virtual Psc::JSON get_custom_state_json() = 0;
|
||||||
Psc::JSON get_state();
|
Psc::JSON get_state();
|
||||||
std::string last_char; // 用于处理奇数字节
|
std::string last_char; // 用于处理奇数字节
|
||||||
Data_Source();
|
Data_Source();
|
||||||
concurrencpp::result<void> loop_coro() final;
|
asio::awaitable<void> loop_coro() final;
|
||||||
std::shared_ptr<SSR::Mode_Msg> last_prase_msg = nullptr;
|
std::shared_ptr<SSR::Mode_Msg> last_prase_msg = nullptr;
|
||||||
virtual concurrencpp::result<void> handle_in_loop_coro() { co_return;}
|
virtual asio::awaitable<void> handle_in_loop_coro() { co_return;}
|
||||||
Frequency_Limit statistic_fl;
|
Frequency_Limit statistic_fl;
|
||||||
std::string thread_key() const;
|
std::string thread_key() const;
|
||||||
Psc::JSON statistic_json() {
|
Psc::JSON statistic_json() {
|
||||||
@@ -110,7 +110,7 @@ public:
|
|||||||
};
|
};
|
||||||
class TCP_Client_Data_Source : public Data_Source, public TCP_Client_Data_Source_Data {
|
class TCP_Client_Data_Source : public Data_Source, public TCP_Client_Data_Source_Data {
|
||||||
public:
|
public:
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
asio::awaitable<void> handle_in_loop_coro() override {
|
||||||
co_await cli.tick_coro();
|
co_await cli.tick_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ public:
|
|||||||
TCP_Client_Data_Source() {
|
TCP_Client_Data_Source() {
|
||||||
type = "TCP_Client_Data_Source";
|
type = "TCP_Client_Data_Source";
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
Psc::asio_socket::Sockaddr_In addr;
|
Psc::asio_socket::Sockaddr_In addr;
|
||||||
addr.ip = ip;
|
addr.ip = ip;
|
||||||
addr.port = port;
|
addr.port = port;
|
||||||
@@ -132,7 +132,7 @@ public:
|
|||||||
co_await cli.connect_coro();
|
co_await cli.connect_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
co_await cli.close_coro();
|
co_await cli.close_coro();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -143,7 +143,7 @@ public:
|
|||||||
Data_Source::from_json(that_json);
|
Data_Source::from_json(that_json);
|
||||||
TCP_Client_Data_Source_Data::from_base_json(that_json);
|
TCP_Client_Data_Source_Data::from_base_json(that_json);
|
||||||
}
|
}
|
||||||
concurrencpp::result<std::string> read_coro() override {
|
asio::awaitable<std::string> read_coro() override {
|
||||||
co_return co_await cli.read_coro();
|
co_return co_await cli.read_coro();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -161,7 +161,7 @@ public:
|
|||||||
Serial_Data_Source() {
|
Serial_Data_Source() {
|
||||||
this->type = "Serial_Data_Source";
|
this->type = "Serial_Data_Source";
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
serial = std::make_unique<Psc::serial::Serial_Coro>();
|
serial = std::make_unique<Psc::serial::Serial_Coro>();
|
||||||
serial->set_serial_name(port_name);
|
serial->set_serial_name(port_name);
|
||||||
serial->set_baud_rate(baud_rate);
|
serial->set_baud_rate(baud_rate);
|
||||||
@@ -181,18 +181,18 @@ public:
|
|||||||
}
|
}
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
if (serial)
|
if (serial)
|
||||||
serial->close();
|
serial->close();
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
asio::awaitable<void> handle_in_loop_coro() override {
|
||||||
if (serial) {
|
if (serial) {
|
||||||
co_await serial->tick_coro();
|
co_await serial->tick_coro();
|
||||||
}
|
}
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
concurrencpp::result<std::string> read_coro() override {
|
asio::awaitable<std::string> read_coro() override {
|
||||||
if (!serial) {
|
if (!serial) {
|
||||||
co_return "";
|
co_return "";
|
||||||
}
|
}
|
||||||
@@ -261,8 +261,8 @@ public:
|
|||||||
std::string get_true_file_path() const {
|
std::string get_true_file_path() const {
|
||||||
return Psc::get_abs_path(file_path);
|
return Psc::get_abs_path(file_path);
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _close() override;
|
asio::awaitable<void> _close() override;
|
||||||
concurrencpp::result<void> _open() override;
|
asio::awaitable<void> _open() override;
|
||||||
std::vector<std::string> readBinaryFileAsString(const std::string& filepath,
|
std::vector<std::string> readBinaryFileAsString(const std::string& filepath,
|
||||||
size_t part_size);
|
size_t part_size);
|
||||||
void from_json(const Psc::JSON* that_json) override {
|
void from_json(const Psc::JSON* that_json) override {
|
||||||
@@ -316,11 +316,11 @@ public:
|
|||||||
using Call_Back = void (*)(char* buf, std::size_t len);
|
using Call_Back = void (*)(char* buf, std::size_t len);
|
||||||
using set_Call_back = void (*)(Call_Back);
|
using set_Call_back = void (*)(Call_Back);
|
||||||
std::string state;
|
std::string state;
|
||||||
concurrencpp::result<void> _close() override {
|
asio::awaitable<void> _close() override {
|
||||||
Psc::free_library(lib);
|
Psc::free_library(lib);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _open() override {
|
asio::awaitable<void> _open() override {
|
||||||
{
|
{
|
||||||
auto r = Psc::try_load_library(Psc::get_abs_path(library_path));
|
auto r = Psc::try_load_library(Psc::get_abs_path(library_path));
|
||||||
if (!r) {
|
if (!r) {
|
||||||
@@ -363,8 +363,8 @@ public:
|
|||||||
Psc::JSON to_json() override {
|
Psc::JSON to_json() override {
|
||||||
return Data_Source::to_json() += Shared_Memory_Data_Source_Data::to_base_json();
|
return Data_Source::to_json() += Shared_Memory_Data_Source_Data::to_base_json();
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> _open() override;
|
asio::awaitable<void> _open() override;
|
||||||
concurrencpp::result<void> _close() override;
|
asio::awaitable<void> _close() override;
|
||||||
void origin_data_transform_mode_data(std::string& data) override;
|
void origin_data_transform_mode_data(std::string& data) override;
|
||||||
protected:
|
protected:
|
||||||
std::unique_ptr<Psc::SM_RingBuffer> sm;
|
std::unique_ptr<Psc::SM_RingBuffer> sm;
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ void run_external_database_async(Work &&work, Callback &&callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename Starter>
|
template <typename T, typename Starter>
|
||||||
concurrencpp::result<T> await_external_database_callback(Starter starter) {
|
asio::awaitable<T> await_external_database_callback(Starter starter) {
|
||||||
auto result = co_await Psc::coro::callback_result<T>(
|
auto result = co_await Psc::coro::callback_result<T>(
|
||||||
[starter = std::move(starter)](auto done) mutable {
|
[starter = std::move(starter)](auto done) mutable {
|
||||||
starter([done = std::move(done)](std::exception_ptr exception,
|
starter([done = std::move(done)](std::exception_ptr exception,
|
||||||
@@ -417,7 +417,7 @@ void External_Resources_Manager::async_external_database_status(
|
|||||||
[this]() { return external_database_status(); }, std::move(callback));
|
[this]() { return external_database_status(); }, std::move(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<std::vector<External_Resource_Status>>
|
asio::awaitable<std::vector<External_Resource_Status>>
|
||||||
External_Resources_Manager::refresh_external_databases_coro(
|
External_Resources_Manager::refresh_external_databases_coro(
|
||||||
const bool force_download) {
|
const bool force_download) {
|
||||||
co_return co_await await_external_database_callback<
|
co_return co_await await_external_database_callback<
|
||||||
@@ -427,7 +427,7 @@ External_Resources_Manager::refresh_external_databases_coro(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<External_Resource_Status>
|
asio::awaitable<External_Resource_Status>
|
||||||
External_Resources_Manager::refresh_external_database_coro(
|
External_Resources_Manager::refresh_external_database_coro(
|
||||||
std::string resource_name, const bool force_download) {
|
std::string resource_name, const bool force_download) {
|
||||||
co_return co_await await_external_database_callback<External_Resource_Status>(
|
co_return co_await await_external_database_callback<External_Resource_Status>(
|
||||||
@@ -438,7 +438,7 @@ External_Resources_Manager::refresh_external_database_coro(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<External_Resource_Status>
|
asio::awaitable<External_Resource_Status>
|
||||||
External_Resources_Manager::import_external_database_coro(
|
External_Resources_Manager::import_external_database_coro(
|
||||||
std::string resource_name, std::filesystem::path source_file) {
|
std::string resource_name, std::filesystem::path source_file) {
|
||||||
co_return co_await await_external_database_callback<External_Resource_Status>(
|
co_return co_await await_external_database_callback<External_Resource_Status>(
|
||||||
@@ -450,7 +450,7 @@ External_Resources_Manager::import_external_database_coro(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<External_Resource_Status>
|
asio::awaitable<External_Resource_Status>
|
||||||
External_Resources_Manager::clear_external_database_table_coro(
|
External_Resources_Manager::clear_external_database_table_coro(
|
||||||
std::string resource_name) {
|
std::string resource_name) {
|
||||||
co_return co_await await_external_database_callback<External_Resource_Status>(
|
co_return co_await await_external_database_callback<External_Resource_Status>(
|
||||||
@@ -461,7 +461,7 @@ External_Resources_Manager::clear_external_database_table_coro(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<std::optional<External_Database_Row>>
|
asio::awaitable<std::optional<External_Database_Row>>
|
||||||
External_Resources_Manager::query_external_database_coro(
|
External_Resources_Manager::query_external_database_coro(
|
||||||
std::string resource_name,
|
std::string resource_name,
|
||||||
std::vector<std::string> primary_key_values) const {
|
std::vector<std::string> primary_key_values) const {
|
||||||
@@ -476,7 +476,7 @@ External_Resources_Manager::query_external_database_coro(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<std::shared_ptr<External_Database_Row_Map>>
|
asio::awaitable<std::shared_ptr<External_Database_Row_Map>>
|
||||||
External_Resources_Manager::query_aircraft_external_databases_coro(
|
External_Resources_Manager::query_aircraft_external_databases_coro(
|
||||||
std::string icao24) const {
|
std::string icao24) const {
|
||||||
auto result = co_await Psc::coro::callback_result<
|
auto result = co_await Psc::coro::callback_result<
|
||||||
@@ -497,7 +497,7 @@ External_Resources_Manager::query_aircraft_external_databases_coro(
|
|||||||
co_return result;
|
co_return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<std::shared_ptr<External_Database_Row_Map>>
|
asio::awaitable<std::shared_ptr<External_Database_Row_Map>>
|
||||||
External_Resources_Manager::query_callsign_external_databases_coro(
|
External_Resources_Manager::query_callsign_external_databases_coro(
|
||||||
std::optional<std::string> callsign) const {
|
std::optional<std::string> callsign) const {
|
||||||
auto result = co_await Psc::coro::callback_result<
|
auto result = co_await Psc::coro::callback_result<
|
||||||
@@ -518,7 +518,7 @@ External_Resources_Manager::query_callsign_external_databases_coro(
|
|||||||
co_return result;
|
co_return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrencpp::result<std::vector<External_Resource_Status>>
|
asio::awaitable<std::vector<External_Resource_Status>>
|
||||||
External_Resources_Manager::external_database_status_coro() const {
|
External_Resources_Manager::external_database_status_coro() const {
|
||||||
co_return co_await await_external_database_callback<
|
co_return co_await await_external_database_callback<
|
||||||
std::vector<External_Resource_Status>>(
|
std::vector<External_Resource_Status>>(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Core/Base/JSON.h"
|
#include "Core/Base/JSON.h"
|
||||||
#include <SQLiteCpp/Database.h>
|
#include <SQLiteCpp/Database.h>
|
||||||
#include <concurrencpp/concurrencpp.h>
|
#include <asio/awaitable.hpp>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@@ -103,14 +103,14 @@ public:
|
|||||||
void async_query_aircraft_external_databases(std::string icao24, Row_Map_Callback callback) const;
|
void async_query_aircraft_external_databases(std::string icao24, Row_Map_Callback callback) const;
|
||||||
void async_query_callsign_external_databases(std::optional<std::string> callsign, Row_Map_Callback callback) const;
|
void async_query_callsign_external_databases(std::optional<std::string> callsign, Row_Map_Callback callback) const;
|
||||||
void async_external_database_status(Status_List_Callback callback) const;
|
void async_external_database_status(Status_List_Callback callback) const;
|
||||||
[[nodiscard]] concurrencpp::result<std::vector<External_Resource_Status>> refresh_external_databases_coro(bool force_download = true);
|
[[nodiscard]] asio::awaitable<std::vector<External_Resource_Status>> refresh_external_databases_coro(bool force_download = true);
|
||||||
[[nodiscard]] concurrencpp::result<External_Resource_Status> refresh_external_database_coro(std::string resource_name, bool force_download = true);
|
[[nodiscard]] asio::awaitable<External_Resource_Status> refresh_external_database_coro(std::string resource_name, bool force_download = true);
|
||||||
[[nodiscard]] concurrencpp::result<External_Resource_Status> import_external_database_coro(std::string resource_name, std::filesystem::path source_file);
|
[[nodiscard]] asio::awaitable<External_Resource_Status> import_external_database_coro(std::string resource_name, std::filesystem::path source_file);
|
||||||
[[nodiscard]] concurrencpp::result<External_Resource_Status> clear_external_database_table_coro(std::string resource_name);
|
[[nodiscard]] asio::awaitable<External_Resource_Status> clear_external_database_table_coro(std::string resource_name);
|
||||||
[[nodiscard]] concurrencpp::result<std::optional<External_Database_Row>> query_external_database_coro(std::string resource_name, std::vector<std::string> primary_key_values) const;
|
[[nodiscard]] asio::awaitable<std::optional<External_Database_Row>> query_external_database_coro(std::string resource_name, std::vector<std::string> primary_key_values) const;
|
||||||
[[nodiscard]] concurrencpp::result<std::shared_ptr<External_Database_Row_Map>> query_aircraft_external_databases_coro(std::string icao24) const;
|
[[nodiscard]] asio::awaitable<std::shared_ptr<External_Database_Row_Map>> query_aircraft_external_databases_coro(std::string icao24) const;
|
||||||
[[nodiscard]] concurrencpp::result<std::shared_ptr<External_Database_Row_Map>> query_callsign_external_databases_coro(std::optional<std::string> callsign) const;
|
[[nodiscard]] asio::awaitable<std::shared_ptr<External_Database_Row_Map>> query_callsign_external_databases_coro(std::optional<std::string> callsign) const;
|
||||||
[[nodiscard]] concurrencpp::result<std::vector<External_Resource_Status>> external_database_status_coro() const;
|
[[nodiscard]] asio::awaitable<std::vector<External_Resource_Status>> external_database_status_coro() const;
|
||||||
void register_external_resource(std::unique_ptr<External_Resources> external_res);
|
void register_external_resource(std::unique_ptr<External_Resources> external_res);
|
||||||
void initialize(SQLite::Database& db, std::filesystem::path cache_pos);
|
void initialize(SQLite::Database& db, std::filesystem::path cache_pos);
|
||||||
std::vector<External_Resource_Status> sync_missing(SQLite::Database& db);
|
std::vector<External_Resource_Status> sync_missing(SQLite::Database& db);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <concurrencpp/concurrencpp.h>
|
#include <asio/awaitable.hpp>
|
||||||
|
#include <asio/co_spawn.hpp>
|
||||||
|
#include <asio/thread_pool.hpp>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
#include <drogon/drogon.h>
|
#include <drogon/drogon.h>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
@@ -7,113 +9,105 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <trantor/net/EventLoop.h>
|
#include <trantor/net/EventLoop.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace Ecap_Coro {
|
namespace Ecap_Coro {
|
||||||
template <typename T> class Concurrencpp_Drogon_Awaiter {
|
inline asio::thread_pool& asio_drogon_pool() {
|
||||||
|
static asio::thread_pool pool(1);
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
template <typename T> class Asio_Drogon_Awaiter {
|
||||||
public:
|
public:
|
||||||
explicit Concurrencpp_Drogon_Awaiter(concurrencpp::result<T> &&task)
|
explicit Asio_Drogon_Awaiter(asio::awaitable<T>&& task)
|
||||||
: state_(std::make_shared<State>()), task_(std::move(task)) {}
|
: state_(std::make_shared<State>()), task_(std::move(task)) {}
|
||||||
bool await_ready() const noexcept { return false; }
|
bool await_ready() const noexcept { return false; }
|
||||||
void await_suspend(std::coroutine_handle<> continuation) {
|
void await_suspend(std::coroutine_handle<> continuation) {
|
||||||
state_->continuation = continuation;
|
state_->continuation = continuation;
|
||||||
state_->loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
state_->loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
||||||
if (state_->loop == nullptr) {
|
if (state_->loop == nullptr) {
|
||||||
state_->loop = drogon::app().getLoop();
|
state_->loop = drogon::app().getLoop();
|
||||||
}
|
}
|
||||||
state_->running_task.emplace(run(state_, std::move(task_)));
|
asio::co_spawn(asio_drogon_pool(), std::move(task_),
|
||||||
}
|
[state = state_](std::exception_ptr exception, T value) mutable {
|
||||||
T await_resume() {
|
state->exception = exception;
|
||||||
if (state_->exception) {
|
if (!exception) {
|
||||||
std::rethrow_exception(state_->exception);
|
state->value.emplace(std::move(value));
|
||||||
}
|
}
|
||||||
return std::move(*state_->value);
|
resume(std::move(state));
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
T await_resume() {
|
||||||
|
if (state_->exception) {
|
||||||
|
std::rethrow_exception(state_->exception);
|
||||||
|
}
|
||||||
|
return std::move(*state_->value);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
struct State {
|
struct State {
|
||||||
trantor::EventLoop *loop{};
|
trantor::EventLoop* loop{};
|
||||||
std::coroutine_handle<> continuation{};
|
std::coroutine_handle<> continuation{};
|
||||||
std::optional<T> value{};
|
std::optional<T> value{};
|
||||||
std::exception_ptr exception{};
|
std::exception_ptr exception{};
|
||||||
std::optional<concurrencpp::result<void>> running_task{};
|
};
|
||||||
};
|
static void resume(std::shared_ptr<State> state) {
|
||||||
static concurrencpp::result<void> run(std::shared_ptr<State> state,
|
auto resume_fn = [state] { state->continuation.resume(); };
|
||||||
concurrencpp::result<T> task) {
|
if (state->loop != nullptr) {
|
||||||
try {
|
state->loop->queueInLoop(std::move(resume_fn));
|
||||||
state->value.emplace(co_await task);
|
}
|
||||||
} catch (...) {
|
else {
|
||||||
state->exception = std::current_exception();
|
resume_fn();
|
||||||
}
|
}
|
||||||
resume(std::move(state));
|
}
|
||||||
co_return;
|
std::shared_ptr<State> state_;
|
||||||
}
|
asio::awaitable<T> task_;
|
||||||
static void resume(std::shared_ptr<State> state) {
|
|
||||||
auto resume_fn = [state]() { state->continuation.resume(); };
|
|
||||||
if (state->loop != nullptr) {
|
|
||||||
state->loop->queueInLoop(std::move(resume_fn));
|
|
||||||
} else {
|
|
||||||
resume_fn();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::shared_ptr<State> state_;
|
|
||||||
concurrencpp::result<T> task_;
|
|
||||||
};
|
};
|
||||||
template <> class Concurrencpp_Drogon_Awaiter<void> {
|
template <> class Asio_Drogon_Awaiter<void> {
|
||||||
public:
|
public:
|
||||||
explicit Concurrencpp_Drogon_Awaiter(concurrencpp::result<void> &&task)
|
explicit Asio_Drogon_Awaiter(asio::awaitable<void>&& task)
|
||||||
: state_(std::make_shared<State>()), task_(std::move(task)) {}
|
: state_(std::make_shared<State>()), task_(std::move(task)) {}
|
||||||
bool await_ready() const noexcept { return false; }
|
bool await_ready() const noexcept { return false; }
|
||||||
void await_suspend(std::coroutine_handle<> continuation) {
|
void await_suspend(std::coroutine_handle<> continuation) {
|
||||||
state_->continuation = continuation;
|
state_->continuation = continuation;
|
||||||
state_->loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
state_->loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
||||||
if (state_->loop == nullptr) {
|
if (state_->loop == nullptr) {
|
||||||
state_->loop = drogon::app().getLoop();
|
state_->loop = drogon::app().getLoop();
|
||||||
}
|
}
|
||||||
state_->running_task.emplace(run(state_, std::move(task_)));
|
asio::co_spawn(asio_drogon_pool(), std::move(task_),
|
||||||
}
|
[state = state_](std::exception_ptr exception) mutable {
|
||||||
void await_resume() {
|
state->exception = exception;
|
||||||
if (state_->exception) {
|
resume(std::move(state));
|
||||||
std::rethrow_exception(state_->exception);
|
});
|
||||||
}
|
}
|
||||||
}
|
void await_resume() {
|
||||||
|
if (state_->exception) {
|
||||||
|
std::rethrow_exception(state_->exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
struct State {
|
struct State {
|
||||||
trantor::EventLoop *loop{};
|
trantor::EventLoop* loop{};
|
||||||
std::coroutine_handle<> continuation{};
|
std::coroutine_handle<> continuation{};
|
||||||
std::exception_ptr exception{};
|
std::exception_ptr exception{};
|
||||||
std::optional<concurrencpp::result<void>> running_task{};
|
};
|
||||||
};
|
static void resume(std::shared_ptr<State> state) {
|
||||||
static concurrencpp::result<void> run(std::shared_ptr<State> state,
|
auto resume_fn = [state] { state->continuation.resume(); };
|
||||||
concurrencpp::result<void> task) {
|
if (state->loop != nullptr) {
|
||||||
try {
|
state->loop->queueInLoop(std::move(resume_fn));
|
||||||
co_await task;
|
}
|
||||||
} catch (...) {
|
else {
|
||||||
state->exception = std::current_exception();
|
resume_fn();
|
||||||
}
|
}
|
||||||
resume(std::move(state));
|
}
|
||||||
co_return;
|
std::shared_ptr<State> state_;
|
||||||
}
|
asio::awaitable<void> task_;
|
||||||
static void resume(std::shared_ptr<State> state) {
|
|
||||||
auto resume_fn = [state]() { state->continuation.resume(); };
|
|
||||||
if (state->loop != nullptr) {
|
|
||||||
state->loop->queueInLoop(std::move(resume_fn));
|
|
||||||
} else {
|
|
||||||
resume_fn();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::shared_ptr<State> state_;
|
|
||||||
concurrencpp::result<void> task_;
|
|
||||||
};
|
};
|
||||||
template <typename T> auto to_drogon(concurrencpp::result<T> &&task) {
|
template <typename T> auto to_drogon(asio::awaitable<T>&& task) {
|
||||||
return Concurrencpp_Drogon_Awaiter<T>{std::move(task)};
|
return Asio_Drogon_Awaiter<T>{std::move(task)};
|
||||||
}
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
drogon::Task<T> to_drogon_task(concurrencpp::result<T> &&task) {
|
drogon::Task<T> to_drogon_task(asio::awaitable<T>&& task) {
|
||||||
co_return co_await to_drogon(std::move(task));
|
co_return co_await to_drogon(std::move(task));
|
||||||
|
}
|
||||||
|
inline drogon::Task<> to_drogon_task(asio::awaitable<void>&& task) {
|
||||||
|
co_await to_drogon(std::move(task));
|
||||||
|
co_return;
|
||||||
}
|
}
|
||||||
inline drogon::Task<> to_drogon_task(concurrencpp::result<void> &&task) {
|
|
||||||
co_await to_drogon(std::move(task));
|
|
||||||
co_return;
|
|
||||||
}
|
}
|
||||||
} // namespace Ecap_Coro
|
|
||||||
|
|||||||
@@ -1,46 +1,99 @@
|
|||||||
#include "With_Loop_Coro.h"
|
#include "With_Loop_Coro.h"
|
||||||
|
#include "io_coro.h"
|
||||||
With_Loop_Coro::~With_Loop_Coro() = default;
|
With_Loop_Coro::~With_Loop_Coro() = default;
|
||||||
|
|
||||||
void With_Loop_Coro::async_stop() {
|
void With_Loop_Coro::async_stop() {
|
||||||
enable = false;
|
std::string name = type + ":" + key;
|
||||||
|
set_state(State::Force_Quit, std::format("任务正常收到请求,等待退出 {}!\n", name).c_str());
|
||||||
|
loop_running = false;
|
||||||
}
|
}
|
||||||
|
void With_Loop_Coro::sync_wait() {
|
||||||
void With_Loop_Coro::sync_wait() const {
|
std::string name = type + ":" + key;
|
||||||
if (!loop_task) {
|
if (!loop_task) {
|
||||||
|
std::cout << std::format("{}退出成功! loop_task 未启动 \n", name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto last = std::chrono::steady_clock::now();
|
while (loop_task->wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) {
|
||||||
while (loop_task->status() == concurrencpp::result_status::idle) {
|
std::cout << std::format("{}等待退出 当前状态为{} \n", name, Psc::to_string(this->state));
|
||||||
auto now = std::chrono::steady_clock::now();
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
if (now - last > std::chrono::seconds(1)) {
|
|
||||||
std::cout << std::format("等待任务退出 {}:{}\n", type, key);
|
|
||||||
last = now;
|
|
||||||
}
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
||||||
}
|
}
|
||||||
|
std::cout << std::format("{}退出成功! \n", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool With_Loop_Coro::running() const {
|
bool With_Loop_Coro::running() const {
|
||||||
if (!loop_task) {
|
return loop_running;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return loop_task->status() == concurrencpp::result_status::idle;
|
|
||||||
}
|
}
|
||||||
|
void With_Loop_Coro::set_state(State state, std::string action) {
|
||||||
concurrencpp::result<void> With_Loop_Coro::sync_coro_loop_and_enable() {
|
if (!action.empty()) {
|
||||||
|
std::string name = type + "_" + key;
|
||||||
|
std::cout << std::format("{} {} {} ==> {} \n", name, action, Psc::to_string(this->state), Psc::to_string(state));
|
||||||
|
}
|
||||||
|
this->state = state;
|
||||||
|
}
|
||||||
|
With_Loop_Coro::With_Loop_Coro() : stop(0.1) {}
|
||||||
|
asio::awaitable<void> With_Loop_Coro::run_loop_coro() {
|
||||||
|
loop_running.store(true, std::memory_order_release);
|
||||||
|
try {
|
||||||
|
co_await loop_coro();
|
||||||
|
loop_running.store(false, std::memory_order_release);
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
loop_running.store(false, std::memory_order_release);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asio::awaitable<void> With_Loop_Coro::tick() {
|
||||||
std::string name = type + ":" + key;
|
std::string name = type + ":" + key;
|
||||||
if (loop_task && !running()) {
|
if (state == State::Force_Quit) {
|
||||||
loop_task.reset();
|
std::cout << std::format("{} 正在强制退出!\n", name);
|
||||||
co_await _close();
|
|
||||||
}
|
}
|
||||||
if (enable && !loop_task) {
|
if (state == State::Start) {
|
||||||
co_await _open();
|
if (enable) {
|
||||||
loop_task = std::make_shared<concurrencpp::result<void>>(loop_coro());
|
set_state(State::Before_Request_Start_Loop);
|
||||||
std::cout << std::format("启动任务 {}!\n", name);
|
}
|
||||||
}
|
}
|
||||||
else if (!enable && running()) {
|
else if (state == State::Before_Request_Start_Loop) {
|
||||||
std::cout << std::format("请求停止任务 {}!\n", name);
|
co_await this->_open();
|
||||||
force_close();
|
loop_running = enable;
|
||||||
|
loop_task = Coro::instance()->spawn(run_loop_coro());
|
||||||
|
set_state(State::Waiting_Loop_Start, "开始启动任务");
|
||||||
|
}
|
||||||
|
else if (state == State::Waiting_Loop_Start) {
|
||||||
|
if (running()) {
|
||||||
|
set_state(State::Loop_Running, "启动任务成功!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (state == State::Loop_Running) {
|
||||||
|
if (enable && !running()) {
|
||||||
|
std::cout << std::format("任务未知原因已经退出 {}!\n", name);
|
||||||
|
try {
|
||||||
|
loop_task->get();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
std::cout << std::format("任务异常退出 {}! {}\n", name, e.what());
|
||||||
|
loop_running = false;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
std::cout << std::format("任务未知异常退出 {}!\n", name);
|
||||||
|
loop_running = false;
|
||||||
|
}
|
||||||
|
loop_task.reset();
|
||||||
|
co_await _close();
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
if (!enable) {
|
||||||
|
set_state(State::Before_Request_Stop_Loop, std::format("{} 检测到 enable变化 异步退出开始!", name).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (state == State::Before_Request_Stop_Loop) {
|
||||||
|
loop_running = false;
|
||||||
|
set_state(State::Waiting_Stop_Loop, "退出变量已设置 等待退出");
|
||||||
|
}
|
||||||
|
else if (state == State::Waiting_Stop_Loop) {
|
||||||
|
if (!running()) {
|
||||||
|
loop_task.reset();
|
||||||
|
co_await this->_close();
|
||||||
|
set_state(State::Start, "任务正常退出");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <asio/awaitable.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <concurrencpp/concurrencpp.h>
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <future>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -11,29 +12,38 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Core/Base/JSON.h"
|
#include "Core/Base/JSON.h"
|
||||||
|
#include "Core/Statistics/Frequency_Limit.h"
|
||||||
class With_Loop_Coro_Data {
|
class With_Loop_Coro_Data {
|
||||||
public:
|
public:
|
||||||
|
std::string type;
|
||||||
std::string key;
|
std::string key;
|
||||||
Psc::Copyable_Atomic<bool> enable{};
|
Psc::Copyable_Atomic<bool> enable{};
|
||||||
std::string type;
|
|
||||||
PSC_USE_JSON
|
PSC_USE_JSON
|
||||||
};
|
};
|
||||||
class With_Loop_Coro : public With_Loop_Coro_Data {
|
class With_Loop_Coro : public With_Loop_Coro_Data {
|
||||||
public:
|
public:
|
||||||
virtual ~With_Loop_Coro();
|
virtual ~With_Loop_Coro();
|
||||||
std::shared_ptr<concurrencpp::result<void>> loop_task;
|
std::shared_ptr<std::future<void>> loop_task;
|
||||||
virtual concurrencpp::result<void> loop_coro() = 0;
|
virtual asio::awaitable<void> loop_coro() = 0;
|
||||||
void async_stop();
|
void async_stop();
|
||||||
void sync_wait() const;
|
void sync_wait();
|
||||||
bool running() const;
|
bool running() const;
|
||||||
virtual void force_close() {}
|
asio::awaitable<void> tick();
|
||||||
concurrencpp::result<void> sync_coro_loop_and_enable();
|
virtual asio::awaitable<void> _open() = 0;
|
||||||
|
virtual asio::awaitable<void> _close() = 0;
|
||||||
virtual concurrencpp::result<void> _open() {
|
enum class State {
|
||||||
co_return;
|
Force_Quit,
|
||||||
}
|
Start,
|
||||||
|
Before_Request_Start_Loop,
|
||||||
virtual concurrencpp::result<void> _close() {
|
Waiting_Loop_Start,
|
||||||
co_return;
|
Loop_Running,
|
||||||
}
|
Before_Request_Stop_Loop,
|
||||||
|
Waiting_Stop_Loop
|
||||||
|
} state = State::Start;
|
||||||
|
void set_state(State state, std::string action = "");
|
||||||
|
With_Loop_Coro();
|
||||||
|
protected:
|
||||||
|
Psc::Copyable_Atomic<bool> loop_running{};
|
||||||
|
asio::awaitable<void> run_loop_coro();
|
||||||
|
Frequency_Limit_Multi stop;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,11 +4,16 @@
|
|||||||
#include "Core/Statistics/Frequency_Limit.h"
|
#include "Core/Statistics/Frequency_Limit.h"
|
||||||
#include "Local_Server/Data_Feed/Data_Feed.h"
|
#include "Local_Server/Data_Feed/Data_Feed.h"
|
||||||
#include "Local_Server/Data_Source/Data_Source.h"
|
#include "Local_Server/Data_Source/Data_Source.h"
|
||||||
|
#include <asio/detached.hpp>
|
||||||
|
#include <asio/post.hpp>
|
||||||
|
#include <asio/steady_timer.hpp>
|
||||||
|
#include <asio/this_coro.hpp>
|
||||||
|
#include <asio/use_awaitable.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <exception>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <exception>
|
|
||||||
static std::string thread_id_str() {
|
static std::string thread_id_str() {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << std::this_thread::get_id();
|
oss << std::this_thread::get_id();
|
||||||
@@ -39,43 +44,30 @@ bool Coro::has_running_loop_tasks() {
|
|||||||
void Coro::start() {
|
void Coro::start() {
|
||||||
std::cout << "start_io_coro" << std::endl;
|
std::cout << "start_io_coro" << std::endl;
|
||||||
running.store(true, std::memory_order_release);
|
running.store(true, std::memory_order_release);
|
||||||
io = runtime_.make_executor<concurrencpp::worker_thread_executor>(
|
io.restart();
|
||||||
[](std::string_view thread_name) {
|
io_work = std::make_unique<asio::executor_work_guard<asio::io_context::executor_type>>(io.get_executor());
|
||||||
std::cout << std::format("协程io:{} 协程启动!\n", thread_name);
|
io_thread = std::thread([this] {
|
||||||
},
|
std::cout << std::format("协程io:{} 协程启动!\n", thread_id_str());
|
||||||
[](std::string_view thread_name) {
|
io.run();
|
||||||
std::cout << std::format("协程io:{} 销毁!\n", thread_name);
|
std::cout << std::format("协程io:{} 销毁!\n", thread_id_str());
|
||||||
});
|
});
|
||||||
// process_data = runtime_.make_executor<concurrencpp::worker_thread_executor>(
|
auto n = std::max<unsigned int>(1, std::thread::hardware_concurrency());
|
||||||
// [](std::string_view thread_name) {
|
auto other_need = std::max<unsigned int>(2, n / 2);
|
||||||
// std::cout << std::format("process_data 协程:{} 协程启动!\n", thread_name);
|
|
||||||
// },
|
|
||||||
// [](std::string_view thread_name) {
|
|
||||||
// std::cout << std::format("process_data 协程:{} 销毁!\n", thread_name);
|
|
||||||
// });
|
|
||||||
auto n = std::thread::hardware_concurrency();
|
|
||||||
auto other_need = std::max<size_t>(2, std::thread::hardware_concurrency() / 2);
|
|
||||||
if (n > other_need) {
|
if (n > other_need) {
|
||||||
n -= other_need;
|
n -= other_need;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
n = n / 2;
|
n = n / 2;
|
||||||
}
|
}
|
||||||
process_data = runtime_.make_executor<concurrencpp::thread_pool_executor>(
|
n = std::max<unsigned int>(1, n);
|
||||||
"process_data",
|
process_data = std::make_unique<asio::thread_pool>(n);
|
||||||
n,
|
std::cout << std::format("协程已经启动 process_data concurrency: 在n个线程上{}\n", n);
|
||||||
std::chrono::seconds(5),
|
data_feed_thread_task = std::make_unique<std::future<void>>(asio::co_spawn(io, coro_thread(), asio::use_future));
|
||||||
[](std::string_view thread_name) {
|
|
||||||
std::cout << std::format("多线程协程{} 启动!\n", thread_name);
|
|
||||||
},
|
|
||||||
[](std::string_view thread_name) {
|
|
||||||
std::cout << std::format("多线程协程{} 销毁!\n", thread_name);
|
|
||||||
});
|
|
||||||
std::cout << std::format("协程已经启动 process_data concurrency: 在n个线程上{}\n", process_data->max_concurrency_level());
|
|
||||||
data_feed_thread_task = std::make_unique<concurrencpp::result<void>>(coro_thread());
|
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> Coro::sleep_for(std::chrono::milliseconds ms) {
|
asio::awaitable<void> Coro::sleep_for(std::chrono::milliseconds ms) {
|
||||||
co_await runtime_.timer_queue()->make_delay_object(ms, io);
|
auto executor = co_await asio::this_coro::executor;
|
||||||
|
asio::steady_timer timer(executor, ms);
|
||||||
|
co_await timer.async_wait(asio::use_awaitable);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
void Coro::stop() {
|
void Coro::stop() {
|
||||||
@@ -93,6 +85,7 @@ void Coro::stop() {
|
|||||||
}
|
}
|
||||||
// 停止状态更新状态机
|
// 停止状态更新状态机
|
||||||
running = false;
|
running = false;
|
||||||
|
asio::post(io, [] {});
|
||||||
if (data_feed_thread_task) {
|
if (data_feed_thread_task) {
|
||||||
std::cout << "等待 coro_thread 退出" << std::endl;
|
std::cout << "等待 coro_thread 退出" << std::endl;
|
||||||
data_feed_thread_task->get();
|
data_feed_thread_task->get();
|
||||||
@@ -103,12 +96,18 @@ void Coro::stop() {
|
|||||||
for (auto& li : list) {
|
for (auto& li : list) {
|
||||||
li->loop_task.reset();
|
li->loop_task.reset();
|
||||||
}
|
}
|
||||||
process_data.reset();
|
if (process_data) {
|
||||||
io.reset();
|
process_data->join();
|
||||||
|
process_data.reset();
|
||||||
|
}
|
||||||
|
io_work.reset();
|
||||||
|
io.stop();
|
||||||
|
if (io_thread.joinable()) {
|
||||||
|
io_thread.join();
|
||||||
|
}
|
||||||
std::cout << "stop_io_coro end" << std::endl;
|
std::cout << "stop_io_coro end" << std::endl;
|
||||||
}
|
}
|
||||||
concurrencpp::result<void> Coro::coro_thread() {
|
asio::awaitable<void> Coro::coro_thread() {
|
||||||
co_await concurrencpp::resume_on(io);
|
|
||||||
while (running.load(std::memory_order_acquire)) {
|
while (running.load(std::memory_order_acquire)) {
|
||||||
auto list = get_all();
|
auto list = get_all();
|
||||||
for (auto& li : list) {
|
for (auto& li : list) {
|
||||||
@@ -119,10 +118,10 @@ concurrencpp::result<void> Coro::coro_thread() {
|
|||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
bool data_feed_debug = false;
|
bool data_feed_debug = false;
|
||||||
concurrencpp::result<void> Data_Feed::loop_coro() {
|
asio::awaitable<void> Data_Feed::loop_coro() {
|
||||||
auto feed = this;
|
auto feed = this;
|
||||||
auto co = Coro::instance();
|
auto co = Coro::instance();
|
||||||
co_await concurrencpp::resume_on(co->io);
|
co_await asio::post(co->io, asio::use_awaitable);
|
||||||
auto& mode_acs = Global::instance()->mode_acs;
|
auto& mode_acs = Global::instance()->mode_acs;
|
||||||
auto& cfg = mode_acs.data_feed_config;
|
auto& cfg = mode_acs.data_feed_config;
|
||||||
auto& pool = cfg.pool_;
|
auto& pool = cfg.pool_;
|
||||||
@@ -178,16 +177,16 @@ concurrencpp::result<void> Data_Feed::loop_coro() {
|
|||||||
std::cout << std::format("{} after send {}\n", key, thread_id_str());
|
std::cout << std::format("{} after send {}\n", key, thread_id_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
co_await concurrencpp::resume_on(co->io);
|
co_await asio::post(co->io, asio::use_awaitable);
|
||||||
}
|
}
|
||||||
std::cout << std::format("{} feed loop exit {}\n", key, thread_id_str());
|
std::cout << std::format("{} feed loop exit {}\n", key, thread_id_str());
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
bool data_source_debug = false;
|
bool data_source_debug = false;
|
||||||
concurrencpp::result<void> Data_Source::loop_coro() {
|
asio::awaitable<void> Data_Source::loop_coro() {
|
||||||
auto source = this;
|
auto source = this;
|
||||||
auto co = Coro::instance();
|
auto co = Coro::instance();
|
||||||
co_await concurrencpp::resume_on(co->io);
|
co_await asio::post(co->io, asio::use_awaitable);
|
||||||
while (source->running()) {
|
while (source->running()) {
|
||||||
if (data_source_debug) {
|
if (data_source_debug) {
|
||||||
std::cout << std::format("{} source loop begin {}\n", key, thread_id_str());
|
std::cout << std::format("{} source loop begin {}\n", key, thread_id_str());
|
||||||
@@ -209,7 +208,7 @@ concurrencpp::result<void> Data_Source::loop_coro() {
|
|||||||
if (!enable || !source->running() || !source->registered()) {
|
if (!enable || !source->running() || !source->registered()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
co_await concurrencpp::resume_on(co->process_data);
|
co_await asio::post(*co->process_data, asio::use_awaitable);
|
||||||
if (data_source_debug) {
|
if (data_source_debug) {
|
||||||
std::cout << std::format("{} before process {}\n", key, thread_id_str());
|
std::cout << std::format("{} before process {}\n", key, thread_id_str());
|
||||||
}
|
}
|
||||||
@@ -232,7 +231,7 @@ concurrencpp::result<void> Data_Source::loop_coro() {
|
|||||||
std::cout << std::format("{} 解析到数据 {} {}\n", name, num, thread_id_str());
|
std::cout << std::format("{} 解析到数据 {} {}\n", name, num, thread_id_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
co_await concurrencpp::resume_on(co->io);
|
co_await asio::post(co->io, asio::use_awaitable);
|
||||||
}
|
}
|
||||||
std::cout << std::format("{} source loop exit {}\n", key, thread_id_str());
|
std::cout << std::format("{} source loop exit {}\n", key, thread_id_str());
|
||||||
co_return;
|
co_return;
|
||||||
|
|||||||
@@ -1,21 +1,34 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <asio/awaitable.hpp>
|
||||||
|
#include <asio/co_spawn.hpp>
|
||||||
|
#include <asio/executor_work_guard.hpp>
|
||||||
|
#include <asio/io_context.hpp>
|
||||||
|
#include <asio/thread_pool.hpp>
|
||||||
|
#include <asio/use_future.hpp>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <concurrencpp/concurrencpp.h>
|
#include <future>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
#include "psc_global_include/Singleton.hpp"
|
#include "psc_global_include/Singleton.hpp"
|
||||||
class Coro : public Psc::Singleton<Coro> {
|
class Coro : public Psc::Singleton<Coro> {
|
||||||
public:
|
public:
|
||||||
void start();
|
void start();
|
||||||
void stop();
|
void stop();
|
||||||
concurrencpp::result<void> sleep_for(std::chrono::milliseconds ms);
|
asio::awaitable<void> sleep_for(std::chrono::milliseconds ms);
|
||||||
std::shared_ptr<concurrencpp::thread_pool_executor> process_data;
|
asio::io_context io;
|
||||||
std::shared_ptr<concurrencpp::worker_thread_executor> io;
|
std::unique_ptr<asio::thread_pool> process_data;
|
||||||
|
template <typename Awaitable>
|
||||||
|
std::shared_ptr<std::future<void>> spawn(Awaitable&& awaitable) {
|
||||||
|
auto future = asio::co_spawn(io, std::forward<Awaitable>(awaitable), asio::use_future);
|
||||||
|
return std::make_shared<std::future<void>>(std::move(future));
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
concurrencpp::result<void> coro_thread();
|
asio::awaitable<void> coro_thread();
|
||||||
bool has_running_loop_tasks();
|
bool has_running_loop_tasks();
|
||||||
concurrencpp::runtime runtime_;
|
std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>> io_work;
|
||||||
std::unique_ptr<concurrencpp::result<void>> data_feed_thread_task;
|
std::thread io_thread;
|
||||||
|
std::unique_ptr<std::future<void>> data_feed_thread_task;
|
||||||
std::atomic<bool> running = false;
|
std::atomic<bool> running = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user