串口改协程前
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
#include "Data_Source.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <asio/post.hpp>
|
||||
#include <asio/thread_pool.hpp>
|
||||
#include <codecvt>
|
||||
#include <thread>
|
||||
|
||||
#include "../server/Global.h"
|
||||
#include "Database.h"
|
||||
|
||||
namespace {
|
||||
asio::thread_pool& data_source_process_pool() {
|
||||
const auto hardware_threads = std::thread::hardware_concurrency();
|
||||
const auto worker_count = std::max(2u, hardware_threads > 2 ? hardware_threads - 2 : hardware_threads);
|
||||
static asio::thread_pool pool(worker_count);
|
||||
return pool;
|
||||
}
|
||||
}
|
||||
|
||||
Psc::serial::Serial* create_serial(const std::string& serial_name, Baud_Rate_Type baud_rate) {
|
||||
auto serial = new Psc::serial::Serial;
|
||||
serial->set_serial_name(serial_name);
|
||||
@@ -55,17 +68,9 @@ std::string Data_Source::thread_key() const {
|
||||
}
|
||||
|
||||
void Data_Source::test_and_attach_thread() {
|
||||
//static int t = Global::instance()->mode_acs.read_milliseconds;
|
||||
Global::instance()->thread_manager.test_and_start_thread(thread_key(), [this](std::atomic<bool>& running) {
|
||||
if (enable) {
|
||||
open();
|
||||
}
|
||||
while (running.load(std::memory_order_acquire) == true)
|
||||
{
|
||||
handle_in_loop();
|
||||
handle_mode_acs_source();
|
||||
}
|
||||
});
|
||||
if (enable) {
|
||||
open();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +179,10 @@ void Data_Source::handle_mode_acs_source() {
|
||||
return;
|
||||
}
|
||||
|
||||
process_mode_acs_data(mode_data);
|
||||
}
|
||||
|
||||
void Data_Source::process_mode_acs_data(const std::string& mode_data) {
|
||||
auto size = mode_data.size();
|
||||
read_speed.update(size);
|
||||
value_statistics.update(size);
|
||||
@@ -204,6 +212,49 @@ void Data_Source::handle_mode_acs_source() {
|
||||
});
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> Data_Source::handle_in_loop_coro() {
|
||||
handle_in_loop();
|
||||
co_return;
|
||||
}
|
||||
|
||||
ucoro::awaitable<std::string> Data_Source::recv_coro() {
|
||||
co_return read();
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> Data_Source::source_step_coro() {
|
||||
co_await handle_in_loop_coro();
|
||||
auto mode_data = co_await recv_coro();
|
||||
if (mode_data.empty()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
co_return;
|
||||
}
|
||||
struct Process_Result {
|
||||
std::exception_ptr exception;
|
||||
};
|
||||
auto self = that();
|
||||
auto result = co_await ucoro::callback_awaitable<Process_Result>(
|
||||
[this, self = std::move(self), mode_data = std::move(mode_data)](auto done) mutable {
|
||||
asio::post(
|
||||
data_source_process_pool(),
|
||||
[this, self = std::move(self), mode_data = std::move(mode_data), done = std::move(done)]() mutable {
|
||||
try {
|
||||
process_mode_acs_data(mode_data);
|
||||
done(Process_Result{});
|
||||
} catch (...) {
|
||||
done(Process_Result{std::current_exception()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (result.exception) {
|
||||
std::rethrow_exception(result.exception);
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Data_Source::test_and_stop_thread() const {
|
||||
Global::instance()->thread_manager.test_and_stop_thread(thread_key());
|
||||
@@ -675,6 +726,11 @@ void File_Data_Source::handle_mode_acs_source() {
|
||||
}
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> File_Data_Source::source_step_coro() {
|
||||
handle_mode_acs_source();
|
||||
co_return;
|
||||
}
|
||||
|
||||
std::string Dll_Data_Source::read() {
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
Reference in New Issue
Block a user