From 5c912c70e9333298ff48f7ea15424f72ca977b99 Mon Sep 17 00:00:00 2001
From: Fu Juntang <StrongTiger_001@163.com>
Date: 星期五, 17 九月 2021 09:43:55 +0800
Subject: [PATCH] Add the heartbeat logic feature.

---
 src/bh_api.cpp | 1201 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 643 insertions(+), 558 deletions(-)

diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index f707e6f..635fe95 100644
--- a/src/bh_api.cpp
+++ b/src/bh_api.cpp
@@ -7,8 +7,13 @@
 #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 "../proto/source/bhome_msg.pb.h"
 #include "../proto/source/bhome_msg_api.pb.h"
+
+#define TIME_WAIT       3
 
 static Logger *logger = LoggerFactory::getLogger();
 
@@ -17,7 +22,43 @@
 
 static pthread_mutex_t mutex;
 
-static char errString[100] = { 0x00 };
+static pthread_t gTids;
+
+static void *client_run_check(void *skptr) {
+  
+  pthread_detach(pthread_self());
+ 
+  int data;
+  int sec, nsec;
+  int rv;
+  int key;
+  char buf[MAX_STR_LEN] = { 0x00 };
+  void *buf_temp = NULL;
+  int size;
+
+  sec = TIME_WAIT;
+  nsec = 0;
+  sprintf(buf, "%s", "Success");
+  data = net_mod_socket_int_get(gNetmod_socket);
+  while(true) {
+    
+    rv = net_mod_socket_recvfrom(gNetmod_socket, &buf_temp, &size, &key, SVR_STR, data);
+    if (rv == 0) {
+      
+      BHFree(buf_temp, size);
+
+      rv = net_mod_socket_sendto_timeout(gNetmod_socket, buf, strlen(buf), key, sec, nsec, SVR_STR, data);
+      if (rv != 0) {
+        logger->error("the process check response failed with error: %s!\n", bus_strerror(rv));
+      }
+      
+    } else {
+      
+      logger->error("the process check failed with error: %s!\n", bus_strerror(rv));
+
+    }
+  }
+}
 
 int BHRegister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
 {
@@ -26,6 +67,7 @@
   int count = 0;
   void *buf = NULL;
   int min = 0;
+  char *errString = NULL;
   ProcInfo pData;
   
 #if defined(PRO_DE_SERIALIZE)
@@ -38,13 +80,11 @@
 	}_input;
   
   ::bhome_msg::ProcInfo input;
-	if(!input.ParseFromArray(proc_info, proc_info_len)) {
+	if ((!input.ParseFromArray(proc_info, proc_info_len)) || (reply == NULL) || (reply_len == NULL)) {
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    goto exit_entry;
+    bus_errorset(rv);
+    
+    return false;
   }
   
 	_input.proc_id = input.proc_id().c_str();
@@ -53,13 +93,11 @@
 	_input.private_info = input.private_info().c_str();
   
 #else   
-  if ((proc_info == NULL) || (proc_info_len == 0)) {
+  if ((proc_info == NULL) || (proc_info_len == 0) || (reply == NULL) || (reply_len == NULL)) {
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x90, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    goto exit_entry;
+    bus_errorset(rv);
+    
+    return false;
   }
 #endif 
  
@@ -71,10 +109,9 @@
     logger->error("the process has already registered!\n");
 
     rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
-    goto exit_entry;
+    return false;
   }
 
   rv = pthread_mutex_trylock(&mutex);
@@ -133,49 +170,65 @@
     }
 #endif 
 
+    if (pData.proc_id == NULL) {
+      rv = EBUS_INVALID_PARA;
+
+      bus_errorset(rv);
+
+      pthread_mutex_unlock(&mutex);
+      
+      return false;
+    }
+
     gNetmod_socket = net_mod_socket_open();
     hashtable_t *hashtable = mm_get_hashtable();
     key = hashtable_alloc_key(hashtable);
+    count = hashtable_alloc_key(hashtable);
+    rv = hashtable_alloc_key(hashtable);
+    net_mod_socket_int_set(gNetmod_socket, count);
+    net_mod_socket_svr_set(gNetmod_socket, rv);
+    sprintf(pData.int_info, "%d", count);
+    sprintf(pData.svr_info, "%d", rv);
     net_mod_socket_bind(gNetmod_socket, key);
   
     rv = net_mod_socket_reg(gNetmod_socket, &pData, sizeof(ProcInfo), NULL, 0, timeout_ms, PROC_REG);
 
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
     pthread_mutex_unlock(&mutex);
 
   } else {
 
     rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
+    return false;
   }
   
-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));
-    mcr.mutable_errmsg()->set_errstring(errString);
-    *reply_len = mcr.ByteSizeLong();
-    *reply = malloc(*reply_len);
-    mcr.SerializePartialToArray(*reply, *reply_len);
+  ::bhome_msg::MsgCommonReply mcr;
+  mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
+  mcr.mutable_errmsg()->set_errstring(errString);
+  *reply_len = mcr.ByteSizeLong();
+  *reply = malloc(*reply_len);
+  mcr.SerializePartialToArray(*reply, *reply_len);
 #else 
-    min = strlen(errString) + 1;
-    buf = malloc(min) ;
-    memcpy(buf, errString, strlen(errString));
-    *((char *)buf + min - 1) = '\0';
-    
-    *reply = buf;
-    *reply_len = min;
+  min = strlen(errString) + 1;
+  buf = malloc(min) ;
+  memcpy(buf, errString, strlen(errString));
+  *((char *)buf + min - 1) = '\0';
+  
+  *reply = buf;
+  *reply_len = min;
     
 #endif 
   
