From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期一, 10 五月 2021 16:05:28 +0800 Subject: [PATCH] test lock code. --- src/socket.h | 186 ++++++++++++++++++++++++++++++++++++--------- 1 files changed, 147 insertions(+), 39 deletions(-) diff --git a/src/socket.h b/src/socket.h index e65ac83..981677f 100644 --- a/src/socket.h +++ b/src/socket.h @@ -19,56 +19,164 @@ #ifndef SOCKET_GWTJHBPO #define SOCKET_GWTJHBPO -#include "shm_queue.h" -#include <vector> -#include <thread> -#include <memory> -#include <functional> -#include <mutex> -#include <condition_variable> +#include "bh_util.h" +#include "defs.h" +#include "sendq.h" +#include "shm_msg_queue.h" #include <atomic> +#include <boost/noncopyable.hpp> +#include <condition_variable> +#include <functional> +#include <memory> +#include <mutex> +#include <thread> +#include <vector> -class ShmSocket +using namespace bhome_msg; +class ShmSocket : private boost::noncopyable { - typedef bhome_shm::ShmMsgQueue Queue; + +protected: + typedef ShmMsgQueue Queue; + public: - enum Type { - eSockRequest, - eSockReply, - eSockSubscribe, - eSockPublish, - }; - typedef std::function<void (bhome_msg::BHMsg &msg)> RecvCB; + typedef ShmMsgQueue::MQId MQId; + typedef bhome_shm::SharedMemory Shm; + typedef std::function<void(ShmSocket &sock, MsgI &imsg)> RawRecvCB; + 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(Type type); - ShmSocket(Type type, bhome_shm::SharedMemory &shm); - ~ShmSocket(); + ShmSocket(Shm &shm, const MQId id, const int len); + ShmSocket(Shm &shm, const bool create_or_else_find, 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()); } + MQId id() const { return mq().Id(); } + // start recv. + bool Start(const RawRecvCB &onData, const IdleCB &onIdle, int nworker = 1); + 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(); - // bool Request(const std::string &topic, const void *data, const size_t size, onReply); - bool RequestAndWait() { return false; } // call Request, and wait onReply notify cv + template <class Body> + bool Send(const MQId remote, BHMsgHead &head, Body &body, RecvCB &&cb = RecvCB()) + { + 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; + } + } - // bool HandleRequest(onData); - bool ReadRequest(); // exclude with HandleRequest - bool SendReply(); // exclude with HandleRequest + bool Send(const MQId remote, const MsgI &imsg) + { + return SendImpl(remote, imsg); + } - bool Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms); - bool Subscribe(const std::vector<std::string> &topics, const int timeout_ms); - bool RecvSub(std::string &topic, std::string &data, const int timeout_ms); - bool SetRecvCallback(const RecvCB &onRecv); + bool SyncRecv(MsgI &msg, bhome_msg::BHMsgHead &head, const int timeout_ms); + + template <class Body> + bool SendAndRecv(const MQId remote, BHMsgHead &head, Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms) + { + struct State { + std::mutex mutex; + std::condition_variable cv; + bool canceled = false; + }; + + try { + std::shared_ptr<State> st(new State); + auto endtime = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); + + auto OnRecv = [st, &reply, &reply_head](ShmSocket &sock, MsgI &msg, BHMsgHead &head) { + std::unique_lock<std::mutex> lk(st->mutex); + if (!st->canceled) { + reply.swap(msg); + reply_head.Swap(&head); + st->cv.notify_one(); + } else { // ignore + } + }; + + std::unique_lock<std::mutex> lk(st->mutex); + bool sendok = Send(remote, head, body, std::move(OnRecv)); + if (!sendok) { + LOG_DEBUG() << "send timeout"; + } + if (sendok && st->cv.wait_until(lk, endtime) == std::cv_status::no_timeout) { + return true; + } else { + st->canceled = true; + SetLastError(ETIMEDOUT, "timeout"); + return false; + } + } catch (...) { + return false; + } + } + + Shm &shm() const { return mq().shm(); } + +protected: + Queue &mq() { return mq_; } // programmer should make sure that mq_ is valid. + const Queue &mq() const { return mq_; } + std::mutex &mutex() { return mutex_; } + private: - bool HasRecvCB(); - void Stop(); + bool StopNoLock(); + bool RunningNoLock() { return !workers_.empty(); } - bhome_shm::SharedMemory &shm_; - Type type_; - std::vector<std::thread> workers_; - std::mutex mutex_; - std::condition_variable cv_recv_cb_; - std::atomic<bool> run_; - RecvCB onRecv_; + template <class... Rest> + bool SendImpl(const MQId remote, Rest &&...rest) + { + // TODO send alloc request, and pack later, higher bit means alloc? + send_buffer_.Append(remote, std::forward<decltype(rest)>(rest)...); + return true; + } - std::unique_ptr<Queue> mq_; + std::vector<std::thread> workers_; + std::mutex mutex_; + std::atomic<bool> run_; + + Queue mq_; + template <class Key> + class CallbackRecords + { + std::unordered_map<Key, RecvCB> store_; + + public: + bool empty() const { return store_.empty(); } + bool Store(const Key &id, RecvCB &&cb) { return store_.emplace(id, std::move(cb)).second; } + bool Pick(const Key &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<CallbackRecords<std::string>> per_msg_cbs_; + + SendQ send_buffer_; }; - #endif // end of include guard: SOCKET_GWTJHBPO -- Gitblit v1.8.0