ECSessionGroupManager.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/lockhelper.hpp>
  19. #include <mapicode.h>
  20. #include <mapix.h>
  21. #include "ECNotifyMaster.h"
  22. #include "ECSessionGroupManager.h"
  23. #include "SessionGroupData.h"
  24. #include "SSLUtil.h"
  25. /* std::algorithm helper structures/functions */
  26. struct findSessionGroupId {
  27. ECSESSIONGROUPID ecSessionGroupId;
  28. findSessionGroupId(ECSESSIONGROUPID ecSessionGroupId) : ecSessionGroupId(ecSessionGroupId)
  29. {
  30. }
  31. bool operator()(const SESSIONGROUPMAP::value_type &entry) const
  32. {
  33. return entry.second->GetSessionGroupId() == ecSessionGroupId;
  34. }
  35. };
  36. /* Global SessionManager for entire client */
  37. ECSessionGroupManager g_ecSessionManager;
  38. ECSESSIONGROUPID ECSessionGroupManager::GetSessionGroupId(const sGlobalProfileProps &sProfileProps)
  39. {
  40. ECSESSIONGROUPID ecSessionGroupId;
  41. scoped_rlock lock(m_hMutex);
  42. ECSessionGroupInfo ecSessionGroup = ECSessionGroupInfo(sProfileProps.strServerPath, sProfileProps.strProfileName);
  43. auto result = m_mapSessionGroupIds.insert(SESSIONGROUPIDMAP::value_type(ecSessionGroup, 0));
  44. if (result.second == true) {
  45. // Not found, generate one now
  46. ssl_random((sizeof(ecSessionGroupId) == 8), &ecSessionGroupId);
  47. // Register the new SessionGroupId, this is needed because we are not creating a SessionGroupData
  48. // object yet, and thus we are not putting anything in the m_mapSessionGroups yet. To prevent 2
  49. // threads to obtain 2 different SessionGroup IDs for the same server & profile combination we
  50. // use this separate map containing SessionGroup IDs.
  51. result.first->second = ecSessionGroupId;
  52. }
  53. else {
  54. ecSessionGroupId = result.first->second;
  55. }
  56. return ecSessionGroupId;
  57. }
  58. /*
  59. * Threading comment:
  60. *
  61. * This function is safe since we hold the sessiongroup list lock, and we AddRef() the object before releasing the sessiongroup list
  62. * lock. This means that any Release can run at any time; if a release on the sessiongroup happens during the running of this function,
  63. * we are simply upping the refcount by 1, possibly leaving the refcount at 1 (the release in the other thread leaving the refcount at 0, does nothing)
  64. *
  65. * The object cannot be destroyed during the function since the deletion is done in DeleteSessionGroupDataIfOrphan requires the same lock
  66. * and that is the only place the object can be deleted.
  67. */
  68. HRESULT ECSessionGroupManager::GetSessionGroupData(ECSESSIONGROUPID ecSessionGroupId, const sGlobalProfileProps &sProfileProps, SessionGroupData **lppData)
  69. {
  70. HRESULT hr = hrSuccess;
  71. ECSessionGroupInfo ecSessionGroup = ECSessionGroupInfo(sProfileProps.strServerPath, sProfileProps.strProfileName);
  72. SessionGroupData *lpData = NULL;
  73. scoped_rlock lock(m_hMutex);
  74. auto result = m_mapSessionGroups.insert(SESSIONGROUPMAP::value_type(ecSessionGroup, NULL));
  75. if (result.second == true) {
  76. hr = SessionGroupData::Create(ecSessionGroupId, &ecSessionGroup, sProfileProps, &lpData);
  77. if (hr == hrSuccess)
  78. result.first->second = lpData;
  79. else
  80. m_mapSessionGroups.erase(result.first);
  81. } else {
  82. lpData = result.first->second;
  83. lpData->AddRef();
  84. }
  85. *lppData = lpData;
  86. return hr;
  87. }
  88. HRESULT ECSessionGroupManager::DeleteSessionGroupDataIfOrphan(ECSESSIONGROUPID ecSessionGroupId)
  89. {
  90. HRESULT hr = hrSuccess;
  91. SessionGroupData *lpSessionGroupData = NULL;
  92. ulock_rec biglock(m_hMutex);
  93. auto iter = find_if(m_mapSessionGroups.cbegin(), m_mapSessionGroups.cend(), findSessionGroupId(ecSessionGroupId));
  94. if (iter != m_mapSessionGroups.cend()) {
  95. if(iter->second->IsOrphan()) {
  96. // If the group is an orphan now, we can delete it safely since the only way
  97. // a new session would connect to the sessiongroup would be through us, and we
  98. // hold the mutex.
  99. lpSessionGroupData = iter->second;
  100. m_mapSessionGroups.erase(iter);
  101. }
  102. }
  103. biglock.unlock();
  104. // Delete the object outside the lock; we can do this because nobody can access this group
  105. // now (since it is not in the map anymore), and the delete() will cause a pthread_join(),
  106. // which could be blocked by the m_hMutex.
  107. delete lpSessionGroupData;
  108. return hr;
  109. }