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() :
_sync_mode{ SyncModes::Async }, _messageToSend{ nullptr }, _notifyHandshake{ nullptr },
_adapter_manages_sync_thread{ false }, _inject_action{ nullptr }
std::mutex mut{};
SyncModes Session::_sync_mode{ SyncModes::Async };
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");
}
@ -61,37 +67,35 @@ namespace pEp {
_cb_notifyHandshake = notifyHandshake;
_sync_mode = sync_mode;
_adapter_manages_sync_thread = adapter_manages_sync_thread;
refresh();
::adapter_group_init();
_is_initialized = true;
}
void Session::_new()
{
std::lock_guard<mutex> lock(mut);
// Switch to mode "Sync" ensures the sync thread to be shutdown
if (_sync_mode == SyncModes::Sync) {
// process the event directly
_inject_action = _process_sync_event;
if (!_adapter_manages_sync_thread) {
stop_sync();
} else {
// The adapter needs to shutdown sync thread
}
_cb_handle_sync_event_from_engine = _cb_pass_sync_event_to_engine;
}
// Switch to mode "ASync", sync thread needs to be started using start_sync
if (_sync_mode == SyncModes::Async) {
// put the event on queue
_inject_action = _inject_sync_event;
_cb_handle_sync_event_from_engine = _cb_enqueue_sync_event;
}
// create
::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);
// replace "atomically"
release();
_session = SessionPtr{ session_, ::release };
}

42
src/Adapter.hh

@ -55,56 +55,50 @@ namespace pEp {
// bool adapter_manages_sync_thread:
// * true: libpEpAdapter will not manage the sync thread, the adapter impl will have to implement it.
// * false: libpEpAdapter will manage the sync thread
//
// 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(
static void initialize(
SyncModes sync_mode = SyncModes::Async,
bool adapter_manages_sync_thread = false);
// initialize()
// Same as the initialize() method above, but you can specify arbitrary callbacks
// to be registered.
void initialize(
static void initialize(
SyncModes sync_mode,
bool adapter_manages_sync_thread,
::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake);
// re-creates the session using same values
void refresh();
void release();
// returns the PEP_SESSION handle
PEP_SESSION operator()();
SyncModes _sync_mode;
::messageToSend_t _messageToSend;
::notifyHandshake_t _notifyHandshake;
bool _adapter_manages_sync_thread;
::inject_sync_event_t _inject_action;
bool adapter_manages_sync_thread()
{
return _adapter_manages_sync_thread;
}
private:
using SessionPtr = std::unique_ptr<_pEpSession, std::function<void(PEP_SESSION)>>;
void _init(
static void _init(
::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake,
SyncModes sync_mode,
bool adapter_manages_sync_thread);
// creates the session
void _new();
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;

Loading…
Cancel
Save