lichao
2021-04-14 aa1542b6d6a4680088ac715c4ce40f97ada554fb
src/socket.h
@@ -21,6 +21,7 @@
#include "bh_util.h"
#include "defs.h"
#include "sendq.h"
#include "shm_queue.h"
#include <atomic>
#include <boost/noncopyable.hpp>
@@ -35,13 +36,10 @@
class ShmSocket : private boost::noncopyable
{
   template <class DoSend>
   inline bool SendImpl(MsgI &msg, const int timeout_ms, const DoSend &doSend)
   bool SendImpl(const void *valid_remote, const MsgI &imsg, SendQ::OnMsgEvent onExpire = SendQ::OnMsgEvent())
   {
      bool r = false;
      DEFER1(if (msg.IsCounted() || !r) { msg.Release(shm()); });
      r = doSend(msg);
      return r;
      send_buffer_->Append(*static_cast<const MQId *>(valid_remote), imsg, onExpire);
      return true;
   }
protected:
@@ -58,7 +56,6 @@
   ~ShmSocket();
   static bool Remove(SharedMemory &shm, const MQId &id) { return Queue::Remove(shm, id); }
   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); }
@@ -66,29 +63,36 @@
   bool Stop();
   size_t Pending() const { return mq().Pending(); }
   bool Send(const void *valid_remote, const MsgI &imsg, const int timeout_ms)
   template <class Body>
   bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body)
   {
      assert(valid_remote);
      return mq().Send(*static_cast<const MQId *>(valid_remote), imsg, timeout_ms);
      MsgI msg;
      return msg.Make(shm(), head, body) && SendImpl(valid_remote, msg);
   }
   //TODO reimplment, using async.
   template <class Body>
   bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const RecvCB &cb)
   {
      //TODO send_buffer_ need flag, and remove callback on expire.
      MsgI msg;
      if (msg.Make(shm(), head, body)) {
         std::string msg_id(head.msg_id());
         per_msg_cbs_->Add(msg_id, cb);
         auto onExpireRemoveCB = [this, msg_id](MsgI const &msg) {
            RecvCB cb_no_use;
            per_msg_cbs_->Find(msg_id, cb_no_use);
         };
         return SendImpl(valid_remote, msg, onExpireRemoveCB);
      }
      return false;
   }
   bool Send(const void *valid_remote, const MsgI &imsg)
   {
      return SendImpl(valid_remote, imsg);
   }
   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)
   {
      auto DoSend = [&](MsgI &msg) { return mq().Send(*static_cast<const MQId *>(valid_remote), msg, timeout_ms, [&]() { per_msg_cbs_->Add(head.msg_id(), cb); }); };
      MsgI msg;
      return msg.Make(shm(), head, body) && SendImpl(msg, timeout_ms, DoSend);
   }
   template <class Body>
   bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const int timeout_ms)
   {
      auto DoSend = [&](MsgI &msg) { return mq().Send(*static_cast<const MQId *>(valid_remote), msg, timeout_ms); };
      MsgI msg;
      return msg.Make(shm(), head, body) && SendImpl(msg, timeout_ms, DoSend);
   }
   template <class Body>
   bool SendAndRecv(const void *remote, const BHMsgHead &head, const Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms)
@@ -114,7 +118,7 @@
         };
         std::unique_lock<std::mutex> lk(st->mutex);
         bool sendok = Send(remote, head, body, timeout_ms, OnRecv);
         bool sendok = Send(remote, head, body, OnRecv);
         if (!sendok) {
            printf("send timeout\n");
         }
@@ -129,8 +133,9 @@
      }
   }
   Shm &shm() const { return mq().shm(); }
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_; }
@@ -139,7 +144,6 @@
   bool StopNoLock();
   bool RunningNoLock() { return !workers_.empty(); }
   Shm &shm_;
   std::vector<std::thread> workers_;
   std::mutex mutex_;
   std::atomic<bool> run_;
@@ -150,6 +154,7 @@
      std::unordered_map<std::string, RecvCB> store_;
   public:
      bool empty() const { return store_.empty(); }
      bool Add(const std::string &id, const RecvCB &cb) { return store_.emplace(id, cb).second; }
      bool Find(const std::string &id, RecvCB &cb)
      {
@@ -165,6 +170,7 @@
   };
   Synced<AsyncCBs> per_msg_cbs_;
   Synced<SendQ> send_buffer_;
};
#endif // end of include guard: SOCKET_GWTJHBPO