SctpAssociation.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef MS_RTC_SCTP_ASSOCIATION_HPP
  2. #define MS_RTC_SCTP_ASSOCIATION_HPP
  3. #ifdef ENABLE_SCTP
  4. #include <usrsctp.h>
  5. #include "Utils.hpp"
  6. #include "Poller/EventPoller.h"
  7. namespace RTC
  8. {
  9. class SctpEnv;
  10. class SctpStreamParameters
  11. {
  12. public:
  13. uint16_t streamId{ 0u };
  14. bool ordered{ true };
  15. uint16_t maxPacketLifeTime{ 0u };
  16. uint16_t maxRetransmits{ 0u };
  17. };
  18. class SctpAssociation
  19. {
  20. public:
  21. enum class SctpState
  22. {
  23. NEW = 1,
  24. CONNECTING,
  25. CONNECTED,
  26. FAILED,
  27. CLOSED
  28. };
  29. private:
  30. enum class StreamDirection
  31. {
  32. INCOMING = 1,
  33. OUTGOING
  34. };
  35. public:
  36. class Listener
  37. {
  38. public:
  39. virtual void OnSctpAssociationConnecting(RTC::SctpAssociation* sctpAssociation) = 0;
  40. virtual void OnSctpAssociationConnected(RTC::SctpAssociation* sctpAssociation) = 0;
  41. virtual void OnSctpAssociationFailed(RTC::SctpAssociation* sctpAssociation) = 0;
  42. virtual void OnSctpAssociationClosed(RTC::SctpAssociation* sctpAssociation) = 0;
  43. virtual void OnSctpAssociationSendData(
  44. RTC::SctpAssociation* sctpAssociation, const uint8_t* data, size_t len) = 0;
  45. virtual void OnSctpAssociationMessageReceived(
  46. RTC::SctpAssociation* sctpAssociation,
  47. uint16_t streamId,
  48. uint32_t ppid,
  49. const uint8_t* msg,
  50. size_t len) = 0;
  51. };
  52. public:
  53. static bool IsSctp(const uint8_t* data, size_t len)
  54. {
  55. // clang-format off
  56. return (
  57. (len >= 12) &&
  58. // Must have Source Port Number and Destination Port Number set to 5000 (hack).
  59. (Utils::Byte::Get2Bytes(data, 0) == 5000) &&
  60. (Utils::Byte::Get2Bytes(data, 2) == 5000)
  61. );
  62. // clang-format on
  63. }
  64. public:
  65. SctpAssociation(
  66. Listener* listener, uint16_t os, uint16_t mis, size_t maxSctpMessageSize, bool isDataChannel);
  67. virtual ~SctpAssociation();
  68. public:
  69. void TransportConnected();
  70. size_t GetMaxSctpMessageSize() const
  71. {
  72. return this->maxSctpMessageSize;
  73. }
  74. SctpState GetState() const
  75. {
  76. return this->state;
  77. }
  78. void ProcessSctpData(const uint8_t* data, size_t len);
  79. void SendSctpMessage(const RTC::SctpStreamParameters &params, uint32_t ppid, const uint8_t* msg, size_t len);
  80. void HandleDataConsumer(const RTC::SctpStreamParameters &params);
  81. void DataProducerClosed(const RTC::SctpStreamParameters &params);
  82. void DataConsumerClosed(const RTC::SctpStreamParameters &params);
  83. private:
  84. void ResetSctpStream(uint16_t streamId, StreamDirection);
  85. void AddOutgoingStreams(bool force = false);
  86. public:
  87. /* Callbacks fired by usrsctp events. */
  88. virtual void OnUsrSctpSendSctpData(void* buffer, size_t len);
  89. virtual void OnUsrSctpReceiveSctpData(uint16_t streamId, uint16_t ssn, uint32_t ppid, int flags, const uint8_t* data, size_t len);
  90. virtual void OnUsrSctpReceiveSctpNotification(union sctp_notification* notification, size_t len);
  91. private:
  92. // Passed by argument.
  93. Listener* listener{ nullptr };
  94. uint16_t os{ 1024u };
  95. uint16_t mis{ 1024u };
  96. size_t maxSctpMessageSize{ 262144u };
  97. bool isDataChannel{ false };
  98. // Allocated by this.
  99. uint8_t* messageBuffer{ nullptr };
  100. // Others.
  101. SctpState state{ SctpState::NEW };
  102. struct socket* socket{ nullptr };
  103. uint16_t desiredOs{ 0u };
  104. size_t messageBufferLen{ 0u };
  105. uint16_t lastSsnReceived{ 0u }; // Valid for us since no SCTP I-DATA support.
  106. std::shared_ptr<SctpEnv> _env;
  107. };
  108. //保证线程安全
  109. class SctpAssociationImp : public SctpAssociation, public std::enable_shared_from_this<SctpAssociationImp>{
  110. public:
  111. using Ptr = std::shared_ptr<SctpAssociationImp>;
  112. template<typename ... ARGS>
  113. SctpAssociationImp(toolkit::EventPoller::Ptr poller, ARGS &&...args) : SctpAssociation(std::forward<ARGS>(args)...) {
  114. _poller = std::move(poller);
  115. }
  116. ~SctpAssociationImp() override = default;
  117. protected:
  118. void OnUsrSctpSendSctpData(void* buffer, size_t len) override;
  119. void OnUsrSctpReceiveSctpData(uint16_t streamId, uint16_t ssn, uint32_t ppid, int flags, const uint8_t* data, size_t len) override;
  120. void OnUsrSctpReceiveSctpNotification(union sctp_notification* notification, size_t len) override;
  121. private:
  122. toolkit::EventPoller::Ptr _poller;
  123. };
  124. } // namespace RTC
  125. #endif //ENABLE_SCTP
  126. #endif //MS_RTC_SCTP_ASSOCIATION_HPP