-  if (rv == 0)
-    return true;
+  pthread_create(&gTids, NULL, client_run_check, NULL);
+    
+  return true;
 
-  return false;
 }
 
 int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
@@ -183,6 +236,7 @@
   int rv;
   int min;
   void *buf = NULL;
+  char *errString = NULL;
 
 #if defined(PRO_DE_SERIALIZE)
   struct _ProcInfo_proto
@@ -195,20 +249,25 @@
   
   ::bhome_msg::ProcInfo input;
   
-	if(!input.ParseFromArray(proc_info, proc_info_len)) {
+	if(!input.ParseFromArray(proc_info, proc_info_len) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
+    bus_errorset(rv);
 
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    goto exit_entry;
+    return false;
   }
   
 	_input.proc_id = input.proc_id().c_str();
 	_input.name = input.name().c_str();
 	_input.public_info = input.public_info().c_str();
 	_input.private_info = input.private_info().c_str();
+#else 
+  if ((reply == NULL) || (reply_len == NULL)) {
+    rv = EBUS_INVALID_PARA;
+    bus_errorset(rv);
+
+    return false;
+  }
 #endif 
   
   if (gRun_stat == 0) {
@@ -216,10 +275,9 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
-    goto exit_entry;
+    return false;
   }
 
   rv = pthread_mutex_trylock(&mutex);
@@ -234,20 +292,19 @@
       gRun_stat = 0;
       
     }
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
     pthread_mutex_unlock(&mutex);
 
   } else {
 
     rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
+    
+    return false;
   }
   
-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));
@@ -265,10 +322,8 @@
   *reply_len = min;
 #endif 
   
-  if (rv == 0)
-    return true;
-
-  return false;
+  return true;
+  
 }
 
 int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
@@ -278,6 +333,7 @@
   void *buf = NULL;
   int total = 0;
   int count = 0; 
+  char *errString = NULL;
   char *topics_buf = NULL;
   
 #if defined(PRO_DE_SERIALIZE)
@@ -288,14 +344,12 @@
 	}_input;
 
   ::bhome_msg::MsgTopicList input;
-	if(!input.ParseFromArray(topics, topics_len)) {
+	if(!input.ParseFromArray(topics, topics_len) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
 
 	_input.amount = input.topic_list_size();
@@ -309,14 +363,12 @@
     total += strlen(_input.topics[i]) + 1;
   }
 #else 
-  if ((topics == NULL) || (topics_len == 0)) {
+  if ((topics == NULL) || (topics_len == 0) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
   
   total = topics_len;
@@ -326,62 +378,50 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
-    goto exit_entry;
+    return false;
   }
 
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
+  topics_buf = (char *)malloc(total);
+  if (topics_buf == NULL) {
+    
+    rv = EBUS_NO_MEM;
+    bus_errorset(rv);
+    
+    logger->error("in BHRegisterTopics: Out of memory!\n");
 
-    topics_buf = (char *)malloc(total);
-    if (topics_buf == NULL) {
-      
-      rv = EBUS_RES_NO;
-      memset(errString, 0x00, sizeof(errString));
-      strncpy(errString, bus_strerror(rv), sizeof(errString));
-      
-      logger->error("in BHRegisterTopics: Out of memory!\n");
-      
-      goto exit_entry;
-    }
-    memset(topics_buf, 0x00, total);
+    return false;
+  }
+  memset(topics_buf, 0x00, total);
 
 #if defined(PRO_DE_SERIALIZE)
-    for (i = 0; i < _input.amount; i++) {
-      
-      len = strlen(_input.topics[i]);
-      strncpy(topics_buf + count, _input.topics[i], len);
-      
-      count += len;
-
-      if ((_input.amount > 1) && (i < (_input.amount - 1))) {
-        strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
-        
-        count++;
-      }
-    }
-
-#else 
-    memcpy(topics_buf, topics, topics_len);
-#endif 
-
-    rv = net_mod_socket_reg(gNetmod_socket, topics_buf, strlen(topics_buf) + 1, NULL, 0, timeout_ms, PROC_REG_TCS);
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+  for (i = 0; i < _input.amount; i++) {
     
-    free(topics_buf);
-    pthread_mutex_unlock(&mutex);
-  
-  } else {
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    len = strlen(_input.topics[i]);
+    strncpy(topics_buf + count, _input.topics[i], len);
+    
+    count += len;
+
+    if ((_input.amount > 1) && (i < (_input.amount - 1))) {
+      strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
+      
+      count++;
+    }
   }
 
-exit_entry:
+#else 
+  memcpy(topics_buf, topics, topics_len);
+  count = topics_len;
+#endif 
+
+  rv = net_mod_socket_reg(gNetmod_socket, topics_buf, count, NULL, 0, timeout_ms, PROC_REG_TCS);
+
+  free(topics_buf);
+  
+  bus_errorset(rv);
+  errString = bus_strerror(0, 1);
+  
 #if defined(PRO_DE_SERIALIZE)
   ::bhome_msg::MsgCommonReply mcr;
 	mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -399,10 +439,8 @@
   *reply_len = len;
 #endif 
   
-  if (rv == 0)
-    return true;
+  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)
@@ -411,10 +449,11 @@
   int min;
   void *buf = NULL;
   int size;
+  char *errString = NULL;
   char topics_buf[MAX_STR_LEN] = { 0x00 };
   ProcInfo_query *ptr = NULL;
   ProcInfo *Proc_ptr = NULL;
-  
+
 #if defined(PRO_DE_SERIALIZE)
   struct _BHAddress
 	{
@@ -428,13 +467,11 @@
  
   ::bhome_msg::BHAddress input0;
   ::bhome_msg::MsgQueryTopic input1;
-	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(topic, topic_len)) {
+	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(topic, topic_len) || (reply == NULL) || (reply_len == NULL)) {
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
   
   _input0.mq_id = input0.mq_id();
@@ -444,13 +481,11 @@
 	_input1 = input1.topic().c_str();
   
 #else 
-  if ((topic == NULL) || (topic_len == 0)) {
+  if ((topic == NULL) || (topic_len == 0) || (reply == NULL) || (reply_len == NULL)) {
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
 #endif 
   
@@ -458,38 +493,22 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
-    goto exit_entry;
+    return false;
   }
-
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
 
 #if defined(PRO_DE_SERIALIZE)
-    min = (strlen(_input1) > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : strlen(_input1));
-    strncpy(topics_buf, _input1, min);
+  min = (strlen(_input1) > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : strlen(_input1));
+  strncpy(topics_buf, _input1, min);
 #else 
-    min = (topic_len > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : topic_len);
-    buf = const_cast<void *>(topic);
-    strncpy(topics_buf, (const char *)buf, min);
+  min = (topic_len > (MAX_STR_LEN - 1) ? (MAX_STR_LEN - 1) : topic_len);
+  buf = const_cast<void *>(topic);
+  strncpy(topics_buf, (const char *)buf, min);
 #endif 
-    rv = net_mod_socket_reg(gNetmod_socket, topics_buf, strlen(topics_buf) + 1, &buf, &size, timeout_ms, PROC_QUE_TCS);
-   
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    pthread_mutex_unlock(&mutex);
-    
-  } else {
-
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-  }
-	
-exit_entry:
+  rv = net_mod_socket_reg(gNetmod_socket, topics_buf, min, &buf, &size, timeout_ms, PROC_QUE_TCS);
+ 
+  bus_errorset(rv);
 #if defined(PRO_DE_SERIALIZE)
 
 	struct _MsgQueryTopicReply
@@ -514,12 +533,14 @@
     
     for(int i = 0; i < mtr_list_num; i++) {
       mtr_list[i].proc_id = ptr->procData.proc_id;
-      mtr_list[i].mq_id = 0x00;
-      mtr_list[i].abs_addr = 0x00;
-      mtr_list[i].ip = "192.168.1.1";
+      mtr_list[i].mq_id = ID_RSV;
+      mtr_list[i].abs_addr = ABS_ID_RSV;
+      mtr_list[i].ip = "127.0.0.1";
       mtr_list[i].port = 5000;
     }
   }
+  
+  errString = bus_strerror(0, 1);
   
   ::bhome_msg::MsgQueryTopicReply mtr;
 	mtr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -554,10 +575,8 @@
   
 #endif 
   
-  if (rv == 0)
-    return true;
+  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)
@@ -566,6 +585,7 @@
   void *buf = NULL;
   int size;
   int min;
