include.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef _WIN32_WINNT
  28. #define _WIN32_WINNT 0x0602
  29. #endif
  30. #endif
  31. #include "log.hpp"
  32. #include <cstddef>
  33. #include <functional>
  34. #include <memory>
  35. #include <mutex>
  36. #include <optional>
  37. #include <string>
  38. #include <vector>
  39. namespace rtc {
  40. using std::byte;
  41. using std::string;
  42. using binary = std::vector<byte>;
  43. using std::nullopt;
  44. using std::size_t;
  45. using std::uint16_t;
  46. using std::uint32_t;
  47. using std::uint64_t;
  48. using std::uint8_t;
  49. const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
  50. const size_t MAX_NUMERICSERV_LEN = 6; // Max port string representation length
  51. const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
  52. const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536; // Remote max message size if not specified in SDP
  53. const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
  54. const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
  55. const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool
  56. // overloaded helper
  57. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  58. template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
  59. // weak_ptr bind helper
  60. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&... _args) {
  61. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&... args) {
  62. using result_type = typename decltype(bound)::result_type;
  63. if (auto shared_this = weak_this.lock())
  64. return bound(args...);
  65. else
  66. return static_cast<result_type>(false);
  67. };
  68. }
  69. template <typename... P> class synchronized_callback {
  70. public:
  71. synchronized_callback() = default;
  72. synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
  73. synchronized_callback(const synchronized_callback &cb) { *this = cb; }
  74. synchronized_callback(std::function<void(P...)> func) { *this = std::move(func); }
  75. ~synchronized_callback() { *this = nullptr; }
  76. synchronized_callback &operator=(synchronized_callback &&cb) {
  77. std::scoped_lock lock(mutex, cb.mutex);
  78. callback = std::move(cb.callback);
  79. cb.callback = nullptr;
  80. return *this;
  81. }
  82. synchronized_callback &operator=(const synchronized_callback &cb) {
  83. std::scoped_lock lock(mutex, cb.mutex);
  84. callback = cb.callback;
  85. return *this;
  86. }
  87. synchronized_callback &operator=(std::function<void(P...)> func) {
  88. std::lock_guard lock(mutex);
  89. callback = std::move(func);
  90. return *this;
  91. }
  92. void operator()(P... args) const {
  93. std::lock_guard lock(mutex);
  94. if (callback)
  95. callback(args...);
  96. }
  97. operator bool() const {
  98. std::lock_guard lock(mutex);
  99. return callback ? true : false;
  100. }
  101. private:
  102. std::function<void(P...)> callback;
  103. mutable std::recursive_mutex mutex;
  104. };
  105. }
  106. #endif