lichao
2021-04-14 aa1542b6d6a4680088ac715c4ce40f97ada554fb
src/socket.cpp
@@ -30,11 +30,11 @@
} // namespace
ShmSocket::ShmSocket(Shm &shm, const MQId &id, const int len) :
    shm_(shm), run_(false), mq_(id, shm, len)
    run_(false), mq_(id, shm, len)
{
}
ShmSocket::ShmSocket(bhome_shm::SharedMemory &shm, const int len) :
    shm_(shm), run_(false), mq_(shm, len) {}
    run_(false), mq_(shm, len) {}
ShmSocket::~ShmSocket()
{
@@ -43,28 +43,38 @@
bool ShmSocket::Start(int nworker, const RecvCB &onData, const IdleCB &onIdle)
{
   auto onRecvWithPerMsgCB = [this, onData](ShmSocket &socket, MsgI &imsg, BHMsgHead &head) {
      RecvCB cb;
      if (per_msg_cbs_->Find(head.msg_id(), cb)) {
         cb(socket, imsg, head);
      } else if (onData) {
         onData(socket, imsg, head);
      } else { // else ignored, or dropped
      }
   };
   auto ioProc = [this, onData, onIdle]() {
      auto DoSend = [this]() { return send_buffer_->TrySend(mq()); };
      auto DoRecv = [=] {
         auto onRecvWithPerMsgCB = [this, onData](ShmSocket &socket, MsgI &imsg, BHMsgHead &head) {
            RecvCB cb;
            if (per_msg_cbs_->Find(head.msg_id(), cb)) {
               cb(socket, imsg, head);
            } else if (onData) {
               onData(socket, imsg, head);
            }
         };
   auto recvLoopBody = [this, onRecvWithPerMsgCB, onIdle]() {
      try {
         MsgI imsg;
         if (mq().Recv(imsg, 10)) {
         // do not recv if no cb is set.
         if (!onData && per_msg_cbs_->empty()) {
            return false;
         }
         auto onMsg = [&](MsgI &imsg) {
            DEFER1(imsg.Release(shm()));
            BHMsgHead head;
            if (imsg.ParseHead(head)) {
               onRecvWithPerMsgCB(*this, imsg, head);
            }
         }
         if (onIdle) {
            onIdle(*this);
         };
         return mq().TryRecvAll(onMsg) > 0; // this will recv all msgs.
      };
      try {
         bool more_to_send = DoSend();
         bool more_to_recv = DoRecv();
         if (onIdle) { onIdle(*this); }
         if (!more_to_send && !more_to_recv) {
            std::this_thread::yield();
         }
      } catch (...) {
      }
@@ -75,7 +85,7 @@
   run_.store(true);
   for (int i = 0; i < nworker; ++i) {
      workers_.emplace_back([this, recvLoopBody]() { while (run_) { recvLoopBody(); } });
      workers_.emplace_back([this, ioProc]() { while (run_) { ioProc(); } });
   }
   return true;
}
@@ -100,18 +110,17 @@
   return false;
}
//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_);
   auto Recv = [&]() {
      if (mq().Recv(msg, timeout_ms)) {
         if (msg.ParseHead(head)) {
            return true;
         } else {
            msg.Release(shm());
         }
   // 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)) {
         return true;
      } else {
         msg.Release(shm());
      }
      return false;
   };
   return !RunningNoLock() && Recv();
   }
   return false;
}