+  char *errString = NULL;
   ProcInfo_sum *Proc_ptr = NULL;
   char data_buf[MAX_STR_LEN] = { 0x00 };
 
@@ -582,13 +602,12 @@
   
   ::bhome_msg::BHAddress input0;
 	::bhome_msg::MsgQueryProc input1;
-	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(query, query_len)) {
+	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(query, query_len) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
   
 	_input0.mq_id = input0.mq_id();
@@ -596,40 +615,31 @@
 	_input0.ip = input0.ip().c_str();
 	_input0.port = input0.port();
 	_input1 = input1.proc_id().c_str();
+#else 
+  if ((reply == NULL) || (reply_len == NULL)) {
+    rv = EBUS_INVALID_PARA;
+    bus_errorset(rv);
+    
+    return false;
+  }
 #endif 
   
   if (gRun_stat == 0) {
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    goto exit_entry;
+    bus_errorset(rv);
+    
+    return false;
   }
 
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
-  
-    if (query != NULL) {
-      strncpy(data_buf, (char *)query, (sizeof(data_buf) - 1) > query_len ? query_len : (sizeof(data_buf) - 1));
-    }
-    
-    rv = net_mod_socket_reg(gNetmod_socket, data_buf, strlen(data_buf), &buf, &size, timeout_ms, PROC_QUE_ATCS);
-    
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    pthread_mutex_unlock(&mutex);
-    
-  } else {
-  
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+  if (query != NULL) {
+    strncpy(data_buf, (char *)query, (sizeof(data_buf) - 1) > query_len ? query_len : (sizeof(data_buf) - 1));
   }
   
-exit_entry:
+  rv = net_mod_socket_reg(gNetmod_socket, data_buf, strlen(data_buf), &buf, &size, timeout_ms, PROC_QUE_ATCS);
+  bus_errorset(rv);
+  
 #if defined(PRO_DE_SERIALIZE)
   struct _MsgQueryProcReply
 	{
@@ -674,6 +684,8 @@
         }
       }
     }
+    
+    errString = bus_strerror(0, 1);
 
     ::bhome_msg::MsgQueryProcReply mpr;
     mpr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -695,7 +707,7 @@
     
     *reply_len = mpr.ByteSizeLong();
     *reply = malloc(*reply_len);
-    mpr.SerializePartialToArray(*reply,*reply_len);
+    mpr.SerializePartialToArray(*reply, *reply_len);
   }
 #else 
   if (rv == 0) {
@@ -712,10 +724,7 @@
   }
 #endif 
 
