2
0

init.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "dtlstransport.hpp"
  20. #include "sctptransport.hpp"
  21. #ifdef _WIN32
  22. #include <winsock2.h>
  23. #endif
  24. #if USE_GNUTLS
  25. // Nothing to do
  26. #else
  27. #include <openssl/err.h>
  28. #include <openssl/ssl.h>
  29. #endif
  30. using std::shared_ptr;
  31. namespace rtc {
  32. std::weak_ptr<Init> Init::Weak;
  33. init_token Init::Global;
  34. std::mutex Init::Mutex;
  35. init_token Init::Token() {
  36. std::lock_guard lock(Mutex);
  37. if (!Global) {
  38. if (auto token = Weak.lock())
  39. Global = token;
  40. else
  41. Global = shared_ptr<Init>(new Init());
  42. }
  43. return Global;
  44. }
  45. void Init::Cleanup() { Global.reset(); }
  46. Init::Init() {
  47. #ifdef _WIN32
  48. WSADATA wsaData;
  49. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  50. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  51. #endif
  52. #if USE_GNUTLS
  53. // Nothing to do
  54. #else
  55. OPENSSL_init_ssl(0, NULL);
  56. SSL_load_error_strings();
  57. ERR_load_crypto_strings();
  58. #endif
  59. DtlsTransport::Init();
  60. SctpTransport::Init();
  61. }
  62. Init::~Init() {
  63. DtlsTransport::Cleanup();
  64. SctpTransport::Cleanup();
  65. #ifdef _WIN32
  66. WSACleanup();
  67. #endif
  68. }
  69. } // namespace rtc