From a76a94009d78a6a41654335dcb9202fb31659de0 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 25 三月 2021 13:23:16 +0800
Subject: [PATCH] bulid msg, refactor.

---
 src/msg.cpp |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/src/msg.cpp b/src/msg.cpp
index e8c6d26..4ddb726 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -32,4 +32,36 @@
     *static_cast<MsgMetaV1*>(p) = *this;
 }
 
+bool Msg::Build(SharedMemory &shm, const MQId &src_id, const void *data, const size_t size, const bool refcount)
+{
+    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;
+
+}
+
+void Msg::FreeFrom(SharedMemory &shm)
+{
+    shm.Dealloc(ptr_);
+    shm.Delete(count_);
+}
 } // namespace bhome_shm

--
Gitblit v1.8.0