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 |   59 ++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/src/sendq.cpp b/src/sendq.cpp
index 36af264..f1e5918 100644
--- a/src/sendq.cpp
+++ b/src/sendq.cpp
@@ -21,6 +21,24 @@
 
 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) {
@@ -34,35 +52,9 @@
 		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) {
-		auto TryLoop = [&](auto &&data) {
-			for (int i = 0; i < 1; ++i) {
-				if (mq.TrySend(remote, data)) {
-					return true;
-				}
-			}
-			return false;
-		};
-		bool r = false;
-		if (d.index() == 0) {
-			auto &msg = boost::variant2::get<0>(pos->data().data_);
-			r = TryLoop(msg);
-			if (r) {
-				msg.Release();
-			}
-		} else {
-			auto command = boost::variant2::get<1>(pos->data().data_);
-			r = TryLoop(command);
-		}
-		return r;
-	};
-
-	while (pos != arr.end() && SendData(pos->data().data_)) {
+	while (pos != arr.end() && mq.TrySend(pos->data().mq_, pos->data().data_)) {
 		++pos;
 	}
 
@@ -85,6 +77,8 @@
 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();
@@ -115,3 +109,14 @@
 
 	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