OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_API_QUICDATACHANNEL_H_ |
| 12 #define WEBRTC_API_QUICDATACHANNEL_H_ |
| 13 |
| 14 #include <string> |
| 15 #include <unordered_map> |
| 16 #include <unordered_set> |
| 17 |
| 18 #include "webrtc/api/datachannelinterface.h" |
| 19 #include "webrtc/base/asyncinvoker.h" |
| 20 #include "webrtc/base/sigslot.h" |
| 21 #include "webrtc/base/thread.h" |
| 22 |
| 23 namespace cricket { |
| 24 class QuicTransportChannel; |
| 25 class ReliableQuicStream; |
| 26 class TransportChannel; |
| 27 } // namepsace cricket |
| 28 |
| 29 namespace net { |
| 30 // TODO(mikescarlett): Make this uint64_t once QUIC uses 64-bit ids. |
| 31 typedef uint32_t QuicStreamId; |
| 32 } // namespace net |
| 33 |
| 34 namespace rtc { |
| 35 class CopyOnWriteBuffer; |
| 36 } // namespace rtc |
| 37 |
| 38 namespace webrtc { |
| 39 |
| 40 // Encodes a QUIC message header with the data channel ID and message ID, then |
| 41 // stores the result in |header|. |
| 42 void WriteQuicDataChannelMessageHeader(int data_channel_id, |
| 43 uint64_t message_id, |
| 44 rtc::CopyOnWriteBuffer* header); |
| 45 |
| 46 // Decodes the data channel ID and message ID from the initial data received by |
| 47 // an incoming QUIC stream. The data channel ID is output to |data_channel_id|, |
| 48 // the message ID is output to |message_id|, and the number of bytes read is |
| 49 // output to |bytes_read|. Returns false if either ID cannot be read. |
| 50 bool ParseQuicDataMessageHeader(const char* data, |
| 51 size_t len, |
| 52 int* data_channel_id, |
| 53 uint64_t* message_id, |
| 54 size_t* bytes_read); |
| 55 |
| 56 // QuicDataChannel is an implementation of DataChannelInterface based on the |
| 57 // QUIC protocol. It uses a QuicTransportChannel to establish encryption and |
| 58 // transfer data, and a QuicDataTransport to receive incoming messages at |
| 59 // the correct data channel. Currently this class implements unordered, reliable |
| 60 // delivery and does not send an "OPEN" message. |
| 61 // |
| 62 // Each time a message is sent: |
| 63 // |
| 64 // - The QuicDataChannel prepends it with the data channel id and message id. |
| 65 // The QuicTransportChannel creates a ReliableQuicStream, then the |
| 66 // ReliableQuicStream sends the message with a FIN. |
| 67 // |
| 68 // - The remote QuicSession creates a ReliableQuicStream to receive the data. |
| 69 // The remote QuicDataTransport dispatches the ReliableQuicStream to the |
| 70 // QuicDataChannel with the same id as this data channel. |
| 71 // |
| 72 // - The remote QuicDataChannel queues data from the ReliableQuicStream. Once |
| 73 // it receives a QUIC stream frame with a FIN, it provides the message to the |
| 74 // DataChannelObserver. |
| 75 // |
| 76 // TODO(mikescarlett): Implement ordered delivery, unreliable delivery, and |
| 77 // an OPEN message similar to the one for SCTP. |
| 78 class QuicDataChannel : public rtc::RefCountedObject<DataChannelInterface>, |
| 79 public sigslot::has_slots<> { |
| 80 public: |
| 81 // Message stores buffered data from the incoming QUIC stream. The QUIC stream |
| 82 // is provided so that remaining data can be received from the remote peer. |
| 83 struct Message { |
| 84 uint64_t id; |
| 85 rtc::CopyOnWriteBuffer buffer; |
| 86 cricket::ReliableQuicStream* stream; |
| 87 }; |
| 88 |
| 89 QuicDataChannel(rtc::Thread* signaling_thread, |
| 90 rtc::Thread* worker_thread, |
| 91 const std::string& label, |
| 92 const DataChannelInit& config); |
| 93 ~QuicDataChannel() override; |
| 94 |
| 95 // DataChannelInterface overrides. |
| 96 std::string label() const override { return label_; } |
| 97 bool reliable() const override { return true; } |
| 98 bool ordered() const override { return false; } |
| 99 uint16_t maxRetransmitTime() const override { return -1; } |
| 100 uint16_t maxRetransmits() const override { return -1; } |
| 101 bool negotiated() const override { return false; } |
| 102 int id() const override { return id_; } |
| 103 DataState state() const override { return state_; } |
| 104 uint64_t buffered_amount() const override { return buffered_amount_; } |
| 105 std::string protocol() const override { return protocol_; } |
| 106 void RegisterObserver(DataChannelObserver* observer) override; |
| 107 void UnregisterObserver() override; |
| 108 void Close() override; |
| 109 bool Send(const DataBuffer& buffer) override; |
| 110 |
| 111 // Called from QuicDataTransport to set the QUIC transport channel that the |
| 112 // QuicDataChannel sends messages with. Returns false if a different QUIC |
| 113 // transport channel is already set or |channel| is NULL. |
| 114 // |
| 115 // The QUIC transport channel is not set in the constructor to allow creating |
| 116 // the QuicDataChannel before the PeerConnection has a QUIC transport channel, |
| 117 // such as before the session description is not set. |
| 118 bool SetTransportChannel(cricket::QuicTransportChannel* channel); |
| 119 |
| 120 // Called from QuicDataTransport when an incoming ReliableQuicStream is |
| 121 // receiving a message received for this data channel. Once this function is |
| 122 // called, |message| is owned by the QuicDataChannel and should not be |
| 123 // accessed by the QuicDataTransport. |
| 124 void OnIncomingMessage(Message&& message); |
| 125 |
| 126 // Methods for testing. |
| 127 // Gets the number of outgoing QUIC streams with write blocked data that are |
| 128 // currently open for this data channel and are not finished writing a |
| 129 // message. This is equivalent to the size of |write_blocked_quic_streams_|. |
| 130 size_t GetNumWriteBlockedStreams() const; |
| 131 // Gets the number of incoming QUIC streams with buffered data that are |
| 132 // currently open for this data channel and are not finished receiving a |
| 133 // message. This is equivalent to the size of |incoming_quic_messages_|. |
| 134 size_t GetNumIncomingStreams() const; |
| 135 |
| 136 private: |
| 137 // Callbacks from ReliableQuicStream. |
| 138 // Called when an incoming QUIC stream in |incoming_quic_messages_| has |
| 139 // received a QUIC stream frame. |
| 140 void OnDataReceived(net::QuicStreamId stream_id, |
| 141 const char* data, |
| 142 size_t len); |
| 143 // Called when a write blocked QUIC stream that has been added to |
| 144 // |write_blocked_quic_streams_| is closed. |
| 145 void OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, int error); |
| 146 // Called when an incoming QUIC stream that has been added to |
| 147 // |incoming_quic_messages_| is closed. |
| 148 void OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, int error); |
| 149 // Called when a write blocked QUIC stream in |write_blocked_quic_streams_| |
| 150 // has written previously queued data. |
| 151 void OnQueuedBytesWritten(net::QuicStreamId stream_id, |
| 152 uint64_t queued_bytes_written); |
| 153 |
| 154 // Callbacks from |quic_transport_channel_|. |
| 155 void OnReadyToSend(cricket::TransportChannel* channel); |
| 156 void OnConnectionClosed(); |
| 157 |
| 158 // Worker thread methods. |
| 159 // Sends the data buffer to the remote peer using an outgoing QUIC stream. |
| 160 // Returns true if the data buffer can be successfully sent, or if it is |
| 161 // queued to be sent later. |
| 162 bool Send_w(const DataBuffer& buffer); |
| 163 // Connects the |quic_transport_channel_| signals to this QuicDataChannel. |
| 164 void SetTransportChannel_w(); |
| 165 // Closes the QUIC streams associated with this QuicDataChannel. |
| 166 void Close_w(); |
| 167 // Sets |buffered_amount_|. |
| 168 void SetBufferedAmount_w(uint64_t buffered_amount); |
| 169 |
| 170 // Signaling thread methods. |
| 171 // Triggers QuicDataChannelObserver::OnMessage when a message from the remote |
| 172 // peer is ready to be read. |
| 173 void OnMessage_s(const DataBuffer& received_data); |
| 174 // Triggers QuicDataChannel::OnStateChange if the state change is valid. |
| 175 // Otherwise does nothing if |state| == |state_| or |state| != kClosed when |
| 176 // the data channel is closing. |
| 177 void SetState_s(DataState state); |
| 178 // Triggers QuicDataChannelObserver::OnBufferedAmountChange when the total |
| 179 // buffered data changes for a QUIC stream. |
| 180 void OnBufferedAmountChange_s(uint64_t buffered_amount); |
| 181 |
| 182 // QUIC transport channel which owns the QUIC session. It is used to create |
| 183 // a QUIC stream for sending outgoing messages. |
| 184 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr; |
| 185 // Signaling thread for DataChannelInterface methods. |
| 186 rtc::Thread* const signaling_thread_; |
| 187 // Worker thread for sending data and |quic_transport_channel_| callbacks. |
| 188 rtc::Thread* const worker_thread_; |
| 189 rtc::AsyncInvoker invoker_; |
| 190 // Map of QUIC stream ID => ReliableQuicStream* for write blocked QUIC |
| 191 // streams. |
| 192 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*> |
| 193 write_blocked_quic_streams_; |
| 194 // Map of QUIC stream ID => Message for each incoming QUIC stream. |
| 195 std::unordered_map<net::QuicStreamId, Message> incoming_quic_messages_; |
| 196 // Handles received data from the remote peer and data channel state changes. |
| 197 DataChannelObserver* observer_ = nullptr; |
| 198 // QuicDataChannel ID. |
| 199 int id_; |
| 200 // Connectivity state of the QuicDataChannel. |
| 201 DataState state_; |
| 202 // Total bytes that are buffered among the QUIC streams. |
| 203 uint64_t buffered_amount_; |
| 204 // Counter for number of sent messages that is used for message IDs. |
| 205 uint64_t next_message_id_; |
| 206 |
| 207 // Variables for application use. |
| 208 const std::string& label_; |
| 209 const std::string& protocol_; |
| 210 }; |
| 211 |
| 212 } // namespace webrtc |
| 213 |
| 214 #endif // WEBRTC_API_QUICDATACHANNEL_H_ |
OLD | NEW |