/* * ===================================================================================== * * 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 "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, 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(); template 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 Send(const MQId remote, const MsgI &imsg) { return SendImpl(remote, imsg); } bool SyncRecv(MsgI &msg, bhome_msg::BHMsgHead &head, const int timeout_ms); template 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 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(const MQId remote, Rest &&...rest) { // TODO send alloc request, and pack later, higher bit means alloc? send_buffer_.Append(remote, std::forward(rest)...); return true; } 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, 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> per_msg_cbs_; SendQ send_buffer_; }; #endif // end of include guard: SOCKET_GWTJHBPO