From bae3a4fd9406635608edf0c0d16c52cf7ca06a66 Mon Sep 17 00:00:00 2001
From: fujuntang <fujuntang@smartai.com>
Date: 星期六, 23 十月 2021 15:20:57 +0800
Subject: [PATCH] Optimize the code source.

---
 src/bh_api.cpp |  598 ++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 346 insertions(+), 252 deletions(-)

diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index 635fe95..37f8377 100644
--- a/src/bh_api.cpp
+++ b/src/bh_api.cpp
@@ -7,9 +7,8 @@
 #include "bh_api.h"
 #include <pthread.h>
 #include <getopt.h>
-#include "bhome_msg_api.pb.h"
-#include "bhome_msg.pb.h"
-#include "error_msg.pb.h"
+#include "msg_mgr.h"
+#include "../proto/source/error_msg.pb.h"
 #include "../proto/source/bhome_msg.pb.h"
 #include "../proto/source/bhome_msg_api.pb.h"
 
@@ -24,8 +23,7 @@
 
 static pthread_t gTids;
 
-static void *client_run_check(void *skptr) {
-  
+static void *client_run_check(void *skptr) { 
   pthread_detach(pthread_self());
  
   int data;
@@ -84,7 +82,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
 	_input.proc_id = input.proc_id().c_str();
@@ -97,13 +95,17 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 #endif 
  
   memset(&pData, 0x00, sizeof(ProcInfo));
   if (gRun_stat == 0) {
     pthread_mutex_init(&mutex, NULL);
+    
+#if defined(MSG_HANDLER)
+    msg_init();
+#endif 
 
   } else {
     logger->error("the process has already registered!\n");
@@ -111,7 +113,7 @@
     rv = EBUS_RES_BUSY;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
 
   rv = pthread_mutex_trylock(&mutex);
@@ -123,49 +125,49 @@
 #if defined(PRO_DE_SERIALIZE)
     if (_input.proc_id != NULL) {
       count = strlen(_input.proc_id) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : count;
       strncpy(pData.proc_id, _input.proc_id, min);
     }
 
     if (_input.name != NULL) {
       count = strlen(_input.name) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1)? (MAX_STR_LEN -1) : count;
       strncpy(pData.name, _input.name, min); 
     }
 
     if (_input.public_info != NULL) {
       count = strlen(_input.public_info) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1)? (MAX_STR_LEN - 1) : count;
       strncpy(pData.public_info, _input.public_info, min);
     }
  
     if (_input.private_info != NULL) {
       count = strlen(_input.private_info) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1)? (MAX_STR_LEN - 1): count;
       strncpy(pData.private_info, _input.private_info, min);
     }
 #else 
     if (strlen((const char *)(((ProcInfo *)proc_info)->proc_id)) > 0) {
       count = strlen((const char *)(((ProcInfo *)proc_info)->proc_id)) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1): count;
       strncpy(pData.proc_id, ((ProcInfo *)proc_info)->proc_id, min);
     }
 
     if (strlen((const char *)(((ProcInfo *)proc_info)->name)) > 0) {
       count = strlen((const char *)(((ProcInfo *)proc_info)->name)) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : count;
       strncpy(pData.name, ((ProcInfo *)proc_info)->name, min); 
     }
 
     if (strlen((const char *)(((ProcInfo *)proc_info)->public_info)) > 0) {
       count = strlen((const char *)(((ProcInfo *)proc_info)->public_info)) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : count;
       strncpy(pData.public_info, ((ProcInfo *)proc_info)->public_info, min);
     }
  
     if (strlen((const char *)(((ProcInfo *)proc_info)->private_info)) > 0) {
       count = strlen((const char *)(((ProcInfo *)proc_info)->private_info)) + 1;
-      min = count > MAX_STR_LEN ? MAX_STR_LEN : count;
+      min = count > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : count;
       strncpy(pData.private_info, ((ProcInfo *)proc_info)->private_info, min);
     }
 #endif 
@@ -177,7 +179,7 @@
 
       pthread_mutex_unlock(&mutex);
       
-      return false;
+      goto exit_entry;
     }
 
     gNetmod_socket = net_mod_socket_open();
