lichao
2021-05-10 77a6c3512a44dfe6540dde71946e6484fe4f173f
src/sendq.cpp
@@ -16,27 +16,13 @@
 * =====================================================================================
 */
#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)
{
   static size_t total = 0;
   static size_t count = 0;
   static size_t max_len = 0;
   static time_t last = 0;
   ++count;
   total += arr.size();
   if (arr.size() > max_len) {
      max_len = arr.size();
   }
   time_t now;
   time(&now);
   if (now > last && count > 0) {
      last = now;
      printf("avg size : %ld, max size: %ld\n", total / count, max_len);
   }
using namespace bhome_shm;
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; };
      return std::lower_bound(l.begin(), l.end(), Now(), Less);
@@ -46,25 +32,42 @@
   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());
      if (info.data_.index() == 0) {
         boost::variant2::get<0>(info.data_).Release();
      }
   }
   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());
   auto SendData = [&](Data &d) {
      bool r = false;
      if (d.index() == 0) {
         auto &msg = boost::variant2::get<0>(pos->data().data_);
         r = mq.TrySend(remote, msg);
         if (r) {
            msg.Release();
         }
      } else {
         auto &content = boost::variant2::get<1>(pos->data().data_);
         MsgI msg;
         if (msg.Make(content)) {
            DEFER1(msg.Release(););
            r = mq.TrySend(remote, msg);
         }
      }
      return r;
   };
   while (pos != arr.end() && SendData(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) {
@@ -75,8 +78,9 @@
   return nsend;
}
bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq)
bool SendQ::TrySend(ShmMsgQueue &mq)
{
   std::unique_lock<std::mutex> lock(mutex_out_);
   size_t nsend = 0;
   if (!out_.empty()) {
      auto rec = out_.begin();
@@ -91,7 +95,7 @@
   }
   auto Collect = [&]() {
      std::unique_lock<std::mutex> lock(mutex_);
      std::unique_lock<std::mutex> lock(mutex_in_);
      if (out_.empty()) {
         out_.swap(in_);
      } else if (nsend == 0) { // remote blocked