datachannel.hpp 4.2 KB

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