@@ -202,11 +204,11 @@
     rv = EBUS_RES_BUSY;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
+exit_entry:
   errString = bus_strerror(0, 1);
-
 #if defined(PRO_DE_SERIALIZE)
   ::bhome_msg::MsgCommonReply mcr;
   mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -225,10 +227,13 @@
     
 #endif 
   
-  pthread_create(&gTids, NULL, client_run_check, NULL);
+  if (rv == 0) {
+    pthread_create(&gTids, NULL, client_run_check, NULL);
     
-  return true;
+    return true;
+  }
 
+  return false;
 }
 
 int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
@@ -254,7 +259,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
   
 	_input.proc_id = input.proc_id().c_str();
@@ -266,7 +271,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
 #endif 
   
@@ -277,7 +282,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
 
   rv = pthread_mutex_trylock(&mutex);
@@ -301,9 +306,10 @@
     rv = EBUS_RES_BUSY;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
+exit_entry:
   errString = bus_strerror(0, 1);
 #if defined(PRO_DE_SERIALIZE)
   ::bhome_msg::MsgCommonReply mcr;
@@ -321,8 +327,12 @@
   *reply = buf;
   *reply_len = min;
 #endif 
-  
-  return true;
+ 
+  if (rv == 0) {
+    return true;
+  }
+
+  return false;
   
 }
 
@@ -349,7 +359,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 
 	_input.amount = input.topic_list_size();
@@ -368,7 +378,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
   total = topics_len;
@@ -380,7 +390,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
 
   topics_buf = (char *)malloc(total);
@@ -391,7 +401,7 @@
     
     logger->error("in BHRegisterTopics: Out of memory!\n");
 
-    return false;
+    goto exit_entry;
   }
   memset(topics_buf, 0x00, total);
 
@@ -420,6 +430,8 @@
   free(topics_buf);
   
   bus_errorset(rv);
+
+exit_entry:
   errString = bus_strerror(0, 1);
   
 #if defined(PRO_DE_SERIALIZE)
@@ -439,8 +451,11 @@
   *reply_len = len;
 #endif 
   
-  return true;
+  if (rv == 0) {
+    return true;
+  }
 
+  return false;
 }
 
 int BHQueryTopicAddress(const void *remote, const int remote_len, const void *topic, const int topic_len, void **reply, int *reply_len, const int timeout_ms)
@@ -464,6 +479,17 @@
 	}_input0;
   
 	const char *_input1;
+
+  struct _MsgQueryTopicReply
+  {
+    std::string proc_id;
+
+    unsigned long long mq_id;
+    long long abs_addr;
+    std::string ip;
+    int port;
+  } mtr_list[128];
+  int mtr_list_num = 0;
  
   ::bhome_msg::BHAddress input0;
   ::bhome_msg::MsgQueryTopic input1;
@@ -471,7 +497,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
   _input0.mq_id = input0.mq_id();
@@ -485,7 +511,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 #endif 
   
@@ -495,7 +521,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
 
 #if defined(PRO_DE_SERIALIZE)
@@ -511,17 +537,6 @@
   bus_errorset(rv);
 #if defined(PRO_DE_SERIALIZE)
 
-	struct _MsgQueryTopicReply
-	{
-		std::string proc_id;
-
-		unsigned long long mq_id;
-		long long abs_addr;
-		std::string ip;
-		int port;
-	}mtr_list[128];
-	int mtr_list_num = 0;
-
   if (rv == 0) {
     
     ptr = (ProcInfo_query *)((char *)buf + sizeof(int));
@@ -530,9 +545,10 @@
     if (mtr_list_num > sizeof(mtr_list) / sizeof(mtr_list[0])) {
       mtr_list_num = sizeof(mtr_list) / sizeof(mtr_list[0]);
     }
-    
+  
+    Proc_ptr = &(ptr->procData);
     for(int i = 0; i < mtr_list_num; i++) {
-      mtr_list[i].proc_id = ptr->procData.proc_id;
+      mtr_list[i].proc_id = (Proc_ptr + i)->proc_id;
       mtr_list[i].mq_id = ID_RSV;
       mtr_list[i].abs_addr = ABS_ID_RSV;
       mtr_list[i].ip = "127.0.0.1";
@@ -540,6 +556,7 @@
     }
   }
   
