123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #include <kopano/platform.h>
- #include <chrono>
- #include <kopano/lockhelper.hpp>
- #include <pthread.h>
- #include "ECNotificationManager.h"
- #include "ECSession.h"
- #include "ECSessionManager.h"
- #include "ECStringCompat.h"
- #include "soapH.h"
- namespace KC {
- static int soapresponse(struct notifyResponse notifications, struct soap *soap)
- {
- soap_serializeheader(soap);
- soap_serialize_notifyResponse(soap, ¬ifications);
- if (soap_begin_count(soap))
- return soap->error;
- if (soap->mode & SOAP_IO_LENGTH)
- { if (soap_envelope_begin_out(soap)
- || soap_putheader(soap)
- || soap_body_begin_out(soap)
- || soap_put_notifyResponse(soap, ¬ifications, "ns:notifyResponse", NULL)
- || soap_body_end_out(soap)
- || soap_envelope_end_out(soap))
- return soap->error;
- };
- if (soap_end_count(soap)
- || soap_response(soap, SOAP_OK)
- || soap_envelope_begin_out(soap)
- || soap_putheader(soap)
- || soap_body_begin_out(soap)
- || soap_put_notifyResponse(soap, ¬ifications, "ns:notifyResponse", NULL)
- || soap_body_end_out(soap)
- || soap_envelope_end_out(soap)
- || soap_end_send(soap))
- return soap->error;
- return soap_closesock(soap);
- }
- void (*kopano_notify_done)(struct soap *) = [](struct soap *) {};
- ECNotificationManager::ECNotificationManager(void)
- {
- pthread_create(&m_thread, NULL, Thread, this);
- set_thread_name(m_thread, "NotificationManager");
- }
- ECNotificationManager::~ECNotificationManager()
- {
- ulock_normal l_ses(m_mutexSessions);
- m_bExit = true;
- m_condSessions.notify_all();
- l_ses.unlock();
- ec_log_info("Shutdown notification manager");
- pthread_join(m_thread, NULL);
-
- for (const auto &p : m_mapRequests) {
-
- kopano_end_soap_connection(p.second.soap);
- soap_destroy(p.second.soap);
- soap_end(p.second.soap);
- soap_free(p.second.soap);
- }
- }
- HRESULT ECNotificationManager::AddRequest(ECSESSIONID ecSessionId, struct soap *soap)
- {
- struct soap *lpItem = NULL;
- ulock_normal l_req(m_mutexRequests);
- auto iterRequest = m_mapRequests.find(ecSessionId);
- if (iterRequest != m_mapRequests.cend()) {
-
-
-
- ec_log_warn("Replacing notification request for ID %llu",
- static_cast<unsigned long long>(ecSessionId));
-
-
- struct notifyResponse notifications;
- soap_default_notifyResponse(iterRequest->second.soap, ¬ifications);
- notifications.er = KCERR_NOT_FOUND;
- if (soapresponse(notifications, iterRequest->second.soap))
-
- soap_send_fault(iterRequest->second.soap);
- soap_destroy(iterRequest->second.soap);
- soap_end(iterRequest->second.soap);
- lpItem = iterRequest->second.soap;
-
-
- kopano_notify_done(lpItem);
- }
-
- NOTIFREQUEST req;
- req.soap = soap;
- time(&req.ulRequestTime);
-
- m_mapRequests[ecSessionId] = req;
- l_req.unlock();
-
-
-
- NotifyChange(ecSessionId);
-
- return hrSuccess;
- }
- HRESULT ECNotificationManager::NotifyChange(ECSESSIONID ecSessionId)
- {
-
- scoped_lock l_ses(m_mutexSessions);
- m_setActiveSessions.insert(ecSessionId);
- m_condSessions.notify_all();
- return hrSuccess;
- }
- void * ECNotificationManager::Thread(void *lpParam)
- {
- return static_cast<ECNotificationManager *>(lpParam)->Work();
- }
- void *ECNotificationManager::Work() {
- ECRESULT er = erSuccess;
- ECSession *lpecSession = NULL;
- struct notifyResponse notifications;
- std::set<ECSESSIONID> setActiveSessions;
- struct soap *lpItem;
- time_t ulNow = 0;
-
-
- while(1) {
- ulock_normal l_ses(m_mutexSessions);
- if (m_bExit)
- break;
- if (m_setActiveSessions.size() == 0)
-
- m_condSessions.wait_for(l_ses, std::chrono::seconds(1));
-
-
- setActiveSessions = m_setActiveSessions;
- m_setActiveSessions.clear();
- l_ses.unlock();
-
-
- for (const auto &ses : setActiveSessions) {
- lpItem = NULL;
- ulock_normal l_req(m_mutexRequests);
-
-
- auto iterRequest = m_mapRequests.find(ses);
- if (iterRequest != m_mapRequests.cend()) {
-
- soap_default_notifyResponse(iterRequest->second.soap, ¬ifications);
- if(g_lpSessionManager->ValidateSession(iterRequest->second.soap, ses, &lpecSession, true) == erSuccess) {
-
- er = lpecSession->GetNotifyItems(iterRequest->second.soap, ¬ifications);
-
- if(er == KCERR_NOT_FOUND) {
- if(time(NULL) - iterRequest->second.ulRequestTime < m_ulTimeout) {
-
-
- l_req.unlock();
- lpecSession->Unlock();
- continue;
- } else {
-
- er = erSuccess;
- notifications.pNotificationArray = (struct notificationArray *)soap_malloc(iterRequest->second.soap, sizeof(notificationArray));
- soap_default_notificationArray(iterRequest->second.soap, notifications.pNotificationArray);
- }
- }
- ULONG ulCapabilities = lpecSession->GetCapabilities();
- if (er == erSuccess && (ulCapabilities & KOPANO_CAP_UNICODE) == 0) {
- ECStringCompat stringCompat(false);
- er = FixNotificationsEncoding(iterRequest->second.soap, stringCompat, notifications.pNotificationArray);
- }
-
- notifications.er = er;
-
- lpecSession->Unlock();
- } else {
-
- notifications.er = KCERR_END_OF_SESSION;
- }
-
- if (soapresponse(notifications, iterRequest->second.soap))
-
- soap_send_fault(iterRequest->second.soap);
-
- soap_destroy(iterRequest->second.soap);
- soap_end(iterRequest->second.soap);
-
-
- lpItem = iterRequest->second.soap;
-
- m_mapRequests.erase(iterRequest);
-
- } else {
-
- }
- l_req.unlock();
- if(lpItem)
- kopano_notify_done(lpItem);
-
- }
-
-
- ulock_normal l_req(m_mutexRequests);
- time(&ulNow);
- for (const auto &req : m_mapRequests)
- if (ulNow - req.second.ulRequestTime > m_ulTimeout)
-
- NotifyChange(req.first);
- }
-
- return NULL;
- }
- }
|