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 #include "webrtc/api/quicdatachannel.h" | |
12 | |
13 #include "webrtc/base/bind.h" | |
14 #include "webrtc/base/bytebuffer.h" | |
15 #include "webrtc/base/copyonwritebuffer.h" | |
16 #include "webrtc/base/logging.h" | |
17 #include "webrtc/p2p/quic/quictransportchannel.h" | |
18 #include "webrtc/p2p/quic/reliablequicstream.h" | |
19 | |
20 namespace webrtc { | |
21 | |
22 void WriteQuicDataChannelMessageHeader(int data_channel_id, | |
23 uint64_t message_id, | |
24 rtc::CopyOnWriteBuffer* header) { | |
25 RTC_DCHECK(header); | |
26 // 64-bit varints require at most 10 bytes (7*10 == 70), and 32-bit varints | |
27 // require at most 5 bytes (7*5 == 35). | |
28 size_t max_length = 15; | |
29 rtc::ByteBufferWriter byte_buffer(nullptr, max_length, | |
30 rtc::ByteBuffer::ByteOrder::ORDER_HOST); | |
31 byte_buffer.WriteUVarint(data_channel_id); | |
32 byte_buffer.WriteUVarint(message_id); | |
33 header->SetData(byte_buffer.Data(), byte_buffer.Length()); | |
34 } | |
35 | |
36 bool ParseQuicDataMessageHeader(const char* data, | |
37 size_t len, | |
38 int* data_channel_id, | |
39 uint64_t* message_id, | |
40 size_t* bytes_read) { | |
41 RTC_DCHECK(data_channel_id); | |
42 RTC_DCHECK(message_id); | |
43 RTC_DCHECK(bytes_read); | |
44 | |
45 rtc::ByteBufferReader byte_buffer(data, len, rtc::ByteBuffer::ORDER_HOST); | |
46 uint64_t dcid; | |
47 if (!byte_buffer.ReadUVarint(&dcid)) { | |
48 LOG(LS_ERROR) << "Could not read the data channel ID"; | |
49 return false; | |
50 } | |
51 *data_channel_id = dcid; | |
52 if (!byte_buffer.ReadUVarint(message_id)) { | |
53 LOG(LS_ERROR) << "Could not read message ID for data channel " | |
54 << *data_channel_id; | |
55 return false; | |
56 } | |
57 size_t remaining_bytes = byte_buffer.Length(); | |
58 *bytes_read = len - remaining_bytes; | |
59 return true; | |
60 } | |
61 | |
62 QuicDataChannel::QuicDataChannel(rtc::Thread* signaling_thread, | |
63 rtc::Thread* worker_thread, | |
64 const std::string& label, | |
65 const DataChannelInit& config) | |
66 : signaling_thread_(signaling_thread), | |
67 worker_thread_(worker_thread), | |
68 id_(config.id), | |
69 state_(kConnecting), | |
70 buffered_amount_(0), | |
71 next_message_id_(0), | |
72 label_(label), | |
73 protocol_(config.protocol) {} | |
74 | |
75 QuicDataChannel::~QuicDataChannel() {} | |
76 | |
77 void QuicDataChannel::RegisterObserver(DataChannelObserver* observer) { | |
78 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
79 observer_ = observer; | |
80 } | |
81 | |
82 void QuicDataChannel::UnregisterObserver() { | |
83 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
84 observer_ = nullptr; | |
85 } | |
86 | |
87 bool QuicDataChannel::Send(const DataBuffer& buffer) { | |
88 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
89 if (state_ != kOpen) { | |
90 LOG(LS_ERROR) << "QUIC data channel " << id_ | |
91 << " is not open so cannot send."; | |
92 return false; | |
93 } | |
94 return worker_thread_->Invoke<bool>( | |
95 rtc::Bind(&QuicDataChannel::Send_w, this, buffer)); | |
96 } | |
97 | |
98 bool QuicDataChannel::Send_w(const DataBuffer& buffer) { | |
99 RTC_DCHECK(worker_thread_->IsCurrent()); | |
100 | |
101 // Encode and send the header containing the data channel ID and message ID. | |
102 rtc::CopyOnWriteBuffer header; | |
103 WriteQuicDataChannelMessageHeader(id_, ++next_message_id_, &header); | |
104 RTC_DCHECK(quic_transport_channel_); | |
105 cricket::ReliableQuicStream* stream = | |
106 quic_transport_channel_->CreateQuicStream(); | |
107 RTC_DCHECK(stream); | |
108 | |
109 // Send the header with a FIN if the message is empty. | |
110 bool header_fin = (buffer.size() == 0); | |
111 rtc::StreamResult header_result = | |
112 stream->Write(header.data<char>(), header.size(), header_fin); | |
113 | |
114 if (header_result == rtc::SR_BLOCK) { | |
115 // The header is write blocked but we should try sending the message. Since | |
116 // the ReliableQuicStream queues data in order, if the header is write | |
117 // blocked then the message will be write blocked. Otherwise if the message | |
118 // is sent then the header is sent. | |
119 LOG(LS_INFO) << "Stream " << stream->id() | |
120 << " header is write blocked for QUIC data channel " << id_; | |
121 } else if (header_result != rtc::SR_SUCCESS) { | |
122 LOG(LS_ERROR) << "Stream " << stream->id() | |
123 << " failed to write header for QUIC data channel " << id_ | |
124 << ". Unexpected error " << header_result; | |
125 return false; | |
126 } | |
127 | |
128 // If the message is not empty, then send the message with a FIN. | |
129 bool message_fin = true; | |
130 rtc::StreamResult message_result = | |
131 header_fin ? header_result : stream->Write(buffer.data.data<char>(), | |
132 buffer.size(), message_fin); | |
133 | |
134 if (message_result == rtc::SR_SUCCESS) { | |
135 // The message is sent and we don't need this QUIC stream. | |
136 LOG(LS_INFO) << "Stream " << stream->id() | |
137 << " successfully wrote message for QUIC data channel " << id_; | |
138 stream->Close(); | |
139 return true; | |
140 } | |
141 // TODO(mikescarlett): Register the ReliableQuicStream's priority to the | |
142 // QuicWriteBlockedList so that the QUIC session doesn't drop messages when | |
143 // the QUIC transport channel becomes unwritable. | |
144 if (message_result == rtc::SR_BLOCK) { | |
145 // The QUIC stream is write blocked, so the message is queued by the QUIC | |
146 // session. If this is due to the QUIC not being writable, it will be sent | |
147 // once QUIC becomes writable again. Otherwise it may be due to exceeding | |
148 // the QUIC flow control limit, in which case the remote peer's QUIC session | |
149 // will tell the QUIC stream to send more data. | |
150 LOG(LS_INFO) << "Stream " << stream->id() | |
151 << " message is write blocked for QUIC data channel " << id_; | |
152 SetBufferedAmount_w(buffered_amount_ + stream->queued_data_bytes()); | |
153 stream->SignalQueuedBytesWritten.connect( | |
154 this, &QuicDataChannel::OnQueuedBytesWritten); | |
155 write_blocked_quic_streams_[stream->id()] = stream; | |
156 // The QUIC stream will be removed from |write_blocked_quic_streams_| once | |
157 // it closes. | |
158 stream->SignalClosed.connect(this, | |
159 &QuicDataChannel::OnWriteBlockedStreamClosed); | |
160 return true; | |
161 } | |
162 LOG(LS_ERROR) << "Stream " << stream->id() | |
163 << " failed to write message for QUIC data channel " << id_ | |
164 << ". Unexpected error: " << message_result; | |
165 return false; | |
166 } | |
167 | |
168 void QuicDataChannel::OnQueuedBytesWritten(net::QuicStreamId stream_id, | |
169 uint64_t queued_bytes_written) { | |
170 RTC_DCHECK(worker_thread_->IsCurrent()); | |
171 SetBufferedAmount_w(buffered_amount_ - queued_bytes_written); | |
172 const auto& kv = write_blocked_quic_streams_.find(stream_id); | |
173 if (kv == write_blocked_quic_streams_.end()) { | |
174 RTC_DCHECK(false); | |
175 return; | |
176 } | |
177 cricket::ReliableQuicStream* stream = kv->second; | |
178 // True if the QUIC stream is done sending data. | |
179 if (stream->fin_sent()) { | |
180 LOG(LS_INFO) << "Stream " << stream->id() | |
181 << " successfully wrote data for QUIC data channel " << id_; | |
182 stream->Close(); | |
183 } | |
184 } | |
185 | |
186 void QuicDataChannel::SetBufferedAmount_w(uint64_t buffered_amount) { | |
187 buffered_amount_ = buffered_amount; | |
188 invoker_.AsyncInvoke<void>( | |
189 signaling_thread_, rtc::Bind(&QuicDataChannel::OnBufferedAmountChange_s, | |
190 this, buffered_amount)); | |
191 } | |
192 | |
193 void QuicDataChannel::Close() { | |
194 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
195 if (state_ == kClosed || state_ == kClosing) { | |
196 return; | |
197 } | |
198 LOG(LS_INFO) << "Closing QUIC data channel."; | |
199 SetState_s(kClosing); | |
200 worker_thread_->Invoke<void>(rtc::Bind(&QuicDataChannel::Close_w, this)); | |
201 SetState_s(kClosed); | |
202 } | |
203 | |
204 void QuicDataChannel::Close_w() { | |
205 RTC_DCHECK(worker_thread_->IsCurrent()); | |
206 for (auto& kv : incoming_quic_messages_) { | |
207 Message& message = kv.second; | |
208 cricket::ReliableQuicStream* stream = message.stream; | |
209 stream->Close(); | |
210 } | |
211 | |
212 for (auto& kv : write_blocked_quic_streams_) { | |
213 cricket::ReliableQuicStream* stream = kv.second; | |
214 stream->Close(); | |
215 } | |
216 } | |
217 | |
218 bool QuicDataChannel::SetTransportChannel( | |
219 cricket::QuicTransportChannel* channel) { | |
220 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
221 | |
222 if (!channel) { | |
223 LOG(LS_ERROR) << "|channel| is NULL. Cannot set transport channel."; | |
224 return false; | |
225 } | |
226 if (quic_transport_channel_) { | |
227 if (channel == quic_transport_channel_) { | |
228 LOG(LS_WARNING) << "Ignoring duplicate transport channel."; | |
229 return true; | |
230 } | |
231 LOG(LS_ERROR) << "|channel| does not match existing transport channel."; | |
232 return false; | |
233 } | |
234 | |
235 quic_transport_channel_ = channel; | |
236 LOG(LS_INFO) << "Setting QuicTransportChannel for QUIC data channel " << id_; | |
237 worker_thread_->Invoke<void>( | |
238 rtc::Bind(&QuicDataChannel::SetTransportChannel_w, this)); | |
239 return true; | |
240 } | |
241 | |
242 void QuicDataChannel::SetTransportChannel_w() { | |
243 RTC_DCHECK(worker_thread_->IsCurrent()); | |
244 quic_transport_channel_->SignalReadyToSend.connect( | |
245 this, &QuicDataChannel::OnReadyToSend); | |
246 quic_transport_channel_->SignalClosed.connect( | |
247 this, &QuicDataChannel::OnConnectionClosed); | |
248 if (quic_transport_channel_->writable()) { | |
249 invoker_.AsyncInvoke<void>( | |
250 signaling_thread_, | |
251 rtc::Bind(&QuicDataChannel::SetState_s, this, kOpen)); | |
252 } | |
pthatcher1
2016/04/29 22:04:17
Hmmm.... I think what we want to do here is have S
mikescarlett
2016/04/29 22:11:04
Good idea. Done.
| |
253 } | |
254 | |
255 void QuicDataChannel::OnIncomingMessage(Message&& message) { | |
256 RTC_DCHECK(worker_thread_->IsCurrent()); | |
257 RTC_DCHECK(message.stream); | |
258 if (!observer_) { | |
259 LOG(LS_WARNING) << "QUIC data channel " << id_ | |
260 << " received a message but has no observer."; | |
261 message.stream->Close(); | |
262 return; | |
263 } | |
264 // A FIN is received if the message fits into a single QUIC stream frame and | |
265 // the remote peer is done sending. | |
266 if (message.stream->fin_received()) { | |
267 LOG(LS_INFO) << "Stream " << message.stream->id() | |
268 << " has finished receiving data for QUIC data channel " | |
269 << id_; | |
270 DataBuffer final_message(message.buffer, false); | |
271 invoker_.AsyncInvoke<void>(signaling_thread_, | |
272 rtc::Bind(&QuicDataChannel::OnMessage_s, this, | |
273 std::move(final_message))); | |
274 message.stream->Close(); | |
275 return; | |
276 } | |
277 // Otherwise the message is divided across multiple QUIC stream frames, so | |
278 // queue the data. OnDataReceived() will be called each time the remaining | |
279 // QUIC stream frames arrive. | |
280 LOG(LS_INFO) << "QUIC data channel " << id_ | |
281 << " is queuing incoming data for stream " | |
282 << message.stream->id(); | |
283 incoming_quic_messages_[message.stream->id()] = std::move(message); | |
284 message.stream->SignalDataReceived.connect(this, | |
285 &QuicDataChannel::OnDataReceived); | |
286 // The QUIC stream will be removed from |incoming_quic_messages_| once it | |
287 // closes. | |
288 message.stream->SignalClosed.connect( | |
289 this, &QuicDataChannel::OnIncomingQueuedStreamClosed); | |
290 } | |
291 | |
292 void QuicDataChannel::OnDataReceived(net::QuicStreamId stream_id, | |
293 const char* data, | |
294 size_t len) { | |
295 RTC_DCHECK(worker_thread_->IsCurrent()); | |
296 RTC_DCHECK(data); | |
297 const auto& kv = incoming_quic_messages_.find(stream_id); | |
298 if (kv == incoming_quic_messages_.end()) { | |
299 RTC_DCHECK(false); | |
300 return; | |
301 } | |
302 Message& message = kv->second; | |
303 cricket::ReliableQuicStream* stream = message.stream; | |
304 rtc::CopyOnWriteBuffer& received_data = message.buffer; | |
305 // If the QUIC stream has not received a FIN, then the remote peer is not | |
306 // finished sending data. | |
307 if (!stream->fin_received()) { | |
308 received_data.AppendData(data, len); | |
309 return; | |
310 } | |
311 // Otherwise we are done receiving and can provide the data channel observer | |
312 // with the message. | |
313 LOG(LS_INFO) << "Stream " << stream_id | |
314 << " has finished receiving data for QUIC data channel " << id_; | |
315 received_data.AppendData(data, len); | |
316 DataBuffer final_message(std::move(received_data), false); | |
317 invoker_.AsyncInvoke<void>( | |
318 signaling_thread_, | |
319 rtc::Bind(&QuicDataChannel::OnMessage_s, this, std::move(final_message))); | |
320 // Once the stream is closed, OnDataReceived will not fire for the stream. | |
321 stream->Close(); | |
322 } | |
323 | |
324 void QuicDataChannel::OnReadyToSend(cricket::TransportChannel* channel) { | |
325 RTC_DCHECK(worker_thread_->IsCurrent()); | |
326 RTC_DCHECK(channel == quic_transport_channel_); | |
327 LOG(LS_INFO) << "QuicTransportChannel is ready to send"; | |
328 invoker_.AsyncInvoke<void>( | |
329 signaling_thread_, rtc::Bind(&QuicDataChannel::SetState_s, this, kOpen)); | |
330 } | |
331 | |
332 void QuicDataChannel::OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, | |
333 int error) { | |
334 RTC_DCHECK(worker_thread_->IsCurrent()); | |
335 LOG(LS_VERBOSE) << "Write blocked stream " << stream_id << " is closed."; | |
336 write_blocked_quic_streams_.erase(stream_id); | |
337 } | |
338 | |
339 void QuicDataChannel::OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, | |
340 int error) { | |
341 RTC_DCHECK(worker_thread_->IsCurrent()); | |
342 LOG(LS_VERBOSE) << "Incoming queued stream " << stream_id << " is closed."; | |
343 incoming_quic_messages_.erase(stream_id); | |
344 } | |
345 | |
346 void QuicDataChannel::OnConnectionClosed() { | |
347 RTC_DCHECK(worker_thread_->IsCurrent()); | |
348 invoker_.AsyncInvoke<void>(signaling_thread_, | |
349 rtc::Bind(&QuicDataChannel::Close, this)); | |
350 } | |
351 | |
352 void QuicDataChannel::OnMessage_s(const DataBuffer& received_data) { | |
353 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
354 if (observer_) { | |
355 observer_->OnMessage(received_data); | |
356 } | |
357 } | |
358 | |
359 void QuicDataChannel::SetState_s(DataState state) { | |
360 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
361 if (state_ == state || state_ == kClosed) { | |
362 return; | |
363 } | |
364 if (state_ == kClosing && state != kClosed) { | |
365 return; | |
366 } | |
367 LOG(LS_INFO) << "Setting state to " << state << " for QUIC data channel " | |
368 << id_; | |
369 state_ = state; | |
370 if (observer_) { | |
371 observer_->OnStateChange(); | |
372 } | |
373 } | |
374 | |
375 void QuicDataChannel::OnBufferedAmountChange_s(uint64_t buffered_amount) { | |
376 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
377 if (observer_) { | |
378 observer_->OnBufferedAmountChange(buffered_amount); | |
379 } | |
380 } | |
381 | |
382 size_t QuicDataChannel::GetNumWriteBlockedStreams() const { | |
383 return write_blocked_quic_streams_.size(); | |
384 } | |
385 | |
386 size_t QuicDataChannel::GetNumIncomingStreams() const { | |
387 return incoming_quic_messages_.size(); | |
388 } | |
389 | |
390 } // namespace webrtc | |
OLD | NEW |