datachannel.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 final : public std::enable_shared_from_this<DataChannel>, public Channel {
  34. public:
  35. DataChannel(std::weak_ptr<PeerConnection> pc, unsigned int stream, string label,
  36. string protocol, Reliability reliability);
  37. DataChannel(std::weak_ptr<PeerConnection> pc, std::weak_ptr<SctpTransport> transport,
  38. unsigned int stream);
  39. ~DataChannel();
  40. unsigned int stream() const;
  41. string label() const;
  42. string protocol() const;
  43. Reliability reliability() const;
  44. void close(void) override;
  45. bool send(const std::variant<binary, string> &data) override;
  46. bool send(const byte *data, size_t size);
  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<std::variant<binary, string>> receive() override;
  55. private:
  56. void remoteClose();
  57. void open(std::shared_ptr<SctpTransport> transport);
  58. bool outgoing(message_ptr message);
  59. void incoming(message_ptr message);
  60. void processOpenMessage(message_ptr message);
  61. const std::weak_ptr<PeerConnection> mPeerConnection;
  62. std::weak_ptr<SctpTransport> mSctpTransport;
  63. unsigned int 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. Queue<message_ptr> mRecvQueue;
  70. friend class PeerConnection;
  71. };
  72. template <typename Buffer> std::pair<const byte *, size_t> to_bytes(const Buffer &buf) {
  73. using T = typename std::remove_pointer<decltype(buf.data())>::type;
  74. using E = typename std::conditional<std::is_void<T>::value, byte, T>::type;
  75. return std::make_pair(static_cast<const byte *>(static_cast<const void *>(buf.data())),
  76. buf.size() * sizeof(E));
  77. }
  78. template <typename Buffer> bool DataChannel::sendBuffer(const Buffer &buf) {
  79. auto [bytes, size] = to_bytes(buf);
  80. auto message = std::make_shared<Message>(size);
  81. std::copy(bytes, bytes + size, message->data());
  82. return outgoing(message);
  83. }
  84. template <typename Iterator> bool DataChannel::sendBuffer(Iterator first, Iterator last) {
  85. size_t size = 0;
  86. for (Iterator it = first; it != last; ++it)
  87. size += it->size();
  88. auto message = std::make_shared<Message>(size);
  89. auto pos = message->begin();
  90. for (Iterator it = first; it != last; ++it) {
  91. auto [bytes, len] = to_bytes(*it);
  92. pos = std::copy(bytes, bytes + len, pos);
  93. }
  94. return outgoing(message);
  95. }
  96. } // namespace rtc
  97. #endif