From 4e122537c7c45290272c35721e38c95d9c8ae218 Mon Sep 17 00:00:00 2001
From: Fu Juntang <StrongTiger_001@163.com>
Date: 星期五, 03 九月 2021 12:17:36 +0800
Subject: [PATCH] Verify the seriliazed parameters parsing logic.
---
/dev/null | 595 ------------------------------------------
src/socket/bus_server_socket.cpp | 1
src/bus_error.h | 1
src/bh_api.cpp | 181 +++++++++---
src/proc_def.h | 11
CMakeLists.txt | 2
src/bus_error.cpp | 3
7 files changed, 147 insertions(+), 647 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 049e0b2..fd63739 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,7 +21,7 @@
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/include/usgcommon")
-list(APPEND EXTRA_LIBS ${PROJECT_SOURCE_DIR}/lib/libusgcommon.a pthread rt protobuf-lite.a)
+list(APPEND EXTRA_LIBS ${PROJECT_SOURCE_DIR}/lib/libusgcommon.a pthread rt protobuf protobuf-lite.a)
# build api doc
if (BUILD_DOC)
diff --git a/src/bh_api.cc b/src/bh_api.cc
deleted file mode 100644
index a13701e..0000000
--- a/src/bh_api.cc
+++ /dev/null
@@ -1,595 +0,0 @@
-#include "bh_api.h"
-#include <stdlib.h>
-#include <string.h>
-#include "../proto/bhome_msg.pb.h"
-#include "../proto/bhome_msg_api.pb.h"
-
-using namespace bhome_msg;
-
-int BHRegister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- ProcInfo input;
- if(!input.ParseFromArray(proc_info,proc_info_len))
- return false;
- struct _ProcInfo
- {
- const char *proc_id;
- const char *name;
- const char *public_info;
- const char *private_info;
- }_input;
- _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();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_Register(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true:false;
-}
-int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- ProcInfo input;
- if(!input.ParseFromArray(proc_info,proc_info_len))
- return false;
- struct _ProcInfo
- {
- const char *proc_id;
- const char *name;
- const char *public_info;
- const char *private_info;
- }_input;
- _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();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_UnRegister(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true:false;
-}
-
-int BHHeartbeatEasy(const int timeout_ms)
-{
- bool Rel=true;
- //Rel=call_HeartbeatEasy(timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- return Rel;
-}
-
-int BHHeartbeat(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- ProcInfo input;
- if(!input.ParseFromArray(proc_info,proc_info_len))
- return false;
- struct _ProcInfo
- {
- const char *proc_id;
- const char *name;
- const char *public_info;
- const char *private_info;
- }_input;
- _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();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_Heartbeat(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true:false;
-}
-
-int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- MsgTopicList input;
- if(!input.ParseFromArray(topics,topics_len))
- return false;
- struct _MsgTopicList
- {
- int amount;
- const char *topics[128];
- }_input;
- _input.amount=input.topic_list_size();
- for(int i=0;i<_input.amount;i++)
- _input.topics[i]=input.topic_list(i).c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_RegisterTopics(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true: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)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- BHAddress input0;
- MsgQueryTopic input1;
- if (!input0.ParseFromArray(remote,remote_len)||!input1.ParseFromArray(topic,topic_len))
- return false;
- struct _BHAddress
- {
- unsigned long long mq_id;
- long long abs_addr;
- const char *ip;
- int port;
- }_input0;
- const char *_input1;
- _input0.mq_id=input0.mq_id();
- _input0.abs_addr=input0.abs_addr();
- _input0.ip=input0.ip().c_str();
- _input0.port=input0.port();
- _input1=input1.topic().c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //姝ょ浠ュ浐瀹氱粨鏋勮繑鍥炴暟鎹殑鏂瑰紡,鍙兘鏆傛椂鍥哄畾鏁伴噺涓婇檺銆傝繖閲岄檺鍒舵渶澶�128涓�
- //鑻ヤ笉闄愭暟閲�,闇�閲嶆柊璁捐鏁版嵁杩斿洖鐨勬柟寮�
- 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;//瀹為檯鍦╩tr_list涓~鍐欎簡澶氬皯涓」鐩�
- //errCode=call_QueryTopicAddress(&_input0,_input1,mtr_list,&mtr_list_num,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgQueryTopicReply mtr;
- mtr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mtr.mutable_errmsg()->set_errstring(errString);
- for(int i=0;i<mtr_list_num;i++)
- {
- MsgQueryTopicReply_BHNodeAddress *mtrb=mtr.add_node_address();
- mtrb->set_proc_id(mtr_list[i].proc_id);
- mtrb->mutable_addr()->set_mq_id(mtr_list[i].mq_id);
- mtrb->mutable_addr()->set_abs_addr(mtr_list[i].abs_addr);
- mtrb->mutable_addr()->set_ip(mtr_list[i].ip);
- mtrb->mutable_addr()->set_port(mtr_list[i].port);
- }
- *reply_len=mtr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mtr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true: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)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- BHAddress input0;
- MsgQueryProc input1;
- if (!input0.ParseFromArray(remote,remote_len)||!input1.ParseFromArray(query,query_len))
- return false;
- struct _BHAddress
- {
- unsigned long long mq_id;
- long long abs_addr;
- const char *ip;
- int port;
- }_input0;
- const char *_input1;
- _input0.mq_id=input0.mq_id();
- _input0.abs_addr=input0.abs_addr();
- _input0.ip=input0.ip().c_str();
- _input0.port=input0.port();
- _input1=input1.proc_id().c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //姝ょ浠ュ浐瀹氱粨鏋勮繑鍥炴暟鎹殑鏂瑰紡,鍙兘鏆傛椂鍥哄畾鏁伴噺涓婇檺銆傝繖閲岄檺鍒舵渶澶�128涓猚lient,姣忎釜client鏈�澶�128涓猼opic
- //鑻ヤ笉闄愭暟閲�,闇�閲嶆柊璁捐鏁版嵁杩斿洖鐨勬柟寮�
- struct _MsgQueryProcReply
- {
- std::string proc_id;
- std::string name;
- std::string public_info;
- std::string private_info;
-
- bool online;
-
- std::string topic_list[128];
- int topic_list_num;//瀹為檯鍦╰opic_list涓~鍐欎簡澶氬皯涓」鐩�
- }mpr_list[128];
- int mpr_list_num=0;//瀹為檯鍦╩pr_list涓~鍐欎簡澶氬皯涓」鐩�
- //errCode=call_QueryProcs(&_input0,_input1,mpr_list,&mpr_list_num,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgQueryProcReply mpr;
- mpr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mpr.mutable_errmsg()->set_errstring(errString);
- for(int i=0;i<mpr_list_num;i++)
- {
- 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);
-
- return errCode==0?true:false;
-}
-
-int BHSubscribeTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- MsgTopicList input;
- if(!input.ParseFromArray(topics,topics_len))
- return false;
- struct _MsgTopicList
- {
- int amount;
- const char *topics[128];
- }_input;
- _input.amount=input.topic_list_size();
- for(int i=0;i<_input.amount;i++)
- _input.topics[i]=input.topic_list(i).c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_SubscribeTopics(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true:false;
-}
-
-int BHSubscribeNetTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
-{
- //same as BHSubscribeTopics
-
- //鍙嶅簭鍒楀寲鍏ュ弬
- MsgTopicList input;
- if(!input.ParseFromArray(topics,topics_len))
- return false;
- struct _MsgTopicList
- {
- int amount;
- const char *topics[128];
- }_input;
- _input.amount=input.topic_list_size();
- for(int i=0;i<_input.amount;i++)
- _input.topics[i]=input.topic_list(i).c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_SubscribeTopics(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- MsgCommonReply mcr;
- mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- mcr.mutable_errmsg()->set_errstring(errString);
- *reply_len=mcr.ByteSizeLong();
- *reply=malloc(*reply_len);
- mcr.SerializePartialToArray(*reply,*reply_len);
-
- return errCode==0?true:false;
-}
-
-int BHPublish(const void *msgpub,
- const int msgpub_len,
- const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- MsgPublish input;
- if(!input.ParseFromArray(msgpub,msgpub_len))
- return false;
- struct _MsgPublish
- {
- const char *topic;
- const char *data;
- }_input;
- _input.topic=input.topic().c_str();
- _input.data=input.data().c_str();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_Publish(&_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
-
- return errCode==0?true:false;
-}
-
-int BHReadSub(void **proc_id,
- int *proc_id_len,
- void **msgpub,
- int *msgpub_len,
- const int timeout_ms)
-{
- //鏃犲叆鍙�,鐨嗕负杩斿洖
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- struct _ReadSubReply
- {
- std::string proc_id;
- std::string topic;
- std::string data;
- }rsr;
- //errCode=call_ReadSub(&rsr,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- if(errCode!=0)
- return false;
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- *proc_id_len=rsr.proc_id.size();
- *proc_id=malloc(*proc_id_len);
- memcpy(*proc_id,rsr.proc_id.data(),*proc_id_len);
-
- 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);
-
- return true;
-}
-
-int BHAsyncRequest(const void *remote,
- const int remote_len,
- const void *request,
- const int request_len,
- void **msg_id,
- int *msg_id_len)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- BHAddress input0;
- MsgRequestTopic input1;
- if (!input0.ParseFromArray(remote,remote_len)||!input1.ParseFromArray(request,request_len))
- return false;
- struct _BHAddress
- {
- unsigned long long mq_id;
- long long abs_addr;
- const char *ip;
- int port;
- }_input0;
- struct _MsgRequestTopic
- {
- const char *topic;
- const char *data;
- }_input1;
- _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();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- std::string MsgID;
- //errCode=call_AsyncRequest(&_input0,&_input1,&MsgID,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- if(errCode!=0)
- return false;
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- if(msg_id==nullptr||msg_id_len==nullptr)//杩欓噷鏈夊彲鑳借皟鐢ㄨ�呬笉闇�瑕佽繑鍥�
- return true;
- *msg_id_len=MsgID.size();
- *msg_id=malloc(*msg_id_len);
- memcpy(*msg_id,MsgID.data(),*msg_id_len);
-
- return true;
-}
-
-int BHRequest(const void *remote,
- const int remote_len,
- const void *request,
- const int request_len,
- void **proc_id,
- int *proc_id_len,
- void **reply,
- int *reply_len,
- const int timeout_ms)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- BHAddress input0;
- MsgRequestTopic input1;
- if (!input0.ParseFromArray(remote,remote_len)||!input1.ParseFromArray(request,request_len))
- return false;
- struct _BHAddress
- {
- unsigned long long mq_id;
- long long abs_addr;
- const char *ip;
- int port;
- }_input0;
- struct _MsgRequestTopic
- {
- const char *topic;
- const char *data;
- }_input1;
- _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();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- struct _RequestReply
- {
- std::string proc_id;
- std::string data;
- }rr;
- //errCode=call_Request(&_input0,&_input1,&rr,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- char errString[32]={0};
- if(errCode!=0)
- strcpy(errString,"閿欒鍘熷洜鎻忚堪...");
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- *proc_id_len=rr.proc_id.size();
- *proc_id=malloc(*proc_id_len);
- memcpy(*proc_id,rr.proc_id.data(),*proc_id_len);
-
- MsgRequestTopicReply mrt;
- mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(errCode));
- 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);
-
- return errCode==0?true:false;
-}
-
-int BHReadRequest(void **proc_id,
- int *proc_id_len,
- void **request,
- int *request_len,
- void **src,
- const int timeout_ms)
-{
- //鏃犲叆鍙�,鐨嗕负杩斿洖
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- struct _ReadRequestReply
- {
- std::string proc_id;
- std::string topic;
- std::string data;
- void *src;
- }rrr;
- //errCode=call_ReadRequest(&rrr,&timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- if(errCode!=0)
- return false;
-
- //搴忓垪鍖栬繑鍥炴暟鎹�
- *proc_id_len=rrr.proc_id.size();
- *proc_id=malloc(*proc_id_len);
- memcpy(*proc_id,rrr.proc_id.data(),*proc_id_len);
-
- 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);
-
- *src=rrr.src;
-
- return true;
-}
-
-int BHSendReply(void *src,
- const void *reply,
- const int reply_len)
-{
- //鍙嶅簭鍒楀寲鍏ュ弬
- MsgRequestTopicReply input;
- if(!input.ParseFromArray(reply,reply_len))
- return false;
- const char *_input;
- _input=input.data().data();
-
- //璋冪敤鍑芥暟瀹炵幇
- int errCode=0;//0 is success
- //errCode=call_SendReply(src,_input,timeout_ms);//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
-
- return errCode==0?true:false;
-}
-
-void BHFree(void *data, int size)
-{
- free(data);
-}
-
-int BHCleanup()
-{
- int errCode=0;//0 is success
- //errCode=call_Cleanup();//杩欏彞鏄姛鑳界殑鍏蜂綋瀹炵幇
- return errCode==0?true:false;
-}
\ No newline at end of file
diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index 4d0f10b..94637ad 100644
--- a/src/bh_api.cpp
+++ b/src/bh_api.cpp
@@ -7,6 +7,9 @@
#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"
@@ -338,7 +341,7 @@
topics_buf = (char *)malloc(total);
if (topics_buf == NULL) {
- rv = EBUS_RES_NO;
+ rv = EBUS_NO_MEM;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
@@ -417,7 +420,7 @@
char topics_buf[MAX_STR_LEN] = { 0x00 };
ProcInfo_query *ptr = NULL;
ProcInfo *Proc_ptr = NULL;
-
+
#if defined(PRO_DE_SERIALIZE)
struct _BHAddress
{
@@ -517,9 +520,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;
}
}
@@ -698,7 +701,7 @@
*reply_len = mpr.ByteSizeLong();
*reply = malloc(*reply_len);
- mpr.SerializePartialToArray(*reply,*reply_len);
+ mpr.SerializePartialToArray(*reply, *reply_len);
}
#else
if (rv == 0) {
@@ -787,7 +790,7 @@
topics_buf = (char *)malloc(total);
if (topics_buf == NULL) {
- rv = EBUS_RES_NO;
+ rv = EBUS_NO_MEM;
memset(errString, 0x00, sizeof(errString));
strncpy(errString, bus_strerror(rv), sizeof(errString));
@@ -930,7 +933,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
@@ -1145,7 +1148,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
@@ -1204,12 +1208,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);
@@ -1218,12 +1222,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;
@@ -1238,7 +1261,10 @@
} else {
rv = net_mod_socket_sendto(gNetmod_socket, topics_buf, len, val);
- }
+ }
+
+ free(topics_buf);
+
} else {
rv = EBUS_RES_UNSUPPORT;
@@ -1294,8 +1320,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
{
@@ -1360,13 +1386,14 @@
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);
@@ -1374,23 +1401,53 @@
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);
+ memset(buf, 0x00, size);
strncpy((char *)buf, (char *)recv_arr[0].content, size);
#if !defined(PRO_DE_SERIALIZE)
*reply = buf;
@@ -1409,6 +1466,8 @@
} else {
rv = EBUS_TIMEOUT;
}
+
+ free(topics_buf);
} else {
rv = EBUS_RES_UNSUPPORT;
@@ -1419,17 +1478,17 @@
strncpy(errString, bus_strerror(rv), sizeof(errString));
if (rv == 0) {
- memset(topics_buf, 0x00, sizeof(topics_buf));
- sprintf(topics_buf, "%d", node.key);
+ memset(buf_temp, 0x00, sizeof(buf_temp));
+ sprintf(buf_temp, "%d", node.key);
- rr.proc_id = topics_buf;
+ rr.proc_id = buf_temp;
*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;
+ memset(buf_temp, 0x00, sizeof(buf_temp));
+ memcpy(buf_temp, buf, size);
+ rr.data = buf_temp;
}
pthread_mutex_unlock(&mutex);
@@ -1473,8 +1532,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");
@@ -1513,17 +1574,42 @@
void *src;
} rrr;
- sprintf(topics_buf, "%d", key);
- rrr.proc_id = topics_buf;
+ 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);
+ 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");
+
+ pthread_mutex_unlock(&mutex);
+
+ return false;
+ }
+ memset(topics_buf, 0x00, size + MIN_STR_LEN);
+
+ len = strlen((char *)buf);
+ if (len > size) {
+ len = size;
+ }
+ strncpy(topics_buf, (char *)buf, len);
rrr.topic = topics_buf;
- rrr.data = 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;
@@ -1536,6 +1622,8 @@
*request = buf;
*request_len = size;
#endif
+
+ free(buf);
buf = malloc(sizeof(int));
*(int *)buf = key;
@@ -1563,7 +1651,8 @@
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)) {
@@ -1575,9 +1664,8 @@
return false;
}
- const char *_input;
_input = input.data().data();
-
+
#else
if ((src == NULL) || (reply == NULL) || (reply_len == 0)) {
@@ -1587,6 +1675,9 @@
return false;
}
+
+ _input = (char *)reply;
+
#endif
if (gRun_stat == 0) {
@@ -1602,7 +1693,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));
diff --git a/src/bus_error.cpp b/src/bus_error.cpp
index 49244cd..29d5683 100644
--- a/src/bus_error.cpp
+++ b/src/bus_error.cpp
@@ -24,7 +24,8 @@
"Service not supported",
"Resource busy",
"Resource not provide",
- "Invalid parameters"
+ "Invalid parameters",
+ "No enough memory"
};
diff --git a/src/bus_error.h b/src/bus_error.h
index 84f2e89..e625790 100644
--- a/src/bus_error.h
+++ b/src/bus_error.h
@@ -17,6 +17,7 @@
#define EBUS_RES_BUSY 510
#define EBUS_RES_NO 511
#define EBUS_INVALID_PARA 512
+#define EBUS_NO_MEM 513
extern int bus_errno;
diff --git a/src/proc_def.h b/src/proc_def.h
index cb4dc0a..1d84362 100644
--- a/src/proc_def.h
+++ b/src/proc_def.h
@@ -8,7 +8,7 @@
#define MAX_STR_LEN 128 //keep the same with serializer in proc check
#define MIN_STR_LEN 10
#define MAX_PROC_NUM 128
-#define MAX_TOPICS_NUN 60
+#define MAX_TOPICS_NUN 60
#define PROC_REG 1
#define PROC_UNREG 2
@@ -16,6 +16,9 @@
#define PROC_QUE_TCS 4
#define PROC_QUE_STCS 5
#define PROC_QUE_ATCS 6
+
+#define ID_RSV 16
+#define ABS_ID_RSV 18
#define STR_MAGIC ","
@@ -40,9 +43,9 @@
ProcInfo procData;
int stat;
- char reg_info[MAX_STR_LEN];
- char local_info[MAX_STR_LEN];
- char net_info[MAX_STR_LEN];
+ char reg_info[MAX_STR_LEN * MAX_TOPICS_NUN];
+ char local_info[MAX_STR_LEN * MAX_TOPICS_NUN];
+ char net_info[MAX_STR_LEN * MAX_TOPICS_NUN];
int list_num;
diff --git a/src/socket/bus_server_socket.cpp b/src/socket/bus_server_socket.cpp
index 80f7338..8b022e1 100644
--- a/src/socket/bus_server_socket.cpp
+++ b/src/socket/bus_server_socket.cpp
@@ -676,7 +676,6 @@
int flag;
char * action, *topic, *topics, *buf, *content;
size_t head_len;
- char resp_buf[128];
bus_head_t head;
int rv;
--
Gitblit v1.8.0