message.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2019-2020 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. #include "message.hpp"
  19. namespace rtc::impl {
  20. message_ptr make_message(size_t size, Message::Type type, unsigned int stream,
  21. shared_ptr<Reliability> reliability) {
  22. auto message = std::make_shared<Message>(size, type);
  23. message->stream = stream;
  24. message->reliability = reliability;
  25. return message;
  26. }
  27. message_ptr make_message(binary &&data, Message::Type type, unsigned int stream,
  28. shared_ptr<Reliability> reliability) {
  29. auto message = std::make_shared<Message>(std::move(data), type);
  30. message->stream = stream;
  31. message->reliability = reliability;
  32. return message;
  33. }
  34. message_ptr make_message(message_variant data) {
  35. return std::visit( //
  36. overloaded{
  37. [&](binary data) { return make_message(std::move(data), Message::Binary); },
  38. [&](string data) {
  39. auto b = reinterpret_cast<const byte *>(data.data());
  40. return make_message(b, b + data.size(), Message::String);
  41. },
  42. },
  43. std::move(data));
  44. }
  45. message_variant to_variant(Message &&message) {
  46. switch (message.type) {
  47. case Message::String:
  48. return string(reinterpret_cast<const char *>(message.data()), message.size());
  49. default:
  50. return std::move(message);
  51. }
  52. }
  53. } // namespace rtc::impl