diff --git a/src/Adapter.cc b/src/Adapter.cc index 347aaed..1bb6c59 100644 --- a/src/Adapter.cc +++ b/src/Adapter.cc @@ -15,66 +15,10 @@ thread_local pEp::Adapter::Session pEp::Adapter::session{}; namespace pEp { namespace Adapter { - std::thread _sync_thread; - ::utility::locked_queue sync_evt_q; - std::mutex mut; - - // private - std::thread::id sync_thread_id() - { - return _sync_thread.get_id(); - } - - // private - int _process_sync_event(::SYNC_EVENT ev, void *management) - { - if (ev != nullptr) { - ::do_sync_protocol_step(session(), nullptr, ev); - } - return 0; - } - - // public (json adapter needs it, but should use Session mgmt from libpEpAdapter eventually) - int _inject_sync_event(::SYNC_EVENT ev, void *management) - { - try { - if (ev == nullptr) { - sync_evt_q.clear(); - sync_evt_q.push_back(ev); - } else { - sync_evt_q.push_front(ev); - } - } catch (exception &) { - return 1; - } - return 0; - } - - // private - PEP_STATUS _ensure_passphrase(::PEP_SESSION session, const char *fpr) - { - return passphrase_cache.ensure_passphrase(session, fpr); - } - - // public - ::SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold) - { - ::SYNC_EVENT syncEvent = nullptr; - const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); - - if (!success) { - return ::new_sync_timeout_event(); - } - - return syncEvent; - } - - // public - bool on_sync_thread() - { - return _sync_thread.get_id() == this_thread::get_id(); - } + ::SYNC_EVENT _cb_dequeue_next_sync_event(void *management, unsigned threshold); + // --------------------------------------------------------------------------------------- + // SESSION // --------------------------------------------------------------------------------------- Session::Session() : _sync_mode{ SyncModes::Async }, _messageToSend{ nullptr }, _notifyHandshake{ nullptr }, @@ -167,6 +111,62 @@ namespace pEp { return _session.get(); } + // --------------------------------------------------------------------------------------- + // SYNC + // --------------------------------------------------------------------------------------- + std::thread _sync_thread{}; + ::utility::locked_queue sync_evt_q{}; + + // private + std::thread::id sync_thread_id() + { + return _sync_thread.get_id(); + } + + // private + int _cb_pass_sync_event_to_engine(::SYNC_EVENT ev, void *management) + { + if (ev != nullptr) { + ::do_sync_protocol_step(session(), nullptr, ev); + } + return 0; + } + + // public (json adapter needs it, but should use Session mgmt from libpEpAdapter eventually) + int _cb_enqueue_sync_event(::SYNC_EVENT ev, void *management) + { + try { + if (ev == nullptr) { + sync_evt_q.clear(); + sync_evt_q.push_back(ev); + } else { + sync_evt_q.push_front(ev); + } + } catch (...) { + return 1; + } + return 0; + } + + // public + ::SYNC_EVENT _cb_dequeue_next_sync_event(void *management, unsigned threshold) + { + ::SYNC_EVENT syncEvent = nullptr; + const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); + + if (!success) { + return ::new_sync_timeout_event(); + } + + return syncEvent; + } + + // public + bool on_sync_thread() + { + return _sync_thread.get_id() == this_thread::get_id(); + } + // public void inject_sync_shutdown() { @@ -214,6 +214,12 @@ namespace pEp { } return !ev; } + + PEP_STATUS _ensure_passphrase(::PEP_SESSION session, const char *fpr) + { + return passphrase_cache.ensure_passphrase(session, fpr); + } + } // namespace Adapter void throw_status(::PEP_STATUS status) diff --git a/src/Adapter.hh b/src/Adapter.hh index ba07308..312a145 100644 --- a/src/Adapter.hh +++ b/src/Adapter.hh @@ -20,26 +20,6 @@ namespace pEp { Async }; - int _inject_sync_event(::SYNC_EVENT ev, void *management); - int _process_sync_event(::SYNC_EVENT ev, void *management); - - ::PEP_STATUS _ensure_passphrase(::PEP_SESSION session, const char *fpr); - - void start_sync(); - - template - void startup( - T *obj = nullptr, - std::function _startup = nullptr, - std::function _shutdown = nullptr); - - // returns 'true' when called from the "sync" thread, 'false' otherwise. - bool on_sync_thread(); - - // returns the thread id of the sync thread - std::thread::id sync_thread_id(); - - // The thread-local pEp-session // CAVEAT: there is a default constructor Sesssion(), // BUT @@ -129,6 +109,26 @@ namespace pEp { extern thread_local Session session; + // --------------------------------------------------------------------------------------- + // SYNC + // --------------------------------------------------------------------------------------- + int _cb_enqueue_sync_event(::SYNC_EVENT ev, void *management); + int _cb_pass_sync_event_to_engine(::SYNC_EVENT ev, void *management); + + void start_sync(); + + template + void startup( + T *obj = nullptr, + std::function _startup = nullptr, + std::function _shutdown = nullptr); + + // returns 'true' when called from the "sync" thread, 'false' otherwise. + bool on_sync_thread(); + + // returns the thread id of the sync thread + std::thread::id sync_thread_id(); + // only injects a NULL event into sync_event_queue // Use this if adapter_manages_sync_thread // Inject first, then join your thread. @@ -140,6 +140,8 @@ namespace pEp { bool is_sync_running(); bool in_shutdown(); + + ::PEP_STATUS _ensure_passphrase(::PEP_SESSION session, const char *fpr); } // namespace Adapter // throws std::bad_alloc if status==PEP_OUT_OF_MEMORY,