-	if (rv == 0)
-    return true;
-
-  return false;
+  return true;
 
 }
 
@@ -727,6 +736,7 @@
   int count = 0;
   int len, i;
   void *buf = NULL;
+  char *errString = NULL;
   char *topics_buf = NULL;
   
 #if defined(PRO_DE_SERIALIZE)
@@ -737,13 +747,12 @@
 	}_input;
   
   ::bhome_msg::MsgTopicList input;
-	if(!input.ParseFromArray(topics, topics_len)) {
+	if(!input.ParseFromArray(topics, topics_len) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
   
   _input.amount = input.topic_list_size();
@@ -759,12 +768,11 @@
   }
 
 #else 
-  if ((topics == NULL) || (topics_len == 0)) {
+  if ((topics == NULL) || (topics_len == 0) || (reply == NULL) || (reply_len == NULL)) {
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
-		goto exit_entry;
+    return false;
   }
 #endif 
 
@@ -772,76 +780,62 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
-    goto exit_entry;
+    return false;
   }
+  
+  topics_buf = (char *)malloc(total);
+  if (topics_buf == NULL) {
+    
+    rv = EBUS_NO_MEM;
+    bus_errorset(rv);
+    
+    logger->error("in BHSubscribeTopics: Out of memory!\n");
 
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
-
-    topics_buf = (char *)malloc(total);
-    if (topics_buf == NULL) {
-      
-      rv = EBUS_RES_NO;
-      memset(errString, 0x00, sizeof(errString));
-      strncpy(errString, bus_strerror(rv), sizeof(errString));
-      
-      logger->error("in BHSubscribeTopics: Out of memory!\n");
-      
-      goto exit_entry;
-    }
-    memset(topics_buf, 0x00, total);
-   
+    return false;
+  }
+  memset(topics_buf, 0x00, total);
+ 
 #if defined(PRO_DE_SERIALIZE)   
-    for (i = 0; i < _input.amount; i++) {
-      len = strlen(_input.topics[i]);
-      strncpy(topics_buf + count, _input.topics[i], min);
-      
-      count += len;
+  for (i = 0; i < _input.amount; i++) {
+    len = strlen(_input.topics[i]);
+    strncpy(topics_buf + count, _input.topics[i], len);
+    
+    count += len;
 
-      if ((_input.amount > 1) && (i < (_input.amount - 1))) {
-        strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
-        
-        count++;
-      }
+    if ((_input.amount > 1) && (i < (_input.amount - 1))) {
+      strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
+      
+      count++;
     }
+  }
 
 #else 
-    memcpy(topics_buf, topics, topics_len);
+  memcpy(topics_buf, topics, topics_len);
+  count = topics_len;
 #endif 
 
-    if (timeout_ms > 0) {
-    
-      sec = timeout_ms / 1000;
-      nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
-      rv = net_mod_socket_sub_timeout(gNetmod_socket, topics_buf, strlen(topics_buf) + 1, sec, nsec); 
+  if (timeout_ms > 0) {
+  
+    sec = timeout_ms / 1000;
+    nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
+    rv = net_mod_socket_sub_timeout(gNetmod_socket, topics_buf, count, sec, nsec); 
 
-    } else if (timeout_ms == 0) {
-    
-      rv = net_mod_socket_sub_nowait(gNetmod_socket, topics_buf, strlen(topics_buf) + 1);
-    
-    } else {
-    
-      rv = net_mod_socket_sub(gNetmod_socket, topics_buf, strlen(topics_buf) + 1);
-    
-    }
-   
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-   
-    free(topics_buf);
-    pthread_mutex_unlock(&mutex);
-
+  } else if (timeout_ms == 0) {
+  
+    rv = net_mod_socket_sub_nowait(gNetmod_socket, topics_buf, count);
+  
   } else {
-
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+  
+    rv = net_mod_socket_sub(gNetmod_socket, topics_buf, count);
+  
   }
+ 
+  errString = bus_strerror(0, 1);
+ 
+  free(topics_buf);
 
-exit_entry:  
 #if defined(PRO_DE_SERIALIZE)
   ::bhome_msg::MsgCommonReply mcr;
 	mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -859,10 +853,7 @@
   *reply_len = len;
 #endif 
   
-  if (rv == 0)
-    return true;
-
-  return false;
+  return true;
 
 }
 
@@ -882,6 +873,7 @@
 int BHHeartbeat(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
 {
   int rv;
+  char *errString = NULL;
   
 #if defined(PRO_DE_SERIALIZE)
   struct _ProcInfo_proto
@@ -896,9 +888,7 @@
 	if(!input.ParseFromArray(proc_info,proc_info_len)) {
     
     rv = EBUS_INVALID_PARA;
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
 		return false;
   }
@@ -909,14 +899,14 @@
 	_input.private_info = input.private_info().c_str();
 
   rv = 0;
-  memset(errString, 0x00, sizeof(errString));
-  strncpy(errString, bus_strerror(rv), sizeof(errString));
+  bus_errorset(rv);
+  errString = bus_strerror(0, 1);
   
   ::bhome_msg::MsgCommonReply mcr;
 	mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
 	mcr.mutable_errmsg()->set_errstring(errString);
-	*reply_len=mcr.ByteSizeLong();
-	*reply=malloc(*reply_len);
+	*reply_len = mcr.ByteSizeLong();
+	*reply = malloc(*reply_len);
 	mcr.SerializePartialToArray(*reply,*reply_len);
 #endif 
 
@@ -924,7 +914,7 @@
 }
 
 #if defined(PRO_DE_SERIALIZE)
-int BHPublish(const char *msgpub, const int msgpub_len, const int timeout_ms)
+int BHPublish(const void *msgpub, const int msgpub_len, const int timeout_ms)
 #else
 int BHPublish(const char *topic, const char *content, const int timeout_ms)
 #endif
@@ -946,8 +936,7 @@
 	if(!input.ParseFromArray(msgpub, msgpub_len)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
 		return false;
   }
@@ -958,8 +947,7 @@
   if ((topic == NULL) || (content == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
 		return false;
   }
@@ -969,51 +957,38 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
   }
-
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {  
+ 
 #if defined(PRO_DE_SERIALIZE)
-    if (timeout_ms > 0) {
-      rv = net_mod_socket_pub_timeout(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data), timeout_ms);
-    } else if (timeout_ms == 0) {
-      rv = net_mod_socket_pub_nowait(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data)); 
-      
-    } else {
-      
-      rv = net_mod_socket_pub(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data));
-    }
-#else 
-    if (timeout_ms > 0) {
-      rv = net_mod_socket_pub_timeout(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content), timeout_ms);   
-      
-    } else if (timeout_ms == 0) {
-      rv = net_mod_socket_pub_nowait(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content));
+  if (timeout_ms > 0) {
+    rv = net_mod_socket_pub_timeout(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data), timeout_ms);
+  } else if (timeout_ms == 0) {
+    rv = net_mod_socket_pub_nowait(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data)); 
     
