Browse Source

update time lock

LiuZe 1 năm trước cách đây
mục cha
commit
e0572cf8aa

+ 6 - 2
include/rabbitmq/rabbitmq_base.cpp

@@ -11,7 +11,7 @@ Rabbitmq_base::Rabbitmq_base() {
     mp_receive_analysis_thread = NULL;
     mp_send_thread = NULL;
     mp_encapsulate_status_thread = NULL;
-    m_encapsulate_status_cycle_time = 1000;//默认1000ms,就自动封装一次状态信息
+    m_encapsulate_status_cycle_time = 100;//默认1000ms,就自动封装一次状态信息
 
     check_msg_callback = NULL;
     check_executer_callback = NULL;
@@ -34,7 +34,7 @@ Error_manager Rabbitmq_base::rabbitmq_init_from_protobuf(std::string prototxt_pa
     Rabbitmq_proto::Rabbitmq_parameter_all t_rabbitmq_parameter_all;
     if (loadProtobufFile(prototxt_path, t_rabbitmq_parameter_all) != SUCCESS) {
         return Error_manager(RABBITMQ_READ_PROTOBUF_ERROR, MINOR_ERROR,
-                             "rabbitmq_init_from_protobuf read_proto_param  failed");
+                             "rabbitmq_init_from_file: %s read_proto_param  failed", prototxt_path.c_str());
     }
     return rabbitmq_init_from_protobuf(t_rabbitmq_parameter_all);
 }
@@ -816,6 +816,10 @@ Error_manager Rabbitmq_base::encapsulate_task_msg(std::string message, int vecto
 
 //手动封装状态消息, 系统会使用rabbitmq.proto的配置参数,
 Error_manager Rabbitmq_base::encapsulate_status_msg(std::string message, int vector_index) {
+    if (vector_index >= m_rabbitmq_parameter_all.rabbitmq_parameters().rabbitmq_sender_status_vector().size()) {
+        LOG(WARNING) << "vector index error.";
+        return {FAILED, NORMAL};
+    }
     int channel = m_rabbitmq_parameter_all.rabbitmq_parameters().rabbitmq_sender_status_vector(vector_index).channel();
     std::string exchange_name = m_rabbitmq_parameter_all.rabbitmq_parameters().rabbitmq_sender_status_vector(
             vector_index).exchange_name();

+ 2 - 2
include/rabbitmq/rabbitmq_base.h

@@ -70,10 +70,10 @@ public:
 
 public://API functions
     //初始化 通信 模块。如下三选一
-    Error_manager rabbitmq_init();
+    virtual Error_manager rabbitmq_init();
 
     //初始化 通信 模块。从文件读取
-    Error_manager rabbitmq_init_from_protobuf(std::string prototxt_path);
+    virtual Error_manager rabbitmq_init_from_protobuf(std::string prototxt_path);
 
     //初始化 通信 模块。从protobuf读取
     Error_manager rabbitmq_init_from_protobuf(Rabbitmq_proto::Rabbitmq_parameter_all &rabbitmq_parameter_all);

+ 14 - 15
include/tool/timedlockdata.hpp

@@ -7,47 +7,46 @@
 #include <mutex>
 
 template <typename T>
-class TimedLockData {
-public:
+class TimedLockData
+{
+ public:
     TimedLockData();
-    void reset(const T& tdata,double timeout=0.1);
-    bool timeout();
-    T Get();
+    TimedLockData<T>& operator=(const T& data);
+    bool timeout(const double &timeout = 0.2);
+    T operator()(void);
 
-protected:
+ protected:
     T data_;
     std::chrono::steady_clock::time_point tp_;
     std::mutex mutex_;
-    double timeout_;
 
 };
 
 template <typename T>
 TimedLockData<T>::TimedLockData()
 {
-  timeout_=0.1;
 }
 
+
 template <typename T>
-void TimedLockData<T>::reset(const T& tdata,double timeout)
-{
+TimedLockData<T>& TimedLockData<T>::operator=(const T& data){
   std::lock_guard<std::mutex> lck (mutex_);
-  data_=tdata;
-  timeout_=timeout;
+  data_=data;
   tp_=std::chrono::steady_clock::now();
+  return *this;
 }
 
 template <typename T>
-bool TimedLockData<T>::timeout()
+bool TimedLockData<T>::timeout(const double &timeout)
 {
   auto now=std::chrono::steady_clock::now();
   auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - tp_);
   double time = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
-  return time>timeout_;
+  return time>timeout;
 }
 
 template <typename T>
-T TimedLockData<T>::Get()
+T TimedLockData<T>::operator()(void)
 {
   std::lock_guard<std::mutex> lck (mutex_);
   return data_;