datachannel.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_DATA_CHANNEL_H
  19. #define RTC_DATA_CHANNEL_H
  20. #include "channel.hpp"
  21. #include "common.hpp"
  22. #include "reliability.hpp"
  23. #include <type_traits>
  24. namespace rtc {
  25. namespace impl {
  26. struct DataChannel;
  27. struct PeerConnection;
  28. } // namespace impl
  29. class RTC_CPP_EXPORT DataChannel final : private CheshireCat<impl::DataChannel>, public Channel {
  30. public:
  31. DataChannel(impl_ptr<impl::DataChannel> impl);
  32. virtual ~DataChannel();
  33. optional<uint16_t> stream() const;
  34. optional<uint16_t> id() const;
  35. string label() const;
  36. string protocol() const;
  37. Reliability reliability() const;
  38. bool isOpen(void) const override;
  39. bool isClosed(void) const override;
  40. size_t maxMessageSize() const override;
  41. void close(void) override;
  42. bool send(message_variant data) override;
  43. bool send(const byte *data, size_t size) override;
  44. template <typename Buffer> bool sendBuffer(const Buffer &buf);
  45. template <typename Iterator> bool sendBuffer(Iterator first, Iterator last);
  46. private:
  47. using CheshireCat<impl::DataChannel>::impl;
  48. };
  49. template <typename Buffer> std::pair<const byte *, size_t> to_bytes(const Buffer &buf) {
  50. using T = typename std::remove_pointer<decltype(buf.data())>::type;
  51. using E = typename std::conditional<std::is_void<T>::value, byte, T>::type;
  52. return std::make_pair(static_cast<const byte *>(static_cast<const void *>(buf.data())),
  53. buf.size() * sizeof(E));
  54. }
  55. template <typename Buffer> bool DataChannel::sendBuffer(const Buffer &buf) {
  56. auto [bytes, size] = to_bytes(buf);
  57. return send(bytes, size);
  58. }
  59. template <typename Iterator> bool DataChannel::sendBuffer(Iterator first, Iterator last) {
  60. size_t size = 0;
  61. for (Iterator it = first; it != last; ++it)
  62. size += it->size();
  63. binary buffer(size);
  64. byte *pos = buffer.data();
  65. for (Iterator it = first; it != last; ++it) {
  66. auto [bytes, len] = to_bytes(*it);
  67. pos = std::copy(bytes, bytes + len, pos);
  68. }
  69. return send(std::move(buffer));
  70. }
  71. } // namespace rtc
  72. #endif