-    } else {
-      rv = net_mod_socket_pub(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content));
-    }
+  } else {
+    
+    rv = net_mod_socket_pub(gNetmod_socket, &node_arr, node_arr_len, _input.topic, strlen(_input.topic), _input.data, strlen(_input.data));
+  }
+#else 
+  if (timeout_ms > 0) {
+    rv = net_mod_socket_pub_timeout(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content), timeout_ms);   
+    
+  } else if (timeout_ms == 0) {
+    rv = net_mod_socket_pub_nowait(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content));
+  
+  } else {
+    rv = net_mod_socket_pub(gNetmod_socket, &node_arr, node_arr_len, topic, strlen(topic), content, strlen(content));
+  }
 #endif 
 
-    pthread_mutex_unlock(&mutex);
-
-    if (rv > 0)
-      return true;
-    
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-  } else {
-
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-  }
-
+  if (rv > 0)
+    return true;
+  
+  bus_errorset(rv);
+  
   return false;
 }
 
@@ -1025,8 +1000,9 @@
   int key;
   int size;
   int sec, nsec;
-  char topics_buf[MAX_STR_LEN] = { 0x00 };
-  char data_buf[MAX_STR_LEN * 3] = { 0x00 }; 
+  char buf_temp[100] = { 0x00 };
+  char *topics_buf = NULL;
+  char *data_buf = NULL; 
 
   struct _ReadSubReply
   {
@@ -1039,10 +1015,16 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
+  }
+  
+  if ((msgpub == NULL) || (msgpub_len == NULL)) {
+    rv = EBUS_INVALID_PARA;
+    bus_errorset(rv);
+    
+		return false;
   }
 
   if (timeout_ms > 0) {
@@ -1066,11 +1048,38 @@
     if (len > size) {
       len = size;
     }
-    strncpy(topics_buf, (char *)buf, len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : len);
+    
+    topics_buf = (char *)malloc(len + 10);
+    if (topics_buf == NULL) {
+      
+      rv = EBUS_NO_MEM;
+      bus_errorset(rv);
+      
+      logger->error("in BHRequest: Out of memory!\n");
+
+      return false;
+    }
+    memset(topics_buf, 0x00, len + 10);
+    
+    strncpy(topics_buf, (char *)buf, len);
     
     if (len < size) {
-      len = strlen(topics_buf) + 1;
+      
+      data_buf = (char *)malloc(size - len + 10);
+      if (data_buf == NULL) {
+        
+        rv = EBUS_NO_MEM;
+        bus_errorset(rv);
+        
+        logger->error("in BHRequest: Out of memory!\n");
+        
+        free(topics_buf);
 
+        return false;
+      }
+      memset(data_buf, 0x00, size - len + 10);
+
+      len = strlen(topics_buf) + 1;
       strncpy(data_buf, (char *)buf + len, size - len);
     }
 
@@ -1078,15 +1087,23 @@
 
 #if defined(PRO_DE_SERIALIZE)
     rsr.topic = topics_buf;
-    rsr.data = data_buf;
+    if (data_buf != NULL) {
+      rsr.data = data_buf;
+    } else {
+      rsr.data = topics_buf;
+    }
 
-    memset(topics_buf, 0x00, sizeof(topics_buf));
-    sprintf(topics_buf, "%d", key);
+    sprintf(buf_temp, "%d", key);
 
-    rsr.proc_id = topics_buf;
-    *proc_id_len = rsr.proc_id.size();
-    *proc_id = malloc(*proc_id_len);
-    memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len);
+    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);
@@ -1106,22 +1123,25 @@
     }
     *msgpub = ptr;
     *msgpub_len = len;
-    
-    memset(topics_buf, 0x00, sizeof(topics_buf));
-    sprintf(topics_buf, "%d", key);
-    
-    *proc_id_len = strlen(topics_buf);
-    *proc_id = malloc(*proc_id_len);
-    memcpy(*proc_id, topics_buf, *proc_id_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 
 
-    pthread_mutex_unlock(&mutex);
-
   } else {
 
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
+
   }
 
   if (rv == 0)
@@ -1138,10 +1158,12 @@
   int val;
   int len;
   int min;
