lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
src/socket.cpp
@@ -28,25 +28,13 @@
using namespace bhome_shm;
ShmSocket::ShmSocket(Shm &shm, const MQId id, const int len) :
    run_(false), mq_(id, shm, len), alloc_id_(0)
{
   Start();
}
    run_(false), mq_(shm, id, len), alloc_id_(0) { Start(); }
ShmSocket::ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len) :
    run_(false), mq_(id, create_or_else_find, shm, len), alloc_id_(0)
{
   Start();
}
ShmSocket::ShmSocket(bhome_shm::SharedMemory &shm, const int len) :
    run_(false), mq_(shm, len), alloc_id_(0)
{
   Start();
}
    run_(false), mq_(shm, create_or_else_find, id, len), alloc_id_(0) { Start(); }
ShmSocket::ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) :
    run_(false), mq_(abs_addr, shm, id), alloc_id_(0) { Start(); }
ShmSocket::~ShmSocket()
{
   Stop();
}
ShmSocket::~ShmSocket() { Stop(); }
bool ShmSocket::Start(int nworker, const RecvCB &onData, const RawRecvCB &onRaw, const IdleCB &onIdle)
{
@@ -92,15 +80,13 @@
            }
         };
         ShmMsgQueue::RawData val = 0;
         auto TryRecvMore = [&]() {
            for (int i = 0; i < 100; ++i) {
               if (mq().TryRecv(val)) {
                  return true;
               }
         for (int i = 0; i < 100; ++i) {
            if (mq().TryRecv(val)) {
               onRecv(val);
               return true;
            }
            return false;
         };
         return TryRecvMore() ? (onRecv(val), true) : false;
         }
         return false;
      };
      try {
@@ -153,23 +139,40 @@
   return false;
}
bool ShmSocket::SyncRecv(int64_t &cmd, const int timeout_ms)
bool ShmSocket::Send(const MQInfo &remote, std::string &&content, const std::string &msg_id, RecvCB &&cb)
{
   return (timeout_ms == 0) ? mq().TryRecv(cmd) : mq().Recv(cmd, timeout_ms);
}
//maybe reimplment, using async cbs?
bool ShmSocket::SyncRecv(bhome_msg::MsgI &msg, bhome_msg::BHMsgHead &head, const int timeout_ms)
{
   // std::lock_guard<std::mutex> lock(mutex_); // seems no need to lock mutex_.
   bool got = (timeout_ms == 0) ? mq().TryRecv(msg) : mq().Recv(msg, timeout_ms);
   if (got) {
      if (msg.ParseHead(head)) {
   size_t size = content.size();
   auto OnResult = [content = std::move(content), msg_id, remote, cb = std::move(cb), 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);
         }
         return true;
      } else {
         msg.Release();
      } catch (...) {
         SetLastError(eError, "Send internal error.");
         return false;
      }
   };
#if 0
   // self alloc
   MsgI msg;
   if (msg.Make(size)) {
      DEFER1(msg.Release());
      return OnResult(msg);
   }
   return false;
#else
   // center alloc
   return RequestAlloc(size, OnResult);
#endif
}
bool ShmSocket::RequestAlloc(const int64_t size, std::function<void(MsgI &msg)> const &onResult)
@@ -196,5 +199,6 @@
      RawRecvCB cb_no_use;
      alloc_cbs_->Pick(id, cb_no_use);
   };
   return Send(BHTopicCenterAddress(), cmd, onExpireRemoveCB);
}