+exit_entry:
   errString = bus_strerror(0, 1);
   
   ::bhome_msg::MsgQueryTopicReply mtr;
@@ -575,8 +592,11 @@
   
 #endif 
   
-  return true;
+  if (rv == 0) {
+    return true;
+  }
 
+  return false;
 }
 
 int BHQueryProcs(const void *remote, const int remote_len, const void *query, const int query_len, void **reply, int *reply_len, const int timeout_ms)
@@ -607,7 +627,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
 	_input0.mq_id = input0.mq_id();
@@ -620,7 +640,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 #endif 
   
@@ -630,7 +650,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 
   if (query != NULL) {
@@ -639,7 +659,8 @@
   
   rv = net_mod_socket_reg(gNetmod_socket, data_buf, strlen(data_buf), &buf, &size, timeout_ms, PROC_QUE_ATCS);
   bus_errorset(rv);
-  
+
+exit_entry:
 #if defined(PRO_DE_SERIALIZE)
   struct _MsgQueryProcReply
 	{
@@ -684,31 +705,32 @@
         }
       }
     }
-    
-    errString = bus_strerror(0, 1);
-
-    ::bhome_msg::MsgQueryProcReply mpr;
-    mpr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
-    mpr.mutable_errmsg()->set_errstring(errString);
-    
-    for(int i = 0; i < mpr_list_num; i++)
-    {
-      ::bhome_msg::MsgQueryProcReply_Info *mpri = mpr.add_proc_list();
-      mpri->mutable_proc()->set_proc_id(mpr_list[i].proc_id);
-      mpri->mutable_proc()->set_name(mpr_list[i].name);
-      mpri->mutable_proc()->set_public_info(mpr_list[i].public_info);
-      mpri->mutable_proc()->set_private_info(mpr_list[i].private_info);
-      mpri->set_online(mpr_list[i].online);
-      for(int j = 0; j < mpr_list[i].topic_list_num; j++)
-      {
-        mpri->mutable_topics()->add_topic_list(mpr_list[i].topic_list[j]);
-      }
-    }
-    
-    *reply_len = mpr.ByteSizeLong();
-    *reply = malloc(*reply_len);
-    mpr.SerializePartialToArray(*reply, *reply_len);
   }
+    
+  errString = bus_strerror(0, 1);
+
+  ::bhome_msg::MsgQueryProcReply mpr;
+  mpr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
+  mpr.mutable_errmsg()->set_errstring(errString);
+  
+  for(int i = 0; i < mpr_list_num; i++)
+  {
+    ::bhome_msg::MsgQueryProcReply_Info *mpri = mpr.add_proc_list();
+    mpri->mutable_proc()->set_proc_id(mpr_list[i].proc_id);
+    mpri->mutable_proc()->set_name(mpr_list[i].name);
+    mpri->mutable_proc()->set_public_info(mpr_list[i].public_info);
+    mpri->mutable_proc()->set_private_info(mpr_list[i].private_info);
+    mpri->set_online(mpr_list[i].online);
+    for(int j = 0; j < mpr_list[i].topic_list_num; j++)
+    {
+      mpri->mutable_topics()->add_topic_list(mpr_list[i].topic_list[j]);
+    }
+  }
+  
+  *reply_len = mpr.ByteSizeLong();
+  *reply = malloc(*reply_len);
+  mpr.SerializePartialToArray(*reply, *reply_len);
+
 #else 
   if (rv == 0) {
     *reply = buf;
@@ -724,8 +746,11 @@
   }
 #endif 
 
-  return true;
+  if (rv == 0) {
+    return true;
+  }
 
+  return false;
 }
 
 int BHSubscribeTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
@@ -752,7 +777,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
   
   _input.amount = input.topic_list_size();
@@ -772,7 +797,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-    return false;
+    goto exit_entry;
   }
 #endif 
 
@@ -782,7 +807,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;    
   }
   
   topics_buf = (char *)malloc(total);
