/* * ===================================================================================== * * Filename: socket.h * * Description: * * Version: 1.0 * Created: 2021年03月30日 15时49分19秒 * Revision: none * Compiler: gcc * * Author: Li Chao (), * Organization: * * ===================================================================================== */ #ifndef SOCKET_GWTJHBPO #define SOCKET_GWTJHBPO #include "defs.h" #include "shm_queue.h" #include #include #include #include #include #include #include #include using namespace bhome_msg; class ShmSocket : private boost::noncopyable { protected: typedef bhome_shm::ShmMsgQueue Queue; public: typedef bhome_shm::SharedMemory Shm; typedef std::function RecvCB; typedef std::function PartialRecvCB; typedef std::function IdleCB; ShmSocket(Shm &shm, const MQId &id, const int len); ShmSocket(Shm &shm, const int len = 12); ~ShmSocket(); 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); } 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 *id, const MsgI &imsg, const int timeout_ms) { return mq().Send(*static_cast(id), imsg, timeout_ms); } //TODO reimplment, using async. bool SyncRecv(MsgI &msg, bhome::msg::BHMsgHead &head, const int timeout_ms); template bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const int timeout_ms, const RecvCB &cb = RecvCB()) { assert(valid_remote); try { if (cb) { auto RegisterCB = [&]() { std::lock_guard lock(mutex()); async_cbs_.emplace(head.msg_id(), cb); }; return mq().Send(*static_cast(valid_remote), head, body, timeout_ms, RegisterCB); } else { return mq().Send(*static_cast(valid_remote), head, body, timeout_ms); } } catch (...) { return false; } } template bool SendAndRecv(const void *remote, const BHMsgHead &head, const 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 { } }; std::unique_lock lk(st->mutex); bool sendok = Send(remote, head, body, timeout_ms, OnRecv); if (sendok && st->cv.wait_until(lk, endtime) == std::cv_status::no_timeout) { return true; } else { st->canceled = true; return false; } } catch (...) { return false; } } 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_; } private: bool StopNoLock(); bool RunningNoLock() { return !workers_.empty(); } Shm &shm_; std::vector workers_; std::mutex mutex_; std::atomic run_; Queue mq_; std::unordered_map async_cbs_; }; #endif // end of include guard: SOCKET_GWTJHBPO