From 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 19 五月 2021 19:14:13 +0800 Subject: [PATCH] add api BHQueryProcs. --- src/sendq.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 87 insertions(+), 27 deletions(-) diff --git a/src/sendq.cpp b/src/sendq.cpp index 290e9d1..f1e5918 100644 --- a/src/sendq.cpp +++ b/src/sendq.cpp @@ -16,47 +16,107 @@ * ===================================================================================== */ #include "sendq.h" -#include "shm_queue.h" +#include "shm_msg_queue.h" #include <chrono> -bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq) +using namespace bhome_shm; + +void SendQ::AppendData(const MQInfo &mq, const Data data, const TimePoint &expire, OnMsgEvent onExpire) { - auto FirstNotExpired = [](MsgList &l) { + 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; }; return std::lower_bound(l.begin(), l.end(), Now(), Less); }; - auto SendOneRemote = [&](const Remote &remote, MsgList &msg_list) { - auto pos = FirstNotExpired(msg_list); - for (auto it = msg_list.begin(); it != pos; ++it) { - auto &info = it->data(); - if (info.on_expire_) { - info.on_expire_(info.msg_); - } - info.msg_.Release(mq.shm()); + auto pos = FirstNotExpired(arr); + for (auto it = arr.begin(); it != pos; ++it) { + auto &info = it->data(); + if (info.on_expire_) { + info.on_expire_(info.data_); } + } - //TODO maybe use TrySendAll ? - while (pos != msg_list.end() && mq.TrySend(*(MQId *) remote.data(), pos->data().msg_)) { - auto &msg = pos->data().msg_; - if (msg.IsCounted()) { - msg.Release(mq.shm()); - } - ++pos; - } - msg_list.erase(msg_list.begin(), pos); + 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 nprocessed; +} + +int SendQ::DoSend1Remote(ShmMsgQueue &mq, const Remote remote, ArrayList &al) +{ + int nsend = 0; + auto AllSent = [&](Array &arr) { + nsend += DoSend1Remote(mq, remote, arr); + return arr.empty(); }; + for (auto it = al.begin(); it != al.end() && AllSent(*it); it = al.erase(it)) {} + return nsend; +} - if (!store_.empty()) { - auto rec = store_.begin(); +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(); do { - SendOneRemote(rec->first, rec->second); + nsend += DoSend1Remote(mq, rec->first, rec->second); if (rec->second.empty()) { - rec = store_.erase(rec); + rec = out_.erase(rec); } else { ++rec; } - } while (rec != store_.end()); + } while (rec != out_.end()); } - return !store_.empty(); -} \ No newline at end of file + + auto Collect = [&]() { + std::unique_lock<std::mutex> lock(mutex_in_); + if (out_.empty()) { + out_.swap(in_); + } else if (nsend == 0) { // remote blocked + for (auto &kv : in_) { + auto &to = out_[kv.first]; + to.splice(to.end(), kv.second); + } + in_.clear(); + } + }; + + 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. \ No newline at end of file -- Gitblit v1.8.0