From 72bffb0807925a156b076b71f78c848a08d27b87 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期四, 29 四月 2021 10:55:35 +0800 Subject: [PATCH] refactor mutex. --- src/socket.h | 85 +++++++++++++++++++++++------------------- 1 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/socket.h b/src/socket.h index 5973ab6..a5dd72c 100644 --- a/src/socket.h +++ b/src/socket.h @@ -21,7 +21,8 @@ #include "bh_util.h" #include "defs.h" -#include "shm_queue.h" +#include "sendq.h" +#include "shm_msg_queue.h" #include <atomic> #include <boost/noncopyable.hpp> #include <condition_variable> @@ -32,66 +33,61 @@ #include <vector> using namespace bhome_msg; - class ShmSocket : private boost::noncopyable { - template <class DoSend> - inline bool SendImpl(MsgI &msg, const int timeout_ms, const DoSend &doSend) - { - bool r = false; - DEFER1(if (msg.IsCounted() || !r) { msg.Release(shm()); }); - r = doSend(msg); - return r; - } protected: - typedef bhome_shm::ShmMsgQueue Queue; + typedef ShmMsgQueue Queue; public: + typedef ShmMsgQueue::MQId MQId; typedef bhome_shm::SharedMemory Shm; typedef std::function<void(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> RecvCB; typedef std::function<bool(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> PartialRecvCB; typedef std::function<void(ShmSocket &sock)> IdleCB; - ShmSocket(Shm &shm, const MQId &id, const int len); + 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); } - const MQId &id() const { return mq().Id(); } - Shm &shm() { return shm_; } + static bool Remove(SharedMemory &shm, const MQId id) { return Queue::Remove(shm, id); } + bool Remove() { return Remove(shm(), id()); } + MQId id() const { return mq().Id(); } // 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); } bool Start(const RecvCB &onData, int nworker = 1) { return Start(nworker, onData); } bool Stop(); - size_t Pending() const { return mq().Pending(); } - - bool Send(const void *valid_remote, const MsgI &imsg, const int timeout_ms) - { - assert(valid_remote); - return mq().Send(*static_cast<const MQId *>(valid_remote), 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) + bool Send(const MQId remote, BHMsgHead &head, Body &body, RecvCB &&cb = RecvCB()) { - auto DoSend = [&](MsgI &msg) { return mq().Send(*static_cast<const MQId *>(valid_remote), msg, timeout_ms, [&]() { per_msg_cbs_->Add(head.msg_id(), cb); }); }; - MsgI msg; - return msg.Make(shm(), head, body) && SendImpl(msg, timeout_ms, DoSend); + try { + if (!cb) { + return SendImpl(remote, MsgI::Serialize(head, body)); + } else { + std::string msg_id(head.msg_id()); + per_msg_cbs_->Store(msg_id, std::move(cb)); + auto onExpireRemoveCB = [this, msg_id](SendQ::Data const &msg) { + RecvCB cb_no_use; + per_msg_cbs_->Pick(msg_id, cb_no_use); + }; + return SendImpl(remote, MsgI::Serialize(head, body), onExpireRemoveCB); + } + } catch (...) { + SetLastError(eError, "Send internal error."); + return false; + } } - template <class Body> - bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const int timeout_ms) + bool Send(const MQId remote, const MsgI &imsg) { - auto DoSend = [&](MsgI &msg) { return mq().Send(*static_cast<const MQId *>(valid_remote), msg, timeout_ms); }; - MsgI msg; - return msg.Make(shm(), head, body) && SendImpl(msg, timeout_ms, DoSend); + return SendImpl(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) + bool SendAndRecv(const MQId remote, BHMsgHead &head, Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms) { struct State { std::mutex mutex; @@ -114,7 +110,7 @@ }; std::unique_lock<std::mutex> lk(st->mutex); - bool sendok = Send(remote, head, body, timeout_ms, OnRecv); + bool sendok = Send(remote, head, body, std::move(OnRecv)); if (!sendok) { printf("send timeout\n"); } @@ -122,6 +118,7 @@ return true; } else { st->canceled = true; + SetLastError(ETIMEDOUT, "timeout"); return false; } } catch (...) { @@ -129,8 +126,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_; } @@ -139,7 +137,13 @@ bool StopNoLock(); bool RunningNoLock() { return !workers_.empty(); } - Shm &shm_; + template <class... Rest> + bool SendImpl(const MQId remote, Rest &&...rest) + { + send_buffer_.Append(remote, std::forward<decltype(rest)>(rest)...); + return true; + } + std::vector<std::thread> workers_; std::mutex mutex_; std::atomic<bool> run_; @@ -150,8 +154,9 @@ std::unordered_map<std::string, RecvCB> store_; public: - bool Add(const std::string &id, const RecvCB &cb) { return store_.emplace(id, cb).second; } - bool Find(const std::string &id, RecvCB &cb) + bool empty() const { return store_.empty(); } + bool Store(const std::string &id, RecvCB &&cb) { return store_.emplace(id, std::move(cb)).second; } + bool Pick(const std::string &id, RecvCB &cb) { auto pos = store_.find(id); if (pos != store_.end()) { @@ -165,6 +170,8 @@ }; Synced<AsyncCBs> per_msg_cbs_; + SendQ send_buffer_; + // Synced<SendQ> send_buffer_; }; #endif // end of include guard: SOCKET_GWTJHBPO -- Gitblit v1.8.0