init.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Copyright (c) 2020 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 "impl/certificate.hpp"
  21. #include "impl/dtlstransport.hpp"
  22. #include "impl/sctptransport.hpp"
  23. #include "impl/threadpool.hpp"
  24. #include "impl/tls.hpp"
  25. #if RTC_ENABLE_WEBSOCKET
  26. #include "impl/tlstransport.hpp"
  27. #endif
  28. #if RTC_ENABLE_MEDIA
  29. #include "impl/dtlssrtptransport.hpp"
  30. #endif
  31. #ifdef _WIN32
  32. #include <winsock2.h>
  33. #endif
  34. namespace rtc {
  35. struct Init::TokenPayload {
  36. TokenPayload(std::shared_future<void> *cleanupFuture) {
  37. Init::Instance().doInit();
  38. if(cleanupFuture)
  39. *cleanupFuture = cleanupPromise.get_future().share();
  40. }
  41. ~TokenPayload() {
  42. std::thread t(
  43. [](std::promise<void> promise) {
  44. try {
  45. Init::Instance().doCleanup();
  46. promise.set_value();
  47. } catch (const std::exception &e) {
  48. PLOG_WARNING << e.what();
  49. promise.set_exception(std::make_exception_ptr(e));
  50. }
  51. },
  52. std::move(cleanupPromise));
  53. t.detach();
  54. }
  55. std::promise<void> cleanupPromise;
  56. };
  57. Init &Init::Instance() {
  58. static Init *instance = new Init;
  59. return *instance;
  60. }
  61. Init::Init() {
  62. std::promise<void> p;
  63. p.set_value();
  64. mCleanupFuture = p.get_future(); // make it ready
  65. }
  66. Init::~Init() {}
  67. init_token Init::token() {
  68. std::lock_guard lock(mMutex);
  69. if (auto locked = mWeak.lock())
  70. return locked;
  71. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  72. mWeak = *mGlobal;
  73. return *mGlobal;
  74. }
  75. void Init::preload() {
  76. std::lock_guard lock(mMutex);
  77. if (!mGlobal) {
  78. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  79. mWeak = *mGlobal;
  80. }
  81. }
  82. std::shared_future<void> Init::cleanup() {
  83. std::lock_guard lock(mMutex);
  84. mGlobal.reset();
  85. return mCleanupFuture;
  86. }
  87. void Init::setSctpSettings(SctpSettings s) {
  88. std::lock_guard lock(mMutex);
  89. if (mGlobal)
  90. impl::SctpTransport::SetSettings(s);
  91. mCurrentSctpSettings = std::move(s); // store for next init
  92. }
  93. void Init::doInit() {
  94. // mMutex needs to be locked
  95. if (std::exchange(mInitialized, true))
  96. return;
  97. PLOG_DEBUG << "Global initialization";
  98. #ifdef _WIN32
  99. WSADATA wsaData;
  100. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  101. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  102. #endif
  103. impl::ThreadPool::Instance().spawn(THREADPOOL_SIZE);
  104. #if USE_GNUTLS
  105. // Nothing to do
  106. #else
  107. openssl::init();
  108. #endif
  109. impl::SctpTransport::Init();
  110. impl::SctpTransport::SetSettings(mCurrentSctpSettings);
  111. impl::DtlsTransport::Init();
  112. #if RTC_ENABLE_WEBSOCKET
  113. impl::TlsTransport::Init();
  114. #endif
  115. #if RTC_ENABLE_MEDIA
  116. impl::DtlsSrtpTransport::Init();
  117. #endif
  118. }
  119. void Init::doCleanup() {
  120. std::lock_guard lock(mMutex);
  121. if (mGlobal)
  122. return;
  123. if (!std::exchange(mInitialized, false))
  124. return;
  125. PLOG_DEBUG << "Global cleanup";
  126. impl::ThreadPool::Instance().join();
  127. impl::SctpTransport::Cleanup();
  128. impl::DtlsTransport::Cleanup();
  129. #if RTC_ENABLE_WEBSOCKET
  130. impl::TlsTransport::Cleanup();
  131. #endif
  132. #if RTC_ENABLE_MEDIA
  133. impl::DtlsSrtpTransport::Cleanup();
  134. #endif
  135. #ifdef _WIN32
  136. WSACleanup();
  137. #endif
  138. }
  139. } // namespace rtc