| | |
| | | #ifndef SOCKET_GWTJHBPO |
| | | #define SOCKET_GWTJHBPO |
| | | |
| | | #include "defs.h" |
| | | #include "shm_queue.h" |
| | | #include <atomic> |
| | | #include <boost/noncopyable.hpp> |
| | | #include <condition_variable> |
| | | #include <functional> |
| | | #include <memory> |
| | | #include <mutex> |
| | | #include <thread> |
| | | #include <unordered_map> |
| | | #include <vector> |
| | | |
| | | using namespace bhome_msg; |
| | | |
| | | class ShmSocket : private boost::noncopyable |
| | | { |
| | | protected: |
| | | typedef bhome_shm::ShmMsgQueue Queue; |
| | | |
| | | public: |
| | | enum Type { |
| | | eSockRequest, |
| | | eSockReply, |
| | | eSockSubscribe, |
| | | eSockPublish, |
| | | eSockBus, |
| | | }; |
| | | typedef std::function<void(bhome_msg::BHMsg &msg)> RecvCB; |
| | | typedef std::function<void(bhome_msg::MsgI &imsg)> RecvRawCB; |
| | | typedef std::function<void(const void *data, const size_t size)> RequestResultCB; |
| | | 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(Type type, bhome_shm::SharedMemory &shm); |
| | | ShmSocket(Type type); |
| | | ShmSocket(Shm &shm, const MQId &id, const int len); |
| | | ShmSocket(Shm &shm, const int len = 12); |
| | | ~ShmSocket(); |
| | | bool RequestRPC(const std::string &topic, const void *data, const size_t size, const int timeout_ms, const RequestResultCB &rrcb); |
| | | bool RequestRPC(const std::string &topic, const void *data, const size_t size, const int timeout_ms, std::string &out); |
| | | |
| | | // bool HandleRequest(onData); |
| | | bool ReadRequest(); // exclude with HandleRequest |
| | | bool SendReply(); // exclude with HandleRequest |
| | | |
| | | 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); |
| | | |
| | | const MQId &id() const { return mq().Id(); } |
| | | Shm &shm() { return shm_; } |
| | | // start recv. |
| | | bool Start(const RecvCB &onData, int nworker = 1); |
| | | bool StartRaw(const RecvRawCB &onData, int nworker = 1); |
| | | bool StartAsync(int nworker = 2); |
| | | 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_ ? mq_->Pending() : 0; } |
| | | 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()) |
| | | { |
| | | 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); |
| | | } else { |
| | | return mq().Send(*static_cast<const MQId *>(valid_remote), head, body, timeout_ms); |
| | | } |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | template <class Body> |
| | | 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<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 { |
| | | } |
| | | }; |
| | | |
| | | std::unique_lock<std::mutex> 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 AsyncRequest(const void *remote, const void *msg, const int timeout_ms, const RecvCB &cb); |
| | | bool SyncRequest(const void *remote, const void *msg, void *result, const int timeout_ms); |
| | | bool QueryRPCTopic(const std::string &topic, bhome::msg::BHAddress &addr, const int timeout_ms); |
| | | bool StopNoLock(); |
| | | bhome_shm::SharedMemory &shm_; |
| | | const Type type_; |
| | | bool RunningNoLock() { return !workers_.empty(); } |
| | | |
| | | Shm &shm_; |
| | | std::vector<std::thread> workers_; |
| | | std::mutex mutex_; |
| | | std::atomic<bool> run_; |
| | | |
| | | std::unique_ptr<Queue> mq_; |
| | | Queue mq_; |
| | | std::unordered_map<std::string, RecvCB> async_cbs_; |
| | | }; |
| | | |