Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: webrtc/api/quicdatachannel.cc

Issue 1886623002: Add QuicDataChannel and QuicDataTransport classes (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Make changes before committing Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/api/quicdatachannel.h ('k') | webrtc/api/quicdatachannel_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 RTC_DCHECK(worker_thread_->IsCurrent());
188 buffered_amount_ = buffered_amount;
189 invoker_.AsyncInvoke<void>(
190 signaling_thread_, rtc::Bind(&QuicDataChannel::OnBufferedAmountChange_s,
191 this, buffered_amount));
192 }
193
194 void QuicDataChannel::Close() {
195 RTC_DCHECK(signaling_thread_->IsCurrent());
196 if (state_ == kClosed || state_ == kClosing) {
197 return;
198 }
199 LOG(LS_INFO) << "Closing QUIC data channel.";
200 SetState_s(kClosing);
201 worker_thread_->Invoke<void>(rtc::Bind(&QuicDataChannel::Close_w, this));
202 SetState_s(kClosed);
203 }
204
205 void QuicDataChannel::Close_w() {
206 RTC_DCHECK(worker_thread_->IsCurrent());
207 for (auto& kv : incoming_quic_messages_) {
208 Message& message = kv.second;
209 cricket::ReliableQuicStream* stream = message.stream;
210 stream->Close();
211 }
212
213 for (auto& kv : write_blocked_quic_streams_) {
214 cricket::ReliableQuicStream* stream = kv.second;
215 stream->Close();
216 }
217 }
218
219 bool QuicDataChannel::SetTransportChannel(
220 cricket::QuicTransportChannel* channel) {
221 RTC_DCHECK(signaling_thread_->IsCurrent());
222
223 if (!channel) {
224 LOG(LS_ERROR) << "|channel| is NULL. Cannot set transport channel.";
225 return false;
226 }
227 if (quic_transport_channel_) {
228 if (channel == quic_transport_channel_) {
229 LOG(LS_WARNING) << "Ignoring duplicate transport channel.";
230 return true;
231 }
232 LOG(LS_ERROR) << "|channel| does not match existing transport channel.";
233 return false;
234 }
235
236 quic_transport_channel_ = channel;
237 LOG(LS_INFO) << "Setting QuicTransportChannel for QUIC data channel " << id_;
238 DataState data_channel_state = worker_thread_->Invoke<DataState>(
239 rtc::Bind(&QuicDataChannel::SetTransportChannel_w, this));
240 SetState_s(data_channel_state);
241 return true;
242 }
243
244 DataChannelInterface::DataState QuicDataChannel::SetTransportChannel_w() {
245 RTC_DCHECK(worker_thread_->IsCurrent());
246 quic_transport_channel_->SignalReadyToSend.connect(
247 this, &QuicDataChannel::OnReadyToSend);
248 quic_transport_channel_->SignalClosed.connect(
249 this, &QuicDataChannel::OnConnectionClosed);
250 if (quic_transport_channel_->writable()) {
251 return kOpen;
252 }
253 return kConnecting;
254 }
255
256 void QuicDataChannel::OnIncomingMessage(Message&& message) {
257 RTC_DCHECK(worker_thread_->IsCurrent());
258 RTC_DCHECK(message.stream);
259 if (!observer_) {
260 LOG(LS_WARNING) << "QUIC data channel " << id_
261 << " received a message but has no observer.";
262 message.stream->Close();
263 return;
264 }
265 // A FIN is received if the message fits into a single QUIC stream frame and
266 // the remote peer is done sending.
267 if (message.stream->fin_received()) {
268 LOG(LS_INFO) << "Stream " << message.stream->id()
269 << " has finished receiving data for QUIC data channel "
270 << id_;
271 DataBuffer final_message(message.buffer, false);
272 invoker_.AsyncInvoke<void>(signaling_thread_,
273 rtc::Bind(&QuicDataChannel::OnMessage_s, this,
274 std::move(final_message)));
275 message.stream->Close();
276 return;
277 }
278 // Otherwise the message is divided across multiple QUIC stream frames, so
279 // queue the data. OnDataReceived() will be called each time the remaining
280 // QUIC stream frames arrive.
281 LOG(LS_INFO) << "QUIC data channel " << id_
282 << " is queuing incoming data for stream "
283 << message.stream->id();
284 incoming_quic_messages_[message.stream->id()] = std::move(message);
285 message.stream->SignalDataReceived.connect(this,
286 &QuicDataChannel::OnDataReceived);
287 // The QUIC stream will be removed from |incoming_quic_messages_| once it
288 // closes.
289 message.stream->SignalClosed.connect(
290 this, &QuicDataChannel::OnIncomingQueuedStreamClosed);
291 }
292
293 void QuicDataChannel::OnDataReceived(net::QuicStreamId stream_id,
294 const char* data,
295 size_t len) {
296 RTC_DCHECK(worker_thread_->IsCurrent());
297 RTC_DCHECK(data);
298 const auto& kv = incoming_quic_messages_.find(stream_id);
299 if (kv == incoming_quic_messages_.end()) {
300 RTC_DCHECK(false);
301 return;
302 }
303 Message& message = kv->second;
304 cricket::ReliableQuicStream* stream = message.stream;
305 rtc::CopyOnWriteBuffer& received_data = message.buffer;
306 // If the QUIC stream has not received a FIN, then the remote peer is not
307 // finished sending data.
308 if (!stream->fin_received()) {
309 received_data.AppendData(data, len);
310 return;
311 }
312 // Otherwise we are done receiving and can provide the data channel observer
313 // with the message.
314 LOG(LS_INFO) << "Stream " << stream_id
315 << " has finished receiving data for QUIC data channel " << id_;
316 received_data.AppendData(data, len);
317 DataBuffer final_message(std::move(received_data), false);
318 invoker_.AsyncInvoke<void>(
319 signaling_thread_,
320 rtc::Bind(&QuicDataChannel::OnMessage_s, this, std::move(final_message)));
321 // Once the stream is closed, OnDataReceived will not fire for the stream.
322 stream->Close();
323 }
324
325 void QuicDataChannel::OnReadyToSend(cricket::TransportChannel* channel) {
326 RTC_DCHECK(worker_thread_->IsCurrent());
327 RTC_DCHECK(channel == quic_transport_channel_);
328 LOG(LS_INFO) << "QuicTransportChannel is ready to send";
329 invoker_.AsyncInvoke<void>(
330 signaling_thread_, rtc::Bind(&QuicDataChannel::SetState_s, this, kOpen));
331 }
332
333 void QuicDataChannel::OnWriteBlockedStreamClosed(net::QuicStreamId stream_id,
334 int error) {
335 RTC_DCHECK(worker_thread_->IsCurrent());
336 LOG(LS_VERBOSE) << "Write blocked stream " << stream_id << " is closed.";
337 write_blocked_quic_streams_.erase(stream_id);
338 }
339
340 void QuicDataChannel::OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id,
341 int error) {
342 RTC_DCHECK(worker_thread_->IsCurrent());
343 LOG(LS_VERBOSE) << "Incoming queued stream " << stream_id << " is closed.";
344 incoming_quic_messages_.erase(stream_id);
345 }
346
347 void QuicDataChannel::OnConnectionClosed() {
348 RTC_DCHECK(worker_thread_->IsCurrent());
349 invoker_.AsyncInvoke<void>(signaling_thread_,
350 rtc::Bind(&QuicDataChannel::Close, this));
351 }
352
353 void QuicDataChannel::OnMessage_s(const DataBuffer& received_data) {
354 RTC_DCHECK(signaling_thread_->IsCurrent());
355 if (observer_) {
356 observer_->OnMessage(received_data);
357 }
358 }
359
360 void QuicDataChannel::SetState_s(DataState state) {
361 RTC_DCHECK(signaling_thread_->IsCurrent());
362 if (state_ == state || state_ == kClosed) {
363 return;
364 }
365 if (state_ == kClosing && state != kClosed) {
366 return;
367 }
368 LOG(LS_INFO) << "Setting state to " << state << " for QUIC data channel "
369 << id_;
370 state_ = state;
371 if (observer_) {
372 observer_->OnStateChange();
373 }
374 }
375
376 void QuicDataChannel::OnBufferedAmountChange_s(uint64_t buffered_amount) {
377 RTC_DCHECK(signaling_thread_->IsCurrent());
378 if (observer_) {
379 observer_->OnBufferedAmountChange(buffered_amount);
380 }
381 }
382
383 size_t QuicDataChannel::GetNumWriteBlockedStreams() const {
384 return write_blocked_quic_streams_.size();
385 }
386
387 size_t QuicDataChannel::GetNumIncomingStreams() const {
388 return incoming_quic_messages_.size();
389 }
390
391 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/api/quicdatachannel.h ('k') | webrtc/api/quicdatachannel_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698