| | |
| | | * ===================================================================================== |
| | | */ |
| | | #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)); |
| | | } |
| | | count_in_.Count1(); |
| | | } catch (std::exception &e) { |
| | | LOG_ERROR() << "sendq error: " << e.what(); |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | int SendQ::DoSend1Remote(const Remote remote, Array &arr) |
| | | { |
| | | auto FirstNotExpired = [](Array &l) { |
| | | auto Less = [](const TimedMsg &msg, const TimePoint &tp) { return msg.expire() < tp; }; |
| | |
| | | if (info.on_expire_) { |
| | | info.on_expire_(info.data_); |
| | | } |
| | | if (info.data_.index() == 0) { |
| | | boost::variant2::get<0>(info.data_).Release(); |
| | | } |
| | | } |
| | | |
| | | 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_)) { |
| | | auto TrySend1 = [this](MsgInfo const &info) { return ShmMsgQueue::TrySend(shm_, info.mq_, info.data_); }; |
| | | while (pos != arr.end() && TrySend1(pos->data())) { |
| | | ++pos; |
| | | } |
| | | |
| | |
| | | return nprocessed; |
| | | } |
| | | |
| | | int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote remote, ArrayList &al) |
| | | int SendQ::DoSend1Remote(const Remote remote, ArrayList &al) |
| | | { |
| | | int nsend = 0; |
| | | auto AllSent = [&](Array &arr) { |
| | | nsend += DoSend1Remote(mq, remote, arr); |
| | | nsend += DoSend1Remote(remote, arr); |
| | | return arr.empty(); |
| | | }; |
| | | for (auto it = al.begin(); it != al.end() && AllSent(*it); it = al.erase(it)) {} |
| | | return nsend; |
| | | } |
| | | |
| | | bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq) |
| | | bool SendQ::TrySend() |
| | | { |
| | | std::unique_lock<std::mutex> lock(mutex_out_); |
| | | |
| | | size_t nsend = 0; |
| | | if (!out_.empty()) { |
| | | auto rec = out_.begin(); |
| | | do { |
| | | nsend += DoSend1Remote(mq, rec->first, rec->second); |
| | | nsend += DoSend1Remote(rec->first, rec->second); |
| | | if (rec->second.empty()) { |
| | | rec = out_.erase(rec); |
| | | } else { |
| | |
| | | } |
| | | } while (rec != out_.end()); |
| | | } |
| | | count_out_.Count(nsend); |
| | | |
| | | auto Collect = [&]() { |
| | | std::unique_lock<std::mutex> lock(mutex_in_); |
| | |
| | | Collect(); |
| | | |
| | | return !out_.empty(); |
| | | } |
| | | } |