|
|
@ -6,15 +6,12 @@ |
|
|
|
#include <iomanip>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "status_to_string.hh"
|
|
|
|
#include "utils.hh"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
|
|
|
namespace pEp { |
|
|
|
void throw_status(PEP_STATUS status) |
|
|
|
{ |
|
|
|
//pEpLog("called");
|
|
|
|
if (status == PEP_STATUS_OK) |
|
|
|
return; |
|
|
|
if (status >= 0x400 && status <= 0x4ff) |
|
|
@ -31,7 +28,7 @@ namespace pEp { |
|
|
|
RuntimeError::RuntimeError(const std::string& _text, PEP_STATUS _status) |
|
|
|
: std::runtime_error(_text.c_str()), text(_text), status(_status) |
|
|
|
{ |
|
|
|
pEpLog("called"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
namespace Adapter { |
|
|
@ -39,25 +36,21 @@ namespace pEp { |
|
|
|
notifyHandshake_t _notifyHandshake = nullptr; |
|
|
|
std::thread *_sync_thread = nullptr; |
|
|
|
|
|
|
|
::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_q; |
|
|
|
std::mutex mutex_global; |
|
|
|
::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; |
|
|
|
std::mutex m; |
|
|
|
|
|
|
|
int _inject_sync_event(SYNC_EVENT ev, void *management) |
|
|
|
{ |
|
|
|
pEpLog("called"); |
|
|
|
try { |
|
|
|
if (ev == nullptr) { |
|
|
|
pEpLog("SYNC_EVENT: NULL"); |
|
|
|
sync_q.clear(); |
|
|
|
sync_q.push_back(ev); |
|
|
|
q.clear(); |
|
|
|
q.push_back(ev); |
|
|
|
} |
|
|
|
else { |
|
|
|
pEpLog("SYNC_EVENT:" << ev); |
|
|
|
sync_q.push_front(ev); |
|
|
|
q.push_front(ev); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (exception&) { |
|
|
|
pEpErr("Exception"); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
if (ev == nullptr) { |
|
|
@ -68,7 +61,7 @@ namespace pEp { |
|
|
|
delete _sync_thread; |
|
|
|
_sync_thread = nullptr; |
|
|
|
pEpLog("...thread joined"); |
|
|
|
sync_q.clear(); |
|
|
|
q.clear(); |
|
|
|
} else { |
|
|
|
//FATAL
|
|
|
|
pEpLog("FATAL: sync thread not joinable/detached"); |
|
|
@ -81,22 +74,17 @@ namespace pEp { |
|
|
|
// threshold: max waiting time in seconds
|
|
|
|
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold) |
|
|
|
{ |
|
|
|
pEpLog("called"); |
|
|
|
SYNC_EVENT syncEvent = nullptr; |
|
|
|
const bool success = sync_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); |
|
|
|
const bool success = q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); |
|
|
|
|
|
|
|
if (!success) { |
|
|
|
pEpLog("timeout after [sec]: " << threshold); |
|
|
|
if (!success) |
|
|
|
return new_sync_timeout_event(); |
|
|
|
} |
|
|
|
|
|
|
|
pEpLog("returning SYNC_EVENT: " << syncEvent); |
|
|
|
return syncEvent; |
|
|
|
} |
|
|
|
|
|
|
|
bool on_sync_thread() |
|
|
|
{ |
|
|
|
//pEpLog("called");
|
|
|
|
if (_sync_thread && _sync_thread->get_id() == this_thread::get_id()) |
|
|
|
return true; |
|
|
|
else |
|
|
@ -105,8 +93,7 @@ namespace pEp { |
|
|
|
|
|
|
|
PEP_SESSION session(session_action action) |
|
|
|
{ |
|
|
|
pEpLog("called"); |
|
|
|
std::lock_guard<mutex> lock(mutex_global); |
|
|
|
std::lock_guard<mutex> lock(m); |
|
|
|
bool in_sync = on_sync_thread(); |
|
|
|
|
|
|
|
thread_local static PEP_SESSION _session = nullptr; |
|
|
@ -115,19 +102,14 @@ namespace pEp { |
|
|
|
switch (action) { |
|
|
|
case release: |
|
|
|
if (_session) { |
|
|
|
pEpLog("action = release: releasing session: " << _session); |
|
|
|
::release(_session); |
|
|
|
_session = nullptr; |
|
|
|
} else { |
|
|
|
pEpLog("action = release: No session to release"); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case init: |
|
|
|
if (!_session) { |
|
|
|
pEpLog("action = init: creating new session"); |
|
|
|
if (!_session) |
|
|
|
status = ::init(&_session, _messageToSend, _inject_sync_event); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
@ -135,7 +117,6 @@ namespace pEp { |
|
|
|
} |
|
|
|
|
|
|
|
throw_status(status); |
|
|
|
pEpLog("returning session: " << _session); |
|
|
|
return _session; |
|
|
|
} |
|
|
|
|
|
|
@ -150,7 +131,6 @@ namespace pEp { |
|
|
|
|
|
|
|
bool is_sync_running() |
|
|
|
{ |
|
|
|
pEpLog("called"); |
|
|
|
return _sync_thread != nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|