@@ -793,7 +818,7 @@
     
     logger->error("in BHSubscribeTopics: Out of memory!\n");
 
-    return false;
+    goto exit_entry;
   }
   memset(topics_buf, 0x00, total);
  
@@ -832,6 +857,7 @@
   
   }
  
+exit_entry:
   errString = bus_strerror(0, 1);
  
   free(topics_buf);
@@ -852,9 +878,12 @@
   *reply = buf;
   *reply_len = len;
 #endif 
-  
-  return true;
+ 
+  if (rv == 0) {
+    return true;
+  }
 
+  return false;
 }
 
 int BHSubscribeNetTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
@@ -890,7 +919,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-		return false;
+		goto exit_entry;
   }
   
 	_input.proc_id = input.proc_id().c_str();
@@ -900,6 +929,8 @@
 
   rv = 0;
   bus_errorset(rv);
+
+exit_entry:
   errString = bus_strerror(0, 1);
   
   ::bhome_msg::MsgCommonReply mcr;
@@ -910,7 +941,11 @@
 	mcr.SerializePartialToArray(*reply,*reply_len);
 #endif 
 
-  return true;
+  if (rv == 0) {
+    return true;
+  }
+
+  return false;
 }
 
 #if defined(PRO_DE_SERIALIZE)
@@ -948,8 +983,8 @@
     
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
-    
-		return false;
+  
+    return false;
   }
 #endif 
   
@@ -986,9 +1021,7 @@
 
   if (rv > 0)
     return true;
-  
-  bus_errorset(rv);
-  
+ 
   return false;
 }
 
@@ -997,7 +1030,7 @@
   int rv;
   int len;
   void *buf;
-  int key;
+  int key = 0;
   int size;
   int sec, nsec;
   char buf_temp[100] = { 0x00 };
@@ -1017,14 +1050,14 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
   
   if ((msgpub == NULL) || (msgpub_len == NULL)) {
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-		return false;
+    goto exit_entry;
   }
 
   if (timeout_ms > 0) {
@@ -1055,9 +1088,9 @@
       rv = EBUS_NO_MEM;
       bus_errorset(rv);
       
-      logger->error("in BHRequest: Out of memory!\n");
+      logger->error("in BHReadSub: Out of memory!\n");
 
-      return false;
+      goto exit_entry;
     }
     memset(topics_buf, 0x00, len + 10);
     
@@ -1071,11 +1104,11 @@
         rv = EBUS_NO_MEM;
         bus_errorset(rv);
         
-        logger->error("in BHRequest: Out of memory!\n");
+        logger->error("in BHReadSub: Out of memory!\n");
         
         free(topics_buf);
 
-        return false;
+        goto exit_entry;
       }
       memset(data_buf, 0x00, size - len + 10);
 
@@ -1093,56 +1126,61 @@
       rsr.data = topics_buf;
     }
 
-    sprintf(buf_temp, "%d", key);
-
-    if ((proc_id != NULL) && (proc_id_len != NULL)) {
-      rsr.proc_id = buf_temp;
-      *proc_id_len = rsr.proc_id.size();
-      *proc_id = malloc(*proc_id_len);
-      memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len);
-    }
-    
     free(topics_buf);
     free(data_buf);
-
-    ::bhome_msg::MsgPublish Mp; 
-    Mp.set_topic(rsr.topic);
-    Mp.set_data(rsr.data.data());
-    *msgpub_len = Mp.ByteSizeLong();
-    *msgpub = malloc(*msgpub_len);
-    Mp.SerializePartialToArray(*msgpub, *msgpub_len);
-#else 
-    void *ptr;
-    if (len < size) {
-      ptr = malloc(size - len);
-      len = size - len;
-      memcpy(ptr, data_buf, len);
-    } else {
-      ptr = malloc(len);
-      memcpy(ptr, topics_buf, len);
-    }
-    *msgpub = ptr;
-    *msgpub_len = len;
-   
-    free(topics_buf);
-    free(data_buf);
-
-    if ((proc_id != NULL) && (proc_id_len != NULL)) {
-      memset(buf_temp, 0x00, sizeof(buf_temp));
-      sprintf(buf_temp, "%d", key);
-      
-      *proc_id_len = strlen(buf_temp);
-      *proc_id = malloc(*proc_id_len);
-      memcpy(*proc_id, buf_temp, *proc_id_len);
-    }
-    
-#endif 
-
-  } else {
-
-    bus_errorset(rv);
-
   }
