message.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "message.hpp"
  9. namespace rtc {
  10. message_ptr make_message(size_t size, Message::Type type, unsigned int stream,
  11. shared_ptr<Reliability> reliability) {
  12. auto message = std::make_shared<Message>(size, type);
  13. message->stream = stream;
  14. message->reliability = reliability;
  15. return message;
  16. }
  17. message_ptr make_message(binary &&data, Message::Type type, unsigned int stream,
  18. shared_ptr<Reliability> reliability) {
  19. auto message = std::make_shared<Message>(std::move(data), type);
  20. message->stream = stream;
  21. message->reliability = reliability;
  22. return message;
  23. }
  24. message_ptr make_message(size_t size, message_ptr orig) {
  25. if(!orig)
  26. return nullptr;
  27. auto message = std::make_shared<Message>(size, orig->type);
  28. std::copy(orig->begin(), std::min(orig->end(), orig->begin() + size), message->begin());
  29. message->stream = orig->stream;
  30. message->reliability = orig->reliability;
  31. return message;
  32. }
  33. message_ptr make_message(message_variant data) {
  34. return std::visit( //
  35. overloaded{
  36. [&](binary data) { return make_message(std::move(data), Message::Binary); },
  37. [&](string data) {
  38. auto b = reinterpret_cast<const byte *>(data.data());
  39. return make_message(b, b + data.size(), Message::String);
  40. },
  41. },
  42. std::move(data));
  43. }
  44. #if RTC_ENABLE_MEDIA
  45. message_ptr make_message_from_opaque_ptr(rtcMessage *&&message) {
  46. auto ptr = std::unique_ptr<Message>(reinterpret_cast<Message *>(message));
  47. return message_ptr(std::move(ptr));
  48. }
  49. #endif
  50. message_variant to_variant(Message &&message) {
  51. switch (message.type) {
  52. case Message::String:
  53. return string(reinterpret_cast<const char *>(message.data()), message.size());
  54. default:
  55. return std::move(message);
  56. }
  57. }
  58. message_variant to_variant(const Message &message) {
  59. switch (message.type) {
  60. case Message::String:
  61. return string(reinterpret_cast<const char *>(message.data()), message.size());
  62. default:
  63. return message;
  64. }
  65. }
  66. } // namespace rtc