From c0cdf332349733f914726ea449d48998e92209fa Mon Sep 17 00:00:00 2001 From: Edouard Tisserant Date: Mon, 28 Nov 2016 17:54:25 +0100 Subject: [PATCH] ENGINE-133 timeout in retrieve_next_sync_msg - half baked, not wrapped --- src/sync_mixin.cc | 35 ++++++++++++++++++++++++++++++++--- src/sync_mixin.hh | 13 ++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/sync_mixin.cc b/src/sync_mixin.cc index 80af846..6ee297d 100644 --- a/src/sync_mixin.cc +++ b/src/sync_mixin.cc @@ -61,34 +61,58 @@ namespace pEp { #ifndef NDEBUG void SyncMixIn::_inject(int event, Identity partner, object extra) { + time_t timeout = 0; PEP_STATUS status = fsm_DeviceState_inject(session, - (DeviceState_event) event, partner, NULL); + (DeviceState_event) event, partner, NULL, &timeout); _throw_status(status); } #endif jmp_buf SyncMixIn::env; + jmp_buf SyncMixIn::env_timeout; void *SyncMixIn::_msg; int SyncMixIn::inject_sync_msg(void *msg, void *management) { _msg = msg; int val = setjmp(env); - if (!val) + if (!val){ + // call python timeout timer cancel + auto that = dynamic_cast< SyncMixIn_callback * >( + static_cast< SyncMixIn * > (management)); + that->cancelTimeout(); do_sync_protocol(session, management); + } return 0; } - void *SyncMixIn::retrieve_next_sync_msg(void *management) + void *SyncMixIn::retrieve_next_sync_msg(void *management, time_t *timeout) { static int twice = 1; twice = !twice; if (!twice) return (void *) _msg; + if (*timeout != 0){ + int val = setjmp(env_timeout); + if (!val){ + // call python timeout timer start + auto that = dynamic_cast< SyncMixIn_callback * >( + static_cast< SyncMixIn * > (management)); + that->setTimeout(*timeout); + }else{ + // this will inject tiemout event + return NULL; + } + } longjmp(env, 1); return (void *) 23; } + // to be called from python timeout timer - may GIL protect us + void SyncMixIn::onTimeout(){ + longjmp(env_timeout, 1); + } + void SyncMixIn_callback::messageToSend(Message msg) { call_method< void >(_self, "messageToSend", msg); @@ -98,6 +122,11 @@ namespace pEp { { call_method< void >(_self, "showHandshake", me, partner); } + + void SyncMixIn_callback::setTimeout(time_t timeout) + { + call_method< void >(_self, "SetTimeout", timeout); + } } } diff --git a/src/sync_mixin.hh b/src/sync_mixin.hh index 0a38d4e..0436cb0 100644 --- a/src/sync_mixin.hh +++ b/src/sync_mixin.hh @@ -22,6 +22,15 @@ namespace pEp { #ifndef NDEBUG virtual void _inject(int event, Identity partner, object extra); #endif + virtual void setTimeout(time_t timeout){ + throw runtime_error("override this method"); + } + + virtual void cancelTimeout(){ + throw runtime_error("override this method"); + } + + virtual void onTimeout(); protected: static PEP_STATUS _messageToSend(void *obj, message *msg); @@ -29,9 +38,10 @@ namespace pEp { pEp_identity *me, pEp_identity *partner); static jmp_buf env; + static jmp_buf env_timeout; static void *_msg; static int inject_sync_msg(void *msg, void *management); - static void *retrieve_next_sync_msg(void *management); + static void *retrieve_next_sync_msg(void *management, time_t *timeout); }; class SyncMixIn_callback : public SyncMixIn { @@ -43,6 +53,7 @@ namespace pEp { void messageToSend(Message msg); void showHandshake(Identity me, Identity partner); + void setTimeout(time_t timeout); }; } }