+  
+exit_entry:
+  sprintf(buf_temp, "%d", key);
+
+  if ((proc_id != NULL) && (proc_id_len != NULL)) {
+    rsr.proc_id = buf_temp;
+    *proc_id_len = rsr.proc_id.size();
+    *proc_id = malloc(*proc_id_len);
+    memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len);
+  }
+
+  if (rv != 0) {
+    rsr.topic = STR_RSV;
+    if (data_buf != NULL) {
+      rsr.data = data_buf;
+    } else {
+      rsr.data = STR_RSV;
+    }
+  }
+
+  ::bhome_msg::MsgPublish Mp; 
+  Mp.set_topic(rsr.topic);
+  Mp.set_data(rsr.data.data());
+  *msgpub_len = Mp.ByteSizeLong();
+  *msgpub = malloc(*msgpub_len);
+  Mp.SerializePartialToArray(*msgpub, *msgpub_len);
+#else 
+  void *ptr;
+  if (len < size) {
+    ptr = malloc(size - len);
+    len = size - len;
+    memcpy(ptr, data_buf, len);
+  } else {
+    ptr = malloc(len);
+    memcpy(ptr, topics_buf, len);
+  }
+  *msgpub = ptr;
+  *msgpub_len = len;
+ 
+  free(topics_buf);
+  free(data_buf);
+
+  if ((proc_id != NULL) && (proc_id_len != NULL)) {
+    memset(buf_temp, 0x00, sizeof(buf_temp));
+    sprintf(buf_temp, "%d", key);
+    
+    *proc_id_len = strlen(buf_temp);
+    *proc_id = malloc(*proc_id_len);
+    memcpy(*proc_id, buf_temp, *proc_id_len);
+  }
+  
+#endif 
 
   if (rv == 0)
     return true;
@@ -1153,15 +1191,17 @@
 int BHAsyncRequest(const void *remote, const int remote_len, const void *request, const int request_len, void **msg_id, int *msg_id_len)
 {
   int rv;
-  void *buf;
+  void *buf = NULL;
   int size;
-  int val;
+  int val = 0;
   int len;
   int min;
   int data;
   int sec, nsec;
+  std::string str;
   std::string MsgID;
   int timeout_ms = 3000;
+  char data_buf[MAX_STR_LEN] = { 0x00 };
   char buf_temp[MAX_STR_LEN] = { 0x00 };
   char *topics_buf = NULL;
   
@@ -1187,7 +1227,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
   
-		return false;
+    goto exit_entry;
   }
 	
 	_input0.mq_id = input0.mq_id();
@@ -1203,7 +1243,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
   
-		return false;
+    goto exit_entry;
   }
 #endif 
   
@@ -1213,7 +1253,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
  
 #if defined(PRO_DE_SERIALIZE)
@@ -1221,14 +1261,30 @@
 #else 
   strncpy(buf_temp, (char *)request, (sizeof(buf_temp) - 1) > strlen((char *)request) ? strlen((char *)request) : (sizeof(buf_temp) - 1));
 #endif 
-  
-  rv = net_mod_socket_reg(gNetmod_socket, buf_temp, strlen(buf_temp), &buf, &size, timeout_ms, PROC_QUE_STCS);
+ 
+  str = buf_temp;
+  val = net_mod_socket_buf_data_get(gNetmod_socket, str);
+  if(val > 0) {
+    
+    rv = 0;
+
+  } else {
+    rv = net_mod_socket_reg(gNetmod_socket, buf_temp, strlen(buf_temp), &buf, &size, timeout_ms, PROC_QUE_STCS);
+    if (rv == 0) {
+
+      len = size > (sizeof(data_buf) - 1) ? (sizeof(data_buf) - 1) : size;
+      memcpy(data_buf, (char *)buf, len);
+      val = atoi((char *)data_buf);
+      if (val > 0) {
+        str = buf_temp;
+        net_mod_socket_buf_data_set(gNetmod_socket, str, val);
+      }
+
+      free(buf);
+    }
+  }
+
   if (rv == 0) {
-
-    val = atoi((char *)buf);
-
-    free(buf);
-
     if (val > 0) {
 
       len = strlen(buf_temp) + 1;
@@ -1242,9 +1298,9 @@
         rv = EBUS_NO_MEM;
         bus_errorset(rv);
         
-        logger->error("in BHRequest: Out of memory!\n");
+        logger->error("in BHAsyncRequest: Out of memory!\n");
 
-        return false;
+        goto exit_entry;
       }
       memset(topics_buf, 0x00, len);
       
@@ -1280,6 +1336,8 @@
   }
 
   bus_errorset(rv);
