datachannel.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "include.hpp"
  22. #include "message.hpp"
  23. #include "queue.hpp"
  24. #include "reliability.hpp"
  25. #include <atomic>
  26. #include <chrono>
  27. #include <functional>
  28. #include <type_traits>
  29. #include <variant>
  30. namespace rtc {
  31. class SctpTransport;
  32. class PeerConnection;
  33. class DataChannel : public std::enable_shared_from_this<DataChannel>, public Channel {
  34. public:
  35. DataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label, string protocol,
  36. Reliability reliability);
  37. virtual ~DataChannel();
  38. uint16_t stream() const;
  39. uint16_t id() const;
  40. string label() const;
  41. string protocol() const;
  42. Reliability reliability() const;
  43. void close(void) override;
  44. bool send(message_variant data) override;
  45. bool send(const byte *data, size_t size);
  46. template <typename Buffer> bool sendBuffer(const Buffer &buf);
  47. template <typename Iterator> bool sendBuffer(Iterator first, Iterator last);
  48. bool isOpen(void) const override;
  49. bool isClosed(void) const override;
  50. size_t maxMessageSize() const override;
  51. // Extended API
  52. size_t availableAmount() const override;
  53. std::optional<message_variant> receive() override;
  54. std::optional<message_variant> peek() override;
  55. protected:
  56. virtual void open(std::shared_ptr<SctpTransport> transport);
  57. virtual void processOpenMessage(message_ptr message);
  58. void remoteClose();
  59. bool outgoing(message_ptr message);
  60. void incoming(message_ptr message);
  61. const std::weak_ptr<PeerConnection> mPeerConnection;
  62. std::weak_ptr<SctpTransport> mSctpTransport;
  63. uint16_t mStream;
  64. string mLabel;
  65. string mProtocol;
  66. std::shared_ptr<Reliability> mReliability;
  67. std::atomic<bool> mIsOpen = false;
  68. std::atomic<bool> mIsClosed = false;
  69. private:
  70. Queue<message_ptr> mRecvQueue;
  71. friend class PeerConnection;
  72. };
  73. class NegociatedDataChannel final : public DataChannel {
  74. public:
  75. NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label,
  76. string protocol, Reliability reliability);
  77. NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, std::weak_ptr<SctpTransport> transport,
  78. uint16_t stream);
  79. ~NegociatedDataChannel();
  80. private:
  81. void open(std::shared_ptr<SctpTransport> transport) override;
  82. void processOpenMessage(message_ptr message) override;
  83. friend class PeerConnection;
  84. };
  85. template <typename Buffer> std::pair<const byte *, size_t> to_bytes(const Buffer &buf) {
  86. using T = typename std::remove_pointer<decltype(buf.data())>::type;
  87. using E = typename std::conditional<std::is_void<T>::value, byte, T>::type;
  88. return std::make_pair(static_cast<const byte *>(static_cast<const void *>(buf.data())),
  89. buf.size() * sizeof(E));
  90. }
  91. template <typename Buffer> bool DataChannel::sendBuffer(const Buffer &buf) {
  92. auto [bytes, size] = to_bytes(buf);
  93. auto message = std::make_shared<Message>(size);
  94. std::copy(bytes, bytes + size, message->data());
  95. return outgoing(message);
  96. }
  97. template <typename Iterator> bool DataChannel::sendBuffer(Iterator first, Iterator last) {
  98. size_t size = 0;
  99. for (Iterator it = first; it != last; ++it)
  100. size += it->size();
  101. auto message = std::make_shared<Message>(size);
  102. auto pos = message->begin();
  103. for (Iterator it = first; it != last; ++it) {
  104. auto [bytes, len] = to_bytes(*it);
  105. pos = std::copy(bytes, bytes + len, pos);
  106. }
  107. return outgoing(message);
  108. }
  109. } // namespace rtc
  110. #endif