2023-07-21 11:21:16 +08:00
|
|
|
#include "MessageManager.h"
|
|
|
|
|
|
|
|
MessageManager::MessageManager() {
|
|
|
|
m_thread = std::thread(&MessageManager::run, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageManager::~MessageManager() {
|
|
|
|
m_exit = true;
|
|
|
|
if (m_thread.joinable()) m_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageManager::run() {
|
2024-09-16 01:32:40 +08:00
|
|
|
#ifndef DISABLE_LOG
|
2023-07-21 11:21:16 +08:00
|
|
|
LOG(trace) << "message runner[" << std::this_thread::get_id() << "] start...";
|
2024-09-16 01:32:40 +08:00
|
|
|
#endif
|
2023-07-21 11:21:16 +08:00
|
|
|
while (!m_exit) {
|
|
|
|
if (m_params.empty()) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto &[topic, args] = m_params.front();
|
|
|
|
callExecutor(topic, std::move(args));
|
|
|
|
m_params.pop();
|
|
|
|
}
|
2024-09-16 01:32:40 +08:00
|
|
|
#ifndef DISABLE_LOG
|
2023-07-21 11:21:16 +08:00
|
|
|
LOG(trace) << "message runner exit...";
|
2024-09-16 01:32:40 +08:00
|
|
|
#endif
|
2023-07-21 11:21:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageManager::callExecutor(const std::string &signature, std::any &¶meter) {
|
|
|
|
auto executors = m_executors.equal_range(signature);
|
|
|
|
for (auto executor = executors.first; executor != executors.second; ++executor) {
|
|
|
|
try {
|
|
|
|
if (!executor->second(parameter)) {
|
|
|
|
LOG(warning) << "topic: " << signature << " execute failed, parameter cast failed.";
|
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(error) << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|