datachannel.hpp 3.6 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 : public Channel {
  34. public:
  35. DataChannel(std::shared_ptr<PeerConnection> pc, unsigned int stream, string label,
  36. string protocol, Reliability reliability);
  37. DataChannel(std::shared_ptr<PeerConnection> pc, std::shared_ptr<SctpTransport> transport,
  38. unsigned int stream);
  39. ~DataChannel();
  40. void close(void);
  41. void send(const std::variant<binary, string> &data);
  42. void send(const byte *data, size_t size);
  43. std::optional<std::variant<binary, string>> receive();
  44. template <typename Buffer> void sendBuffer(const Buffer &buf);
  45. template <typename Iterator> void sendBuffer(Iterator first, Iterator last);
  46. size_t available() const;
  47. size_t availableSize() const;
  48. unsigned int stream() const;
  49. string label() const;
  50. string protocol() const;
  51. Reliability reliability() const;
  52. bool isOpen(void) const;
  53. bool isClosed(void) const;
  54. private:
  55. void open(std::shared_ptr<SctpTransport> sctpTransport);
  56. void outgoing(mutable_message_ptr message);
  57. void incoming(message_ptr message);
  58. void processOpenMessage(message_ptr message);
  59. const std::shared_ptr<PeerConnection> mPeerConnection; // keeps the PeerConnection alive
  60. std::shared_ptr<SctpTransport> mSctpTransport;
  61. unsigned int mStream;
  62. string mLabel;
  63. string mProtocol;
  64. std::shared_ptr<Reliability> mReliability;
  65. std::atomic<bool> mIsOpen = false;
  66. std::atomic<bool> mIsClosed = false;
  67. Queue<message_ptr> mRecvQueue;
  68. std::atomic<size_t> mRecvSize = 0;
  69. friend class PeerConnection;
  70. };
  71. template <typename Buffer> std::pair<const byte *, size_t> to_bytes(const Buffer &buf) {
  72. using T = typename std::remove_pointer<decltype(buf.data())>::type;
  73. using E = typename std::conditional<std::is_void<T>::value, byte, T>::type;
  74. return std::make_pair(static_cast<const byte *>(static_cast<const void *>(buf.data())),
  75. buf.size() * sizeof(E));
  76. }
  77. template <typename Buffer> void DataChannel::sendBuffer(const Buffer &buf) {
  78. auto [bytes, size] = to_bytes(buf);
  79. auto message = std::make_shared<Message>(size);
  80. std::copy(bytes, bytes + size, message->data());
  81. outgoing(message);
  82. }
  83. template <typename Iterator> void DataChannel::sendBuffer(Iterator first, Iterator last) {
  84. size_t size = 0;
  85. for (Iterator it = first; it != last; ++it)
  86. size += it->size();
  87. auto message = std::make_shared<Message>(size);
  88. auto pos = message->begin();
  89. for (Iterator it = first; it != last; ++it) {
  90. auto [bytes, size] = to_bytes(*it);
  91. pos = std::copy(bytes, bytes + size, pos);
  92. }
  93. outgoing(message);
  94. }
  95. } // namespace rtc
  96. #endif