From 330f78f3334bcdcdb4cc2ab2dbf66604e0224d71 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 21 五月 2021 16:21:45 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/valib/bhshmq
---
src/msg.cpp | 72 ++++++++++++++---------------------
1 files changed, 29 insertions(+), 43 deletions(-)
diff --git a/src/msg.cpp b/src/msg.cpp
index 4ddb726..40a7b0d 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -16,52 +16,38 @@
* =====================================================================================
*/
#include "msg.h"
+#include "bh_util.h"
+#include "defs.h"
+#include "shm_socket.h"
-namespace bhome_shm {
-
-
-bool MsgMetaV1::Parse(const void *p)
+namespace bhome_msg
{
- assert(p);
- *this = *static_cast<const MsgMetaV1*>(p);
- return tag_ == kMsgMetaTag;
+
+int ShmMsg::Release()
+{
+ if (!valid()) {
+ return 0;
+ }
+ auto n = meta()->count_.Dec();
+ if (n == 0) {
+ if (meta()->managed_) {
+ int64_t free_cmd = (id() << 4) | EncodeCmd(eCmdFree);
+ DefaultSender(shm()).Send(BHTopicCenterAddress(shm()), free_cmd);
+ } else {
+ Free();
+ }
+ } else if (n < 0) {
+ LOG_FATAL() << "error double release data.";
+ throw std::runtime_error("double release msg.");
+ }
+ return n;
}
-void MsgMetaV1::Pack(void *p)
+void ShmMsg::Free()
{
- *static_cast<MsgMetaV1*>(p) = *this;
+ assert(valid());
+ shm().Dealloc(meta());
+ offset_ = 0;
+ assert(!valid());
}
-
-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
+} // namespace bhome_msg
--
Gitblit v1.8.0