lazy unmarshalling for COM interface on sync thread sync
authorVolker Birk <vb@pep.foundation>
Mon, 22 Oct 2018 20:00:00 +0200
branchsync
changeset 3080017f25f860d
parent 307 eb5012d87405
child 309 35ef78e435a8
lazy unmarshalling for COM interface on sync thread
CpEpEngine.cpp
CpEpEngine.h
     1.1 --- a/CpEpEngine.cpp	Mon Oct 22 17:54:28 2018 +0200
     1.2 +++ b/CpEpEngine.cpp	Mon Oct 22 20:00:00 2018 +0200
     1.3 @@ -12,7 +12,7 @@
     1.4  
     1.5  // CpEpEngine
     1.6  
     1.7 -pEp::pc_container< CpEpEngine::MarshaledCallbacks, IpEpEngineCallbacks > CpEpEngine::sync_callbacks;
     1.8 +CpEpEngine::callback_container CpEpEngine::sync_callbacks;
     1.9  
    1.10  // the init_mutex protects our initialization and destruction
    1.11  // against a running keysync thread, and it ensures that the
    1.12 @@ -736,6 +736,18 @@
    1.13      return _ident;
    1.14  }
    1.15  
    1.16 +static IpEpEngineCallbacks * _unmarshaled_consumer(CpEpEngine::callback_container::Container::const_iterator p)
    1.17 +{
    1.18 +    if (!p->cdata && p->pdata->marshaled) {
    1.19 +        HRESULT r = CoGetInterfaceAndReleaseStream(p->pdata->marshaled, IID_IpEpEngineCallbacks, (LPVOID*) &p->cdata);
    1.20 +        if (!SUCCEEDED(r))
    1.21 +            throw runtime_error("_unmarshaled_consumer(): CoGetInterfaceAndReleaseStream() failed");
    1.22 +        p->pdata->marshaled = nullptr;
    1.23 +    }
    1.24 +
    1.25 +    return p->cdata;
    1.26 +}
    1.27 +
    1.28  PEP_STATUS CpEpEngine::messageToSend(message *msg)
    1.29  {
    1.30      assert(msg);
    1.31 @@ -745,11 +757,7 @@
    1.32      bool in_sync = on_sync_thread();
    1.33  
    1.34      for (auto p = sync_callbacks.begin(); p != sync_callbacks.end(); ++p) {
    1.35 -        IpEpEngineCallbacks *cb;
    1.36 -        if (in_sync)
    1.37 -            cb = p->cdata;
    1.38 -        else
    1.39 -            cb = p->pdata->unmarshaled;
    1.40 +        IpEpEngineCallbacks *cb = in_sync ? _unmarshaled_consumer(p) : p->pdata->unmarshaled;
    1.41  
    1.42          if (cb) {
    1.43              TextMessage _msg;
     2.1 --- a/CpEpEngine.h	Mon Oct 22 17:54:28 2018 +0200
     2.2 +++ b/CpEpEngine.h	Mon Oct 22 20:00:00 2018 +0200
     2.3 @@ -84,11 +84,11 @@
     2.4              return res;
     2.5          }
     2.6  
     2.7 +        startup<CpEpEngine>(messageToSend, notifyHandshake, this, &CpEpEngine::Startup_sync);
     2.8 +
     2.9          ::register_examine_function(session(), CpEpEngine::examine_identity, (void *)this);
    2.10          ::log_event(session(), "Startup", "pEp COM Adapter", NULL, NULL);
    2.11  
    2.12 -        startup<CpEpEngine>(messageToSend, notifyHandshake, this, &CpEpEngine::Startup_sync);
    2.13 -
    2.14          return S_OK;
    2.15      }
    2.16  
    2.17 @@ -96,6 +96,13 @@
    2.18      {
    2.19      }
    2.20  
    2.21 +    struct MarshaledCallbacks {
    2.22 +        IpEpEngineCallbacks *unmarshaled;
    2.23 +        LPSTREAM marshaled;
    2.24 +    };
    2.25 +
    2.26 +    typedef pEp::pc_container< MarshaledCallbacks, IpEpEngineCallbacks > callback_container;
    2.27 +
    2.28  protected:
    2.29      typedef locked_queue<pEp_identity_cpp> identity_queue_t;
    2.30      static ::pEp_identity * retrieve_next_identity(void *management);
    2.31 @@ -117,12 +124,8 @@
    2.32  
    2.33  private:
    2.34      // callbacks for sync
    2.35 -    struct MarshaledCallbacks {
    2.36 -        IpEpEngineCallbacks *unmarshaled;
    2.37 -        LPSTREAM marshaled;
    2.38 -    };
    2.39  
    2.40 -    static pEp::pc_container< MarshaledCallbacks, IpEpEngineCallbacks > sync_callbacks;
    2.41 +    static callback_container sync_callbacks;
    2.42  
    2.43      void Startup_sync()
    2.44      {