From a300adf15342aa4d3b030698d40d61edb6272ea0 Mon Sep 17 00:00:00 2001
From: fujuntang <fujuntang@smartai.com>
Date: 星期二, 07 九月 2021 11:51:20 +0800
Subject: [PATCH] resize the buf allocation as dynamic allocate.
---
src/bh_api.cpp | 708 +++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 451 insertions(+), 257 deletions(-)
diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index b4ba52e..d773369 100644
--- a/src/bh_api.cpp
+++ b/src/bh_api.cpp
@@ -10,8 +10,8 @@
#include "bhome_msg_api.pb.h"
#include "bhome_msg.pb.h"
#include "error_msg.pb.h"
-#include "proto/bhome_msg.pb.h"
-#include "proto/bhome_msg_api.pb.h"
+#include "../proto/source/bhome_msg.pb.h"
+#include "../proto/source/bhome_msg_api.pb.h"
static Logger *logger = LoggerFactory::getLogger();
@@ -41,13 +41,13 @@
}_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;
+ return false;
}
_input.proc_id = input.proc_id().c_str();
@@ -56,13 +56,13 @@
_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;
+ return false;
}
#endif
@@ -77,7 +77,7 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- goto exit_entry;
+ return false;
}
rv = pthread_mutex_trylock(&mutex);
@@ -154,31 +154,29 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+ return false;
}
-
-exit_entry:
+
#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;
- return false;
+ return true;
+
}
int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
@@ -198,20 +196,29 @@
::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;
+ 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;
+
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
+ }
#endif
if (gRun_stat == 0) {
@@ -222,7 +229,7 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- goto exit_entry;
+ return false;
}
rv = pthread_mutex_trylock(&mutex);
@@ -248,9 +255,10 @@
rv = EBUS_RES_BUSY;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
-exit_entry:
#if defined(PRO_DE_SERIALIZE)
::bhome_msg::MsgCommonReply mcr;
mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -268,20 +276,18 @@
*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)
{
int rv;
- int min, i;
+ int len, i;
void *buf = NULL;
int total = 0;
int count = 0;
- char topics_buf[MAX_STR_LEN * MAX_TOPICS_NUN] = { 0x00 };
+ char *topics_buf = NULL;
#if defined(PRO_DE_SERIALIZE)
struct _MsgTopicList
@@ -291,14 +297,14 @@
}_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));
- goto exit_entry;
+ return false;
}
_input.amount = input.topic_list_size();
@@ -308,17 +314,21 @@
for(int i = 0; i < _input.amount; i++) {
_input.topics[i] = input.topic_list(i).c_str();
+
+ 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));
- goto exit_entry;
+ return false;
}
+
+ total = topics_len;
#endif
if (gRun_stat == 0) {
@@ -328,50 +338,63 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- 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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRegisterTopics: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
+
+ return false;
+ }
+ memset(topics_buf, 0x00, total);
+
#if defined(PRO_DE_SERIALIZE)
- total = sizeof(topics_buf) / sizeof(char);
for (i = 0; i < _input.amount; i++) {
- min = (strlen(_input.topics[i]) > (total - 1) ? (total - 1) : strlen(_input.topics[i]));
- if (min > 0) {
- strncpy(topics_buf + count, _input.topics[i], min);
- count += min;
+
+ len = strlen(_input.topics[i]);
+ strncpy(topics_buf + count, _input.topics[i], len);
+
+ count += len;
- total -= min;
-
- if ((_input.amount > 1) && (i < (_input.amount - 1))) {
- strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
- total -= 1;
- count++;
- }
- } else {
- topics_buf[strlen(topics_buf) - 1] = '\0';
+ if ((_input.amount > 1) && (i < (_input.amount - 1))) {
+ strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
+
+ count++;
}
}
-
- //logger->debug("the parsed compound register topics: %s!\n", topics_buf);
+
#else
- memcpy(topics_buf, topics, topics_len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : topics_len);
+ memcpy(topics_buf, topics, topics_len);
+ count = topics_len;
#endif
- rv = net_mod_socket_reg(gNetmod_socket, topics_buf, strlen(topics_buf) + 1, NULL, 0, timeout_ms, PROC_REG_TCS);
+ rv = net_mod_socket_reg(gNetmod_socket, topics_buf, count, NULL, 0, timeout_ms, PROC_REG_TCS);
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
-
+
+ free(topics_buf);
pthread_mutex_unlock(&mutex);
} else {
rv = EBUS_RES_BUSY;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
-exit_entry:
#if defined(PRO_DE_SERIALIZE)
::bhome_msg::MsgCommonReply mcr;
mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -380,19 +403,17 @@
*reply = malloc(*reply_len);
mcr.SerializePartialToArray(*reply, *reply_len);
#else
- min = strlen(errString) + 1;
- buf = malloc(min) ;
+ len = strlen(errString) + 1;
+ buf = malloc(len) ;
memcpy(buf, errString, strlen(errString));
- *((char *)buf + min - 1) = '\0';
+ *((char *)buf + len - 1) = '\0';
*reply = buf;
- *reply_len = min;
+ *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)
@@ -404,7 +425,7 @@
char topics_buf[MAX_STR_LEN] = { 0x00 };
ProcInfo_query *ptr = NULL;
ProcInfo *Proc_ptr = NULL;
-
+
#if defined(PRO_DE_SERIALIZE)
struct _BHAddress
{
@@ -418,13 +439,13 @@
::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));
- goto exit_entry;
+ return false;
}
_input0.mq_id = input0.mq_id();
@@ -434,13 +455,13 @@
_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));
- goto exit_entry;
+ return false;
}
#endif
@@ -451,7 +472,7 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- goto exit_entry;
+ return false;
}
rv = pthread_mutex_trylock(&mutex);
@@ -465,7 +486,7 @@
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);
+ rv = net_mod_socket_reg(gNetmod_socket, topics_buf, min, &buf, &size, timeout_ms, PROC_QUE_TCS);
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
@@ -477,9 +498,10 @@
rv = EBUS_RES_BUSY;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
-exit_entry:
#if defined(PRO_DE_SERIALIZE)
struct _MsgQueryTopicReply
@@ -504,9 +526,9 @@
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;
}
}
@@ -544,10 +566,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)
@@ -572,13 +592,13 @@
::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));
- goto exit_entry;
+ return false;
}
_input0.mq_id = input0.mq_id();
@@ -586,6 +606,14 @@
_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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
+ }
#endif
if (gRun_stat == 0) {
@@ -595,7 +623,7 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- goto exit_entry;
+ return false;
}
rv = pthread_mutex_trylock(&mutex);
@@ -617,9 +645,10 @@
rv = EBUS_RES_BUSY;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
-exit_entry:
#if defined(PRO_DE_SERIALIZE)
struct _MsgQueryProcReply
{
@@ -685,7 +714,7 @@
*reply_len = mpr.ByteSizeLong();
*reply = malloc(*reply_len);
- mpr.SerializePartialToArray(*reply,*reply_len);
+ mpr.SerializePartialToArray(*reply, *reply_len);
}
#else
if (rv == 0) {
@@ -702,10 +731,7 @@
}
#endif
- if (rv == 0)
- return true;
-
- return false;
+ return true;
}
@@ -715,9 +741,9 @@
int sec, nsec;
int total = 0;
int count = 0;
- int min, i;
+ int len, i;
void *buf = NULL;
- char topics_buf[MAX_STR_LEN * MAX_TOPICS_NUN] = { 0x00 };
+ char *topics_buf = NULL;
#if defined(PRO_DE_SERIALIZE)
struct _MsgTopicList
@@ -727,13 +753,13 @@
}_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));
- goto exit_entry;
+ return false;
}
_input.amount = input.topic_list_size();
@@ -742,16 +768,19 @@
_input.amount = MAX_STR_LEN;
}
- for(int i = 0; i < _input.amount; i++)
+ for(int i = 0; i < _input.amount; i++) {
_input.topics[i] = input.topic_list(i).c_str();
+
+ 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));
- goto exit_entry;
+ return false;
}
#endif
@@ -762,54 +791,66 @@
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
- goto exit_entry;
+ return false;
}
rv = pthread_mutex_trylock(&mutex);
if (rv == 0) {
-#if defined(PRO_DE_SERIALIZE)
- total = sizeof(topics_buf) / sizeof(char);
- for (i = 0; i < _input.amount; i++) {
- min = (strlen(_input.topics[i]) > (total - 1) ? (total - 1) : strlen(_input.topics[i]));
- if (min > 0) {
- strncpy(topics_buf + count, _input.topics[i], min);
- count += min;
- total -= strlen(_input.topics[i]);
+ topics_buf = (char *)malloc(total);
+ if (topics_buf == NULL) {
+
+ rv = EBUS_NO_MEM;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHSubscribeTopics: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
- if ((_input.amount > 1) && (i < (_input.amount - 1))) {
- strncpy(topics_buf + count, STR_MAGIC, strlen(STR_MAGIC));
- total -= 1;
- count++;
- }
- } else {
- topics_buf[strlen(topics_buf) - 1] = '\0';
- }
+ return false;
}
- //logger->debug("the parsed compound sub topics: %s!\n", topics_buf);
+ 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 > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : 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);
+ 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);
+ rv = net_mod_socket_sub_nowait(gNetmod_socket, topics_buf, count);
} else {
- rv = net_mod_socket_sub(gNetmod_socket, topics_buf, strlen(topics_buf) + 1);
+ rv = net_mod_socket_sub(gNetmod_socket, topics_buf, count);
}
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+ free(topics_buf);
pthread_mutex_unlock(&mutex);
} else {
@@ -817,9 +858,10 @@
rv = EBUS_RES_BUSY;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
-exit_entry:
#if defined(PRO_DE_SERIALIZE)
::bhome_msg::MsgCommonReply mcr;
mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv));
@@ -828,19 +870,16 @@
*reply=malloc(*reply_len);
mcr.SerializePartialToArray(*reply,*reply_len);
#else
- min = strlen(errString) + 1;
- buf = malloc(min) ;
+ len = strlen(errString) + 1;
+ buf = malloc(len) ;
memcpy(buf, errString, strlen(errString));
- *((char *)buf + min - 1) = '\0';
+ *((char *)buf + len - 1) = '\0';
*reply = buf;
- *reply_len = min;
+ *reply_len = len;
#endif
- if (rv == 0)
- return true;
-
- return false;
+ return true;
}
@@ -902,7 +941,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
@@ -914,7 +953,7 @@
int node_arr_len = 0;
#if defined(PRO_DE_SERIALIZE)
- struct _MsgPublish
+ struct MsgPublish
{
const char *topic;
const char *data;
@@ -1003,8 +1042,8 @@
int key;
int size;
int sec, nsec;
- char topics_buf[MAX_STR_LEN] = { 0x00 };
- char data_buf[MAX_STR_LEN * 3] = { 0x00 };
+ char *topics_buf = NULL;
+ char *data_buf = NULL;
struct _ReadSubReply
{
@@ -1021,6 +1060,14 @@
strncpy(errString, bus_strerror(rv), sizeof(errString));
return false;
+ }
+
+ if ((msgpub == NULL) || (msgpub_len == NULL)) {
+ rv = EBUS_INVALID_PARA;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ return false;
}
if (timeout_ms > 0) {
@@ -1044,11 +1091,43 @@
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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRequest: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
+
+ 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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRequest: Out of memory!\n");
+
+ free(topics_buf);
+ pthread_mutex_unlock(&mutex);
+ return false;
+ }
+ memset(data_buf, 0x00, size - len + 10);
+
+ len = strlen(topics_buf) + 1;
strncpy(data_buf, (char *)buf + len, size - len);
}
@@ -1061,10 +1140,15 @@
memset(topics_buf, 0x00, sizeof(topics_buf));
sprintf(topics_buf, "%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 = 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);
+ }
+
+ free(topics_buf);
+ free(data_buf);
::bhome_msg::MsgPublish Mp;
Mp.set_topic(rsr.topic);
@@ -1085,16 +1169,16 @@
*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);
+ if ((proc_id != NULL) && (proc_id_len != NULL)) {
+ 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);
+ }
#endif
-
- pthread_mutex_unlock(&mutex);
} else {
@@ -1119,7 +1203,8 @@
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
@@ -1178,12 +1263,12 @@
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);
+ 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);
@@ -1192,12 +1277,31 @@
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
+
+ topics_buf = (char *)malloc(len);
+ if (topics_buf == NULL) {
+
+ rv = EBUS_NO_MEM;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRequest: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
+
+ 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
+
if (timeout_ms > 0) {
sec = timeout_ms / 1000;
@@ -1212,7 +1316,10 @@
} else {
rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val);
- }
+ }
+
+ free(topics_buf);
+
} else {
rv = EBUS_RES_UNSUPPORT;
@@ -1239,9 +1346,10 @@
}
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);
@@ -1268,8 +1376,8 @@
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 buf_temp[MAX_STR_LEN] = { 0x00 };
+ char *topics_buf = NULL;
struct _RequestReply
{
@@ -1294,7 +1402,7 @@
::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));
@@ -1311,7 +1419,7 @@
_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));
@@ -1334,42 +1442,76 @@
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, topics_buf, strlen(topics_buf), &buf, &size, timeout_ms, PROC_QUE_STCS);
+ 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);
free(buf);
if (val > 0) {
memset(&node, 0x00, sizeof(node));
-
- 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
+ topics_buf = (char *)malloc(len);
+ if (topics_buf == NULL) {
+
+ rv = EBUS_NO_MEM;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRequest: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
+
+ 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;
- rv = net_mod_socket_sendandrecv(gNetmod_socket, &node, 1, topics_buf, len, &recv_arr, &recv_arr_size, &errarr, &errarr_size);
+
+ 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;
-
- memset(topics_buf, 0x00, sizeof(topics_buf));
+
size = recv_arr[0].content_length;
buf = (char *)malloc(size);
+ if (buf == NULL) {
+ printf("Out of memory\n");
+
+ exit(0);
+ }
+ memset((char *)buf, 0x00, size);
+
strncpy((char *)buf, (char *)recv_arr[0].content, size);
-#if !defined(PRO_DE_SERIALIZE)
- *reply = buf;
- *reply_len = size;
-#endif
+
}
net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
@@ -1383,27 +1525,48 @@
} else {
rv = EBUS_TIMEOUT;
}
-
+
} else {
rv = EBUS_RES_UNSUPPORT;
}
+
+ free(topics_buf);
}
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);
+ if ((proc_id != NULL) && (proc_id_len != NULL)) {
+ memset(buf_temp, 0x00, sizeof(buf_temp));
+ sprintf(buf_temp, "%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);
+ 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);
+ }
- memset(topics_buf, 0x00, sizeof(topics_buf));
+ topics_buf = (char *)malloc(size);
+ if (topics_buf == NULL) {
+
+ rv = EBUS_NO_MEM;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ logger->error("in BHRequest: Out of memory!\n");
+
+ pthread_mutex_unlock(&mutex);
+
+ return false;
+ }
+ memset(topics_buf, 0x00, size);
+
memcpy(topics_buf, buf, size);
rr.data = topics_buf;
+
+ free(buf);
+ free(topics_buf);
}
pthread_mutex_unlock(&mutex);
@@ -1415,16 +1578,18 @@
strncpy(errString, bus_strerror(rv), sizeof(errString));
}
-#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);
+#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));
@@ -1447,8 +1612,10 @@
void *buf;
int key;
int size;
+ int len;
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");
@@ -1459,74 +1626,98 @@
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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
- } else {
+ return false;
+ }
- rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key);
- }
+ 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);
+
+ } else if (timeout_ms == 0) {
+
+ rv = net_mod_socket_recvfrom_nowait(gNetmod_socket, &buf, &size, &key);
+
+ } else {
+
+ rv = net_mod_socket_recvfrom(gNetmod_socket, &buf, &size, &key);
+ }
+
+ 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;
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
+
+ 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;
}
+
+ memset(errString, 0x00, sizeof(errString));
+ strncpy(errString, bus_strerror(rv), sizeof(errString));
if (rv == 0)
return true;
@@ -1537,10 +1728,11 @@
int BHSendReply(void *src, const void *reply, const int reply_len)
{
int rv;
-
+ 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));
@@ -1549,9 +1741,8 @@
return false;
}
- const char *_input;
_input = input.data().data();
-
+
#else
if ((src == NULL) || (reply == NULL) || (reply_len == 0)) {
@@ -1561,6 +1752,9 @@
return false;
}
+
+ _input = (char *)reply;
+
#endif
if (gRun_stat == 0) {
@@ -1576,7 +1770,7 @@
rv = pthread_mutex_trylock(&mutex);
if (rv == 0) {
- rv = net_mod_socket_sendto(gNetmod_socket, reply, reply_len, *(int *)src);
+ rv = net_mod_socket_sendto(gNetmod_socket, _input, strlen(_input), *(int *)src);
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
--
Gitblit v1.8.0