lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
src/sendq.cpp
@@ -16,10 +16,30 @@
 * =====================================================================================
 */
#include "sendq.h"
#include "shm_queue.h"
#include "shm_msg_queue.h"
#include <chrono>
int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, Array &arr)
using namespace bhome_shm;
void SendQ::AppendData(const MQInfo &mq, const Data data, const TimePoint &expire, OnMsgEvent onExpire)
{
   TimedMsg tmp(expire, MsgInfo{mq, data, std::move(onExpire)});
   std::unique_lock<std::mutex> lock(mutex_in_);
   try {
      auto &al = in_[mq.id_];
      if (!al.empty()) {
         al.front().emplace_back(std::move(tmp));
      } else {
         al.insert(al.begin(), Array())->emplace_back(std::move(tmp));
      }
   } catch (std::exception &e) {
      LOG_ERROR() << "sendq error: " << e.what();
      throw e;
   }
}
int SendQ::DoSend1Remote(ShmMsgQueue &mq, const Remote remote, Array &arr)
{
   auto FirstNotExpired = [](Array &l) {
      auto Less = [](const TimedMsg &msg, const TimePoint &tp) { return msg.expire() < tp; };
@@ -30,25 +50,20 @@
   for (auto it = arr.begin(); it != pos; ++it) {
      auto &info = it->data();
      if (info.on_expire_) {
         info.on_expire_(info.msg_);
         info.on_expire_(info.data_);
      }
      info.msg_.Release(mq.shm());
   }
   int n = mq.TrySendAll(*(MQId *) remote.data(), MsgIter(pos), MsgIter(arr.end()));
   for (int i = 0; i < n; ++i) {
      auto &msg = pos->data().msg_;
      if (msg.IsCounted()) {
         msg.Release(mq.shm());
      }
   while (pos != arr.end() && mq.TrySend(pos->data().mq_, pos->data().data_)) {
      ++pos;
   }
   int nprocessed = std::distance(arr.begin(), pos);
   arr.erase(arr.begin(), pos);
   return n;
   return nprocessed;
}
int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, ArrayList &al)
int SendQ::DoSend1Remote(ShmMsgQueue &mq, const Remote remote, ArrayList &al)
{
   int nsend = 0;
   auto AllSent = [&](Array &arr) {
@@ -59,9 +74,11 @@
   return nsend;
}
bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq)
bool SendQ::TrySend(ShmMsgQueue &mq)
{
   std::unique_lock<std::mutex> lock(mutex_out_);
   // if (TooFast()) { return false; }
   size_t nsend = 0;
   if (!out_.empty()) {
      auto rec = out_.begin();
@@ -91,4 +108,15 @@
   Collect();
   return !out_.empty();
}
}
bool SendQ::TooFast()
{
   auto cur = NowSec();
   if (cur > last_time_) {
      last_time_ = cur;
      count_ = 0;
   }
   return ++count_ > 1000 * 100;
} // not accurate in multi-thread.