Browse Source

rewrite session creation function _new() / register all callbacks at session creation

remove the possibility to change SYNC/ASYNC handling of sync events at runtime.

It has to be defined at Session::initialize() and cannot be changed anymore. This is not needed.
pull/15/head
heck 2 years ago
parent
commit
d4bf1c1a5d
  1. 40
      src/Adapter.cc
  2. 42
      src/Adapter.hh

40
src/Adapter.cc

@ -20,9 +20,15 @@ namespace pEp {
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// SESSION // SESSION
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
Session::Session() : std::mutex mut{};
_sync_mode{ SyncModes::Async }, _messageToSend{ nullptr }, _notifyHandshake{ nullptr }, SyncModes Session::_sync_mode{ SyncModes::Async };
_adapter_manages_sync_thread{ false }, _inject_action{ nullptr } bool Session::_adapter_manages_sync_thread{ false };
::messageToSend_t Session::_cb_messageToSend{ nullptr };
::notifyHandshake_t Session::_cb_notifyHandshake{ nullptr };
::inject_sync_event_t Session::_cb_handle_sync_event_from_engine{ nullptr };
bool Session::_is_initialized{ false };
Session::Session()
{ {
pEpLog("libpEpAdapter Session-manager created"); pEpLog("libpEpAdapter Session-manager created");
} }
@ -61,37 +67,35 @@ namespace pEp {
_cb_notifyHandshake = notifyHandshake; _cb_notifyHandshake = notifyHandshake;
_sync_mode = sync_mode; _sync_mode = sync_mode;
_adapter_manages_sync_thread = adapter_manages_sync_thread; _adapter_manages_sync_thread = adapter_manages_sync_thread;
refresh();
::adapter_group_init(); ::adapter_group_init();
_is_initialized = true;
} }
void Session::_new() void Session::_new()
{ {
std::lock_guard<mutex> lock(mut); std::lock_guard<mutex> lock(mut);
// Switch to mode "Sync" ensures the sync thread to be shutdown
if (_sync_mode == SyncModes::Sync) { if (_sync_mode == SyncModes::Sync) {
// process the event directly _cb_handle_sync_event_from_engine = _cb_pass_sync_event_to_engine;
_inject_action = _process_sync_event;
if (!_adapter_manages_sync_thread) {
stop_sync();
} else {
// The adapter needs to shutdown sync thread
}
} }
// Switch to mode "ASync", sync thread needs to be started using start_sync
if (_sync_mode == SyncModes::Async) { if (_sync_mode == SyncModes::Async) {
// put the event on queue _cb_handle_sync_event_from_engine = _cb_enqueue_sync_event;
_inject_action = _inject_sync_event;
} }
// create // create
::PEP_SESSION session_{ nullptr }; ::PEP_SESSION session_{ nullptr };
::PEP_STATUS status = ::init(&session_, _messageToSend, _inject_action, _ensure_passphrase); ::PEP_STATUS status = ::init(&session_, _cb_messageToSend,
_cb_handle_sync_event_from_engine, _ensure_passphrase);
throw_status(status);
status = ::register_sync_callbacks(
session_,
nullptr,
_cb_notifyHandshake,
_cb_dequeue_next_sync_event);
throw_status(status); throw_status(status);
// replace "atomically"
release();
_session = SessionPtr{ session_, ::release }; _session = SessionPtr{ session_, ::release };
} }

42
src/Adapter.hh

@ -55,56 +55,50 @@ namespace pEp {
// bool adapter_manages_sync_thread: // bool adapter_manages_sync_thread:
// * true: libpEpAdapter will not manage the sync thread, the adapter impl will have to implement it. // * true: libpEpAdapter will not manage the sync thread, the adapter impl will have to implement it.
// * false: libpEpAdapter will manage the sync thread // * false: libpEpAdapter will manage the sync thread
// static void initialize(
// TODO:
// CAUTION: This call may result in a partially initialized session,
// If there are any problems with register_sync_callbacks()
// (e.g. due to no own identities yet)
// it will still succeed.
// BUT
// * Sync will not work
// * Group Encryption will not work
// TODO: This needs to be resolved in the engine, by creating a
// new func register_callbacks() that is not sync specific,
// and move the sync-checks to "start-sync()".
// Current workaround: a warning is printed out in this case
void initialize(
SyncModes sync_mode = SyncModes::Async, SyncModes sync_mode = SyncModes::Async,
bool adapter_manages_sync_thread = false); bool adapter_manages_sync_thread = false);
// initialize() // initialize()
// Same as the initialize() method above, but you can specify arbitrary callbacks // Same as the initialize() method above, but you can specify arbitrary callbacks
// to be registered. // to be registered.
void initialize( static void initialize(
SyncModes sync_mode, SyncModes sync_mode,
bool adapter_manages_sync_thread, bool adapter_manages_sync_thread,
::messageToSend_t messageToSend, ::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake); ::notifyHandshake_t notifyHandshake);
// re-creates the session using same values
void refresh();
void release(); void release();
// returns the PEP_SESSION handle // returns the PEP_SESSION handle
PEP_SESSION operator()(); PEP_SESSION operator()();
SyncModes _sync_mode; bool adapter_manages_sync_thread()
::messageToSend_t _messageToSend; {
::notifyHandshake_t _notifyHandshake; return _adapter_manages_sync_thread;
bool _adapter_manages_sync_thread; }
::inject_sync_event_t _inject_action;
private: private:
using SessionPtr = std::unique_ptr<_pEpSession, std::function<void(PEP_SESSION)>>; using SessionPtr = std::unique_ptr<_pEpSession, std::function<void(PEP_SESSION)>>;
void _init( static void _init(
::messageToSend_t messageToSend, ::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake, ::notifyHandshake_t notifyHandshake,
SyncModes sync_mode, SyncModes sync_mode,
bool adapter_manages_sync_thread); bool adapter_manages_sync_thread);
// creates the session
void _new();
SessionPtr _session = nullptr; SessionPtr _session = nullptr;
static SyncModes _sync_mode;
static bool _adapter_manages_sync_thread;
static ::messageToSend_t _cb_messageToSend;
static ::notifyHandshake_t _cb_notifyHandshake;
static ::inject_sync_event_t _cb_handle_sync_event_from_engine;
static bool _is_initialized;
}; };
extern thread_local Session session; extern thread_local Session session;

Loading…
Cancel
Save