From a28d1daed8d89fceaa014a244a506d8092d6ca7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Roman=20Smr=C5=BE?= <roman.smrz@seznam.cz>
Date: Sat, 3 Feb 2024 11:17:13 +0100
Subject: Message: separate thread for peer synchronization

---
 include/erebos/message.h | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

(limited to 'include')

diff --git a/include/erebos/message.h b/include/erebos/message.h
index a74e6a3..b52b84b 100644
--- a/include/erebos/message.h
+++ b/include/erebos/message.h
@@ -3,16 +3,21 @@
 #include <erebos/merge.h>
 #include <erebos/service.h>
 
-#include <chrono>
+#include <condition_variable>
+#include <deque>
 #include <functional>
 #include <memory>
 #include <mutex>
 #include <optional>
 #include <string>
+#include <tuple>
 
 namespace erebos {
 
+using std::condition_variable;
+using std::deque;
 using std::mutex;
+using std::tuple;
 using std::unique_ptr;
 
 class Contact;
@@ -142,7 +147,9 @@ public:
 private:
 	void updateHandler(const DirectMessageThreads &);
 	void peerWatcher(size_t, const class Peer *);
-	static void syncWithPeer(const Head<LocalState> &, const DirectMessageThread &, const Peer &);
+	void syncWithPeer(const DirectMessageThread &, const Peer &);
+	void doSyncWithPeers();
+	void doSyncWithPeer(const DirectMessageThread &, const Peer &);
 
 	const Config config;
 	const Server & server;
@@ -150,6 +157,12 @@ private:
 	vector<Stored<DirectMessageState>> prevState;
 	mutex stateMutex;
 
+	mutex peerSyncMutex;
+	condition_variable peerSyncCond;
+	bool peerSyncRun;
+	deque<tuple<DirectMessageThread, Peer>> peerSyncQueue;
+	std::thread peerSyncThread;
+
 	Watched<DirectMessageThreads> watched;
 };
 
-- 
cgit v1.2.3