zxs 2 days ago
parent
commit
48e8c92828
3 changed files with 54 additions and 20 deletions
  1. 1 1
      robot/robotics/message_bus.hpp
  2. 49 17
      robot/robotics/nexus_net_client.hpp
  3. 4 2
      robot/robotics/utils.hpp

+ 1 - 1
robot/robotics/message_bus.hpp

@@ -46,7 +46,7 @@ namespace robotics {
 			 */
 			template<typename _Fn, typename _Obj>
 			void bind(const std::string& str_topic, _Fn&& f, _Obj&& obj) {
-				auto func = v3::to_function(std::bind_front(std::forward<_Fn>(f), std::forward<_Obj>(obj)));
+				auto func = v3::to_function((v3::function_traits<_Fn>::stl_function_type)(std::bind_front(std::forward<_Fn>(f), std::forward<_Obj>(obj))));
 				add(str_topic, std::move(func));
 			}
 			/**

+ 49 - 17
robot/robotics/nexus_net_client.hpp

@@ -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_;
         };
     }
 }

+ 4 - 2
robot/robotics/utils.hpp

@@ -1128,7 +1128,8 @@ namespace robotics {
 			 */
 			template<typename _Fn, typename ..._Args>
 			inline void for_each(std::tuple<_Args...>const& tuple, _Fn&& fn) {
-				for_each_impl<_Fn, std::tuple<_Args...>, sizeof...(_Args)>::invoke(tuple, std::forward<_Fn>(fn));
+				if constexpr (sizeof...(_Args) > 0)
+					for_each_impl<_Fn, std::tuple<_Args...>, sizeof...(_Args)>::invoke(tuple, std::forward<_Fn>(fn));
 			}
 			/**
 			* @brief
@@ -1163,7 +1164,8 @@ namespace robotics {
 			 */
 			template<typename _Fn, typename ..._Args>
 			inline void for_each_reference(std::tuple<_Args...>& tuple, _Fn&& fn) {
-				for_each_reference_impl<_Fn, std::tuple<_Args...>, sizeof...(_Args)>::invoke(tuple, std::forward<_Fn>(fn));
+				if constexpr (sizeof...(_Args) > 0)
+					for_each_reference_impl<_Fn, std::tuple<_Args...>, sizeof...(_Args)>::invoke(tuple, std::forward<_Fn>(fn));
 			}
 			/**
 			 * @brief