include.hpp 2.5 KB

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