lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
src/socket.h
@@ -21,7 +21,8 @@
#include "bh_util.h"
#include "defs.h"
#include "shm_queue.h"
#include "sendq.h"
#include "shm_msg_queue.h"
#include <atomic>
#include <boost/noncopyable.hpp>
#include <condition_variable>
@@ -32,66 +33,75 @@
#include <vector>
using namespace bhome_msg;
class ShmSocket : private boost::noncopyable
{
   template <class DoSend>
   inline bool SendImpl(MsgI &msg, const int timeout_ms, const DoSend &doSend)
   {
      bool r = false;
      DEFER1(if (msg.IsCounted() || !r) { msg.Release(shm()); });
      r = doSend(msg);
      return r;
   }
protected:
   typedef bhome_shm::ShmMsgQueue Queue;
   typedef ShmMsgQueue Queue;
public:
   typedef ShmMsgQueue::MQId MQId;
   typedef bhome_shm::SharedMemory Shm;
   typedef std::function<void(ShmSocket &sock, Queue::RawData &val)> RawRecvCB;
   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(Shm &shm, const MQId &id, const int len);
   ShmSocket(Shm &shm, const int len = 12);
   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(int64_t offset, Shm &shm, const MQId id);
   ~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_; }
   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 &onData = RecvCB(), const IdleCB &onIdle = IdleCB());
   bool Start(const RecvCB &onData, const IdleCB &onIdle, int nworker = 1) { return Start(nworker, onData, onIdle); }
   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();
   size_t Pending() const { return mq().Pending(); }
   bool Send(const void *valid_remote, const MsgI &imsg, const int timeout_ms)
   {
      assert(valid_remote);
      return mq().Send(*static_cast<const MQId *>(valid_remote), 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)
   bool CenterSend(const MQInfo &remote, BHMsgHead &head, Body &body)
   {
      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);
      try {
         //TODO alloc outsiez and use send.
         MsgI msg;
         if (!msg.Make(head, body)) { return false; }
         DEFER1(msg.Release());
         return Send(remote, msg);
      } catch (...) {
         SetLastError(eError, "Send internal error.");
         return false;
      }
   }
   bool RequestAlloc(const int64_t size, std::function<void(MsgI &msg)> const &onResult);
   template <class Body>
   bool Send(const MQInfo &remote, BHMsgHead &head, Body &body, RecvCB &&cb = RecvCB())
   {
      return Send(remote, MsgI::Serialize(head, body), head.msg_id(), std::move(cb));
   }
   template <class... T>
   bool Send(const MQInfo &remote, const MsgI &imsg, T &&...t)
   {
      return SendImpl(remote, imsg, std::forward<decltype(t)>(t)...);
   }
   template <class... T>
   bool Send(const MQInfo &remote, const int64_t cmd, T &&...t)
   {
      return SendImpl(remote, cmd, std::forward<decltype(t)>(t)...);
   }
   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)
   bool SendAndRecv(const MQInfo &remote, BHMsgHead &head, Body &body, MsgI &reply, BHMsgHead &reply_head, const int timeout_ms)
   {
      struct State {
         std::mutex mutex;
@@ -101,6 +111,7 @@
      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) {
@@ -114,14 +125,15 @@
         };
         std::unique_lock<std::mutex> lk(st->mutex);
         bool sendok = Send(remote, head, body, timeout_ms, OnRecv);
         bool sendok = Send(remote, head, body, std::move(OnRecv));
         if (!sendok) {
            printf("send timeout\n");
            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 (...) {
@@ -129,8 +141,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,19 +152,28 @@
   bool StopNoLock();
   bool RunningNoLock() { return !workers_.empty(); }
   Shm &shm_;
   bool Send(const MQInfo &remote, std::string &&content, const std::string &msg_id, RecvCB &&cb = RecvCB());
   template <class... Rest>
   bool SendImpl(const MQInfo &remote, Rest &&...rest)
   {
      return send_buffer_.Append(remote, std::forward<decltype(rest)>(rest)...);
   }
   std::vector<std::thread> workers_;
   std::mutex mutex_;
   std::atomic<bool> run_;
   Queue mq_;
   class AsyncCBs
   template <class Key, class CB>
   class CallbackRecords
   {
      std::unordered_map<std::string, RecvCB> store_;
      std::unordered_map<Key, CB> store_;
   public:
      bool Add(const std::string &id, const RecvCB &cb) { return store_.emplace(id, cb).second; }
      bool Find(const std::string &id, RecvCB &cb)
      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()) {
@@ -164,7 +186,14 @@
      }
   };
   Synced<AsyncCBs> per_msg_cbs_;
   Synced<CallbackRecords<std::string, RecvCB>> per_msg_cbs_;
   Synced<CallbackRecords<int, RawRecvCB>> alloc_cbs_;
   SendQ send_buffer_;
   // node request center alloc memory.
   int node_proc_index_ = -1;
   int socket_index_ = -1;
   std::atomic<int> alloc_id_;
};
#endif // end of include guard: SOCKET_GWTJHBPO