datachannel.hpp 2.7 KB

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