From aa1542b6d6a4680088ac715c4ce40f97ada554fb Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 14 四月 2021 17:52:31 +0800 Subject: [PATCH] add SendQ TrySend() TryRecv(); handle re-register. --- src/shm_queue.cpp | 25 ++++++++++++------------- 1 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/shm_queue.cpp b/src/shm_queue.cpp index 8e4e56e..2006496 100644 --- a/src/shm_queue.cpp +++ b/src/shm_queue.cpp @@ -73,17 +73,26 @@ { Queue *remote = Find(shm, MsgQIdToName(remote_id)); if (remote) { - return remote->Write(msg, timeout_ms, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); + if (onsend) { + return remote->Write(msg, timeout_ms, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); + } else { + return remote->Write(msg, timeout_ms, [](const MsgI &msg) { msg.AddRef(); }); + } } else { // SetLestError(eNotFound); return false; } } -bool ShmMsgQueue::Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms) + +bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, OnSend const &onsend) { Queue *remote = Find(shm, MsgQIdToName(remote_id)); if (remote) { - return remote->Write(msg, timeout_ms, [](const MsgI &msg) { msg.AddRef(); }); + if (onsend) { + return remote->TryWrite(msg, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); + } else { + return remote->TryWrite(msg, [](const MsgI &msg) { msg.AddRef(); }); + } } else { // SetLestError(eNotFound); return false; @@ -94,15 +103,5 @@ // 1) build msg first, then find remote queue; // 2) find remote queue first, then build msg; // 1 is about 50% faster than 2, maybe cache related. - -// 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