From c6964d5af25d4ec7ed9dbe7674dc4e3896b36ead Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期五, 16 四月 2021 16:10:02 +0800 Subject: [PATCH] node remove mq if never registered; refactor. --- src/socket.h | 90 ++++++++++++++++++++++++++++++++------------ 1 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/socket.h b/src/socket.h index 7c4f83f..dbb161c 100644 --- a/src/socket.h +++ b/src/socket.h @@ -19,7 +19,9 @@ #ifndef SOCKET_GWTJHBPO #define SOCKET_GWTJHBPO +#include "bh_util.h" #include "defs.h" +#include "sendq.h" #include "shm_queue.h" #include <atomic> #include <boost/noncopyable.hpp> @@ -34,6 +36,14 @@ class ShmSocket : private boost::noncopyable { + bool SendImpl(const void *valid_remote, MsgI const &imsg, SendQ::OnMsgEvent onExpire = SendQ::OnMsgEvent()) + { + // if (!mq().TrySend(*(MQId *) valid_remote, imsg)) { + send_buffer_.Append(*static_cast<const MQId *>(valid_remote), imsg, onExpire); + // } + return true; + } + protected: typedef bhome_shm::ShmMsgQueue Queue; @@ -46,8 +56,9 @@ ShmSocket(Shm &shm, const MQId &id, const int len); ShmSocket(Shm &shm, const int len = 12); ~ShmSocket(); + static bool Remove(SharedMemory &shm, const MQId &id) { return Queue::Remove(shm, id); } + bool Remove() { return Remove(shm(), id()); } const MQId &id() const { return mq().Id(); } - Shm &shm() { return shm_; } // start recv. bool Start(int nworker = 1, const RecvCB &onData = RecvCB(), const IdleCB &onIdle = IdleCB()); bool Start(const RecvCB &onData, const IdleCB &onIdle, int nworker = 1) { return Start(nworker, onData, onIdle); } @@ -55,31 +66,35 @@ bool Stop(); size_t Pending() const { return mq().Pending(); } - bool Send(const void *id, const MsgI &imsg, const int timeout_ms) - { - return mq().Send(*static_cast<const MQId *>(id), imsg, timeout_ms); - } - //TODO reimplment, using async. - bool SyncRecv(MsgI &msg, bhome::msg::BHMsgHead &head, const int timeout_ms); - template <class Body> - bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const int timeout_ms, const RecvCB &cb = RecvCB()) + bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const RecvCB &cb = RecvCB()) { - assert(valid_remote); - try { - if (cb) { - auto RegisterCB = [&]() { - std::lock_guard<std::mutex> lock(mutex()); - async_cbs_.emplace(head.msg_id(), cb); - }; - return mq().Send(*static_cast<const MQId *>(valid_remote), head, body, timeout_ms, RegisterCB); + MsgI msg; + if (msg.Make(shm(), head, body)) { + DEFER1(if (msg.IsCounted()) { msg.Release(shm()); }); + std::string msg_id(head.msg_id()); + if (!cb) { + return SendImpl(valid_remote, msg); } else { - return mq().Send(*static_cast<const MQId *>(valid_remote), head, body, timeout_ms); + per_msg_cbs_->Add(msg_id, cb); + auto onExpireRemoveCB = [this, msg_id](MsgI const &msg) { + RecvCB cb_no_use; + per_msg_cbs_->Find(msg_id, cb_no_use); + }; + return SendImpl(valid_remote, msg, onExpireRemoveCB); } - } catch (...) { - return false; + } else { + SetLastError(ENOMEM, "Out of mem"); } + return false; } + + bool Send(const void *valid_remote, const MsgI &imsg) + { + return SendImpl(valid_remote, imsg); + } + + bool SyncRecv(MsgI &msg, bhome::msg::BHMsgHead &head, const int timeout_ms); template <class Body> bool SendAndRecv(const void *remote, const BHMsgHead &head, const Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms) @@ -100,12 +115,15 @@ reply.swap(msg); reply_head.Swap(&head); st->cv.notify_one(); - } else { + } else { // ignore } }; std::unique_lock<std::mutex> lk(st->mutex); - bool sendok = Send(remote, head, body, timeout_ms, OnRecv); + bool sendok = Send(remote, head, body, OnRecv); + if (!sendok) { + printf("send timeout\n"); + } if (sendok && st->cv.wait_until(lk, endtime) == std::cv_status::no_timeout) { return true; } else { @@ -117,8 +135,9 @@ } } + Shm &shm() const { return mq().shm(); } + protected: - const Shm &shm() const { return shm_; } Queue &mq() { return mq_; } // programmer should make sure that mq_ is valid. const Queue &mq() const { return mq_; } std::mutex &mutex() { return mutex_; } @@ -127,13 +146,34 @@ bool StopNoLock(); bool RunningNoLock() { return !workers_.empty(); } - Shm &shm_; std::vector<std::thread> workers_; std::mutex mutex_; std::atomic<bool> run_; Queue mq_; - std::unordered_map<std::string, RecvCB> async_cbs_; + class AsyncCBs + { + std::unordered_map<std::string, RecvCB> store_; + + public: + bool empty() const { return store_.empty(); } + bool Add(const std::string &id, const RecvCB &cb) { return store_.emplace(id, cb).second; } + bool Find(const std::string &id, RecvCB &cb) + { + auto pos = store_.find(id); + if (pos != store_.end()) { + cb.swap(pos->second); + store_.erase(pos); + return true; + } else { + return false; + } + } + }; + + Synced<AsyncCBs> per_msg_cbs_; + SendQ send_buffer_; + // Synced<SendQ> send_buffer_; }; #endif // end of include guard: SOCKET_GWTJHBPO -- Gitblit v1.8.0