+  int data;
   int sec, nsec;
   std::string MsgID;
   int timeout_ms = 3000;
-  char topics_buf[MAX_STR_LEN * MAX_TOPICS_NUN] = { 0x00 };
+  char buf_temp[MAX_STR_LEN] = { 0x00 };
+  char *topics_buf = NULL;
   
 #if defined(PRO_DE_SERIALIZE)
   struct _BHAddress
@@ -1163,8 +1185,7 @@
 	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
   
 		return false;
   }
@@ -1180,8 +1201,7 @@
   if ((request == NULL) || (request_len == 0)) {
 
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
   
 		return false;
   }
@@ -1191,79 +1211,87 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
   }
  
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
 #if defined(PRO_DE_SERIALIZE)
-    strncpy(topics_buf, _input1.topic, (sizeof(topics_buf) - 1) > strlen(_input1.topic) ? strlen(_input1.topic) : (sizeof(topics_buf) - 1));
+  strncpy(buf_temp, _input1.topic, (sizeof(buf_temp) - 1) > strlen(_input1.topic) ? strlen(_input1.topic) : (sizeof(buf_temp) - 1));
 #else 
-    strncpy(topics_buf, (char *)request, (sizeof(topics_buf) - 1) > strlen((char *)request) ? strlen((char *)request) : (sizeof(topics_buf) - 1));
+  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, topics_buf, strlen(topics_buf), &buf, &size, timeout_ms, PROC_QUE_STCS);
-    if (rv == 0) {
+  
+  rv = net_mod_socket_reg(gNetmod_socket, buf_temp, strlen(buf_temp), &buf, &size, timeout_ms, PROC_QUE_STCS);
+  if (rv == 0) {
 
-      val = atoi((char *)buf);
+    val = atoi((char *)buf);
 
-      free(buf);
+    free(buf);
 
-      if (val > 0) {
+    if (val > 0) {
 
-        len = strlen(topics_buf);
+      len = strlen(buf_temp) + 1;
 #if defined(PRO_DE_SERIALIZE)
-        min = (sizeof(topics_buf) - 1 - len ) > strlen(_input1.data) ? strlen(_input1.data) : (sizeof(topics_buf) - 1 - len );
-        strncpy(topics_buf + len + 1, _input1.data, min);
-        len += (min + 1);
+      len += strlen(_input1.data);
 #endif
-        if (timeout_ms > 0) {
 
-          sec = timeout_ms / 1000;
-          nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
-          
-          rv = net_mod_socket_sendto_timeout(gNetmod_socket, topics_buf, len, val, sec, nsec); 
-          
-        } else if (timeout_ms == 0) {
+      topics_buf = (char *)malloc(len);
+      if (topics_buf == NULL) {
+        
+        rv = EBUS_NO_MEM;
+        bus_errorset(rv);
+        
+        logger->error("in BHRequest: Out of memory!\n");
 
-          rv = net_mod_socket_sendto_nowait(gNetmod_socket, topics_buf, len, val); 
+        return false;
+      }
+      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 
 
-        } else {
+      data = net_mod_socket_svr_get(gNetmod_socket); 
+      if (timeout_ms > 0) {
 
-          rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val); 
-        }            
+        sec = timeout_ms / 1000;
+        nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
+        
+        rv = net_mod_socket_sendto_timeout(gNetmod_socket, topics_buf, len, val, sec, nsec, SVR_STR, data); 
+        
+      } else if (timeout_ms == 0) {
+
+        rv = net_mod_socket_sendto_nowait(gNetmod_socket, topics_buf, len, val, SVR_STR, data); 
+
       } else {
 
-        rv = EBUS_RES_UNSUPPORT; 
-        
-      }
-    }
+        rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val, SVR_STR, data); 
+      } 
 
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-    
-    pthread_mutex_unlock(&mutex);
-
-    if((msg_id == NULL) || (msg_id_len == NULL)) { 
-      if (rv == 0)
-        return true;
+      free(topics_buf);
       
-      return false;
-    }
-  } else {
+    } else {
 
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+      rv = EBUS_RES_UNSUPPORT; 
+      
+    }
+  }
+
+  bus_errorset(rv);
+  if((msg_id == NULL) || (msg_id_len == NULL)) { 
+    if (rv == 0)
+      return true;
+    
+    return false;
   }
   
   if (rv == 0) {
-    memset(topics_buf, 0x00, sizeof(topics_buf));
-    sprintf(topics_buf, "%d", val);
-    MsgID = topics_buf;
+    
+    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);
@@ -1284,14 +1312,16 @@
   int size;
   int val;
   int min, len;
+  int data;
   net_node_t node;
   int node_size;  
   int recv_arr_size;
   net_mod_recv_msg_t *recv_arr;
   net_mod_err_t *errarr;
   int errarr_size = 0;
-  int sec, nsec;
-  char topics_buf[MAX_STR_LEN] = { 0x00 };
+  char *errString = NULL;
+  char buf_temp[MAX_STR_LEN] = { 0x00 };
+  char *topics_buf = NULL;
   
   struct _RequestReply
   {
@@ -1316,28 +1346,26 @@
 
   ::bhome_msg::BHAddress input0;
 	::bhome_msg::MsgRequestTopic input1;
-	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len)) {
+	if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len) || (reply == NULL) || (reply_len == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
 		return false;
   }
   
   _input0.mq_id = input0.mq_id();
