You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libpEpAdapter/Adapter.hxx

91 lines
2.5 KiB
C++

// This file is under GNU General Public License 3.0
// see LICENSE.txt
5 years ago
#pragma once
#include <thread>
#include "locked_queue.hh"
#include <cassert>
#include "pEpLog.hh"
5 years ago
namespace pEp {
namespace Adapter {
using std::function;
5 years ago
extern messageToSend_t _messageToSend;
extern notifyHandshake_t _notifyHandshake;
extern std::thread _sync_thread;
5 years ago
extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
extern std::mutex m;
5 years ago
5 years ago
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold);
5 years ago
4 years ago
static std::exception_ptr _ex;
static bool register_done = false;
template< class T >
void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
5 years ago
{
_ex = nullptr;
4 years ago
assert(_messageToSend);
assert(_notifyHandshake);
if (obj && _startup)
_startup(obj);
4 years ago
session();
{
PEP_STATUS status = register_sync_callbacks(session(), nullptr,
_notifyHandshake, _retrieve_next_sync_event);
try {
throw_status(status);
4 years ago
register_done = true;
}
catch (...) {
_ex = std::current_exception();
4 years ago
register_done = true;
return;
}
}
5 years ago
pEpLog("sync protocol loop started");
5 years ago
do_sync_protocol(session(), (void *)obj);
pEpLog("sync protocol loop ended");
5 years ago
unregister_sync_callbacks(session());
session(release);
if (obj && _shutdown)
_shutdown(obj);
}
template< class T >
void startup(
messageToSend_t messageToSend,
notifyHandshake_t notifyHandshake,
T *obj,
5 years ago
function< void(T *) > _startup,
function< void(T *) > _shutdown)
5 years ago
{
if (messageToSend)
_messageToSend = messageToSend;
if (notifyHandshake)
_notifyHandshake = notifyHandshake;
session();
if (!_sync_thread.joinable()) {
4 years ago
register_done = false;
_sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
4 years ago
while (!register_done)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (_ex)
4 years ago
std::rethrow_exception(_ex);
5 years ago
}
}
}
}