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 EncodeQuicHeader(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 DecodeQuicHeader(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 tmp; | |
pthatcher1
2016/04/29 21:08:36
Might as well call that dcid instead of tmp.
mikescarlett
2016/04/29 21:46:11
Renamed.
| |
47 if (!byte_buffer.ReadUVarint(&tmp)) { | |
48 LOG(LS_ERROR) << "Could not read the data channel ID"; | |
49 return false; | |
50 } | |
51 *data_channel_id = tmp; | |
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 num_sent_messages_(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 EncodeQuicHeader(id_, ++num_sent_messages_, &header); | |
pthatcher1
2016/04/29 21:08:36
I'd call this next_message_id_.
mikescarlett
2016/04/29 21:46:11
Renamed.
| |
104 RTC_DCHECK(quic_transport_channel_); | |
105 cricket::ReliableQuicStream* stream = | |
106 quic_transport_channel_->CreateQuicStream(); | |
107 RTC_DCHECK(stream); | |
108 uint64_t old_queued_bytes = stream->queued_data_bytes(); | |
pthatcher1
2016/04/29 21:08:36
How could a stream just created have queued_data_b
mikescarlett
2016/04/29 21:46:11
Clearly nothing should be queued. Removed old_queu
| |
109 | |
110 // Send the header with a FIN if the message is empty. | |
111 bool header_fin = (buffer.size() == 0); | |
112 rtc::StreamResult header_result = | |
113 stream->Write(header.data<char>(), header.size(), header_fin); | |
114 | |
115 if (header_result == rtc::SR_BLOCK) { | |
116 // The header is write blocked but we should try sending the message. Since | |
117 // the ReliableQuicStream queues data in order, if the header is write | |
118 // blocked then the message will be write blocked. Otherwise if the message | |
119 // is sent then the header is sent. | |
120 LOG(LS_INFO) << "Stream " << stream->id() | |
121 << " header is write blocked for QUIC data channel " << id_; | |
122 } else if (header_result != rtc::SR_SUCCESS) { | |
123 LOG(LS_ERROR) << "Stream " << stream->id() | |
124 << " failed to write header for QUIC data channel " << id_ | |
125 << ". Unexpected error " << header_result; | |
126 return false; | |
127 } | |
128 | |
129 // If the message is not empty, then send the message with a FIN. | |
130 bool message_fin = true; | |
131 rtc::StreamResult message_result = | |
132 header_fin ? header_result : stream->Write(buffer.data.data<char>(), | |
133 buffer.size(), message_fin); | |
134 | |
135 if (message_result == rtc::SR_SUCCESS) { | |
136 // The message is sent and we don't need this QUIC stream. | |
137 LOG(LS_INFO) << "Stream " << stream->id() | |
138 << " successfully wrote message for QUIC data channel " << id_; | |
139 stream->Close(); | |
140 return true; | |
141 } | |
142 // TODO(mikescarlett): Register the ReliableQuicStream's priority to the | |
143 // QuicWriteBlockedList so that the QUIC session doesn't drop messages when | |
144 // the QUIC transport channel becomes unwritable. | |
145 if (message_result == rtc::SR_BLOCK) { | |
146 // The QUIC stream is write blocked, so the message is queued by the QUIC | |
147 // session. If this is due to the QUIC not being writable, it will be sent | |
148 // once QUIC becomes writable again. Otherwise it may be due to exceeding | |
149 // the QUIC flow control limit, in which case the remote peer's QUIC session | |
150 // will tell the QUIC stream to send more data. | |
151 LOG(LS_INFO) << "Stream " << stream->id() | |
152 << " message is write blocked for QUIC data channel " << id_; | |
153 invoker_.AsyncInvoke<void>( | |
154 signaling_thread_, rtc::Bind(&QuicDataChannel::OnBufferedAmountChange_s, | |
155 this, buffered_amount_)); | |
156 buffered_amount_ += stream->queued_data_bytes() - old_queued_bytes; | |
157 stream->SignalQueuedBytesWritten.connect( | |
158 this, &QuicDataChannel::OnQueuedBytesWritten); | |
159 write_blocked_quic_streams_[stream->id()] = stream; | |
160 // The QUIC stream will be removed from |write_blocked_quic_streams_| once | |
161 // it closes. | |
162 stream->SignalClosed.connect(this, | |
163 &QuicDataChannel::OnWriteBlockedStreamClosed); | |
164 return true; | |
165 } | |
166 LOG(LS_ERROR) << "Stream " << stream->id() | |
167 << " failed to write message for QUIC data channel " << id_ | |
168 << ". Unexpected error: " << message_result; | |
169 return false; | |
170 } | |
171 | |
172 void QuicDataChannel::OnQueuedBytesWritten(net::QuicStreamId stream_id, | |
173 uint64_t queued_bytes_written) { | |
174 RTC_DCHECK(worker_thread_->IsCurrent()); | |
175 invoker_.AsyncInvoke<void>( | |
176 signaling_thread_, rtc::Bind(&QuicDataChannel::OnBufferedAmountChange_s, | |
177 this, buffered_amount_)); | |
178 buffered_amount_ -= queued_bytes_written; | |
pthatcher1
2016/04/29 21:08:36
I'd suggest moving these two lines into a SetBuffe
mikescarlett
2016/04/29 21:46:11
Done.
| |
179 const auto& kv = write_blocked_quic_streams_.find(stream_id); | |
180 RTC_DCHECK(kv != write_blocked_quic_streams_.end()); | |
pthatcher1
2016/04/29 21:08:36
It would be slight better to do this:
if (kv == w
mikescarlett
2016/04/29 21:46:11
Done.
| |
181 cricket::ReliableQuicStream* stream = kv->second; | |
182 // True if the QUIC stream is done sending data. | |
183 if (stream->fin_sent()) { | |
184 LOG(LS_INFO) << "Stream " << stream->id() | |
185 << " successfully wrote data for QUIC data channel " << id_; | |
186 stream->Close(); | |
187 } | |
188 } | |
189 | |
190 void QuicDataChannel::Close() { | |
191 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
192 if (state_ == kClosed || state_ == kClosing) { | |
193 return; | |
194 } | |
195 LOG(LS_INFO) << "Closing QUIC data channel."; | |
196 SetState_s(kClosing); | |
197 worker_thread_->Invoke<void>(rtc::Bind(&QuicDataChannel::Close_w, this)); | |
198 } | |
199 | |
200 void QuicDataChannel::Close_w() { | |
201 RTC_DCHECK(worker_thread_->IsCurrent()); | |
202 for (auto& kv : incoming_quic_messages_) { | |
203 Message& message = kv.second; | |
204 cricket::ReliableQuicStream* stream = message.stream; | |
205 stream->Close(); | |
206 } | |
207 | |
208 for (auto& kv : write_blocked_quic_streams_) { | |
209 cricket::ReliableQuicStream* stream = kv.second; | |
210 stream->Close(); | |
211 } | |
212 | |
213 invoker_.AsyncInvoke<void>( | |
214 signaling_thread_, | |
215 rtc::Bind(&QuicDataChannel::SetState_s, this, kClosed)); | |
pthatcher1
2016/04/29 21:08:36
I think the better thing to do would be to have Qu
mikescarlett
2016/04/29 21:46:11
Done.
| |
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::ConnectTransportChannel_w, this)); | |
239 if (quic_transport_channel_->writable()) { | |
pthatcher1
2016/04/29 21:08:36
quic_transport_channel_->writable() an only be acc
mikescarlett
2016/04/29 21:46:11
Moved to ConnectTransportChannel_w (and renamed Co
| |
240 SetState_s(kOpen); | |
241 } | |
242 return true; | |
243 } | |
244 | |
245 void QuicDataChannel::ConnectTransportChannel_w() { | |
246 RTC_DCHECK(worker_thread_->IsCurrent()); | |
247 quic_transport_channel_->SignalReadyToSend.connect( | |
248 this, &QuicDataChannel::OnReadyToSend); | |
249 quic_transport_channel_->SignalClosed.connect( | |
250 this, &QuicDataChannel::OnConnectionClosed); | |
251 } | |
252 | |
253 void QuicDataChannel::OnIncomingMessage(Message&& message) { | |
254 RTC_DCHECK(worker_thread_->IsCurrent()); | |
255 RTC_DCHECK(message.stream); | |
256 if (!observer_) { | |
257 LOG(LS_WARNING) << "QUIC data channel " << id_ | |
258 << " received a message but has no observer."; | |
259 message.stream->Close(); | |
260 return; | |
261 } | |
262 // A FIN is received if the message fits into a single QUIC stream frame and | |
263 // the remote peer is done sending. | |
264 if (message.stream->fin_received()) { | |
265 LOG(LS_INFO) << "Stream " << message.stream->id() | |
266 << " has finished receiving data for QUIC data channel " | |
267 << id_; | |
268 DataBuffer final_message(message.buffer, false); | |
269 invoker_.AsyncInvoke<void>(signaling_thread_, | |
270 rtc::Bind(&QuicDataChannel::OnMessage_s, this, | |
271 std::move(final_message))); | |
272 message.stream->Close(); | |
273 return; | |
274 } | |
275 // Otherwise the message is divided across multiple QUIC stream frames, so | |
276 // queue the data. OnDataReceived() will be called each time the remaining | |
277 // QUIC stream frames arrive. | |
278 LOG(LS_INFO) << "QUIC data channel " << id_ | |
279 << " is queuing incoming data for stream " | |
280 << message.stream->id(); | |
281 incoming_quic_messages_[message.stream->id()] = std::move(message); | |
282 message.stream->SignalDataReceived.connect(this, | |
283 &QuicDataChannel::OnDataReceived); | |
284 // The QUIC stream will be removed from |incoming_quic_messages_| once it | |
285 // closes. | |
286 message.stream->SignalClosed.connect( | |
287 this, &QuicDataChannel::OnIncomingQueuedStreamClosed); | |
288 } | |
289 | |
290 void QuicDataChannel::OnDataReceived(net::QuicStreamId stream_id, | |
291 const char* data, | |
292 size_t len) { | |
293 RTC_DCHECK(worker_thread_->IsCurrent()); | |
294 RTC_DCHECK(data); | |
295 const auto& kv = incoming_quic_messages_.find(stream_id); | |
296 RTC_DCHECK(kv != incoming_quic_messages_.end()); | |
pthatcher1
2016/04/29 21:08:36
Again, please make this return so it doesn't fail
mikescarlett
2016/04/29 21:46:11
Done.
| |
297 Message& message = kv->second; | |
298 cricket::ReliableQuicStream* stream = message.stream; | |
299 rtc::CopyOnWriteBuffer& received_data = message.buffer; | |
300 // If the QUIC stream has not received a FIN, then the remote peer is not | |
301 // finished sending data. | |
302 if (!stream->fin_received()) { | |
303 received_data.AppendData(data, len); | |
304 return; | |
305 } | |
306 // Otherwise we are done receiving and can provide the data channel observer | |
307 // with the message. | |
308 LOG(LS_INFO) << "Stream " << stream_id | |
309 << " has finished receiving data for QUIC data channel " << id_; | |
310 received_data.AppendData(data, len); | |
311 DataBuffer final_message(std::move(received_data), false); | |
312 invoker_.AsyncInvoke<void>( | |
313 signaling_thread_, | |
314 rtc::Bind(&QuicDataChannel::OnMessage_s, this, std::move(final_message))); | |
315 // Once the stream is closed, OnDataReceived will not fire for the stream. | |
316 stream->Close(); | |
317 } | |
318 | |
319 void QuicDataChannel::OnReadyToSend(cricket::TransportChannel* channel) { | |
320 RTC_DCHECK(worker_thread_->IsCurrent()); | |
321 RTC_DCHECK(channel == quic_transport_channel_); | |
322 LOG(LS_INFO) << "QuicTransportChannel is ready to send"; | |
323 invoker_.AsyncInvoke<void>( | |
324 signaling_thread_, rtc::Bind(&QuicDataChannel::SetState_s, this, kOpen)); | |
325 } | |
326 | |
327 void QuicDataChannel::OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, | |
328 int error) { | |
329 RTC_DCHECK(worker_thread_->IsCurrent()); | |
330 LOG(LS_VERBOSE) << "Write blocked stream " << stream_id << " is closed."; | |
331 write_blocked_quic_streams_.erase(stream_id); | |
332 } | |
333 | |
334 void QuicDataChannel::OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, | |
335 int error) { | |
336 RTC_DCHECK(worker_thread_->IsCurrent()); | |
337 LOG(LS_VERBOSE) << "Incoming queued stream " << stream_id << " is closed."; | |
338 incoming_quic_messages_.erase(stream_id); | |
339 } | |
340 | |
341 void QuicDataChannel::OnConnectionClosed() { | |
342 RTC_DCHECK(worker_thread_->IsCurrent()); | |
343 invoker_.AsyncInvoke<void>(signaling_thread_, | |
344 rtc::Bind(&QuicDataChannel::Close, this)); | |
345 } | |
346 | |
347 void QuicDataChannel::OnMessage_s(const DataBuffer& received_data) { | |
348 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
349 if (observer_) { | |
350 observer_->OnMessage(received_data); | |
351 } | |
352 } | |
353 | |
354 void QuicDataChannel::SetState_s(DataState state) { | |
355 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
356 if (state_ == state || state_ == kClosed) { | |
357 return; | |
358 } | |
359 if (state_ == kClosing && state != kClosed) { | |
360 return; | |
361 } | |
362 LOG(LS_INFO) << "Setting state to " << state << " for QUIC data channel " | |
363 << id_; | |
364 state_ = state; | |
365 if (observer_) { | |
366 observer_->OnStateChange(); | |
367 } | |
368 } | |
369 | |
370 void QuicDataChannel::OnBufferedAmountChange_s(uint64_t buffered_amount) { | |
371 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
372 if (observer_) { | |
373 observer_->OnBufferedAmountChange(buffered_amount); | |
374 } | |
375 } | |
376 | |
377 size_t QuicDataChannel::GetNumWriteBlockedStreams() const { | |
378 return write_blocked_quic_streams_.size(); | |
379 } | |
380 | |
381 size_t QuicDataChannel::GetNumIncomingStreams() const { | |
382 return incoming_quic_messages_.size(); | |
383 } | |
384 | |
385 } // namespace webrtc | |
OLD | NEW |