From 451e049b833ab22122f56b5ec4ac7a6553da1e5a Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Thu, 4 Oct 2018 13:38:25 +0200 Subject: [PATCH] sync thread implementation --- Adapter.cc | 24 +++++++++++++++++++----- Adapter.hh | 6 ++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Adapter.cc b/Adapter.cc index e9fd588..101b796 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -27,9 +27,10 @@ namespace pEp { messageToSend_t Adapter::_messageToSend = nullptr; notifyHandshake_t Adapter::_notifyHandshake = nullptr; + std::thread *_sync_thread = nullptr; Adapter::Adapter(messageToSend_t messageToSend, - notifyHandshake_t notifyHandshake, bool is_sync_thread) + notifyHandshake_t notifyHandshake) { if (messageToSend) _messageToSend = messageToSend; @@ -39,10 +40,11 @@ namespace pEp { PEP_SESSION _session = session(); - if (is_sync_thread) { - PEP_STATUS status = register_sync_callbacks(_session, nullptr, - notifyHandshake, _retrieve_next_sync_event); - throw_status(status); + { + lock_guard lock(mtx()); + + if (!_sync_thread) + _sync_thread = new thread(); } } @@ -115,5 +117,17 @@ namespace pEp { return queue().pop_front(); } + + void Adapter::sync_thread() + { + PEP_STATUS status = register_sync_callbacks(session(), nullptr, + _notifyHandshake, _retrieve_next_sync_event); + throw_status(status); + + do_sync_protocol(session(), nullptr); + unregister_sync_callbacks(session()); + + session(release); + } } diff --git a/Adapter.hh b/Adapter.hh index 28ebe28..5919a97 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -4,6 +4,7 @@ #pragma once #include "locked_queue.hh" +#include #include namespace pEp { @@ -12,11 +13,11 @@ namespace pEp { class Adapter { static messageToSend_t _messageToSend; static notifyHandshake_t _notifyHandshake; + static std::thread *_sync_thread; public: Adapter(messageToSend_t messageToSend, - notifyHandshake_t notifyHandshake, - bool is_sync_thread = false); + notifyHandshake_t notifyHandshake); virtual ~Adapter() { } enum session_action { @@ -34,6 +35,7 @@ namespace pEp { protected: static int _inject_sync_event(SYNC_EVENT ev, void *management); static SYNC_EVENT _retrieve_next_sync_event(void *management, time_t threshold); + static void sync_thread(); private: static std::mutex& mtx()