/* * ===================================================================================== * * Filename: shm_socket.h * * Description: * * Version: 1.0 * Created: 2021年03月30日 15时49分19秒 * Revision: none * Compiler: gcc * * Author: Li Chao (), * Organization: * * ===================================================================================== */ #ifndef SHM_SOCKET_GWTJHBPO #define SHM_SOCKET_GWTJHBPO #include "bh_util.h" #include "defs.h" #include "sendq.h" #include "shm_msg_queue.h" #include #include #include #include #include #include #include #include using namespace bhome_msg; class ShmSocket : private boost::noncopyable { protected: typedef ShmMsgQueue Queue; public: typedef ShmMsgQueue::MQId MQId; typedef bhome_shm::SharedMemory Shm; typedef std::function RawRecvCB; typedef std::function RecvCB; typedef std::function PartialRecvCB; typedef std::function IdleCB; ShmSocket(Shm &shm, const MQId id, Mode mode) : run_(false), mq_(shm, id, mode), alloc_id_(0), send_buffer_(shm) { Start(); } ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) : run_(false), mq_(abs_addr, shm, id), alloc_id_(0), send_buffer_(shm) { Start(); } ~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(); } int64_t AbsAddr() const { return mq().AbsAddr(); } void SetNodeProc(const int proc_index, const int socket_index) { node_proc_index_ = proc_index; socket_index_ = socket_index; } // start recv. bool Start(int nworker = 1, const RecvCB &onMsg = RecvCB(), const RawRecvCB &onRaw = RawRecvCB(), const IdleCB &onIdle = IdleCB()); bool Start(const RecvCB &onData, const IdleCB &onIdle, int nworker = 1) { return Start(nworker, onData, RawRecvCB(), onIdle); } bool Start(const RecvCB &onData, int nworker = 1) { return Start(nworker, onData); } bool Stop(); bool RequestAlloc(const int64_t size, std::function const &onResult); bool Send(const MQInfo &remote, const MsgI &msg, const std::string &msg_id, RecvCB &&cb); template bool Send(const MQInfo &remote, BHMsgHead &head, Body &body, RecvCB &&cb) { return Send(remote, MsgI::Serialize(head, body), head.msg_id(), std::move(cb)); } bool Send(const MQInfo &remote, std::string &&content, const std::string &msg_id, RecvCB &&cb); template bool Send(const MQInfo &remote, BHMsgHead &head, Body &body) { return Send(remote, MsgI::Serialize(head, body)); } bool Send(const MQInfo &remote, std::string &&content); bool Send(const MQInfo &remote, const MsgI &imsg) { return SendImpl(remote, imsg); } template bool Send(const MQInfo &remote, const int64_t cmd, T &&...t) { return SendImpl(remote, cmd, std::forward(t)...); } template bool SendAndRecv(const MQInfo &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 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 lk(st->mutex); if (!st->canceled) { reply.swap(msg); reply_head.Swap(&head); st->cv.notify_one(); } else { // ignore } }; std::unique_lock 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 StopNoLock(); bool RunningNoLock() { return !workers_.empty(); } template bool SendImpl(Rest &&...rest) { return send_buffer_.Append(std::forward(rest)...); } std::vector workers_; std::mutex mutex_; std::atomic run_; Queue mq_; template class CallbackRecords { std::unordered_map store_; public: bool empty() const { return store_.empty(); } bool Store(const Key &id, CB &&cb) { return store_.emplace(id, std::move(cb)).second; } bool Pick(const Key &id, CB &cb) { auto pos = store_.find(id); if (pos != store_.end()) { cb.swap(pos->second); store_.erase(pos); return true; } else { return false; } } }; Synced> per_msg_cbs_; Synced> alloc_cbs_; // node request center alloc memory. int node_proc_index_ = -1; int socket_index_ = -1; std::atomic alloc_id_; SendQ send_buffer_; }; #endif // end of include guard: SHM_SOCKET_GWTJHBPO