+
+exit_entry:
   if((msg_id == NULL) || (msg_id_len == NULL)) { 
     if (rv == 0)
       return true;
@@ -1287,18 +1345,16 @@
     return false;
   }
   
-  if (rv == 0) {
-    
-    memset(buf_temp, 0x00, sizeof(buf_temp));
-    sprintf(buf_temp, "%d", val);
-    MsgID = buf_temp;
+  memset(buf_temp, 0x00, sizeof(buf_temp));
+  sprintf(buf_temp, "%d", val);
+  MsgID = buf_temp;
 
-    *msg_id_len = MsgID.size();
-	  *msg_id = malloc(*msg_id_len);
-	  memcpy(*msg_id, MsgID.data(), *msg_id_len);
+  *msg_id_len = MsgID.size();
+  *msg_id = malloc(*msg_id_len);
+  memcpy(*msg_id, MsgID.data(), *msg_id_len);
     
+  if (rv == 0)
     return true;
-  }
 	
   return false;
 
@@ -1308,14 +1364,16 @@
               void **reply, int *reply_len, const int timeout_ms)
 {
   int rv;
-  void *buf;
+  void *buf = NULL;
   int size;
   int val;
   int min, len;
   int data;
+  std::string str;
   net_node_t node;
   int node_size;  
   int recv_arr_size;
+  char data_buf[MAX_STR_LEN] = { 0x00 };
   net_mod_recv_msg_t *recv_arr;
   net_mod_err_t *errarr;
   int errarr_size = 0;
@@ -1351,9 +1409,10 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-		return false;
+    goto exit_entry;
   }
   
+  memset(&node, 0x00, sizeof(node));
   _input0.mq_id = input0.mq_id();
   _input0.abs_addr = input0.abs_addr();
   _input0.ip = input0.ip().c_str();
@@ -1367,7 +1426,7 @@
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-		return false;
+    goto exit_entry;
   }
 #endif 
   
@@ -1377,7 +1436,7 @@
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
   
 #if defined(PRO_DE_SERIALIZE)
@@ -1385,42 +1444,57 @@
 #else 
   strncpy(buf_temp, (char *)request, (sizeof(buf_temp) - 1) > request_len ? request_len : (sizeof(buf_temp) - 1));
 #endif 
