removing old sync code sync
authorVolker Birk <vb@pep.foundation>
Fri, 05 Oct 2018 14:32:36 +0200
branchsync
changeset 294b2fe2bac2db0
parent 293 262897d139e5
child 295 f55ec2fd694f
removing old sync code
CpEpEngine.cpp
CpEpEngine.h
     1.1 --- a/CpEpEngine.cpp	Thu Oct 04 08:31:10 2018 +0200
     1.2 +++ b/CpEpEngine.cpp	Fri Oct 05 14:32:36 2018 +0200
     1.3 @@ -1272,228 +1272,6 @@
     1.4      return S_OK;
     1.5  }
     1.6  
     1.7 -// keysync api
     1.8 -
     1.9 -void CpEpEngine::start_keysync()
    1.10 -{
    1.11 -    // acquire the lock
    1.12 -    std::unique_lock<std::recursive_mutex> lock(keysync_mutex);
    1.13 -
    1.14 -    // Assert if we're not already running.
    1.15 -    assert(!this->keysync_thread);
    1.16 -
    1.17 -    // Ensure we are not aborting the new thread due to a
    1.18 -    // left over flag.
    1.19 -    keysync_abort_requested = false;
    1.20 -
    1.21 -    // Init our keysync session
    1.22 -    { // begin lock scope
    1.23 -        std::lock_guard<std::mutex> lock(init_mutex);
    1.24 -        PEP_STATUS status = ::init(&keysync_session);
    1.25 -        ::register_sync_callbacks(keysync_session, (void*)this, messageToSend, notifyHandshake, inject_sync_msg, retrieve_next_sync_msg);
    1.26 -        assert(status == PEP_STATUS_OK);
    1.27 -    } // end lock scope
    1.28 -
    1.29 -    attach_sync_session(get_session(), keysync_session);
    1.30 -
    1.31 -    // We need to marshal the callbacks to the keysync thread
    1.32 -    LPSTREAM marshaled_callbacks;
    1.33 -
    1.34 -    auto result = CoMarshalInterThreadInterfaceInStream(IID_IpEpEngineCallbacks, client_callbacks, &marshaled_callbacks);
    1.35 -    assert(result == S_OK);
    1.36 -
    1.37 -    // Star the keysync thread
    1.38 -    keysync_thread = new thread(do_keysync_in_thread, this, marshaled_callbacks);
    1.39 -}
    1.40 -
    1.41 -void CpEpEngine::do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks)
    1.42 -{
    1.43 -    assert(self);
    1.44 -    assert(marshaled_callbacks);
    1.45 -
    1.46 -    // We need to initialize COM here for successfull delivery of the callbacks.
    1.47 -    // As we don't create any COM instances in our thread, the COMINIT value is
    1.48 -    // currently irrelevant, so we go with the safest value.
    1.49 -    auto res = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    1.50 -    assert(res == S_OK);
    1.51 -
    1.52 -    LPVOID vp;
    1.53 -
    1.54 -    res = CoGetInterfaceAndReleaseStream(marshaled_callbacks, IID_IpEpEngineCallbacks, &vp);
    1.55 -    assert(SUCCEEDED(res));
    1.56 -
    1.57 -    self->client_last_signalled_polling_state = false;
    1.58 -    self->client_callbacks_on_sync_thread = static_cast<IpEpEngineCallbacks*>(vp);
    1.59 -
    1.60 -    ::do_sync_protocol(self->keysync_session, self);
    1.61 -
    1.62 -    self->client_callbacks_on_sync_thread->Release();
    1.63 -
    1.64 -    self->client_callbacks_on_sync_thread = NULL;
    1.65 -
    1.66 -    CoUninitialize();
    1.67 -}
    1.68 -
    1.69 -void CpEpEngine::stop_keysync()
    1.70 -{
    1.71 -    // acquire the lock
    1.72 -    std::unique_lock<std::recursive_mutex> lock(keysync_mutex);
    1.73 -
    1.74 -    // Do nothing if keysync is not running.
    1.75 -    if (!keysync_thread)
    1.76 -        return;
    1.77 -
    1.78 -    assert(!keysync_abort_requested);
    1.79 -    // signal that we're gonna abort
    1.80 -    keysync_abort_requested = true;
    1.81 -
    1.82 -    // Notify the keysync thread
    1.83 -    keysync_condition.notify_all();
    1.84 -
    1.85 -    // Wait for the other thread to finish and clean up
    1.86 -    while (keysync_abort_requested)
    1.87 -        keysync_condition.wait(lock);
    1.88 -
    1.89 -    // collect the child thread for the thread to end
    1.90 -    keysync_thread->join();
    1.91 -
    1.92 -    // clean up
    1.93 -    delete keysync_thread;
    1.94 -    keysync_thread = NULL;
    1.95 -
    1.96 -    ::detach_sync_session(get_session());
    1.97 -    ::unregister_sync_callbacks(keysync_session);
    1.98 -
    1.99 -    std::lock_guard<std::mutex> releaselock(init_mutex);
   1.100 -    release(keysync_session);
   1.101 -    keysync_session = NULL;
   1.102 -}
   1.103 -
   1.104 -int CpEpEngine::inject_sync_msg(void * msg, void * management)
   1.105 -{
   1.106 -    assert(msg);
   1.107 -    assert(management);
   1.108 -    // check argument
   1.109 -    if (!msg)
   1.110 -        return E_INVALIDARG;
   1.111 -    if (!management)
   1.112 -        return ERROR_INVALID_HANDLE;
   1.113 -
   1.114 -    CpEpEngine* me = (CpEpEngine*)management;
   1.115 -
   1.116 -    // acquire the lock
   1.117 -    std::unique_lock<std::recursive_mutex> lock(me->keysync_mutex);
   1.118 -
   1.119 -    // check whether we're in a valid state running:
   1.120 -    if (!me->keysync_thread)
   1.121 -        return E_ASYNC_OPERATION_NOT_STARTED;
   1.122 -
   1.123 -    // queue the message
   1.124 -    me->keysync_queue.push(msg);
   1.125 -
   1.126 -    // notify the receivers
   1.127 -    me->keysync_condition.notify_all();
   1.128 -
   1.129 -    return S_OK;
   1.130 -}
   1.131 -
   1.132 -void * CpEpEngine::retrieve_next_sync_msg(void * management, time_t *timeout)
   1.133 -{
   1.134 -    // sanity check
   1.135 -    assert(management);
   1.136 -    if (!(management))
   1.137 -        return NULL;
   1.138 -
   1.139 -    CpEpEngine* me = (CpEpEngine*)management;
   1.140 -
   1.141 -    if ((timeout && *timeout)
   1.142 -        && me->client_callbacks_on_sync_thread
   1.143 -        && me->client_last_signalled_polling_state == false)
   1.144 -    {
   1.145 -        me->client_callbacks_on_sync_thread->NeedFastPolling(VARIANT_TRUE);
   1.146 -        me->client_last_signalled_polling_state = true;
   1.147 -    }
   1.148 -    else if (!(timeout && *timeout)
   1.149 -        && me->client_callbacks_on_sync_thread
   1.150 -        && me->client_last_signalled_polling_state == true)
   1.151 -    {
   1.152 -        me->client_callbacks_on_sync_thread->NeedFastPolling(VARIANT_FALSE);
   1.153 -        me->client_last_signalled_polling_state = false;
   1.154 -    }
   1.155 -
   1.156 -    // acquire the lock
   1.157 -    std::unique_lock<std::recursive_mutex> lock(me->keysync_mutex);
   1.158 -
   1.159 -    if (me->notify_handshake_finished)
   1.160 -        me->notify_handshake_deliver_result();
   1.161 -
   1.162 -    if (timeout && *timeout) {
   1.163 -        std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now()
   1.164 -            + std::chrono::seconds(*timeout);
   1.165 -
   1.166 -        while (me->keysync_queue.empty() && !me->keysync_abort_requested)
   1.167 -        {
   1.168 -            auto status = me->keysync_condition.wait_until(lock, end_time);
   1.169 -
   1.170 -            if (me->notify_handshake_finished)
   1.171 -                me->notify_handshake_deliver_result();
   1.172 -
   1.173 -            if (status == std::cv_status::timeout)
   1.174 -            {
   1.175 -                *timeout = 1; // Signal timeout
   1.176 -                return NULL;
   1.177 -            }
   1.178 -            else
   1.179 -            {
   1.180 -                std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
   1.181 -
   1.182 -                if (now < end_time)
   1.183 -                {
   1.184 -                    *timeout = std::chrono::duration_cast<std::chrono::seconds>(end_time - now).count();
   1.185 -                }
   1.186 -                else
   1.187 -                {
   1.188 -                    *timeout = 0;
   1.189 -                }
   1.190 -            }
   1.191 -        }
   1.192 -    }
   1.193 -    else
   1.194 -    {
   1.195 -        while (me->keysync_queue.empty() && !me->keysync_abort_requested)
   1.196 -        {
   1.197 -            me->keysync_condition.wait(lock);
   1.198 -
   1.199 -            if (me->notify_handshake_finished)
   1.200 -                me->notify_handshake_deliver_result();
   1.201 -        }
   1.202 -    }
   1.203 -
   1.204 -    if (me->keysync_abort_requested) {
   1.205 -        // we acknowledge that we're quitting...
   1.206 -        me->keysync_abort_requested = false;
   1.207 -
   1.208 -        // We signal the main thread that we got his signal
   1.209 -        // so it can gain the mutex again and call join() on us.
   1.210 -        me->keysync_condition.notify_all();
   1.211 -
   1.212 -        // and tell the pep engine we're done.
   1.213 -        if (timeout)
   1.214 -            *timeout = 0; // signal for termination.
   1.215 -        return NULL;
   1.216 -    }
   1.217 -
   1.218 -    assert(!me->keysync_queue.empty());
   1.219 -
   1.220 -    // Pop the message and return it.
   1.221 -    void* msg = me->keysync_queue.front();
   1.222 -    assert(msg);
   1.223 -
   1.224 -    me->keysync_queue.pop();
   1.225 -
   1.226 -    return msg;
   1.227 -}
   1.228 -
   1.229  // Force an update check now
   1.230  STDMETHODIMP CpEpEngine::UpdateNow()
   1.231  {
   1.232 @@ -1524,8 +1302,6 @@
   1.233      this->client_callbacks = new_callbacks;
   1.234      new_callbacks->AddRef();
   1.235  
   1.236 -    start_keysync();
   1.237 -
   1.238      return S_OK;
   1.239  }
   1.240  
   1.241 @@ -1536,10 +1312,7 @@
   1.242      if (!this->client_callbacks)
   1.243          return S_FALSE;
   1.244  
   1.245 -    stop_keysync();
   1.246 -
   1.247      this->client_callbacks->Release();
   1.248 -
   1.249      this->client_callbacks = NULL;
   1.250  
   1.251      return S_OK;
     2.1 --- a/CpEpEngine.h	Thu Oct 04 08:31:10 2018 +0200
     2.2 +++ b/CpEpEngine.h	Fri Oct 05 14:32:36 2018 +0200
     2.3 @@ -39,7 +39,6 @@
     2.4  
     2.5      ~CpEpEngine()
     2.6      {
     2.7 -        stop_keysync();
     2.8          StopKeyserverLookup();
     2.9          if (m_session) // may be zero when FinalConstruct failed to initialize the engine
    2.10          {
    2.11 @@ -148,13 +147,6 @@
    2.12      IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
    2.13      bool client_last_signalled_polling_state = true;
    2.14  
    2.15 -    // Keysync members
    2.16 -    static int inject_sync_msg(void *msg, void* management);
    2.17 -    static void* retrieve_next_sync_msg(void* management, time_t *timeout);
    2.18 -    void start_keysync();
    2.19 -    static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
    2.20 -    void stop_keysync();
    2.21 -
    2.22      static std::mutex init_mutex;
    2.23  
    2.24      std::recursive_mutex keysync_mutex;