|
@@ -1,6 +1,7 @@
|
|
|
#pragma once
|
|
|
//stl
|
|
|
#include <iostream>
|
|
|
+#include <optional>
|
|
|
//asio2
|
|
|
#include <asio2/asio2.hpp>
|
|
|
//boost
|
|
@@ -665,6 +666,20 @@ namespace robotics {
|
|
|
void stop() {
|
|
|
client_.stop();
|
|
|
}
|
|
|
+ /**
|
|
|
+ * @brief 呼叫者消息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ static std::optional<nexus_net_message> caller_message() {
|
|
|
+ nexus_net_client* client = instance();
|
|
|
+ std::lock_guard<std::mutex> locker(client->caller_mutex_);
|
|
|
+ if (client->caller_message_list_.contains(std::this_thread::get_id())) {
|
|
|
+ return client->caller_message_list_[std::this_thread::get_id()];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return std::nullopt;
|
|
|
+ }
|
|
|
+ }
|
|
|
/**
|
|
|
* @brief 添加订阅
|
|
|
* @tparam _Fn
|
|
@@ -750,27 +765,33 @@ namespace robotics {
|
|
|
* @brief 自定义发布
|
|
|
* @tparam _Ret
|
|
|
* @tparam ..._Args
|
|
|
- * @param route
|
|
|
- * @param ...args
|
|
|
+ * @param key 配置KEY
|
|
|
+ * @param format 值
|
|
|
+ * @param ...args 参数列表
|
|
|
* @return
|
|
|
*/
|
|
|
template<typename _Ret, typename ..._Args>
|
|
|
- _Ret custom_publisher(std::string const& route, _Args&&...args) {
|
|
|
- return custom_publisher<_Ret>(2000, route, std::forward<_Args>(args)...);
|
|
|
+ _Ret custom_publisher(std::string const& key, std::string const& format, _Args&&...args) {
|
|
|
+ return custom_publisher<_Ret>(2000, key, format, std::forward<_Args>(args)...);
|
|
|
}
|
|
|
/**
|
|
|
* @brief 自定义发布
|
|
|
* @tparam _Ret
|
|
|
* @tparam ..._Args
|
|
|
- * @param timeout
|
|
|
- * @param route
|
|
|
- * @param ...args
|
|
|
+ * @param timeout 超时时间
|
|
|
+ * @param key 配置KEY
|
|
|
+ * @param format 值
|
|
|
+ * @param ...args 参数列表
|
|
|
* @return
|
|
|
*/
|
|
|
template<typename _Ret, typename ..._Args>
|
|
|
- _Ret custom_publisher(int timeout, std::string const& route, _Args&&...args) {
|
|
|
+ _Ret custom_publisher(int timeout, std::string const& key, std::string const& format, _Args&&...args) {
|
|
|
+ auto& config = nexus_net_config::read();
|
|
|
+ if (!config.publishers.contains(key))
|
|
|
+ throw std::runtime_error("key不存在!");
|
|
|
+
|
|
|
nexus_net_message msg;
|
|
|
- msg.route = route;
|
|
|
+ msg.route = v3::utils::format(config.publishers[key].route, format);
|
|
|
msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::PUBLISHER;
|
|
|
msg.args << std::make_tuple(std::forward<_Args>(args)...);
|
|
|
if constexpr (!std::is_same<void, _Ret>::value) {
|
|
@@ -875,6 +896,7 @@ namespace robotics {
|
|
|
}
|
|
|
void do_work_nexus_net_message(nexus_net_message data) {
|
|
|
nexus_net_config::logger(true, true, data);
|
|
|
+
|
|
|
try {
|
|
|
switch ((nexus_net_msg_type_enum)data.msg_type) {
|
|
|
case nexus_net_msg_type_enum::REPAUTHENTICATE: on_authenticate(data); break;
|
|
@@ -927,7 +949,15 @@ namespace robotics {
|
|
|
return;
|
|
|
}
|
|
|
std::string key = data.route.substr(data.route.find('/', data.route.find('/') + 1) + 1);
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> locker(caller_mutex_);
|
|
|
+ caller_message_list_[std::this_thread::get_id()] = data;
|
|
|
+ }
|
|
|
data.args = functions_.invoke(key, data.args);
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> locker(caller_mutex_);
|
|
|
+ caller_message_list_.erase(std::this_thread::get_id());
|
|
|
+ }
|
|
|
if (!data.args.empty()) {
|
|
|
data.msg_type = (std::uint8_t)nexus_net_msg_type_enum::REPPUBLISHER;
|
|
|
send(data);
|
|
@@ -943,14 +973,16 @@ namespace robotics {
|
|
|
}
|
|
|
}
|
|
|
private:
|
|
|
- v3::timer timer_;
|
|
|
- std::string parent_code_;
|
|
|
- std::string parent_name_;
|
|
|
- bool is_heartbeat_ = false;
|
|
|
- nexus_net_tcp_client client_;
|
|
|
- message_manage message_manage_;
|
|
|
- std::vector<nexus_net_message> subscribes_;
|
|
|
- v3::archive::function_manage<std::string> functions_;
|
|
|
+ v3::timer timer_;
|
|
|
+ std::string parent_code_;
|
|
|
+ std::string parent_name_;
|
|
|
+ bool is_heartbeat_ = false;
|
|
|
+ nexus_net_tcp_client client_;
|
|
|
+ std::mutex caller_mutex_;
|
|
|
+ std::map<std::thread::id, nexus_net_message> caller_message_list_;
|
|
|
+ message_manage message_manage_;
|
|
|
+ std::vector<nexus_net_message> subscribes_;
|
|
|
+ v3::archive::function_manage<std::string> functions_;
|
|
|
};
|
|
|
}
|
|
|
}
|