Volker Birk 7 years ago
parent
commit
89ca327f03
  1. 53
      Adapter.cc
  2. 1
      Adapter.hh
  3. 58
      Adapter.hxx
  4. 1
      libpEpAdapter/libpEpAdapter.vcxproj
  5. 3
      libpEpAdapter/libpEpAdapter.vcxproj.filters

53
Adapter.cc

@ -32,11 +32,12 @@ namespace pEp {
} }
namespace Adapter { namespace Adapter {
static messageToSend_t _messageToSend = nullptr; messageToSend_t _messageToSend = nullptr;
static notifyHandshake_t _notifyHandshake = nullptr; notifyHandshake_t _notifyHandshake = nullptr;
static std::thread *_sync_thread = nullptr; std::thread *_sync_thread = nullptr;
static ::utility::locked_queue< SYNC_EVENT > q;
static std::mutex m; ::utility::locked_queue< SYNC_EVENT > q;
std::mutex m;
static int _inject_sync_event(SYNC_EVENT ev, void *management) static int _inject_sync_event(SYNC_EVENT ev, void *management)
{ {
@ -54,7 +55,7 @@ namespace pEp {
return 0; 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); time_t started = time(nullptr);
bool timeout = false; bool timeout = false;
@ -83,46 +84,6 @@ namespace pEp {
return q.pop_front(); 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) PEP_SESSION session(session_action action)
{ {
lock_guard<mutex> lock(m); lock_guard<mutex> lock(m);

1
Adapter.hh

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

58
Adapter.hxx

@ -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);
}
}
}
}

1
libpEpAdapter/libpEpAdapter.vcxproj

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

3
libpEpAdapter/libpEpAdapter.vcxproj.filters

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