123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #include <kopano/platform.h>
- #include <algorithm>
- #include <new>
- #include <kopano/lockhelper.hpp>
- #include <kopano/memory.hpp>
- #include <mapidefs.h>
- #include "ECNotifyClient.h"
- #include "ECNotifyMaster.h"
- #include "ECSessionGroupManager.h"
- #include <kopano/stringutil.h>
- #include "SOAPUtils.h"
- #include "WSTransport.h"
- #include <sys/signal.h>
- #include <sys/types.h>
- #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
- inline ECNotifySink::ECNotifySink(ECNotifyClient *lpClient, NOTIFYCALLBACK fnCallback)
- : m_lpClient(lpClient)
- , m_fnCallback(fnCallback)
- { }
- inline HRESULT ECNotifySink::Notify(ULONG ulConnection,
- const NOTIFYLIST &lNotifications) const
- {
- return CALL_MEMBER_FN(*m_lpClient, m_fnCallback)(ulConnection, lNotifications);
- }
- inline bool ECNotifySink::IsClient(const ECNotifyClient *lpClient) const
- {
- return lpClient == m_lpClient;
- }
- ECNotifyMaster::ECNotifyMaster(SessionGroupData *lpData) :
- m_lpSessionGroupData(lpData )
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::ECNotifyMaster", "");
- memset(&m_hThread, 0, sizeof(m_hThread));
- m_ulConnection = 1;
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::ECNotifyMaster", "");
- }
- ECNotifyMaster::~ECNotifyMaster(void)
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::~ECNotifyMaster", "");
- assert(m_listNotifyClients.empty());
-
- StopNotifyWatch();
- if (m_lpSessionGroupData)
- m_lpSessionGroupData = NULL;
- if (m_lpTransport)
- m_lpTransport->Release();
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::~ECNotifyMaster", "");
- }
- HRESULT ECNotifyMaster::Create(SessionGroupData *lpData, ECNotifyMaster **lppMaster)
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::Create", "");
- HRESULT hr = hrSuccess;
- auto lpMaster = new(std::nothrow) ECNotifyMaster(lpData);
- if (lpMaster == nullptr)
- return MAPI_E_NOT_ENOUGH_MEMORY;
- lpMaster->AddRef();
- *lppMaster = lpMaster;
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::Create", "hr=0x%08X", hr);
- return hr;
- }
- HRESULT ECNotifyMaster::ConnectToSession()
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::ConnectToSession", "");
- HRESULT hr = hrSuccess;
- scoped_rlock biglock(m_hMutex);
-
- if (m_bThreadExit) {
- hr = MAPI_E_END_OF_SESSION;
- goto exit;
- }
-
- if (m_lpTransport) {
- hr = m_lpTransport->HrCancelIO();
- if (hr != hrSuccess)
- goto exit;
- m_lpTransport->Release();
- m_lpTransport = NULL;
- }
-
- hr = m_lpSessionGroupData->GetTransport(&m_lpTransport);
- if (hr != hrSuccess)
- goto exit;
- exit:
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::ConnectToSession", "hr=0x%08X", hr);
- return hr;
- }
- HRESULT ECNotifyMaster::AddSession(ECNotifyClient* lpClient)
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::AddSession", "");
- scoped_rlock biglock(m_hMutex);
- m_listNotifyClients.push_back(lpClient);
-
- if (StartNotifyWatch() != hrSuccess)
- assert(false);
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::AddSession", "");
- return hrSuccess;
- }
- struct findConnectionClient
- {
- ECNotifyClient* lpClient;
- findConnectionClient(ECNotifyClient* lpClient) : lpClient(lpClient) {}
- bool operator()(const NOTIFYCONNECTIONCLIENTMAP::value_type &entry) const
- {
- return entry.second.IsClient(lpClient);
- }
- };
- HRESULT ECNotifyMaster::ReleaseSession(ECNotifyClient* lpClient)
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::ReleaseSession", "");
- HRESULT hr = hrSuccess;
- scoped_rlock biglock(m_hMutex);
-
- auto iter = m_mapConnections.cbegin();
- while (true) {
- iter = find_if(iter, m_mapConnections.cend(), findConnectionClient(lpClient));
- if (iter == m_mapConnections.cend())
- break;
- m_mapConnections.erase(iter++);
- }
-
- m_listNotifyClients.remove(lpClient);
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::ReleaseSession", "");
- return hr;
- }
- HRESULT ECNotifyMaster::ReserveConnection(ULONG *lpulConnection)
- {
- scoped_rlock lock(m_hMutex);
- *lpulConnection = m_ulConnection++;
- return hrSuccess;
- }
- HRESULT ECNotifyMaster::ClaimConnection(ECNotifyClient* lpClient, NOTIFYCALLBACK fnCallback, ULONG ulConnection)
- {
- scoped_rlock lock(m_hMutex);
- m_mapConnections.insert(NOTIFYCONNECTIONCLIENTMAP::value_type(ulConnection, ECNotifySink(lpClient, fnCallback)));
- return hrSuccess;
- }
- HRESULT ECNotifyMaster::DropConnection(ULONG ulConnection)
- {
- scoped_rlock lock(m_hMutex);
- m_mapConnections.erase(ulConnection);
- return hrSuccess;
- }
- HRESULT ECNotifyMaster::StartNotifyWatch()
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::StartNotifyWatch", "");
- HRESULT hr = hrSuccess;
-
- if (m_bThreadRunning)
- goto exit;
- hr = ConnectToSession();
- if (hr != hrSuccess)
- goto exit;
-
- pthread_attr_t m_hAttrib;
- pthread_attr_init(&m_hAttrib);
- pthread_attr_setdetachstate(&m_hAttrib, PTHREAD_CREATE_JOINABLE);
-
- if (pthread_attr_setstacksize(&m_hAttrib, 1024 * 1024)) {
- hr = MAPI_E_CALL_FAILED;
- goto exit;
- }
- if (pthread_create(&m_hThread, &m_hAttrib, NotifyWatch, (void *)this)) {
- hr = MAPI_E_CALL_FAILED;
- goto exit;
- }
- pthread_attr_destroy(&m_hAttrib);
- set_thread_name(m_hThread, "NotifyThread");
- m_bThreadRunning = TRUE;
- exit:
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::StartNotifyWatch", "hr=0x%08X", hr);
- return hr;
- }
- HRESULT ECNotifyMaster::StopNotifyWatch()
- {
- TRACE_NOTIFY(TRACE_ENTRY, "ECNotifyMaster::StopNotifyWatch", "");
- HRESULT hr = hrSuccess;
- KCHL::object_ptr<WSTransport> lpTransport;
- ulock_rec biglock(m_hMutex, std::defer_lock_t());
-
- if (!m_bThreadRunning)
- goto exit;
-
- biglock.lock();
- m_bThreadExit = TRUE;
- if (m_lpTransport) {
-
- hr = m_lpTransport->HrClone(&~lpTransport);
- if (hr != hrSuccess) {
- biglock.unlock();
- goto exit;
- }
-
- lpTransport->HrLogOff();
-
- m_lpTransport->HrCancelIO();
- }
- biglock.unlock();
- if (pthread_join(m_hThread, NULL) != 0)
- TRACE_NOTIFY(TRACE_WARNING, "ECNotifyMaster::StopNotifyWatch", "Invalid thread join");
- m_bThreadRunning = FALSE;
- exit:
- TRACE_NOTIFY(TRACE_RETURN, "ECNotifyMaster::StopNotifyWatch", "hr=0x%08X", hr);
- return hr;
- }
- void* ECNotifyMaster::NotifyWatch(void *pTmpNotifyMaster)
- {
- TRACE_NOTIFY(TRACE_ENTRY, "NotifyWatch", "");
- auto pNotifyMaster = static_cast<ECNotifyMaster *>(pTmpNotifyMaster);
- assert(pNotifyMaster != NULL);
- HRESULT hr = hrSuccess;
- NOTIFYCONNECTIONMAP mapNotifications;
- notifyResponse notifications;
- bool bReconnect = false;
-
- signal(SIGPIPE, SIG_IGN);
- while (!pNotifyMaster->m_bThreadExit) {
- memset(¬ifications, 0, sizeof(notifications));
- if (pNotifyMaster->m_bThreadExit)
- goto exit;
-
- if (bReconnect) {
- for (ULONG i = 10; i > 0; --i) {
- Sleep(100);
- if (pNotifyMaster->m_bThreadExit)
- goto exit;
- }
- }
-
- notificationArray *pNotifyArray = NULL;
- hr = pNotifyMaster->m_lpTransport->HrGetNotify(&pNotifyArray);
- if (static_cast<unsigned int>(hr) == KCWARN_CALL_KEEPALIVE) {
- if (bReconnect) {
- TRACE_NOTIFY(TRACE_WARNING, "NotifyWatch::Reconnection", "OK connection: %d", pNotifyMaster->m_ulConnection);
- bReconnect = false;
- }
- continue;
- } else if (hr == MAPI_E_NETWORK_ERROR) {
- bReconnect = true;
- TRACE_NOTIFY(TRACE_WARNING, "NotifyWatch::Reconnection", "for connection: %d", pNotifyMaster->m_ulConnection);
- continue;
- } else if (hr != hrSuccess) {
-
- if (!pNotifyMaster->m_bThreadExit) {
- TRACE_NOTIFY(TRACE_WARNING, "NotifyWatch::End of session", "reconnect");
- while (pNotifyMaster->ConnectToSession() != hrSuccess &&
- !pNotifyMaster->m_bThreadExit)
-
-
-
- Sleep(1000);
- }
- if (pNotifyMaster->m_bThreadExit)
- goto exit;
- else {
-
- scoped_rlock lock(pNotifyMaster->m_hMutex);
- for (auto ptr : pNotifyMaster->m_listNotifyClients)
- ptr->NotifyReload();
- continue;
- }
- }
- if (bReconnect) {
- TRACE_NOTIFY(TRACE_WARNING, "NotifyWatch::Reconnection", "OK connection: %d", pNotifyMaster->m_ulConnection);
- bReconnect = false;
- }
-
- if (pNotifyArray == NULL)
- continue;
- TRACE_NOTIFY(TRACE_ENTRY, "NotifyWatch::GetNotify", "%d", pNotifyArray->__size);
-
- for (gsoap_size_t item = 0; item < pNotifyArray->__size; ++item) {
- ULONG ulConnection = pNotifyArray->__ptr[item].ulConnection;
-
- auto iterNotifications =
- mapNotifications.insert(NOTIFYCONNECTIONMAP::value_type(ulConnection, NOTIFYLIST())).first;
- iterNotifications->second.push_back(&pNotifyArray->__ptr[item]);
- }
- for (const auto &p : mapNotifications) {
-
- scoped_rlock lock(pNotifyMaster->m_hMutex);
- auto iterClient = pNotifyMaster->m_mapConnections.find(p.first);
- if (iterClient == pNotifyMaster->m_mapConnections.cend())
- continue;
- iterClient->second.Notify(p.first, p.second);
-
- }
-
- mapNotifications.clear();
-
- if (pNotifyArray != NULL) {
- FreeNotificationArrayStruct(pNotifyArray, true);
- pNotifyArray = NULL;
- }
- }
- exit:
- TRACE_NOTIFY(TRACE_RETURN, "NotifyWatch", "");
- return NULL;
- }
|