include.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright (c) 2019 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. #ifndef RTC_INCLUDE_H
  19. #define RTC_INCLUDE_H
  20. #ifndef RTC_ENABLE_MEDIA
  21. #define RTC_ENABLE_MEDIA 1
  22. #endif
  23. #ifndef RTC_ENABLE_WEBSOCKET
  24. #define RTC_ENABLE_WEBSOCKET 1
  25. #endif
  26. #ifdef _WIN32
  27. #define RTC_CPP_EXPORT __declspec(dllexport)
  28. #ifndef _WIN32_WINNT
  29. #define _WIN32_WINNT 0x0602 // Windows 8
  30. #endif
  31. #ifdef _MSC_VER
  32. #pragma warning(disable : 4251) // disable "X needs to have dll-interface..."
  33. #endif
  34. #else
  35. #define RTC_CPP_EXPORT
  36. #endif
  37. #include "log.hpp"
  38. #include <cstddef>
  39. #include <functional>
  40. #include <memory>
  41. #include <mutex>
  42. #include <optional>
  43. #include <string>
  44. #include <string_view>
  45. #include <vector>
  46. namespace rtc {
  47. using std::byte;
  48. using std::string;
  49. using std::string_view;
  50. using binary = std::vector<byte>;
  51. using binary_ptr = std::shared_ptr<binary>;
  52. using std::nullopt;
  53. using std::size_t;
  54. using std::uint16_t;
  55. using std::uint32_t;
  56. using std::uint64_t;
  57. using std::uint8_t;
  58. const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
  59. const size_t MAX_NUMERICSERV_LEN = 6; // Max port string representation length
  60. const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
  61. const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536; // Remote max message size if not specified in SDP
  62. const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
  63. const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
  64. const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool
  65. const size_t DEFAULT_IPV4_MTU = 1200; // IPv4 safe MTU value recommended by RFC 8261
  66. // overloaded helper
  67. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  68. template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  69. // weak_ptr bind helper
  70. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
  71. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
  72. using result_type = typename decltype(bound)::result_type;
  73. if (auto shared_this = weak_this.lock())
  74. return bound(args...);
  75. else
  76. return static_cast<result_type>(false);
  77. };
  78. }
  79. // scope_guard helper
  80. class scope_guard {
  81. public:
  82. scope_guard(std::function<void()> func) : function(std::move(func)) {}
  83. scope_guard(scope_guard &&other) = delete;
  84. scope_guard(const scope_guard &) = delete;
  85. void operator=(const scope_guard &) = delete;
  86. ~scope_guard() {
  87. if (function)
  88. function();
  89. }
  90. private:
  91. std::function<void()> function;
  92. };
  93. template <typename... Args> class synchronized_callback {
  94. public:
  95. synchronized_callback() = default;
  96. synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
  97. synchronized_callback(const synchronized_callback &cb) { *this = cb; }
  98. synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
  99. ~synchronized_callback() { *this = nullptr; }
  100. synchronized_callback &operator=(synchronized_callback &&cb) {
  101. std::scoped_lock lock(mutex, cb.mutex);
  102. callback = std::move(cb.callback);
  103. cb.callback = nullptr;
  104. return *this;
  105. }
  106. synchronized_callback &operator=(const synchronized_callback &cb) {
  107. std::scoped_lock lock(mutex, cb.mutex);
  108. callback = cb.callback;
  109. return *this;
  110. }
  111. synchronized_callback &operator=(std::function<void(Args...)> func) {
  112. std::lock_guard lock(mutex);
  113. callback = std::move(func);
  114. return *this;
  115. }
  116. void operator()(Args... args) const {
  117. std::lock_guard lock(mutex);
  118. if (callback)
  119. callback(std::move(args)...);
  120. }
  121. operator bool() const {
  122. std::lock_guard lock(mutex);
  123. return callback ? true : false;
  124. }
  125. std::function<void(Args...)> wrap() const {
  126. return [this](Args... args) { (*this)(std::move(args)...); };
  127. }
  128. private:
  129. std::function<void(Args...)> callback;
  130. mutable std::recursive_mutex mutex;
  131. };
  132. } // namespace rtc
  133. #endif