include.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_WEBSOCKET
  21. #define RTC_ENABLE_WEBSOCKET 1
  22. #endif
  23. #ifdef _WIN32
  24. #ifndef _WIN32_WINNT
  25. #define _WIN32_WINNT 0x0602
  26. #endif
  27. #endif
  28. #include "log.hpp"
  29. #include <cstddef>
  30. #include <functional>
  31. #include <memory>
  32. #include <mutex>
  33. #include <optional>
  34. #include <string>
  35. #include <vector>
  36. namespace rtc {
  37. using std::byte;
  38. using std::string;
  39. using binary = std::vector<byte>;
  40. using std::nullopt;
  41. using std::size_t;
  42. using std::uint16_t;
  43. using std::uint32_t;
  44. using std::uint64_t;
  45. using std::uint8_t;
  46. const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
  47. const size_t MAX_NUMERICSERV_LEN = 6; // Max port string representation length
  48. const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
  49. const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536; // Remote max message size if not specified in SDP
  50. const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
  51. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  52. template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
  53. template <typename... P> class synchronized_callback {
  54. public:
  55. synchronized_callback() = default;
  56. synchronized_callback(std::function<void(P...)> func) { *this = std::move(func); };
  57. ~synchronized_callback() { *this = nullptr; }
  58. synchronized_callback &operator=(std::function<void(P...)> func) {
  59. std::lock_guard lock(mutex);
  60. callback = std::move(func);
  61. return *this;
  62. }
  63. void operator()(P... args) const {
  64. std::lock_guard lock(mutex);
  65. if (callback)
  66. callback(args...);
  67. }
  68. operator bool() const {
  69. std::lock_guard lock(mutex);
  70. return callback ? true : false;
  71. }
  72. private:
  73. std::function<void(P...)> callback;
  74. mutable std::recursive_mutex mutex;
  75. };
  76. }
  77. #endif