lichao
2021-04-08 c338820e4db43ad32c20ff429a038b06bcb980f8
src/socket.cpp
@@ -29,83 +29,57 @@
} // namespace
ShmSocket::ShmSocket(Type type, bhome_shm::SharedMemory &shm) :
    shm_(shm), type_(type), run_(false)
ShmSocket::ShmSocket(Shm &shm, const MQId &id, const int len) :
    shm_(shm), run_(false), mq_(id, shm, len)
{
   switch (type) {
   case eSockBus: mq_.reset(new Queue(kBHBusQueueId, shm_, 1000)); break;
   case eSockRequest: mq_.reset(new Queue(shm_, 12)); break;
   case eSockReply: mq_.reset(new Queue(shm_, 64)); break;
   case eSockSubscribe: mq_.reset(new Queue(shm_, 64)); break;
   case eSockPublish: break; // no recv mq needed
   default: break;
   }
}
ShmSocket::ShmSocket(Type type) :
    ShmSocket(type, BHomeShm()) {}
ShmSocket::ShmSocket(bhome_shm::SharedMemory &shm, const int len) :
    shm_(shm), run_(false), mq_(shm, len) {}
ShmSocket::~ShmSocket()
{
   Stop();
   Stop(); //TODO should stop in sub class, incase thread access sub class data.
}
bool ShmSocket::Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms)
bool ShmSocket::Start(int nworker, const RecvCB &onData, const IdleCB &onIdle)
{
   if (type_ != eSockPublish) {
      return false;
   }
   assert(!mq_);
   try {
      MsgI imsg;
      if (!imsg.MakeRC(shm_, MakePub(topic, data, size))) {
         return false;
      }
      DEFER1(imsg.Release(shm_));
      return Queue::Send(shm_, kBHBusQueueId, imsg, timeout_ms);
   } catch (...) {
      return false;
   }
}
   auto onRecv = [this, onData](ShmSocket &socket, MsgI &imsg, BHMsgHead &head) {
      auto Find = [&](RecvCB &cb) {
         std::lock_guard<std::mutex> lock(mutex());
         const std::string &msgid = head.msg_id();
         auto pos = async_cbs_.find(msgid);
         if (pos != async_cbs_.end()) {
            cb.swap(pos->second);
            async_cbs_.erase(pos);
            return true;
         } else {
            return false;
         }
      };
bool ShmSocket::Subscribe(const std::vector<std::string> &topics, const int timeout_ms)
{
   if (type_ != eSockSubscribe) {
      return false;
   }
   assert(mq_);
   try {
      return mq_->Send(kBHBusQueueId, MakeSub(mq_->Id(), topics), timeout_ms);
   } catch (...) {
      return false;
   }
}
bool ShmSocket::StartRaw(const RecvRawCB &onData, int nworker)
{
   auto CanRecv = [this]() {
      switch (type_) {
      case eSockRequest:
      case eSockReply:
      case eSockBus:
      case eSockSubscribe:
         return true;
      default:
         return false;
      }
      RecvCB cb;
      if (Find(cb)) {
         cb(socket, imsg, head);
      } else if (onData) {
         onData(socket, imsg, head);
      } // else ignored, or dropped
   };
   if (!CanRecv()) {
      return false;
   }
   std::lock_guard<std::mutex> lock(mutex_);
   std::lock_guard<std::mutex> lock(mutex_);
   StopNoLock();
   auto RecvProc = [this, onData]() {
   auto RecvProc = [this, onRecv, onIdle]() {
      while (run_) {
         try {
            MsgI imsg;
            DEFER1(imsg.Release(shm_));
            if (mq_->Recv(imsg, 100)) { onData(imsg); }
            if (mq().Recv(imsg, 10)) {
               DEFER1(imsg.Release(shm()));
               BHMsgHead head;
               if (imsg.ParseHead(head)) {
                  onRecv(*this, imsg, head);
               }
            } else if (onIdle) {
               onIdle(*this);
            }
         } catch (...) {
         }
      }
@@ -116,11 +90,6 @@
      workers_.emplace_back(RecvProc);
   }
   return true;
}
bool ShmSocket::Start(const RecvCB &onData, int nworker)
{
   return StartRaw([this, onData](MsgI &imsg) { BHMsg m; if (imsg.Unpack(m)) { onData(m); } }, nworker);
}
bool ShmSocket::Stop()
@@ -137,7 +106,24 @@
            w.join();
         }
      }
      workers_.clear();
      return true;
   }
   return false;
}
bool ShmSocket::SyncRecv(bhome_msg::MsgI &msg, bhome::msg::BHMsgHead &head, const int timeout_ms)
{
   std::lock_guard<std::mutex> lock(mutex_);
   auto Recv = [&]() {
      if (mq().Recv(msg, timeout_ms)) {
         if (msg.ParseHead(head)) {
            return true;
         } else {
            msg.Release(shm());
         }
      }
      return false;
   };
   return !RunningNoLock() && Recv();
}