-	_input0.abs_addr = input0.abs_addr();
-	_input0.ip = input0.ip().c_str();
-	_input0.port = input0.port();
-	_input1.topic = input1.topic().c_str();
-	_input1.data = input1.data().c_str();
+  _input0.abs_addr = input0.abs_addr();
+  _input0.ip = input0.ip().c_str();
+  _input0.port = input0.port();
+  _input1.topic = input1.topic().c_str();
+  _input1.data = input1.data().c_str();
   
 #else 
-  if ((request == NULL) || (request_len == 0)) {
+  if ((request == NULL) || (request_len == 0) || (reply == NULL) || (reply_len == NULL)) {
 
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
 		return false;
   }
@@ -1347,106 +1375,146 @@
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
   }
   
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
 #if defined(PRO_DE_SERIALIZE)
-    strncpy(topics_buf, _input1.topic, (sizeof(topics_buf) - 1) > strlen(_input1.topic) ? strlen(_input1.topic)  : (sizeof(topics_buf) - 1));
+  strncpy(buf_temp, _input1.topic, (sizeof(buf_temp) - 1) > strlen(_input1.topic) ? strlen(_input1.topic)  : (sizeof(buf_temp) - 1));
 #else 
-    strncpy(topics_buf, (char *)request, (sizeof(topics_buf) - 1) > request_len ? request_len : (sizeof(topics_buf) - 1));
+  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);
+  if (rv == 0) {
     
-    rv = net_mod_socket_reg(gNetmod_socket, topics_buf, strlen(topics_buf), &buf, &size, timeout_ms, PROC_QUE_STCS);
-    if (rv == 0) {
-      val = atoi((char *)buf);
+    val = atoi((char *)buf);
 
-      free(buf);
+    free(buf);
 
-      if (val > 0) {
-        memset(&node, 0x00, sizeof(node));
-
-        len = strlen(topics_buf);
+    if (val > 0) {
+      memset(&node, 0x00, sizeof(node));
+      
+      len = strlen(buf_temp) + 1;
 #if defined(PRO_DE_SERIALIZE)
-        min = (sizeof(topics_buf) - 1 - len ) > strlen(_input1.data) ? strlen(_input1.data) : (sizeof(topics_buf) - 1 - len );
-        strncpy(topics_buf + len + 1, _input1.data, min);
-        len += (min + 1);
+      len += strlen(_input1.data);
 #endif
 
-        node.key = val;
-        rv = net_mod_socket_sendandrecv(gNetmod_socket, &node, 1, topics_buf, len, &recv_arr, &recv_arr_size, &errarr, &errarr_size);
-        if (rv > 0) {
-          if (recv_arr_size > 0) {
-
-            node.key = recv_arr[0].key;
-            
-            memset(topics_buf, 0x00, sizeof(topics_buf));
-            size = recv_arr[0].content_length;
-            buf = (char *)malloc(size);
-            strncpy((char *)buf, (char *)recv_arr[0].content, size);
-#if !defined(PRO_DE_SERIALIZE)
-            *reply = buf;
-            *reply_len = size;
+      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;
+      }
+      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 
+
+      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);
+
+      } else if (timeout_ms == 0) {
+
+        rv = net_mod_socket_sendandrecv_nowait(gNetmod_socket, &node, 1, topics_buf, len, &recv_arr, &recv_arr_size, &errarr, &errarr_size);
+      
+      } else {
+
+        rv = net_mod_socket_sendandrecv(gNetmod_socket, &node, 1, topics_buf, len, &recv_arr, &recv_arr_size, &errarr, &errarr_size);
+      }
+      if (rv > 0) {
+        if (recv_arr_size > 0) {
+
+          node.key = recv_arr[0].key;
+
+          size = recv_arr[0].content_length;
+          buf = (char *)malloc(size);
+          if (buf == NULL) {
+            printf("Out of memory\n");
+            
+            exit(0);
           }
-
-          net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
-
-          if(errarr_size > 0) {
-            free(errarr);
-          }
-
-          rv = 0;
-
-        } else {
-          rv = EBUS_TIMEOUT; 
+          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);
+
+        if(errarr_size > 0) {
+          free(errarr);
+        }
+
+        rv = 0;
+
       } else {
-        rv = EBUS_RES_UNSUPPORT; 
+        rv = EBUS_TIMEOUT; 
       }
+      
+    } else {
+      rv = EBUS_RES_UNSUPPORT; 
     }
 
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-
-    if (rv == 0) {
-      memset(topics_buf, 0x00, sizeof(topics_buf));
-      sprintf(topics_buf, "%d", node.key);
-
-      rr.proc_id = topics_buf;
-      *proc_id_len = rr.proc_id.size();
-      *proc_id = malloc(*proc_id_len);
-      memcpy(*proc_id, rr.proc_id.data(), *proc_id_len);
-
-      memset(topics_buf, 0x00, sizeof(topics_buf));
-      memcpy(topics_buf, buf, size);
-      rr.data = topics_buf;
-    }
-    
-    pthread_mutex_unlock(&mutex);
-
-  } else {
-  
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    free(topics_buf);
   }
 
-#if defined(PRO_DE_SERIALIZE)   
-  ::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);
+  bus_errorset(rv);
+  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) {
+      
+      rv = EBUS_NO_MEM;
+      bus_errorset(rv);
+      
+      logger->error("in BHRequest: Out of memory!\n");
+
+      return false;
+    }
+    memset(topics_buf, 0x00, size + 10);
+    
+    memcpy(topics_buf, buf, size);
+    rr.data = topics_buf;
+    
+    free(buf);
+    free(topics_buf);
+  }
+
+  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);
+  }
 #else 
