include.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <cstddef>
  21. #include <functional>
  22. #include <memory>
  23. #include <mutex>
  24. #include <optional>
  25. #include <string>
  26. #include <vector>
  27. #include <plog/Appenders/ColorConsoleAppender.h>
  28. #include <plog/Log.h>
  29. namespace rtc {
  30. using std::byte;
  31. using std::string;
  32. using binary = std::vector<byte>;
  33. using std::nullopt;
  34. using std::size_t;
  35. using std::uint16_t;
  36. using std::uint32_t;
  37. using std::uint64_t;
  38. using std::uint8_t;
  39. const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
  40. const size_t MAX_NUMERICSERV_LEN = 6; // Max port string representation length
  41. const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
  42. const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536; // Remote max message size if not specified in SDP
  43. const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
  44. inline void InitLogger(plog::Severity severity, plog::IAppender *appender = nullptr) {
  45. static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
  46. if (!appender)
  47. appender = &consoleAppender;
  48. plog::init(severity, appender);
  49. PLOG_DEBUG << "Logger initialized";
  50. }
  51. // Don't change, it must match plog severity
  52. enum class LogLevel {
  53. None = 0,
  54. Fatal = 1,
  55. Error = 2,
  56. Warning = 3,
  57. Info = 4,
  58. Debug = 5,
  59. Verbose = 6
  60. };
  61. inline void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
  62. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  63. template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
  64. template <typename... P> class synchronized_callback {
  65. public:
  66. synchronized_callback() = default;
  67. ~synchronized_callback() { *this = nullptr; }
  68. synchronized_callback &operator=(std::function<void(P...)> func) {
  69. std::lock_guard lock(mutex);
  70. callback = func;
  71. return *this;
  72. }
  73. void operator()(P... args) const {
  74. std::lock_guard lock(mutex);
  75. if (callback)
  76. callback(args...);
  77. }
  78. operator bool() const { return callback ? true : false; }
  79. private:
  80. std::function<void(P...)> callback;
  81. mutable std::recursive_mutex mutex;
  82. };
  83. }
  84. #endif