From dc86ace85e437ecb8a2e728e4dce36d02bbb8a6e Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期五, 23 四月 2021 12:59:50 +0800 Subject: [PATCH] move ref count into msg meta, only 1 poinetr now. --- src/msg.cpp | 164 ++++++++++++++++-------------------------------------- 1 files changed, 48 insertions(+), 116 deletions(-) diff --git a/src/msg.cpp b/src/msg.cpp index 41dd459..ba844da 100644 --- a/src/msg.cpp +++ b/src/msg.cpp @@ -20,144 +20,76 @@ namespace bhome_msg { - +/*TODO change msg format, header has proc info; +reply has errer msg + center accept request and route.; +//*/ const uint32_t kMsgTag = 0xf1e2d3c4; -const uint32_t kMsgPrefixLen = 4; -BHMsg InitMsg(MsgType type) +void *MsgI::Alloc(SharedMemory &shm, const size_t size) { - BHMsg msg; - msg.set_type(type); - time_t tm = 0; - msg.set_timestamp(time(&tm)); - return msg; -} - -BHMsg MakeRequest(const MQId &src_id, const void *data, const size_t size) -{ - assert(data && size); - BHMsg msg(InitMsg(kMsgTypeRequest)); - msg.set_body(data, size); - msg.add_route()->set_mq_id(&src_id, sizeof(src_id)); - return msg; -} -BHMsg MakeRequest(const MQId &src_id, const std::string &topic, const void *data, const size_t size) -{ - DataRequest req; - req.set_topic(topic); - req.set_data(data, size); - const std::string &body(req.SerializeAsString()); - return MakeRequest(src_id, body.data(), body.size()); -} - -BHMsg MakeReply(const void *data, const size_t size) -{ - assert(data && size); - BHMsg msg(InitMsg(kMsgTypeReply)); - DataReply reply; - reply.set_data(data, size); - msg.set_body(reply.SerializeAsString()); - return msg; -} - -BHMsg MakeSubUnsub(const MQId &client, const std::vector<std::string> &topics, const MsgType sub_unsub) -{ - assert(sub_unsub == kMsgTypeSubscribe || sub_unsub == kMsgTypeUnsubscribe); - BHMsg msg(InitMsg(sub_unsub)); - msg.add_route()->set_mq_id(&client, sizeof(client)); - DataSub subs; - for (auto &t : topics) { - subs.add_topics(t); - } - msg.set_body(subs.SerializeAsString()); - return msg; -} - -BHMsg MakeSub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeSubscribe); } -BHMsg MakeUnsub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeUnsubscribe); } - -BHMsg MakePub(const std::string &topic, const void *data, const size_t size) -{ - assert(data && size); - BHMsg msg(InitMsg(kMsgTypePublish)); - DataPub pub; - pub.set_topic(topic); - pub.set_data(data, size); - msg.set_body(pub.SerializeAsString()); - return msg; -} - -BHMsg MakeQueryTopic(const std::string &topic) -{ - BHMsg msg(InitMsg(kMsgTypeQueryTopic)); - DataQueryTopic query; - query.set_topic(topic); - msg.set_body(query.SerializeAsString()); - return msg; -} - -void *Pack(SharedMemory &shm, const BHMsg &msg) -{ - uint32_t msg_size = msg.ByteSizeLong(); - void *p = shm.Alloc(4 + msg_size); + void *p = shm.Alloc(sizeof(Meta) + size); if (p) { - Put32(p, msg_size); - if (!msg.SerializeToArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size)) { - shm.Dealloc(p); - p = 0; - } + auto pmeta = new (p) Meta; + p = pmeta + 1; } return p; } - -bool MsgI::Unpack(BHMsg &msg) const +void MsgI::Free(SharedMemory &shm) { - void *p = ptr_.get(); + assert(valid()); + shm.Dealloc(meta()); + ptr_ = nullptr; + assert(!valid()); +} + +void *MsgI::Pack(SharedMemory &shm, + const uint32_t head_len, const ToArray &headToArray, + const uint32_t body_len, const ToArray &bodyToArray) +{ + void *addr = Alloc(shm, sizeof(head_len) + head_len + sizeof(body_len) + body_len); + if (addr) { + auto p = static_cast<char *>(addr); + auto Pack1 = [&p](auto len, auto &writer) { + Put32(p, len); + p += sizeof(len); + writer(p, len); + p += len; + }; + Pack1(head_len, headToArray); + Pack1(body_len, bodyToArray); + } + return addr; +} + +bool MsgI::ParseHead(BHMsgHead &head) const +{ + auto p = get<char>(); assert(p); uint32_t msg_size = Get32(p); - return msg.ParseFromArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size); + p += 4; + return head.ParseFromArray(p, msg_size); } -// with ref count; -bool MsgI::MakeRC(SharedMemory &shm, const BHMsg &msg) +bool MsgI::Make(SharedMemory &shm, void *p) { - void *p = Pack(shm, msg); if (!p) { return false; } - RefCount *rc = shm.New<RefCount>(); - if (!rc) { - shm.Dealloc(p); - return false; - } - MsgI(p, rc).swap(*this); - return true; -} - -bool MsgI::Make(SharedMemory &shm, const BHMsg &msg) -{ - void *p = Pack(shm, msg); - if (!p) { - return false; - } - MsgI(p, 0).swap(*this); + MsgI(p).swap(*this); return true; } int MsgI::Release(SharedMemory &shm) { - if (IsCounted()) { - const int n = count_->Dec(); - if (n != 0) { - return n; - } + if (!valid()) { + return 0; } - // free data - shm.Dealloc(ptr_); - ptr_ = 0; - shm.Delete(count_); - count_ = 0; - return 0; + auto n = meta()->count_.Dec(); + if (n == 0) { + Free(shm); + } + return n; } } // namespace bhome_msg -- Gitblit v1.8.0