-  if (rv > 0) {
+  if (rv == 0) {
     min = strlen(errString) + 1;
     buf = malloc(min);
     memcpy(buf, errString, strlen(errString));
@@ -1469,86 +1537,110 @@
   void *buf;
   int key;
   int size;
+  int len;
+  int data;
   int sec, nsec;
-  char topics_buf[MAX_STR_LEN] = { 0x00 };
+  char buf_temp[MAX_STR_LEN] = { 0x00 };
+  char *topics_buf = NULL;
   
   if (gRun_stat == 0) {
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
   }
-
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
-    if (timeout_ms > 0) {
-
-      sec = timeout_ms / 1000;
-      nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
-
-      rv = net_mod_socket_recvfrom_timeout(gNetmod_socket, &buf, &size, &key, sec, nsec);
-
-    } else if (timeout_ms == 0) {
-
-      rv = net_mod_socket_recvfrom_nowait(gNetmod_socket, &buf, &size, &key);
+  
+  if ((request == NULL) || (request_len == 0) || (src == NULL)) {
+    rv = EBUS_INVALID_PARA;
+    bus_errorset(rv);
     
-    } else {
+		return false;
+  }
 
-      rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key);
-    }
+  data = net_mod_socket_svr_get(gNetmod_socket);
+  if (timeout_ms > 0) {
 
-    if (rv == 0) {
-      struct _ReadRequestReply
-      {
-        std::string proc_id;
-        std::string topic;
-        std::string data;
-        void *src;
-      } rrr;
+    sec = timeout_ms / 1000;
+    nsec = (timeout_ms - sec * 1000) * 1000 * 1000;
 
-      sprintf(topics_buf, "%d", key);
-      rrr.proc_id = topics_buf;
+    rv = net_mod_socket_recvfrom_timeout(gNetmod_socket, &buf, &size, &key, sec, nsec, SVR_STR, data);
+
+  } else if (timeout_ms == 0) {
+
+    rv = net_mod_socket_recvfrom_nowait(gNetmod_socket, &buf, &size, &key, SVR_STR, data);
+  
+  } else {
+
+    rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key, SVR_STR, data);
+  }
+
+  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;
 
       *proc_id_len = rrr.proc_id.size();
       *proc_id = malloc(*proc_id_len);
       memcpy(*proc_id, rrr.proc_id.data(), *proc_id_len);
-
-      memset(topics_buf, 0x00, sizeof(topics_buf));
-      memcpy(topics_buf, buf, size > sizeof(topics_buf) ? sizeof(topics_buf) : size);
-      rrr.topic = topics_buf;
-      rrr.data = 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 
-
-      buf = malloc(sizeof(int));
-      *(int *)buf = key;
-      *src = buf;
     }
 
-    pthread_mutex_unlock(&mutex);
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    topics_buf = (char *)malloc(size + MIN_STR_LEN);
+    if (topics_buf == NULL) {
+      
+      rv = EBUS_NO_MEM;
+      bus_errorset(rv);
+      
+      logger->error("in BHReadRequest: Out of memory!\n");
+      
+      return false;
+    }
+    memset(topics_buf, 0x00, size + MIN_STR_LEN);
     
-  } else {
- 
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    len = strlen((char *)buf);
+    if (len > size) {
+      len = size;
+    }
+    strncpy(topics_buf, (char *)buf, len);
+    rrr.topic = topics_buf;
+    
+    if (len < size) {
+      strncpy(topics_buf + len + 1, (char *)buf + len + 1, size - len - 1);
+    }
+
+    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);
 	
   if (rv == 0)
     return true;
@@ -1559,57 +1651,49 @@
 int BHSendReply(void *src, const void *reply, const int reply_len)
 {
   int rv;
-
+  int data;
+  int sec = 3;
+  int nsec = 0;
+  const char *_input;
+  
 #if defined(PRO_DE_SERIALIZE)
   ::bhome_msg::MsgRequestTopicReply input;
-  if (!input.ParseFromArray(reply, reply_len)) {
+if (!input.ParseFromArray(reply, reply_len) || (src == NULL)) {
     
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
     return false;
   }
   
-  const char *_input;
   _input = input.data().data();
-  
+
 #else 
   if ((src == NULL) || (reply == NULL) || (reply_len == 0)) {
 
     rv = EBUS_INVALID_PARA;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
     
     return false;
   }
+  
+  _input = (char *)reply;
+  
 #endif 
 
   if (gRun_stat == 0) {
     logger->error("the process has not been registered yet!\n");
 
     rv = EBUS_RES_NO;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
+    bus_errorset(rv);
 
     return false;
   }
 
-  rv = pthread_mutex_trylock(&mutex);
-  if (rv == 0) {
+  data = net_mod_socket_svr_get(gNetmod_socket);
+  rv = net_mod_socket_sendto_timeout(gNetmod_socket, _input, strlen(_input), *(int *)src, sec, nsec, SVR_STR, data);
 
-    rv = net_mod_socket_sendto(gNetmod_socket, reply, reply_len, *(int *)src);
-
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-	
-    pthread_mutex_unlock(&mutex);
-  } else {
-
-    rv = EBUS_RES_BUSY;
-    memset(errString, 0x00, sizeof(errString));
-    strncpy(errString, bus_strerror(rv), sizeof(errString));
-  }
+  bus_errorset(rv);
   
   if (rv == 0)
     return true;
@@ -1629,6 +1713,7 @@
 int BHGetLastError(void **msg, int *msg_len)
 {
   void *buf = NULL;
+  char *errString = bus_strerror(0, 1);
 
   buf = malloc(strlen(errString) + 1);
 

--
Gitblit v1.8.0