From 5fb901dd157b4a8bbfc3be916c6c1a9d02b5bfa6 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期日, 29 一月 2023 09:56:56 +0800 Subject: [PATCH] robust --- message.cpp | 91 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 74 insertions(+), 17 deletions(-) diff --git a/message.cpp b/message.cpp index 614018c..b37d0ae 100644 --- a/message.cpp +++ b/message.cpp @@ -13,7 +13,7 @@ #include "google/protobuf/dynamic_message.h" #include "google/protobuf/compiler/importer.h" -#include "3dparty/yyjson/yyjson.h" +#include "3rdparty/yyjson/yyjson.h" #include "bhome_msg_api.pb.h" using namespace bhome_msg; @@ -122,13 +122,38 @@ reg->sub_net_count = subnetcnt; return reg; } +creg* make_creg_from_cproc(const cproc* proc){ + auto reg = ptrT<struct creg>(); + reg->proc = rmConst(proc); + return reg; +} + +static inline void creg_add_topic(char*** dst, size_t* dstC, const char** src, const size_t srcC){ + *dst = rmConst(src); + *dstC = srcC; +} +void creg_add_topic_reply(creg* reg, const char** topic, const size_t count){ + creg_add_topic(®->channel, ®->channel_count, topic, count); +} + +void creg_add_topic_pub(creg* reg, const char** topic, const size_t count){ + creg_add_topic(®->pub, ®->pub_count, topic, count); +} + +void creg_add_topic_sub(creg* reg, const char** topic, const size_t count){ + creg_add_topic(®->sub, ®->sub_count, topic, count); +} + +void creg_add_topic_subnet(creg* reg, const char** topic, const size_t count){ + creg_add_topic(®->sub_net, ®->sub_net_count, topic, count); +} const cproc* creg_proc(const creg* reg){ if (!reg) return NULL; return reg->proc; } -char** creg_rep_topic(const creg* reg, size_t* count){ +char** creg_reply_topic(const creg* reg, size_t* count){ if (!reg) return NULL; *count = reg->channel_count; return reg->channel; @@ -244,8 +269,7 @@ char* entry = NULL; size_t entry_size = 0; json2str(sv, &entry, &entry_size); - arr[i] = ptrT<char>(entry_size+1); - memcpy(arr[i], entry, entry_size); + arr[i] = entry; } return make_tuple(arr, count); }; @@ -449,7 +473,11 @@ tie(msg->procid, msg->procidl) = copymemory(pid, pids); MsgRequestTopic msgRT; - if (!msgRT.ParseFromArray(data, size)) return NULL; + if (!msgRT.ParseFromArray(data, size)) { + free(msg->procid); + free(msg); + return NULL; + } tie(msg->msg, msg->msgl) = copymemory(msgRT.data()); return msg; @@ -463,8 +491,21 @@ msgRT.set_topic(topic, topicl); msgRT.set_data(data, datal); - auto pbstr = msgRT.SerializeAsString(); + const auto& pbstr = msgRT.SerializeAsString(); tie(msg->msg, msg->msgl) = copymemory(pbstr); + auto doc = yyjson_read(msg->msg, msg->msgl, 0); + yyjson_val* root = yyjson_doc_get_root(doc); + + auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; + + auto jp = obj(root, "path"); + auto jb = obj(root, "body"); + if (jp && jb){ + tie(msg->path, msg->pathl) = copymemory(yyjson_get_str(jp), yyjson_get_len(jp)); + tie(msg->body, msg->bodyl) = copymemory(yyjson_get_str(jb), yyjson_get_len(jb)); + } + + yyjson_doc_free(doc); return msg; } @@ -473,11 +514,15 @@ if (msg){ free(msg->procid); free(msg->msg); + free(msg->path); + free(msg->body); free(msg); } } static tuple<const char*, size_t> json2body(yyjson_doc* doc){ + ignoreT(json2body); + yyjson_val* root = yyjson_doc_get_root(doc); auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; @@ -486,15 +531,16 @@ if (!jp) return make_tuple((const char*)NULL, 0); auto jb = obj(root, "body"); if (!jb) return make_tuple((const char*)NULL, 0); - return make_tuple(yyjson_get_str(jb), yyjson_get_len(jp)); + return make_tuple(yyjson_get_str(jb), yyjson_get_len(jb)); } struct cstackmsgerr* get_reqmsg_stackerr(struct creqmsg* msg){ - auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); - auto tpbody = json2body(doc0); - if (!get<0>(tpbody)) return NULL; + // auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); + // auto tpbody = json2body(doc0); + // if (!get<0>(tpbody)) return NULL; + // auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); - auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); + auto doc = yyjson_read(msg->body, msg->bodyl, 0); auto root = yyjson_doc_get_root(doc); auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; @@ -506,7 +552,7 @@ tie(smsg->fileid, smsg->fileidl) = copymemory(yyjson_get_str(jfid), yyjson_get_len(jfid)); yyjson_doc_free(doc); - yyjson_doc_free(doc0); + // yyjson_doc_free(doc0); return smsg; } @@ -519,11 +565,12 @@ } // decode success msg struct cstackmsg* get_reqmsg_stack(struct creqmsg* msg){ - auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); - auto tpbody = json2body(doc0); - if (!get<0>(tpbody)) return NULL; + // auto doc0 = yyjson_read(msg->msg, msg->msgl, 0); + // auto tpbody = json2body(doc0); + // if (!get<0>(tpbody)) return NULL; + // auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); - auto doc = yyjson_read(get<0>(tpbody), get<1>(tpbody), 0); + auto doc = yyjson_read(msg->body, msg->bodyl, 0); auto root = yyjson_doc_get_root(doc); auto obj = [](yyjson_val* v, const char* name){return yyjson_obj_get(v, name);}; @@ -555,7 +602,7 @@ smsg->files[i] = file; } yyjson_doc_free(doc); - yyjson_doc_free(doc0); + // yyjson_doc_free(doc0); return smsg; } @@ -590,9 +637,12 @@ } } +template <class F> void ignoref(F&& f){} static struct cstr make_reply_msg_json(const int success, const char* msg, const size_t msgl, const char* data, const size_t datal) { + ignoref(make_reply_msg_json); + auto doc = yyjson_mut_doc_new(NULL); auto root = yyjson_mut_obj(doc); yyjson_mut_obj_add_bool(doc, root, "success", !!success); @@ -604,3 +654,10 @@ yyjson_mut_doc_free(doc); return cstr_ref(json, jsonl); } + +void free_query_procs(struct cqueryprocs* procs, const size_t count){ + for(size_t i = 0; i < count; i++){ + free(procs[i].id); + } + free(procs); +} -- Gitblit v1.8.0