lichao
2021-05-27 026bbfaf2b5d73a26b8e2fa49158883ef64c211b
src/shm_socket.cpp
@@ -145,46 +145,66 @@
   return false;
}
bool ShmSocket::Send(const MQInfo &remote, std::string &&content, const std::string &msg_id, RecvCB &&cb)
bool ShmSocket::Send(const MQInfo &remote, std::string &&content)
{
   size_t size = content.size();
   auto OnResult = [content = std::move(content), msg_id, remote, cb = std::move(cb), this](MsgI &msg) mutable {
   auto OnResult = [content = std::move(content), remote, this](MsgI &msg) mutable {
      if (!msg.Fill(content)) { return false; }
      try {
         if (!cb) {
            Send(remote, msg);
         } else {
            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);
            };
            Send(remote, msg, onExpireRemoveCB);
         }
         SendImpl(remote, msg);
         return true;
      } catch (...) {
         SetLastError(eError, "Send internal error.");
         return false;
      }
   };
   return RequestAlloc(size, OnResult);
}
bool ShmSocket::Send(const MQInfo &remote, const MsgI &msg, const std::string &msg_id, RecvCB &&cb)
{
   try {
      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);
      };
      SendImpl(remote, msg, onExpireRemoveCB);
      return true;
   } catch (std::exception &e) {
      SetLastError(eError, "Send internal error.");
      return false;
   }
}
bool ShmSocket::Send(const MQInfo &remote, std::string &&content, const std::string &msg_id, RecvCB &&cb)
{
   size_t size = content.size();
   auto OnResult = [content = std::move(content), msg_id, remote, cb = std::move(cb), this](MsgI &msg) mutable {
      return msg.Fill(content) && Send(remote, msg, msg_id, std::move(cb));
   };
   return RequestAlloc(size, OnResult);
}
bool ShmSocket::RequestAlloc(const int64_t size, std::function<void(MsgI &msg)> const &onResult)
{ // 8bit size, 4bit socket index, 16bit proc index, 28bit id, ,4bit cmd+flag
#if 0
   // self alloc
   MsgI msg(shm());
   if (msg.Make(size)) {
      DEFER1(msg.Release());
      return OnResult(msg);
      return onResult(msg);
   } else {
      return false;
   }
#else
   // center alloc
   return RequestAlloc(size, OnResult);
#endif
}
bool ShmSocket::RequestAlloc(const int64_t size, std::function<void(MsgI &msg)> const &onResult)
{ // 8bit size, 4bit socket index, 16bit proc index, 28bit id, ,4bit cmd+flag
   // LOG_FUNCTION;
   if (node_proc_index_ == -1 || socket_index_ == -1) {
      LOG_ERROR() << "socket not inited.";
      return false;
   }
   int id = (++alloc_id_) & MaskBits(28);