message.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_MESSAGE_H
  9. #define RTC_MESSAGE_H
  10. #include "common.hpp"
  11. #include "reliability.hpp"
  12. #include <functional>
  13. namespace rtc {
  14. struct RTC_CPP_EXPORT Message : binary {
  15. enum Type { Binary, String, Control, Reset };
  16. Message(const Message &message) = default;
  17. Message(size_t size, Type type_ = Binary) : binary(size), type(type_) {}
  18. template <typename Iterator>
  19. Message(Iterator begin_, Iterator end_, Type type_ = Binary)
  20. : binary(begin_, end_), type(type_) {}
  21. Message(binary &&data, Type type_ = Binary) : binary(std::move(data)), type(type_) {}
  22. Type type;
  23. unsigned int stream = 0; // Stream id (SCTP stream or SSRC)
  24. unsigned int dscp = 0; // Differentiated Services Code Point
  25. shared_ptr<Reliability> reliability;
  26. };
  27. using message_ptr = shared_ptr<Message>;
  28. using message_callback = std::function<void(message_ptr message)>;
  29. using message_vector = std::vector<message_ptr>;
  30. inline size_t message_size_func(const message_ptr &m) {
  31. return m->type == Message::Binary || m->type == Message::String ? m->size() : 0;
  32. }
  33. template <typename Iterator>
  34. message_ptr make_message(Iterator begin, Iterator end, Message::Type type = Message::Binary,
  35. unsigned int stream = 0, shared_ptr<Reliability> reliability = nullptr) {
  36. auto message = std::make_shared<Message>(begin, end, type);
  37. message->stream = stream;
  38. message->reliability = reliability;
  39. return message;
  40. }
  41. RTC_CPP_EXPORT message_ptr make_message(size_t size, Message::Type type = Message::Binary,
  42. unsigned int stream = 0,
  43. shared_ptr<Reliability> reliability = nullptr);
  44. RTC_CPP_EXPORT message_ptr make_message(binary &&data, Message::Type type = Message::Binary,
  45. unsigned int stream = 0,
  46. shared_ptr<Reliability> reliability = nullptr);
  47. RTC_CPP_EXPORT message_ptr make_message(message_variant data);
  48. #if RTC_ENABLE_MEDIA
  49. // Reconstructs a message_ptr from an opaque rtcMessage pointer that
  50. // was allocated by rtcCreateOpaqueMessage().
  51. message_ptr make_message_from_opaque_ptr(rtcMessage *&&message);
  52. #endif
  53. RTC_CPP_EXPORT message_variant to_variant(Message &&message);
  54. RTC_CPP_EXPORT message_variant to_variant(const Message &message);
  55. } // namespace rtc
  56. #endif