Browse Source

just cosmetics (less error prone to read, makes you think about the missing else case)

pull/5/head
heck 1 year ago
parent
commit
b0a57da9fa
4 changed files with 58 additions and 31 deletions
  1. +7
    -4
      Adapter.cc
  2. +12
    -6
      Adapter.hxx
  3. +23
    -11
      callback_dispatcher.cc
  4. +16
    -10
      callback_dispatcher.hh

+ 7
- 4
Adapter.cc View File

@ -78,8 +78,9 @@ namespace pEp {
SYNC_EVENT syncEvent = nullptr;
const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
if (!success)
if (!success) {
return new_sync_timeout_event();
}
return syncEvent;
}
@ -98,8 +99,9 @@ namespace pEp {
switch (action) {
case release:
if (_session.get())
if (_session.get()) {
_session = nullptr;
}
break;
case init:
@ -143,10 +145,11 @@ namespace pEp {
catch (std::underflow_error&) {
return false;
}
if (ev)
if (ev) {
return false;
else
} else {
return true;
}
}
}
}

+ 12
- 6
Adapter.hxx View File

@ -30,8 +30,9 @@ namespace pEp {
_ex = nullptr;
assert(_messageToSend);
assert(_notifyHandshake);
if (obj && _startup)
if (obj && _startup) {
_startup(obj);
}
session();
@ -56,8 +57,9 @@ namespace pEp {
session(release);
if (obj && _shutdown)
if (obj && _shutdown) {
_shutdown(obj);
}
}
template< class T >
@ -68,22 +70,26 @@ namespace pEp {
function< void(T *) > _startup,
function< void(T *) > _shutdown)
{
if (messageToSend)
if (messageToSend) {
_messageToSend = messageToSend;
}
if (notifyHandshake)
if (notifyHandshake) {
_notifyHandshake = notifyHandshake;
}
session();
if (!_sync_thread.joinable()) {
register_done = false;
_sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
while (!register_done)
while (!register_done) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (_ex)
if (_ex) {
std::rethrow_exception(_ex);
}
}
}
}


+ 23
- 11
callback_dispatcher.cc View File

@ -25,8 +25,9 @@ namespace pEp {
)
{
assert(messageToSend);
if (!messageToSend)
if (!messageToSend) {
throw std::invalid_argument("messageToSend must be set");
}
targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown});
}
@ -34,8 +35,9 @@ namespace pEp {
void CallbackDispatcher::remove(::messageToSend_t messageToSend)
{
assert(messageToSend);
if (!messageToSend)
if (!messageToSend) {
throw std::invalid_argument("messageToSend argument needed");
}
for (auto target = targets.begin(); target != targets.end(); ++target) {
if (target->messageToSend == messageToSend) {
@ -43,23 +45,27 @@ namespace pEp {
break;
}
}
if (targets.empty())
if (targets.empty()) {
stop_sync();
}
}
void CallbackDispatcher::on_startup()
{
for (auto target : targets) {
if (target.on_startup)
if (target.on_startup) {
target.on_startup();
}
}
}
void CallbackDispatcher::on_shutdown()
{
for (auto target : targets) {
if (target.on_shutdown)
if (target.on_shutdown) {
target.on_shutdown();
}
}
}
@ -73,8 +79,9 @@ namespace pEp {
&CallbackDispatcher::on_shutdown);
for (auto target : callback_dispatcher.targets) {
if (target.notifyHandshake)
if (target.notifyHandshake) {
target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_START);
}
}
}
@ -85,8 +92,9 @@ namespace pEp {
callback_dispatcher.semaphore.go();
for (auto target : callback_dispatcher.targets) {
if (target.notifyHandshake)
if (target.notifyHandshake) {
target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_STOP);
}
}
}
@ -95,14 +103,16 @@ namespace pEp {
if (Adapter::on_sync_thread() && !msg) {
semaphore.try_wait();
if (Adapter::in_shutdown())
if (Adapter::in_shutdown()) {
return PEP_SYNC_NO_CHANNEL;
}
PEP_STATUS status = PassphraseCache::config_next_passphrase();
// if the cache has no valid passphrase ask the app
if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE)
if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) {
semaphore.stop();
}
// the pEp engine must try again
return status;
@ -117,8 +127,9 @@ namespace pEp {
::message *_msg = nullptr;
if (msg) {
_msg = ::message_dup(msg);
if (!_msg)
if (!_msg) {
return PEP_OUT_OF_MEMORY;
}
}
assert(target.messageToSend);
target.messageToSend(_msg);
@ -135,8 +146,9 @@ namespace pEp {
::pEp_identity *_me = nullptr;
if (me) {
_me = ::identity_dup(me);
if (!_me)
if (!_me) {
return PEP_OUT_OF_MEMORY;
}
}
::pEp_identity *_partner = nullptr;
if (partner) {


+ 16
- 10
callback_dispatcher.hh View File

@ -27,30 +27,36 @@ namespace pEp {
public:
void add(
::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake,
proc on_startup = nullptr,
proc on_shutdown = nullptr
);
::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake,
proc on_startup = nullptr,
proc on_shutdown = nullptr
);
void remove(::messageToSend_t messageToSend);
static void start_sync();
static void stop_sync();
static PEP_STATUS messageToSend(::message *msg);
static PEP_STATUS notifyHandshake(::pEp_identity *me,
::pEp_identity *partner, ::sync_handshake_signal signal);
static PEP_STATUS notifyHandshake(
::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
protected:
void on_startup();
void on_shutdown();
PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS _notifyHandshake(::pEp_identity *me,
::pEp_identity *partner, ::sync_handshake_signal signal);
PEP_STATUS _notifyHandshake(
::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
friend const char *PassphraseCache::add(const std::string& passphrase);
};
extern CallbackDispatcher callback_dispatcher;
};
}

Loading…
Cancel
Save