From ad4f3dcedab29a690c5eedbb08ba1b393917db0b Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 21 四月 2021 17:39:34 +0800 Subject: [PATCH] update go api. --- src/msg.cpp | 119 ++++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/msg.cpp b/src/msg.cpp index bb193ec..06b817e 100644 --- a/src/msg.cpp +++ b/src/msg.cpp @@ -16,63 +16,90 @@ * ===================================================================================== */ #include "msg.h" +#include "bh_util.h" -namespace bhome_msg { - - -bool MsgMetaV1::Parse(const void *p) +namespace bhome_msg { - assert(p); - *this = *static_cast<const MsgMetaV1*>(p); - return tag_ == kMsgMetaTag; +/*TODO change msg format, header has proc info; +reply has errer msg + center accept request and route.; +//*/ +const uint32_t kMsgTag = 0xf1e2d3c4; + +void *MsgI::Pack(SharedMemory &shm, + const uint32_t head_len, const ToArray &headToArray, + const uint32_t body_len, const ToArray &bodyToArray) +{ + void *addr = shm.Alloc(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; } -void MsgMetaV1::Pack(void *p) +bool MsgI::ParseHead(BHMsgHead &head) const { - *static_cast<MsgMetaV1*>(p) = *this; + auto p = static_cast<char *>(ptr_.get()); + assert(p); + uint32_t msg_size = Get32(p); + p += 4; + return head.ParseFromArray(p, msg_size); } -bool Msg::Build(SharedMemory &shm, const MQId &src_id, const void *data, const size_t size, const bool refcount) +// with ref count; +bool MsgI::MakeRC(SharedMemory &shm, void *p) { - if (!data || !size) { - return false; - } - void *p = shm.Alloc(sizeof(MsgMetaV1) + size); - if (!p) { - return false; - } - RefCount *rc = 0; - if (refcount) { - rc = shm.New<RefCount>(); - if (!rc) { - shm.Dealloc(p); - return false; - } - } - MsgMetaV1 meta; - meta.data_size_ = size; - meta.src_id_ = src_id; - meta.Pack(p); - memcpy(static_cast<char *>(p) + sizeof(meta), data, size); - Msg(p, rc).swap(*this); - return true; - + if (!p) { + return false; + } + RefCount *rc = shm.New<RefCount>(); + if (!rc) { + shm.Dealloc(p); + return false; + } + MsgI(p, rc).swap(*this); + return true; } -int Msg::Release(SharedMemory &shm) +bool MsgI::Make(SharedMemory &shm, void *p) { - if (IsCounted()) { - const int n = count_->Dec(); - if (n != 0) { - return n; - } - } - // free data - shm.Dealloc(ptr_); - ptr_ = 0; - shm.Delete(count_); - count_ = 0; - return 0; + if (!p) { + return false; + } + MsgI(p, 0).swap(*this); + return true; +} + +bool MsgI::EnableRefCount(SharedMemory &shm) +{ + if (!IsCounted()) { + count_ = shm.New<RefCount>(); + } + return IsCounted(); +} + +int MsgI::Release(SharedMemory &shm) +{ + if (IsCounted()) { + const int n = count_->Dec(); + if (n != 0) { + return n; + } + } + // free data + shm.Dealloc(ptr_); + ptr_ = 0; + shm.Delete(count_); + count_ = 0; + return 0; } } // namespace bhome_msg -- Gitblit v1.8.0