concurrencpp 换成asio协程
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#include <codecvt>
|
||||
#include "../server/Global.h"
|
||||
#include "Database.h"
|
||||
|
||||
Psc::serial::Serial* create_serial(const std::string& serial_name,
|
||||
Baud_Rate_Type baud_rate) {
|
||||
auto serial = new Psc::serial::Serial;
|
||||
@@ -25,8 +24,7 @@ Psc::serial::Serial* create_serial(const std::string& serial_name,
|
||||
}
|
||||
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::shared_ptr<Data_Source> ret = create_data_source_from_type(type);
|
||||
ret->from_json(that_json);
|
||||
@@ -36,11 +34,9 @@ std::shared_ptr<Data_Source> create_from_json(const JSON* that_json) {
|
||||
// }
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::shared_ptr<Data_Source> Data_Source::that() {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
bool Data_Source::registered() const {
|
||||
auto all_source = Global::instance()->mode_acs.data_source_config.map.list();
|
||||
for (auto& item : all_source) {
|
||||
@@ -50,7 +46,6 @@ bool Data_Source::registered() const {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Psc::JSON Data_Source::get_state() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
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});
|
||||
return ret;
|
||||
}
|
||||
|
||||
Data_Source::Data_Source() {
|
||||
// parse_format = std::make_shared<Input_Format>();
|
||||
// 写入到配置文件是懒加载 其他保存时他跟着保存
|
||||
@@ -70,12 +64,10 @@ Data_Source::Data_Source() {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::string Data_Source::thread_key() const {
|
||||
// return "Data_Source_Handle_Thread:[" + key + "]";
|
||||
return key + "_DS_HT";
|
||||
}
|
||||
|
||||
void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
|
||||
auto cur = std::dynamic_pointer_cast<SSR::Mode_Msg>(msg);
|
||||
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> lines;
|
||||
std::filesystem::path p =
|
||||
@@ -146,7 +137,6 @@ std::vector<std::string> readLines(const std::string& path) {
|
||||
std::cout << oss.str() << std::flush;
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::string extractID(const std::string& logLine) {
|
||||
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
||||
if (lastSpacePos != std::string::npos) {
|
||||
@@ -154,7 +144,6 @@ std::string extractID(const std::string& logLine) {
|
||||
}
|
||||
return logLine;
|
||||
}
|
||||
|
||||
time_t convert_to_timestamp(const std::string& str) {
|
||||
// 创建一个结构体 tm 来存储解析后的时间
|
||||
std::tm timeStruct = {};
|
||||
@@ -172,7 +161,6 @@ time_t convert_to_timestamp(const std::string& str) {
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
std::tuple<std::string, time_t> extractID2(const std::string& logLine) {
|
||||
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
|
||||
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};
|
||||
}
|
||||
|
||||
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 +
|
||||
currentTime->tm_sec;
|
||||
SSR::MLAT_timestamp a(sec, 0);
|
||||
return create_Binary_Format_memory(hex, 0, &a);
|
||||
}
|
||||
|
||||
std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
|
||||
if (index == 0 && part_infos.empty()) {
|
||||
ret_index = -1;
|
||||
@@ -220,9 +206,8 @@ std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
|
||||
ret_index = index + 1;
|
||||
return part_infos[index++];
|
||||
}
|
||||
|
||||
// 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) {
|
||||
std::string ret;
|
||||
// 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;
|
||||
}
|
||||
|
||||
concurrencpp::result<void> File_Data_Source::_close() {
|
||||
asio::awaitable<void> File_Data_Source::_close() {
|
||||
std::lock_guard g(mtx);
|
||||
index = 0;
|
||||
part_infos.clear();
|
||||
co_return;
|
||||
}
|
||||
|
||||
concurrencpp::result<void> File_Data_Source::_open() {
|
||||
asio::awaitable<void> File_Data_Source::_open() {
|
||||
std::lock_guard g(mtx);
|
||||
auto path = get_true_file_path();
|
||||
namespace fs = std::filesystem;
|
||||
@@ -351,7 +334,6 @@ concurrencpp::result<void> File_Data_Source::_open() {
|
||||
state = "已加载" + to_string(part_infos.size()) + "长度数据!";
|
||||
co_return;
|
||||
}
|
||||
|
||||
std::vector<std::string> File_Data_Source::readBinaryFileAsString(const std::string& filepath,
|
||||
size_t part_size) {
|
||||
// 打开文件(以二进制模式)
|
||||
@@ -385,7 +367,6 @@ std::vector<std::string> File_Data_Source::readBinaryFileAsString(const std::str
|
||||
file.close();
|
||||
return parts;
|
||||
}
|
||||
|
||||
void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
|
||||
std::lock_guard g(mtx);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
|
||||
if (!msg)
|
||||
return;
|
||||
if (!msg) return;
|
||||
cache_list.push_back(std::make_shared<Cache_Msg>(std::move(msg)));
|
||||
if (cache_list.empty())
|
||||
return;
|
||||
if (cache_list.empty()) return;
|
||||
if (pre_cache == nullptr) {
|
||||
pre_cache = cache_list.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();
|
||||
while (true) {
|
||||
if (cache_list.empty())
|
||||
break;
|
||||
if (cache_list.empty()) break;
|
||||
auto pre = pre_cache;
|
||||
auto cur = cache_list.front();
|
||||
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) {
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
if (read_func_ptr == nullptr)
|
||||
return;
|
||||
if (read_func_ptr == nullptr) return;
|
||||
auto buf = reinterpret_cast<char*>(buffer.data());
|
||||
auto len = read_func_ptr(buf, buffer_size);
|
||||
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";
|
||||
mode_data = ret;
|
||||
}
|
||||
|
||||
concurrencpp::result<void> Shared_Memory_Data_Source::_open() {
|
||||
asio::awaitable<void> Shared_Memory_Data_Source::_open() {
|
||||
sm = std::make_unique<Psc::SM_RingBuffer>();
|
||||
sm->init(shared_memory_name, shared_memory_size);
|
||||
co_return;
|
||||
}
|
||||
|
||||
concurrencpp::result<void> Shared_Memory_Data_Source::_close() {
|
||||
asio::awaitable<void> Shared_Memory_Data_Source::_close() {
|
||||
sm.reset();
|
||||
co_return;
|
||||
}
|
||||
|
||||
void Shared_Memory_Data_Source::origin_data_transform_mode_data(
|
||||
std::string& mode_data) {
|
||||
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);
|
||||
mode_data = ret;
|
||||
}
|
||||
|
||||
void Data_Source_Config::server(Global* g) {
|
||||
void Data_Source_Config::server(Global *g) {
|
||||
auto& svr = g->svr;
|
||||
auto& api = g->api;
|
||||
std::string name = "data_source";
|
||||
@@ -536,9 +507,6 @@ void Data_Source_Config::server(Global* g) {
|
||||
CHECK_JSON_PARAM
|
||||
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
|
||||
HTTP_REQUIRE_VALUE(ds, map.get(key))
|
||||
// 直接关闭
|
||||
// ds->enable = false;
|
||||
// ds->close();
|
||||
ds->from_json(¶ms);
|
||||
g->save();
|
||||
g->mode_acs.source_feed_relation_config.set_need_refresh();
|
||||
|
||||
Reference in New Issue
Block a user