datachannel.hpp 2.8 KB

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