init.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * Copyright (c) 2020-2022 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "init.hpp"
  19. #include "internals.hpp"
  20. #include "certificate.hpp"
  21. #include "dtlstransport.hpp"
  22. #include "pollservice.hpp"
  23. #include "sctptransport.hpp"
  24. #include "icetransport.hpp"
  25. #include "threadpool.hpp"
  26. #include "tls.hpp"
  27. #if RTC_ENABLE_WEBSOCKET
  28. #include "tlstransport.hpp"
  29. #endif
  30. #if RTC_ENABLE_MEDIA
  31. #include "dtlssrtptransport.hpp"
  32. #endif
  33. #ifdef _WIN32
  34. #include <winsock2.h>
  35. #endif
  36. namespace rtc::impl {
  37. struct Init::TokenPayload {
  38. TokenPayload(std::shared_future<void> *cleanupFuture) {
  39. Init::Instance().doInit();
  40. if (cleanupFuture)
  41. *cleanupFuture = cleanupPromise.get_future().share();
  42. }
  43. ~TokenPayload() {
  44. std::thread t(
  45. [](std::promise<void> promise) {
  46. try {
  47. Init::Instance().doCleanup();
  48. promise.set_value();
  49. } catch (const std::exception &e) {
  50. PLOG_WARNING << e.what();
  51. promise.set_exception(std::make_exception_ptr(e));
  52. }
  53. },
  54. std::move(cleanupPromise));
  55. t.detach();
  56. }
  57. std::promise<void> cleanupPromise;
  58. };
  59. Init &Init::Instance() {
  60. static Init *instance = new Init;
  61. return *instance;
  62. }
  63. Init::Init() {
  64. std::promise<void> p;
  65. p.set_value();
  66. mCleanupFuture = p.get_future(); // make it ready
  67. }
  68. Init::~Init() {}
  69. init_token Init::token() {
  70. std::lock_guard lock(mMutex);
  71. if (auto locked = mWeak.lock())
  72. return locked;
  73. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  74. mWeak = *mGlobal;
  75. return *mGlobal;
  76. }
  77. void Init::preload() {
  78. std::lock_guard lock(mMutex);
  79. if (!mGlobal) {
  80. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  81. mWeak = *mGlobal;
  82. }
  83. }
  84. std::shared_future<void> Init::cleanup() {
  85. std::lock_guard lock(mMutex);
  86. mGlobal.reset();
  87. return mCleanupFuture;
  88. }
  89. void Init::setSctpSettings(SctpSettings s) {
  90. std::lock_guard lock(mMutex);
  91. if (mGlobal)
  92. SctpTransport::SetSettings(s);
  93. mCurrentSctpSettings = std::move(s); // store for next init
  94. }
  95. void Init::doInit() {
  96. // mMutex needs to be locked
  97. if (std::exchange(mInitialized, true))
  98. return;
  99. PLOG_DEBUG << "Global initialization";
  100. #ifdef _WIN32
  101. WSADATA wsaData;
  102. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  103. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  104. #endif
  105. ThreadPool::Instance().spawn(THREADPOOL_SIZE);
  106. #if RTC_ENABLE_WEBSOCKET
  107. PollService::Instance().start();
  108. #endif
  109. #if USE_GNUTLS
  110. // Nothing to do
  111. #else
  112. openssl::init();
  113. #endif
  114. SctpTransport::Init();
  115. SctpTransport::SetSettings(mCurrentSctpSettings);
  116. DtlsTransport::Init();
  117. #if RTC_ENABLE_WEBSOCKET
  118. TlsTransport::Init();
  119. #endif
  120. #if RTC_ENABLE_MEDIA
  121. DtlsSrtpTransport::Init();
  122. #endif
  123. }
  124. void Init::doCleanup() {
  125. std::lock_guard lock(mMutex);
  126. if (mGlobal)
  127. return;
  128. if (!std::exchange(mInitialized, false))
  129. return;
  130. PLOG_DEBUG << "Global cleanup";
  131. ThreadPool::Instance().join();
  132. ThreadPool::Instance().clear();
  133. #if RTC_ENABLE_WEBSOCKET
  134. PollService::Instance().join();
  135. #endif
  136. SctpTransport::Cleanup();
  137. DtlsTransport::Cleanup();
  138. #if RTC_ENABLE_WEBSOCKET
  139. TlsTransport::Cleanup();
  140. #endif
  141. #if RTC_ENABLE_MEDIA
  142. DtlsSrtpTransport::Cleanup();
  143. #endif
  144. #ifdef _WIN32
  145. WSACleanup();
  146. #endif
  147. }
  148. } // namespace rtc::impl