From ad4f3dcedab29a690c5eedbb08ba1b393917db0b Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期三, 21 四月 2021 17:39:34 +0800
Subject: [PATCH] update go api.

---
 src/sendq.cpp |   54 +++++++++++++++++++++++++++++-------------------------
 1 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/sendq.cpp b/src/sendq.cpp
index 3f988ca..4be24f1 100644
--- a/src/sendq.cpp
+++ b/src/sendq.cpp
@@ -21,22 +21,6 @@
 
 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);
-	}
-
 	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,22 +30,41 @@
 	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(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());
+	auto SendData = [&](Data &d) {
+		bool r = false;
+		if (d.index() == 0) {
+			auto &msg = boost::variant2::get<0>(pos->data().data_);
+			r = mq.TrySend(*(MQId *) remote.data(), msg);
+			if (r && msg.IsCounted()) {
+				msg.Release(mq.shm());
+			}
+		} else {
+			auto &content = boost::variant2::get<1>(pos->data().data_);
+			MsgI msg;
+			if (msg.Make(mq.shm(), content)) {
+				r = mq.TrySend(*(MQId *) remote.data(), msg);
+				if (!r || msg.IsCounted()) {
+					msg.Release(mq.shm());
+				}
+			}
 		}
+		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)
@@ -77,6 +80,7 @@
 
 bool SendQ::TrySend(bhome_shm::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

--
Gitblit v1.8.0