-  
-  rv = net_mod_socket_reg(gNetmod_socket, buf_temp, strlen(buf_temp), &buf, &size, timeout_ms, PROC_QUE_STCS);
+ 
+  str = buf_temp;
+  val = net_mod_socket_buf_data_get(gNetmod_socket, str);
+  if(val > 0) {
+    
+    rv = 0;
+
+  } else {
+    rv = net_mod_socket_reg(gNetmod_socket, buf_temp, strlen(buf_temp), &buf, &size, timeout_ms, PROC_QUE_STCS);
+    if (rv == 0) {
+    
+      len = size > (sizeof(data_buf) - 1) ? (sizeof(data_buf) - 1) : size;
+      memcpy(data_buf, (char *)buf, len);
+      val = atoi((char *)data_buf);
+      if (val > 0) {
+        str = buf_temp;
+        net_mod_socket_buf_data_set(gNetmod_socket, str, val);
+      }
+
+      free(buf);
+    }
+  }
+
   if (rv == 0) {
     
-    val = atoi((char *)buf);
-
-    free(buf);
-
     if (val > 0) {
       memset(&node, 0x00, sizeof(node));
-      
+    
       len = strlen(buf_temp) + 1;
 #if defined(PRO_DE_SERIALIZE)
       len += strlen(_input1.data);
 #endif
 
-      data = net_mod_socket_svr_get(gNetmod_socket);
       topics_buf = (char *)malloc(len);
       if (topics_buf == NULL) {
-        
+      
         rv = EBUS_NO_MEM;
         bus_errorset(rv);
-        
+      
         logger->error("in BHRequest: Out of memory!\n");
-        
-        return false;
+      
+        goto exit_entry;
       }
       memset(topics_buf, 0x00, len);
-      
+    
       strncpy(topics_buf, buf_temp, strlen(buf_temp) + 1);
 #if defined(PRO_DE_SERIALIZE)
       strncpy(topics_buf + strlen(buf_temp) + 1, _input1.data, strlen(_input1.data));
-#endif 
+#endif
 
       node.key = val;
-      
       if (timeout_ms > 0) {
         
         rv = net_mod_socket_sendandrecv_timeout(gNetmod_socket, &node, 1, topics_buf, len, &recv_arr, &recv_arr_size, &errarr, &errarr_size, timeout_ms);
@@ -1441,14 +1515,15 @@
           size = recv_arr[0].content_length;
           buf = (char *)malloc(size);
           if (buf == NULL) {
-            printf("Out of memory\n");
+            logger->error("in BHRequest: Out of memory!\n");
             
-            exit(0);
+            free(topics_buf);
+
+            goto exit_entry;
           }
           memset((char *)buf, 0x00, size); 
           
           strncpy((char *)buf, (char *)recv_arr[0].content, size);
-          
         }
 
         net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
@@ -1458,30 +1533,31 @@
         }
 
         rv = 0;
-
       } else {
+
         rv = EBUS_TIMEOUT; 
       }
       
     } else {
       rv = EBUS_RES_UNSUPPORT; 
     }
-
+   
     free(topics_buf);
   }
-
+  
   bus_errorset(rv);
+
+  if ((proc_id != NULL) && (proc_id_len != NULL)) {
+    memset(buf_temp, 0x00, sizeof(buf_temp));
+    sprintf(buf_temp, "%d", node.key);
+
+    rr.proc_id = buf_temp;
+    *proc_id_len = rr.proc_id.size();
+    *proc_id = malloc(*proc_id_len);
+    memcpy(*proc_id, rr.proc_id.c_str(), *proc_id_len);
+  }
+
   if (rv == 0) {
-    if ((proc_id != NULL) && (proc_id_len != NULL)) {
-      memset(buf_temp, 0x00, sizeof(buf_temp));
-      sprintf(buf_temp, "%d", node.key);
-
-      rr.proc_id = buf_temp;
-      *proc_id_len = rr.proc_id.size();
-      *proc_id = malloc(*proc_id_len);
-      memcpy(*proc_id, rr.proc_id.c_str(), *proc_id_len);
-    }
-
     topics_buf = (char *)malloc(size + 10);
     if (topics_buf == NULL) {
       
@@ -1490,7 +1566,7 @@
       
       logger->error("in BHRequest: Out of memory!\n");
 
-      return false;
+      goto exit_entry;
     }
     memset(topics_buf, 0x00, size + 10);
     
@@ -1499,33 +1575,33 @@
     
     free(buf);
     free(topics_buf);
+  } else {
+
+    rr.data = STR_RSV;
   }
 
+exit_entry:
   errString = bus_strerror(0, 1);
   
 #if defined(PRO_DE_SERIALIZE) 
-  if (rv == 0) {
-    ::bhome_msg::MsgRequestTopicReply mrt; 
-    mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
-    mrt.mutable_errmsg()->set_errstring(errString);
-    mrt.set_data(rr.data.data());
-    *reply_len = mrt.ByteSizeLong();
-    *reply = malloc(*reply_len);
-    mrt.SerializePartialToArray(*reply, *reply_len);
-  }
+  ::bhome_msg::MsgRequestTopicReply mrt; 
+  mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
+  mrt.mutable_errmsg()->set_errstring(errString);
+  mrt.set_data(rr.data.data());
+  *reply_len = mrt.ByteSizeLong();
+  *reply = malloc(*reply_len);
+  mrt.SerializePartialToArray(*reply, *reply_len);
 #else 
