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 class QuicMessageDispatcher; | |
41 | |
42 // QuicDataChannel is an implementation of DataChannelInterface based on the | |
43 // QUIC protocol. It uses a QuicTransportChannel to establish encryption and | |
44 // transfer data, and a QuicMessageDispatcher to receive incoming messages at | |
45 // the correct data channel. Currently this class implements unordered, reliable | |
46 // delivery and does not send an "open" message. | |
47 // | |
48 // Each time a message is sent: | |
49 // | |
50 // - The QuicDataChannel's QuicMessageDispatcher prepends it with the data | |
51 // channel id and message id. The local peer's QuicTransportChannel creates a | |
52 // ReliableQuicStream, then the ReliableQuicStream sends it with a FIN. | |
53 // | |
54 // - The remote QuicSession creates a ReliableQuicStream to receive the data. | |
55 // The remote QuicMessageDispatcher dispatches the ReliableQuicStream to the | |
56 // QuicDataChannel with the same id as this data channel. | |
57 // | |
58 // - The remote QuicDataChannel queues data from the ReliableQuicStream. Once | |
59 // it receives a QUIC stream frame with a FIN, it provides the message to the | |
60 // DataChannelObserver. | |
61 // | |
62 // TODO(mikescarlett): Implement ordered delivery, unreliable delivery, and | |
63 // an OPEN message similar to the one for SCTP. | |
64 class QuicDataChannel : public rtc::RefCountedObject<DataChannelInterface>, | |
65 public sigslot::has_slots<> { | |
66 public: | |
67 QuicDataChannel(rtc::Thread* signaling_thread, | |
68 rtc::Thread* worker_thread, | |
69 const std::string& label, | |
70 const DataChannelInit& config, | |
71 const QuicMessageDispatcher& message_dispatcher); | |
72 ~QuicDataChannel() override; | |
73 | |
74 // DataChannelInterface overrides. | |
75 std::string label() const override { return label_; } | |
76 bool reliable() const override { return true; } | |
77 bool ordered() const override { return false; } | |
78 uint16_t maxRetransmitTime() const override { return -1; } | |
79 uint16_t maxRetransmits() const override { return -1; } | |
80 bool negotiated() const override { return false; } | |
81 int id() const override { return id_; } | |
82 DataState state() const override { return state_; } | |
83 uint64_t buffered_amount() const override { return buffered_amount_; } | |
84 std::string protocol() const override { return protocol_; } | |
85 void RegisterObserver(DataChannelObserver* observer) override; | |
86 void UnregisterObserver() override; | |
87 void Close() override; | |
88 bool Send(const DataBuffer& buffer) override; | |
89 | |
90 // Sets the QUIC transport channel that will send messages. The QUIC transport | |
91 // channel must not already be set. | |
pthatcher1
2016/04/14 17:21:56
Can you specify in the comment why we can't put th
mikescarlett
2016/04/14 22:16:51
Done.
| |
92 void SetTransportChannel(cricket::QuicTransportChannel* channel); | |
pthatcher1
2016/04/14 17:21:56
Should we return a bool for failure if it fails?
mikescarlett
2016/04/14 22:16:51
That's ok I changed it. Previously I was asserting
| |
93 // Gets the number of outgoing QUIC streams with write blocked data that are | |
94 // currently open for this data channel. | |
95 size_t GetNumWriteBlockedStreams() const; | |
pthatcher1
2016/04/14 17:21:57
What is this used for? Can you expand the comment
mikescarlett
2016/04/14 22:16:51
This makes it possible to test that write blocked
| |
96 // Gets the number of incoming QUIC streams with buffered data that are | |
97 // currently open for this data channel. | |
98 size_t GetNumIncomingStreams() const; | |
pthatcher1
2016/04/14 17:21:57
Same here
mikescarlett
2016/04/14 22:16:51
Done.
| |
99 | |
100 private: | |
101 // Message contains the incoming QUIC stream being used to receive a message | |
102 // from the remote peer, and buffered data from the QUIC stream. | |
103 struct Message { | |
104 cricket::ReliableQuicStream* stream; | |
105 rtc::CopyOnWriteBuffer buffer; | |
106 }; | |
107 | |
108 // Callbacks for ReliableQuicStream. | |
109 void OnDataReceived(net::QuicStreamId stream_id, | |
110 const char* data, | |
111 size_t len); | |
112 void OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, int error); | |
113 void OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, int error); | |
114 void OnQueuedBytesWritten(net::QuicStreamId stream_id, | |
115 uint64_t queued_bytes_written); | |
116 | |
117 // Callbacks for |quic_transport_channel_|. | |
118 void OnReadyToSend(cricket::TransportChannel* channel); | |
119 void OnConnectionClosed(); | |
120 | |
121 // Callback for |message_dispatcher_|. | |
pthatcher1
2016/04/14 17:21:56
for message_dispatcher_ or *from* message_dispatch
mikescarlett
2016/04/14 22:16:51
Done.
| |
122 // Called when the first QUIC stream frame for an incoming ReliableQuicStream | |
123 // has been received for this data channel. | |
124 virtual void OnIncomingStream(uint64_t message_id, | |
125 const char* first_bytes, | |
126 size_t len, | |
127 cricket::ReliableQuicStream* stream); | |
128 | |
129 // Worker thread methods. | |
130 // Sends the data buffer to the remote peer using an outgoing QUIC stream. | |
131 // Returns true if the data buffer can be successfully sent. | |
132 bool Send_w(const DataBuffer& buffer); | |
133 // Connects the |quic_transport_channel_| signals to this QuicDataChannel. | |
134 void ConnectTransportChannel_w(); | |
135 // Closes the QUIC streams associated with this QuicDataChannel. | |
136 void Close_w(); | |
137 | |
138 // Signaling thread methods. | |
139 // Triggers QuicDataChannelObserver::OnMessage when a message from the remote | |
140 // peer is ready to be read. | |
141 void OnMessage_s(const DataBuffer& received_data); | |
142 // Triggers QuicDataChannel::OnStateChange if the state change is valid. | |
143 // Otherwise does nothing if |state| == |state_| or |state| != kClosed when | |
144 // the data channel is closing. | |
145 void SetState_s(DataState state); | |
146 // Triggers QuicDataChannelObserver::OnBufferedAmountChange when the total | |
147 // buffered data changes for a QUIC stream. | |
148 void OnBufferedAmountChange_s(uint64_t buffered_amount); | |
149 | |
150 // QUIC transport channel which owns the QUIC session. It is used to create | |
151 // a QUIC stream for sending outgoing messages. | |
152 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr; | |
153 // Signaling thread for DataChannelInterface methods. | |
154 rtc::Thread* const signaling_thread_; | |
155 // Worker thread for sending data and |quic_transport_channel_| callbacks. | |
156 rtc::Thread* const worker_thread_; | |
157 // Invokes asyncronous method calls. | |
pthatcher1
2016/04/14 17:21:56
This comment isn't needed.
mikescarlett
2016/04/14 22:16:51
Done.
| |
158 rtc::AsyncInvoker invoker_; | |
159 // Helper for encoding messages with the data channel/message id so they can | |
160 // be dispatched to the appropriate data channel. | |
161 const QuicMessageDispatcher& message_dispatcher_; | |
pthatcher1
2016/04/14 17:21:57
Why are we storing a const ref instead of a const
mikescarlett
2016/04/14 22:16:51
I'm removing this. See explanation below.
| |
162 // Map of QUIC stream ID => ReliableQuicStream* for write blocked QUIC | |
163 // streams. | |
164 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*> | |
165 write_blocked_quic_streams_; | |
166 // Map of QUIC stream ID => Message for each incoming QUIC stream. | |
167 std::unordered_map<net::QuicStreamId, Message> incoming_quic_streams_; | |
168 // Handles received data from the remote peer and data channel state changes. | |
169 DataChannelObserver* observer_ = nullptr; | |
170 // QuicDataChannel id. | |
171 int id_; | |
172 // Connectivity state of the QuicDataChannel. | |
173 DataState state_; | |
174 // Total bytes that are buffered among the QUIC streams. | |
175 uint64_t buffered_amount_; | |
176 // Counter for number of sent messages that is used for message ids. | |
177 uint64_t num_sent_messages_; | |
178 | |
179 // Variables for application use. | |
180 const std::string& label_; | |
181 const std::string& protocol_; | |
182 | |
183 friend QuicMessageDispatcher; | |
pthatcher1
2016/04/14 17:21:57
Why is this necessary?
mikescarlett
2016/04/14 22:16:51
This was for keeping OnIncomingStream private. See
| |
184 }; | |
185 | |
186 // QuicMessageDispatcher is an abstract class that provides incoming QUIC | |
187 // streams to the QuicDataChannel via Dispatch(). It also encodes messages sent | |
188 // by the QuicDataChannel with the data channel id and message id, so that the | |
189 // remote peer's QuicMessageDispatcher can decode these to dispatch the incoming | |
190 // QUIC stream to the correct data channel. | |
191 // | |
192 // Subclasses are responsible for receiving incoming QUIC streams from the | |
193 // QuicTransportChannel, and parsing the data channel ID and message ID from | |
194 // the initial QUIC stream frame. | |
195 class QuicMessageDispatcher { | |
pthatcher1
2016/04/14 17:21:56
I don't quite understand the design of the QuicMes
mikescarlett
2016/04/14 22:16:50
I'm removing this. Its main purpose was to keep On
| |
196 public: | |
197 virtual ~QuicMessageDispatcher() {} | |
198 // Encodes the message header with the data channel ID and message ID before | |
199 // it is sent by a QUIC stream. | |
200 virtual void EncodeHeader(int data_channel_id, | |
201 uint64_t message_id, | |
202 rtc::CopyOnWriteBuffer* header) const = 0; | |
203 | |
204 protected: | |
205 // Dispatches the incoming ReliableQuicStream to the QuicDataChannel. The | |
206 // QuicDataChannel must have this instance as its QuicMessageDispatcher. | |
207 // |message_id| is the ID of the data channel's message, and |first_bytes| | |
208 // contains |len| bytes from the first QUIC stream frame of the incoming | |
209 // ReliableQuicStream. The QuicDataChannel is responsible for reading the | |
210 // remaining data from the ReliableQuicStream. | |
211 void Dispatch(QuicDataChannel* data_channel, | |
212 uint64_t message_id, | |
213 const char* first_bytes, | |
214 size_t len, | |
215 cricket::ReliableQuicStream* stream) const { | |
216 RTC_DCHECK(&data_channel->message_dispatcher_ == this); | |
217 data_channel->OnIncomingStream(message_id, first_bytes, len, stream); | |
218 } | |
219 }; | |
220 | |
221 } // namespace webrtc | |
222 | |
223 #endif // WEBRTC_API_QUICDATACHANNEL_H_ | |
OLD | NEW |