Volker Birk 4 years ago
parent 6bbe1edff7
commit 89ca327f03

@ -32,11 +32,12 @@ namespace pEp {
}
namespace Adapter {
static messageToSend_t _messageToSend = nullptr;
static notifyHandshake_t _notifyHandshake = nullptr;
static std::thread *_sync_thread = nullptr;
static ::utility::locked_queue< SYNC_EVENT > q;
static std::mutex m;
messageToSend_t _messageToSend = nullptr;
notifyHandshake_t _notifyHandshake = nullptr;
std::thread *_sync_thread = nullptr;
::utility::locked_queue< SYNC_EVENT > q;
std::mutex m;
static int _inject_sync_event(SYNC_EVENT ev, void *management)
{
@ -54,7 +55,7 @@ namespace pEp {
return 0;
}
static SYNC_EVENT _retrieve_next_sync_event(void *management, time_t threshold)
SYNC_EVENT _retrieve_next_sync_event(void *management, time_t threshold)
{
time_t started = time(nullptr);
bool timeout = false;
@ -83,46 +84,6 @@ namespace pEp {
return q.pop_front();
}
template< class T > static void sync_thread(T *obj, function< void (T *) > _startup, function< void (T *) > _shutdown)
{
PEP_STATUS status = register_sync_callbacks(session(), nullptr,
_notifyHandshake, _retrieve_next_sync_event);
throw_status(status);
if (obj && startup)
_startup(obj);
do_sync_protocol(session(), (void *) obj);
unregister_sync_callbacks(session());
session(release);
if (obj && _shutdown)
_shutdown(obj);
}
template< class T > void startup(messageToSend_t messageToSend,
notifyHandshake_t notifyHandshake, T *obj,
function< void (T *) > _startup,
function< void (T *) > _shutdown
)
{
if (messageToSend)
_messageToSend = messageToSend;
if (notifyHandshake)
_notifyHandshake = notifyHandshake;
session();
{
lock_guard<mutex> lock(m);
if (!_sync_thread)
_sync_thread = new thread(sync_thread<T>, obj, _startup, _shutdown);
}
}
PEP_SESSION session(session_action action)
{
lock_guard<mutex> lock(m);

@ -8,6 +8,7 @@
#include <functional>
#include <string>
#include <pEp/sync_api.h>
#include "Adapter.hxx"
using namespace std;

@ -0,0 +1,58 @@
#pragma once
#include <functional>
using namespace std;
namespace pEp {
namespace Adapter {
extern messageToSend_t _messageToSend;
extern notifyHandshake_t _notifyHandshake;
extern std::thread *_sync_thread;
extern ::utility::locked_queue< SYNC_EVENT > q;
extern std::mutex m;
SYNC_EVENT _retrieve_next_sync_event(void *management, time_t threshold);
template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
{
PEP_STATUS status = register_sync_callbacks(session(), nullptr,
_notifyHandshake, _retrieve_next_sync_event);
throw_status(status);
if (obj && _startup)
_startup(obj);
do_sync_protocol(session(), (void *)obj);
unregister_sync_callbacks(session());
session(release);
if (obj && _shutdown)
_shutdown(obj);
}
template< class T > void startup(messageToSend_t messageToSend,
notifyHandshake_t notifyHandshake, T *obj,
function< void(T *) > _startup,
function< void(T *) > _shutdown
)
{
if (messageToSend)
_messageToSend = messageToSend;
if (notifyHandshake)
_notifyHandshake = notifyHandshake;
session();
{
lock_guard<mutex> lock(m);
if (!_sync_thread)
_sync_thread = new thread(sync_thread<T>, obj, _startup, _shutdown);
}
}
}
}

@ -139,6 +139,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Adapter.hh" />
<ClInclude Include="..\Adapter.hxx" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

@ -26,5 +26,8 @@
<ClInclude Include="..\Adapter.hh">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\Adapter.hxx">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save