-  if (rv == 0) {
-    min = strlen(errString) + 1;
-    buf = malloc(min);
-    memcpy(buf, errString, strlen(errString));
-    *((char *)buf + min - 1) = '\0';
+  min = strlen(errString) + 1;
+  buf = malloc(min);
+  memcpy(buf, errString, strlen(errString));
+  *((char *)buf + min - 1) = '\0';
 
-    *reply = buf;
-    *reply_len = min;
-  }
+  *reply = buf;
+  *reply_len = min;
 #endif
 
-   if (rv == 0)
+  if (rv == 0)
     return true;
 
   return false; 
@@ -1542,21 +1618,29 @@
   int sec, nsec;
   char buf_temp[MAX_STR_LEN] = { 0x00 };
   char *topics_buf = NULL;
-  
+
+  struct _ReadRequestReply
+  {
+    std::string proc_id;
+    std::string topic;
+    std::string data;
+    void *src;
+  } rrr;
+
   if (gRun_stat == 0) {
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
     bus_errorset(rv);
 
-    return false;
+    goto exit_entry;
   }
   
   if ((request == NULL) || (request_len == 0) || (src == NULL)) {
     rv = EBUS_INVALID_PARA;
     bus_errorset(rv);
     
-		return false;
+    goto exit_entry;
   }
 
   data = net_mod_socket_svr_get(gNetmod_socket);
@@ -1577,14 +1661,7 @@
   }
 
   if (rv == 0) {
-    struct _ReadRequestReply
-    {
-      std::string proc_id;
-      std::string topic;
-      std::string data;
-      void *src;
-    } rrr;
-
+ 
     if ((proc_id != NULL) && (proc_id_len != NULL)) {
       sprintf(buf_temp, "%d", key);
       rrr.proc_id = buf_temp;
@@ -1602,7 +1679,7 @@
       
       logger->error("in BHReadRequest: Out of memory!\n");
       
-      return false;
+      goto exit_entry;
     }
     memset(topics_buf, 0x00, size + MIN_STR_LEN);
     
@@ -1620,28 +1697,34 @@
     rrr.data = topics_buf + len + 1;
     
     free(topics_buf);
-
-#if defined(PRO_DE_SERIALIZE)
-    ::bhome_msg::MsgRequestTopic mrt;
-    mrt.set_topic(rrr.topic);
-    mrt.set_data(rrr.data.data());
-    *request_len = mrt.ByteSizeLong();
-    *request = malloc(*request_len);
-    mrt.SerializePartialToArray(*request,*request_len);
-#else 
-    *request = buf;
-    *request_len = size;
-#endif 
-
     free(buf);
 
-    buf = malloc(sizeof(int));
-    *(int *)buf = key;
-    *src = buf;
+    bus_errorset(rv);
   }
 
-  bus_errorset(rv);
-	
+exit_entry:
+
+  if (rv != 0) { 
+    rrr.topic = STR_RSV;
+    rrr.data = STR_RSV;   
+  }
+
+#if defined(PRO_DE_SERIALIZE)
+  ::bhome_msg::MsgRequestTopic mrt;
+  mrt.set_topic(rrr.topic);
+  mrt.set_data(rrr.data.data());
+  *request_len = mrt.ByteSizeLong();
+  *request = malloc(*request_len);
+  mrt.SerializePartialToArray(*request,*request_len);
+#else 
+  *request = buf;
+  *request_len = size;
+#endif 
+
+  buf = malloc(sizeof(int));
+  *(int *)buf = key;
+  *src = buf;
+
   if (rv == 0)
     return true;
   
@@ -1730,3 +1813,14 @@
   return false;
 
 }
+
+#if defined(MSG_HANDLER)
+int inter_key_get(void)
+{
+  if (gNetmod_socket != NULL)
+    return net_mod_socket_get_key(gNetmod_socket);
+
+  return 0;
+}
+#endif 
+

--
Gitblit v1.8.0