blob: 347d227fe122175e0712863614607ecf99725958 [file] [log] [blame]
mikescarlett9bc517f2016-04-29 18:30:55 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_QUICDATACHANNEL_H_
12#define PC_QUICDATACHANNEL_H_
mikescarlett9bc517f2016-04-29 18:30:55 -070013
14#include <string>
15#include <unordered_map>
16#include <unordered_set>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/datachannelinterface.h"
19#include "rtc_base/asyncinvoker.h"
20#include "rtc_base/sigslot.h"
21#include "rtc_base/thread.h"
mikescarlett9bc517f2016-04-29 18:30:55 -070022
23namespace cricket {
24class QuicTransportChannel;
25class ReliableQuicStream;
26class TransportChannel;
27} // namepsace cricket
28
29namespace net {
30// TODO(mikescarlett): Make this uint64_t once QUIC uses 64-bit ids.
31typedef uint32_t QuicStreamId;
32} // namespace net
33
34namespace rtc {
35class CopyOnWriteBuffer;
36} // namespace rtc
37
38namespace webrtc {
39
40// Encodes a QUIC message header with the data channel ID and message ID, then
41// stores the result in |header|.
42void 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.
50bool 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.
78class 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,
zhihuangf2c2f8f2016-07-13 14:13:49 -070091 rtc::Thread* network_thread,
mikescarlett9bc517f2016-04-29 18:30:55 -070092 const std::string& label,
93 const DataChannelInit& config);
94 ~QuicDataChannel() override;
95
96 // DataChannelInterface overrides.
97 std::string label() const override { return label_; }
98 bool reliable() const override { return true; }
99 bool ordered() const override { return false; }
100 uint16_t maxRetransmitTime() const override { return -1; }
101 uint16_t maxRetransmits() const override { return -1; }
102 bool negotiated() const override { return false; }
103 int id() const override { return id_; }
104 DataState state() const override { return state_; }
105 uint64_t buffered_amount() const override { return buffered_amount_; }
106 std::string protocol() const override { return protocol_; }
107 void RegisterObserver(DataChannelObserver* observer) override;
108 void UnregisterObserver() override;
109 void Close() override;
110 bool Send(const DataBuffer& buffer) override;
111
112 // Called from QuicDataTransport to set the QUIC transport channel that the
113 // QuicDataChannel sends messages with. Returns false if a different QUIC
114 // transport channel is already set or |channel| is NULL.
115 //
116 // The QUIC transport channel is not set in the constructor to allow creating
117 // the QuicDataChannel before the PeerConnection has a QUIC transport channel,
118 // such as before the session description is not set.
119 bool SetTransportChannel(cricket::QuicTransportChannel* channel);
120
121 // Called from QuicDataTransport when an incoming ReliableQuicStream is
122 // receiving a message received for this data channel. Once this function is
123 // called, |message| is owned by the QuicDataChannel and should not be
124 // accessed by the QuicDataTransport.
125 void OnIncomingMessage(Message&& message);
126
127 // Methods for testing.
128 // Gets the number of outgoing QUIC streams with write blocked data that are
129 // currently open for this data channel and are not finished writing a
130 // message. This is equivalent to the size of |write_blocked_quic_streams_|.
131 size_t GetNumWriteBlockedStreams() const;
132 // Gets the number of incoming QUIC streams with buffered data that are
133 // currently open for this data channel and are not finished receiving a
134 // message. This is equivalent to the size of |incoming_quic_messages_|.
135 size_t GetNumIncomingStreams() const;
136
137 private:
138 // Callbacks from ReliableQuicStream.
139 // Called when an incoming QUIC stream in |incoming_quic_messages_| has
140 // received a QUIC stream frame.
141 void OnDataReceived(net::QuicStreamId stream_id,
142 const char* data,
143 size_t len);
144 // Called when a write blocked QUIC stream that has been added to
145 // |write_blocked_quic_streams_| is closed.
146 void OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, int error);
147 // Called when an incoming QUIC stream that has been added to
148 // |incoming_quic_messages_| is closed.
149 void OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, int error);
150 // Called when a write blocked QUIC stream in |write_blocked_quic_streams_|
151 // has written previously queued data.
152 void OnQueuedBytesWritten(net::QuicStreamId stream_id,
153 uint64_t queued_bytes_written);
154
155 // Callbacks from |quic_transport_channel_|.
156 void OnReadyToSend(cricket::TransportChannel* channel);
157 void OnConnectionClosed();
158
zhihuangf2c2f8f2016-07-13 14:13:49 -0700159 // Network thread methods.
mikescarlett9bc517f2016-04-29 18:30:55 -0700160 // Sends the data buffer to the remote peer using an outgoing QUIC stream.
161 // Returns true if the data buffer can be successfully sent, or if it is
162 // queued to be sent later.
zhihuangf2c2f8f2016-07-13 14:13:49 -0700163 bool Send_n(const DataBuffer& buffer);
164
165 // Worker thread methods.
mikescarlett9bc517f2016-04-29 18:30:55 -0700166 // Connects the |quic_transport_channel_| signals to this QuicDataChannel,
167 // then returns the new QuicDataChannel state.
168 DataState SetTransportChannel_w();
169 // Closes the QUIC streams associated with this QuicDataChannel.
170 void Close_w();
171 // Sets |buffered_amount_|.
172 void SetBufferedAmount_w(uint64_t buffered_amount);
173
174 // Signaling thread methods.
175 // Triggers QuicDataChannelObserver::OnMessage when a message from the remote
176 // peer is ready to be read.
177 void OnMessage_s(const DataBuffer& received_data);
178 // Triggers QuicDataChannel::OnStateChange if the state change is valid.
179 // Otherwise does nothing if |state| == |state_| or |state| != kClosed when
180 // the data channel is closing.
181 void SetState_s(DataState state);
182 // Triggers QuicDataChannelObserver::OnBufferedAmountChange when the total
183 // buffered data changes for a QUIC stream.
184 void OnBufferedAmountChange_s(uint64_t buffered_amount);
185
186 // QUIC transport channel which owns the QUIC session. It is used to create
187 // a QUIC stream for sending outgoing messages.
188 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr;
189 // Signaling thread for DataChannelInterface methods.
190 rtc::Thread* const signaling_thread_;
zhihuangf2c2f8f2016-07-13 14:13:49 -0700191 // Worker thread for |quic_transport_channel_| callbacks.
mikescarlett9bc517f2016-04-29 18:30:55 -0700192 rtc::Thread* const worker_thread_;
zhihuangf2c2f8f2016-07-13 14:13:49 -0700193 // Network thread for sending data and |quic_transport_channel_| callbacks.
194 rtc::Thread* const network_thread_;
mikescarlett9bc517f2016-04-29 18:30:55 -0700195 rtc::AsyncInvoker invoker_;
196 // Map of QUIC stream ID => ReliableQuicStream* for write blocked QUIC
197 // streams.
198 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*>
199 write_blocked_quic_streams_;
200 // Map of QUIC stream ID => Message for each incoming QUIC stream.
201 std::unordered_map<net::QuicStreamId, Message> incoming_quic_messages_;
202 // Handles received data from the remote peer and data channel state changes.
203 DataChannelObserver* observer_ = nullptr;
204 // QuicDataChannel ID.
205 int id_;
206 // Connectivity state of the QuicDataChannel.
207 DataState state_;
208 // Total bytes that are buffered among the QUIC streams.
209 uint64_t buffered_amount_;
210 // Counter for number of sent messages that is used for message IDs.
211 uint64_t next_message_id_;
212
213 // Variables for application use.
214 const std::string& label_;
215 const std::string& protocol_;
216};
217
218} // namespace webrtc
219
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200220#endif // PC_QUICDATACHANNEL_H_