sctptransport.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "queue.hpp"
  23. #include "transport.hpp"
  24. #include <condition_variable>
  25. #include <functional>
  26. #include <map>
  27. #include <mutex>
  28. #include "usrsctp.h"
  29. namespace rtc {
  30. class SctpTransport : public Transport {
  31. public:
  32. enum class State { Disconnected, Connecting, Connected, Failed };
  33. using amount_callback = std::function<void(uint16_t streamId, size_t amount)>;
  34. using state_callback = std::function<void(State state)>;
  35. SctpTransport(std::shared_ptr<Transport> lower, uint16_t port, message_callback recvCallback,
  36. amount_callback bufferedAmountCallback, state_callback stateChangeCallback);
  37. ~SctpTransport();
  38. State state() const;
  39. void stop() override;
  40. bool send(message_ptr message) override; // false if buffered
  41. void flush();
  42. void reset(unsigned int stream);
  43. private:
  44. // Order seems wrong but these are the actual values
  45. // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-8
  46. enum PayloadId : uint32_t {
  47. PPID_CONTROL = 50,
  48. PPID_STRING = 51,
  49. PPID_BINARY_PARTIAL = 52,
  50. PPID_BINARY = 53,
  51. PPID_STRING_PARTIAL = 54,
  52. PPID_STRING_EMPTY = 56,
  53. PPID_BINARY_EMPTY = 57
  54. };
  55. void connect();
  56. void shutdown();
  57. void incoming(message_ptr message) override;
  58. void changeState(State state);
  59. bool trySendQueue();
  60. bool trySendMessage(message_ptr message);
  61. void updateBufferedAmount(uint16_t streamId, long delta);
  62. int handleRecv(struct socket *sock, union sctp_sockstore addr, const byte *data, size_t len,
  63. struct sctp_rcvinfo recv_info, int flags);
  64. int handleSend(size_t free);
  65. int handleWrite(byte *data, size_t len, uint8_t tos, uint8_t set_df);
  66. void processData(const byte *data, size_t len, uint16_t streamId, PayloadId ppid);
  67. void processNotification(const union sctp_notification *notify, size_t len);
  68. const uint16_t mPort;
  69. struct socket *mSock;
  70. std::mutex mSendMutex;
  71. Queue<message_ptr> mSendQueue;
  72. std::map<uint16_t, size_t> mBufferedAmount;
  73. amount_callback mBufferedAmountCallback;
  74. std::recursive_mutex mWriteMutex;
  75. std::condition_variable_any mWrittenCondition;
  76. bool mWritten = false;
  77. bool mWrittenOnce = false;
  78. std::atomic<bool> mShutdown = false;
  79. state_callback mStateChangeCallback;
  80. std::atomic<State> mState;
  81. binary mPartialRecv, mPartialStringData, mPartialBinaryData;
  82. static int RecvCallback(struct socket *sock, union sctp_sockstore addr, void *data, size_t len,
  83. struct sctp_rcvinfo recv_info, int flags, void *user_data);
  84. static int SendCallback(struct socket *sock, uint32_t sb_free);
  85. static int WriteCallback(void *sctp_ptr, void *data, size_t len, uint8_t tos, uint8_t set_df);
  86. void GlobalInit();
  87. void GlobalCleanup();
  88. static std::mutex GlobalMutex;
  89. static int InstancesCount;
  90. };
  91. } // namespace rtc
  92. #endif