改进协程循环逻辑2
This commit is contained in:
@@ -348,23 +348,21 @@ std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type, in
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void File_Data_Source::_close() {
|
||||
concurrencpp::result<void> File_Data_Source::_close() {
|
||||
std::lock_guard g(mtx);
|
||||
index = 0;
|
||||
part_infos.clear();
|
||||
co_return;
|
||||
}
|
||||
|
||||
bool File_Data_Source::_open() {
|
||||
concurrencpp::result<void> File_Data_Source::_open() {
|
||||
std::lock_guard g(mtx);
|
||||
auto path = get_true_file_path();
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
if (!fs::exists(path)) {
|
||||
state = "文件不存在";
|
||||
return false;
|
||||
co_return;
|
||||
}
|
||||
|
||||
if (data_type == File_Data_Type::BIN) {
|
||||
@@ -374,7 +372,7 @@ bool File_Data_Source::_open() {
|
||||
part_infos = readLines(path);
|
||||
}
|
||||
state = "已加载" + to_string(part_infos.size()) + "长度数据!";
|
||||
return true;
|
||||
co_return;
|
||||
}
|
||||
|
||||
|
||||
@@ -517,14 +515,14 @@ void Dll_Data_Source::origin_data_transform_mode_data(std::string& mode_data)
|
||||
mode_data = ret;
|
||||
}
|
||||
|
||||
|
||||
bool Shared_Memory_Data_Source::_open() {
|
||||
concurrencpp::result<void> Shared_Memory_Data_Source::_open() {
|
||||
sm = std::make_unique<Psc::SM_RingBuffer>();
|
||||
sm->init(shared_memory_name, shared_memory_size);
|
||||
return true;
|
||||
co_return;
|
||||
}
|
||||
void Shared_Memory_Data_Source::_close() {
|
||||
concurrencpp::result<void> Shared_Memory_Data_Source::_close() {
|
||||
sm.reset();
|
||||
co_return;
|
||||
}
|
||||
|
||||
void Shared_Memory_Data_Source::origin_data_transform_mode_data(std::string& mode_data)
|
||||
|
||||
@@ -164,17 +164,19 @@ public:
|
||||
TCP_Client_Data_Source() {
|
||||
type = "TCP_Client_Data_Source";
|
||||
}
|
||||
bool _open() override {
|
||||
concurrencpp::result<void> _open() override {
|
||||
Psc::asio_socket::Sockaddr_In addr;
|
||||
addr.ip = ip;
|
||||
addr.port = port;
|
||||
|
||||
cli.set_dest_address(addr);
|
||||
cli.create();
|
||||
(cli.connect_coro()).get();
|
||||
return true;
|
||||
co_await cli.connect_coro();
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
co_await cli.close_coro();
|
||||
co_return;
|
||||
}
|
||||
void _close() override { cli.close(); }
|
||||
|
||||
Psc::JSON to_json() override {
|
||||
Psc::JSON ret = Data_Source::to_json();
|
||||
@@ -201,7 +203,7 @@ public:
|
||||
return Psc::JSON::object();
|
||||
}
|
||||
Serial_Data_Source() { this->type = "Serial_Data_Source"; }
|
||||
bool _open() override {
|
||||
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);
|
||||
@@ -216,9 +218,12 @@ public:
|
||||
} else {
|
||||
// std::cerr << "createSerial " + serial_name + ":" + std::to_string(baud_rate) + " 打开串口成功!\n";
|
||||
}
|
||||
return ok;
|
||||
co_return;
|
||||
}
|
||||
concurrencpp::result<void> _close() override {
|
||||
if (serial) serial->close();
|
||||
co_return;
|
||||
}
|
||||
void _close() override { if (serial) serial->close(); }
|
||||
|
||||
concurrencpp::result<void> handle_in_loop_coro() override {
|
||||
if (serial) {
|
||||
@@ -297,8 +302,8 @@ public:
|
||||
|
||||
|
||||
std::string get_true_file_path() const { return Psc::get_abs_path(file_path); }
|
||||
void _close() override;
|
||||
bool _open() override;
|
||||
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);
|
||||
@@ -368,13 +373,16 @@ public:
|
||||
using set_Call_back = void (*)(Call_Back);
|
||||
|
||||
std::string state;
|
||||
void _close() override { Psc::free_library(lib); }
|
||||
bool _open() override {
|
||||
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();
|
||||
return false;
|
||||
co_return;
|
||||
}
|
||||
lib = r.value();
|
||||
}
|
||||
@@ -383,12 +391,12 @@ public:
|
||||
if (!r) {
|
||||
state = r.error().message();
|
||||
std::cout << LOG_POS << " [" << function_name << "] 函数指针加载失败!" << std::endl;
|
||||
return false;
|
||||
co_return;
|
||||
}
|
||||
read_func_ptr = (Func_Type) r.value();
|
||||
}
|
||||
state = "加载成功";
|
||||
return true;
|
||||
co_return;
|
||||
}
|
||||
void origin_data_transform_mode_data(std::string& data) override;
|
||||
};
|
||||
@@ -412,8 +420,8 @@ public:
|
||||
Ret_J(data_type);
|
||||
return ret;
|
||||
}
|
||||
bool _open() override;
|
||||
void _close() override;
|
||||
concurrencpp::result<void> _open() override;
|
||||
concurrencpp::result<void> _close() override;
|
||||
|
||||
void origin_data_transform_mode_data(std::string& data) override;
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user