datachannel.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_IMPL_DATA_CHANNEL_H
  19. #define RTC_IMPL_DATA_CHANNEL_H
  20. #include "channel.hpp"
  21. #include "common.hpp"
  22. #include "message.hpp"
  23. #include "peerconnection.hpp"
  24. #include "queue.hpp"
  25. #include "reliability.hpp"
  26. #include "sctptransport.hpp"
  27. #include <atomic>
  28. #include <shared_mutex>
  29. namespace rtc::impl {
  30. struct PeerConnection;
  31. struct DataChannel : Channel, std::enable_shared_from_this<DataChannel> {
  32. static bool IsOpenMessage(message_ptr message);
  33. DataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label, string protocol,
  34. Reliability reliability);
  35. virtual ~DataChannel();
  36. void close();
  37. void remoteClose();
  38. bool outgoing(message_ptr message);
  39. void incoming(message_ptr message);
  40. optional<message_variant> receive() override;
  41. optional<message_variant> peek() override;
  42. size_t availableAmount() const override;
  43. uint16_t stream() const;
  44. string label() const;
  45. string protocol() const;
  46. Reliability reliability() const;
  47. bool isOpen(void) const;
  48. bool isClosed(void) const;
  49. size_t maxMessageSize() const;
  50. virtual void shiftStream();
  51. virtual void open(shared_ptr<SctpTransport> transport);
  52. virtual void processOpenMessage(message_ptr);
  53. protected:
  54. const weak_ptr<impl::PeerConnection> mPeerConnection;
  55. weak_ptr<SctpTransport> mSctpTransport;
  56. uint16_t mStream;
  57. string mLabel;
  58. string mProtocol;
  59. shared_ptr<Reliability> mReliability;
  60. mutable std::shared_mutex mMutex;
  61. Queue<message_ptr> mRecvQueue;
  62. std::atomic<bool> mIsOpen = false;
  63. std::atomic<bool> mIsClosed = false;
  64. };
  65. struct OutgoingDataChannel final : public DataChannel {
  66. OutgoingDataChannel(weak_ptr<PeerConnection> pc, uint16_t stream, string label,
  67. string protocol, Reliability reliability);
  68. ~OutgoingDataChannel();
  69. void shiftStream() override;
  70. void open(shared_ptr<SctpTransport> transport) override;
  71. void processOpenMessage(message_ptr message) override;
  72. };
  73. struct IncomingDataChannel final : public DataChannel {
  74. IncomingDataChannel(weak_ptr<PeerConnection> pc, weak_ptr<SctpTransport> transport,
  75. uint16_t stream);
  76. ~IncomingDataChannel();
  77. void open(shared_ptr<SctpTransport> transport) override;
  78. void processOpenMessage(message_ptr message) override;
  79. };
  80. } // namespace rtc::impl
  81. #endif