sctptransport.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_SCTP_TRANSPORT_H
  19. #define RTC_SCTP_TRANSPORT_H
  20. #include "include.hpp"
  21. #include "peerconnection.hpp"
  22. #include "processor.hpp"
  23. #include "queue.hpp"
  24. #include "transport.hpp"
  25. #include <condition_variable>
  26. #include <functional>
  27. #include <map>
  28. #include <mutex>
  29. #include <shared_mutex>
  30. #include <unordered_set>
  31. #include "usrsctp.h"
  32. namespace rtc {
  33. class SctpTransport final : public Transport {
  34. public:
  35. static void Init();
  36. static void Cleanup();
  37. using amount_callback = std::function<void(uint16_t streamId, size_t amount)>;
  38. SctpTransport(std::shared_ptr<Transport> lower, uint16_t port, std::optional<size_t> mtu,
  39. message_callback recvCallback, amount_callback bufferedAmountCallback,
  40. state_callback stateChangeCallback);
  41. ~SctpTransport();
  42. void start() override;
  43. bool stop() override;
  44. bool send(message_ptr message) override; // false if buffered
  45. void closeStream(unsigned int stream);
  46. void flush();
  47. // Stats
  48. void clearStats();
  49. size_t bytesSent();
  50. size_t bytesReceived();
  51. std::optional<std::chrono::milliseconds> rtt();
  52. private:
  53. // Order seems wrong but these are the actual values
  54. // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-8
  55. enum PayloadId : uint32_t {
  56. PPID_CONTROL = 50,
  57. PPID_STRING = 51,
  58. PPID_BINARY_PARTIAL = 52,
  59. PPID_BINARY = 53,
  60. PPID_STRING_PARTIAL = 54,
  61. PPID_STRING_EMPTY = 56,
  62. PPID_BINARY_EMPTY = 57
  63. };
  64. void connect();
  65. void shutdown();
  66. void close();
  67. void incoming(message_ptr message) override;
  68. bool outgoing(message_ptr message) override;
  69. void doRecv();
  70. bool trySendQueue();
  71. bool trySendMessage(message_ptr message);
  72. void updateBufferedAmount(uint16_t streamId, long delta);
  73. void sendReset(uint16_t streamId);
  74. bool safeFlush();
  75. void handleUpcall();
  76. int handleWrite(byte *data, size_t len, uint8_t tos, uint8_t set_df);
  77. void processData(binary &&data, uint16_t streamId, PayloadId ppid);
  78. void processNotification(const union sctp_notification *notify, size_t len);
  79. const uint16_t mPort;
  80. struct socket *mSock;
  81. Processor mProcessor;
  82. std::atomic<int> mPendingRecvCount;
  83. std::mutex mRecvMutex, mSendMutex;
  84. Queue<message_ptr> mSendQueue;
  85. std::map<uint16_t, size_t> mBufferedAmount;
  86. amount_callback mBufferedAmountCallback;
  87. std::mutex mWriteMutex;
  88. std::condition_variable mWrittenCondition;
  89. std::atomic<bool> mWritten = false; // written outside lock
  90. std::atomic<bool> mWrittenOnce = false; // same
  91. binary mPartialMessage, mPartialNotification;
  92. binary mPartialStringData, mPartialBinaryData;
  93. // Stats
  94. std::atomic<size_t> mBytesSent = 0, mBytesReceived = 0;
  95. static void UpcallCallback(struct socket *sock, void *arg, int flags);
  96. static int WriteCallback(void *sctp_ptr, void *data, size_t len, uint8_t tos, uint8_t set_df);
  97. static std::unordered_set<SctpTransport *> Instances;
  98. static std::shared_mutex InstancesMutex;
  99. };
  100. } // namespace rtc
  101. #endif