From c0d61292ef6bdd8e2e93525b5df6a8ec5c017c32 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 14 五月 2021 14:09:27 +0800
Subject: [PATCH] change refcount, avoid ShmMsgQueue AddRef on send.

---
 box/center.cpp      |    3 +--
 src/shm_msg_queue.h |    9 +--------
 src/sendq.cpp       |   22 ++--------------------
 3 files changed, 4 insertions(+), 30 deletions(-)

diff --git a/box/center.cpp b/box/center.cpp
index b440a03..445d307 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -270,8 +270,7 @@
 	bool SendAllocMsg(ShmSocket &socket, const Address dest, const MsgI &msg)
 	{
 		RecordMsg(msg);
-		auto onExpireFree = [this, msg](const SendQ::Data &) { msgs_.FreeMsg(msg.id()); };
-		return socket.Send(dest, msg, onExpireFree);
+		return socket.Send(dest, msg);
 	}
 
 	void OnAlloc(ShmSocket &socket, const int64_t val)
diff --git a/src/sendq.cpp b/src/sendq.cpp
index 36af264..94e315e 100644
--- a/src/sendq.cpp
+++ b/src/sendq.cpp
@@ -40,26 +40,8 @@
 	}
 
 	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;
+		auto TrySend = [&](auto &&data) { return mq.TrySend(remote, data); };
+		return boost::variant2::visit(TrySend, pos->data().data_);
 	};
 
 	while (pos != arr.end() && SendData(pos->data().data_)) {
diff --git a/src/shm_msg_queue.h b/src/shm_msg_queue.h
index 4b7aed8..56ea076 100644
--- a/src/shm_msg_queue.h
+++ b/src/shm_msg_queue.h
@@ -75,14 +75,7 @@
 	bool TryRecv(MsgI &msg) { return TryRecv(msg.OffsetRef()); }
 	static Queue *Find(ShmType &shm, const MQId remote_id);
 	static bool TrySend(ShmType &shm, const MQId remote_id, const RawData val);
-	static bool TrySend(ShmType &shm, const MQId remote_id, MsgI msg)
-	{
-		bool r = false;
-		msg.AddRef(); // TODO check if we could avoid addref here.
-		DEFER1(if (!r) { msg.Release(); });
-		r = TrySend(shm, remote_id, msg.Offset());
-		return r;
-	}
+	static bool TrySend(ShmType &shm, const MQId remote_id, MsgI msg) { return TrySend(shm, remote_id, msg.Offset()); }
 	bool TrySend(const MQId remote_id, const MsgI &msg) { return TrySend(shm(), remote_id, msg); }
 	bool TrySend(const MQId remote_id, const RawData val) { return TrySend(shm(), remote_id, val); }
 

--
Gitblit v1.8.0