From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 10 五月 2021 16:05:28 +0800
Subject: [PATCH] test lock code.

---
 src/sendq.h |   68 ++++++++++++++++++---------------
 1 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/src/sendq.h b/src/sendq.h
index 92e566b..0e565d5 100644
--- a/src/sendq.h
+++ b/src/sendq.h
@@ -21,69 +21,75 @@
 #include "defs.h"
 #include "msg.h"
 #include "timed_queue.h"
+#include <boost/variant2/variant.hpp>
 #include <deque>
 #include <functional>
+#include <list>
 #include <mutex>
 #include <string>
 #include <unordered_map>
 
-namespace bhome_shm
-{
 class ShmMsgQueue;
-} // namespace bhome_shm
 
 class SendQ
 {
 public:
-	typedef std::string Remote;
+	typedef MQId Remote;
 	typedef bhome_msg::MsgI MsgI;
-	typedef std::function<void(const MsgI &msg)> OnMsgEvent;
+	typedef std::string Content;
+	typedef boost::variant2::variant<MsgI, Content> Data;
+	typedef std::function<void(const Data &)> OnMsgEvent;
 	struct MsgInfo {
-		MsgI msg_;
+		Data data_;
 		OnMsgEvent on_expire_;
-		// OnMsgEvent on_send_;
 	};
 	typedef TimedData<MsgInfo> TimedMsg;
 	typedef TimedMsg::TimePoint TimePoint;
 	typedef TimedMsg::Duration Duration;
 
-	void Append(const MQId &id, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
+	// template <class... Rest>
+	// void Append(const MQId &id, Rest &&...rest)
+	// {
+	// 	Append(std::string((const char *) &id, sizeof(id)), std::forward<decltype(rest)>(rest)...);
+	// }
+
+	void Append(const Remote addr, const MsgI msg, OnMsgEvent onExpire = OnMsgEvent())
 	{
-		Append(std::string((const char *) &id, sizeof(id)), msg, onExpire);
+		msg.AddRef();
+		AppendData(addr, Data(msg), DefaultExpire(), onExpire);
 	}
-	void Append(const Remote &addr, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
+	void Append(const Remote addr, Content &&content, OnMsgEvent onExpire = OnMsgEvent())
 	{
-		using namespace std::chrono_literals;
-		Append(addr, msg, Now() + 60s, onExpire);
+		AppendData(addr, Data(std::move(content)), DefaultExpire(), onExpire);
 	}
-	bool TrySend(bhome_shm::ShmMsgQueue &mq);
+	bool TrySend(ShmMsgQueue &mq);
 	// bool empty() const { return store_.empty(); }
 
 private:
 	static TimePoint Now() { return TimedMsg::Clock::now(); }
-	void Append(const Remote &addr, const MsgI &msg, const TimePoint &expire, OnMsgEvent onExpire)
+	static TimePoint DefaultExpire() { return Now() + std::chrono::seconds(60); }
+	void AppendData(const Remote addr, Data &&data, const TimePoint &expire, OnMsgEvent onExpire)
 	{
 		//TODO simple queue, organize later ?
 
-		msg.AddRef();
-		TimedMsg tmp(expire, MsgInfo{msg, onExpire});
-		std::unique_lock<std::mutex> lock(mutex_);
-		in_[addr].emplace_back(std::move(tmp));
+		TimedMsg tmp(expire, MsgInfo{std::move(data), std::move(onExpire)});
+		std::unique_lock<std::mutex> lock(mutex_in_);
+		auto &al = in_[addr];
+		if (!al.empty()) {
+			al.front().emplace_back(std::move(tmp));
+		} else {
+			al.insert(al.begin(), Array())->emplace_back(std::move(tmp));
+		}
 	}
-	typedef std::deque<TimedMsg> MsgList;
-	typedef std::unordered_map<Remote, MsgList> Store;
-	class MsgIter
-	{
-		MsgList::iterator iter_;
+	typedef std::deque<TimedMsg> Array;
+	typedef std::list<Array> ArrayList;
+	typedef std::unordered_map<Remote, ArrayList> Store;
 
-	public:
-		MsgIter(MsgList::iterator iter) :
-		    iter_(iter) {}
-		MsgIter &operator++() { return ++iter_, *this; }
-		bool operator==(const MsgIter &a) { return iter_ == a.iter_; }
-		MsgI &operator*() { return iter_->data().msg_; }
-	};
-	std::mutex mutex_;
+	int DoSend1Remote(ShmMsgQueue &mq, const Remote remote, Array &arr);
+	int DoSend1Remote(ShmMsgQueue &mq, const Remote remote, ArrayList &arr);
+
+	std::mutex mutex_in_;
+	std::mutex mutex_out_;
 	Store in_;
 	Store out_;
 };

--
Gitblit v1.8.0