From 58d904a328c0d849769b483e901a0be9426b8209 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 20 七月 2021 20:20:44 +0800
Subject: [PATCH] 调整Request C.BHFree的位置

---
 src/sendq.cpp |   71 ++++++++++++++++++-----------------
 1 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/src/sendq.cpp b/src/sendq.cpp
index 3f988ca..2a772b0 100644
--- a/src/sendq.cpp
+++ b/src/sendq.cpp
@@ -16,27 +16,32 @@
  * =====================================================================================
  */
 #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;
 
+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; };
 		return std::lower_bound(l.begin(), l.end(), Now(), Less);
@@ -46,42 +51,39 @@
 	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());
 	}
-
-	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 TrySend1 = [this](MsgInfo const &info) { return ShmMsgQueue::TrySend(shm_, info.mq_, info.data_); };
+	while (pos != arr.end() && TrySend1(pos->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(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 {
@@ -89,9 +91,10 @@
 			}
 		} while (rec != out_.end());
 	}
+	count_out_.Count(nsend);
 
 	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
@@ -106,4 +109,4 @@
 	Collect();
 
 	return !out_.empty();
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0