From 65ef4d68321e56906920be75831b5e968f7abd7b Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 13 四月 2021 09:34:05 +0800 Subject: [PATCH] add heartbeat; refactor. --- src/shm_queue.cpp | 45 ++++++++++++++++++++++++++------------------- 1 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/shm_queue.cpp b/src/shm_queue.cpp index dcb5a9e..8e4e56e 100644 --- a/src/shm_queue.cpp +++ b/src/shm_queue.cpp @@ -60,26 +60,34 @@ } ShmMsgQueue::ShmMsgQueue(ShmType &segment, const int len) : - ShmMsgQueue(NewId(), segment, len) -{ -} + ShmMsgQueue(NewId(), segment, len) {} -ShmMsgQueue::~ShmMsgQueue() +ShmMsgQueue::~ShmMsgQueue() {} + +bool ShmMsgQueue::Remove(SharedMemory &shm, const MQId &id) { - // It's not safe to remove, others may still holder pointers and write to it. - // TODO use smart_ptr or garbage collection. - //Remove(); + return Super::Remove(shm, MsgQIdToName(id)); } bool ShmMsgQueue::Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms, OnSend const &onsend) { Queue *remote = Find(shm, MsgQIdToName(remote_id)); - return remote && remote->Write(msg, timeout_ms, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); + if (remote) { + return remote->Write(msg, timeout_ms, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); + } else { + // SetLestError(eNotFound); + return false; + } } bool ShmMsgQueue::Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms) { Queue *remote = Find(shm, MsgQIdToName(remote_id)); - return remote && remote->Write(msg, timeout_ms, [](const MsgI &msg) { msg.AddRef(); }); + if (remote) { + return remote->Write(msg, timeout_ms, [](const MsgI &msg) { msg.AddRef(); }); + } else { + // SetLestError(eNotFound); + return false; + } } // Test shows that in the 2 cases: @@ -87,15 +95,14 @@ // 2) find remote queue first, then build msg; // 1 is about 50% faster than 2, maybe cache related. -bool ShmMsgQueue::Recv(BHMsg &msg, const int timeout_ms) -{ - MsgI imsg; - if (Read(imsg, timeout_ms)) { - DEFER1(imsg.Release(shm());); - return imsg.Unpack(msg); - } else { - return false; - } -} +// bool ShmMsgQueue::Recv(MsgI &imsg, BHMsgHead &head, const int timeout_ms) +// { +// if (Read(imsg, timeout_ms)) { +// // DEFER1(imsg.Release(shm());); +// return imsg.ParseHead(head); +// } else { +// return false; +// } +// } } // namespace bhome_shm -- Gitblit v1.8.0