Chromium Code Reviews| 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/quicdatatransport.h" | |
| 12 | |
| 13 #include "webrtc/base/logging.h" | |
| 14 #include "webrtc/p2p/quic/quictransportchannel.h" | |
| 15 #include "webrtc/p2p/quic/reliablequicstream.h" | |
| 16 | |
| 17 namespace webrtc { | |
| 18 | |
| 19 QuicDataTransport::QuicDataTransport(rtc::Thread* signaling_thread, | |
| 20 rtc::Thread* worker_thread) | |
| 21 : signaling_thread_(signaling_thread), worker_thread_(worker_thread) { | |
| 22 RTC_DCHECK(signaling_thread_); | |
| 23 RTC_DCHECK(worker_thread_); | |
| 24 } | |
| 25 | |
| 26 QuicDataTransport::~QuicDataTransport() {} | |
| 27 | |
| 28 bool QuicDataTransport::SetTransportChannel( | |
| 29 cricket::QuicTransportChannel* channel) { | |
| 30 if (!channel) { | |
| 31 LOG(LS_ERROR) << "|channel| is NULL. Cannot set transport channel."; | |
| 32 return false; | |
| 33 } | |
| 34 if (quic_transport_channel_) { | |
| 35 if (channel == quic_transport_channel_) { | |
| 36 LOG(LS_WARNING) << "Ignoring duplicate transport channel."; | |
| 37 return true; | |
| 38 } | |
| 39 LOG(LS_ERROR) << "|channel| does not match existing transport channel."; | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 43 LOG(LS_INFO) << "Setting QuicTransportChannel for QuicDataTransport"; | |
| 44 quic_transport_channel_ = channel; | |
| 45 quic_transport_channel_->SignalIncomingStream.connect( | |
| 46 this, &QuicDataTransport::OnIncomingStream); | |
| 47 | |
| 48 bool success = true; | |
| 49 for (const auto& kv : data_channel_by_id_) { | |
| 50 rtc::scoped_refptr<QuicDataChannel> data_channel = kv.second; | |
| 51 if (!data_channel->SetTransportChannel(quic_transport_channel_)) { | |
| 52 LOG(LS_ERROR) | |
| 53 << "Cannot set QUIC transport channel for QUIC data channel " | |
| 54 << kv.first; | |
| 55 success = false; | |
| 56 } | |
| 57 } | |
| 58 return success; | |
| 59 } | |
| 60 | |
| 61 rtc::scoped_refptr<DataChannelInterface> QuicDataTransport::CreateDataChannel( | |
| 62 const std::string& label, | |
| 63 const DataChannelInit* config) { | |
| 64 if (config == nullptr) { | |
| 65 return nullptr; | |
| 66 } | |
| 67 if (data_channel_by_id_.find(config->id) != data_channel_by_id_.end()) { | |
| 68 LOG(LS_ERROR) << "QUIC data channel already exists with id " << config->id; | |
| 69 return nullptr; | |
| 70 } | |
| 71 rtc::scoped_refptr<QuicDataChannel> data_channel( | |
| 72 new QuicDataChannel(signaling_thread_, worker_thread_, label, *config)); | |
| 73 if (quic_transport_channel_) { | |
| 74 if (!data_channel->SetTransportChannel(quic_transport_channel_)) { | |
| 75 LOG(LS_ERROR) | |
| 76 << "Cannot set QUIC transport channel for QUIC data channel " | |
| 77 << config->id; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 data_channel_by_id_[data_channel->id()] = data_channel; | |
| 82 return data_channel; | |
| 83 } | |
| 84 | |
| 85 void QuicDataTransport::DestroyDataChannel(int id) { | |
| 86 data_channel_by_id_.erase(id); | |
| 87 } | |
| 88 | |
| 89 bool QuicDataTransport::HasDataChannel(int id) const { | |
| 90 return data_channel_by_id_.find(id) != data_channel_by_id_.end(); | |
| 91 } | |
| 92 | |
| 93 bool QuicDataTransport::HasDataChannels() const { | |
| 94 return !data_channel_by_id_.empty(); | |
| 95 } | |
| 96 | |
| 97 // Called when a QUIC stream is created for incoming data. | |
| 98 void QuicDataTransport::OnIncomingStream(cricket::ReliableQuicStream* stream) { | |
| 99 RTC_DCHECK(stream != nullptr); | |
| 100 quic_stream_by_id_[stream->id()] = stream; | |
| 101 stream->SignalDataReceived.connect(this, &QuicDataTransport::OnDataReceived); | |
| 102 } | |
| 103 | |
| 104 // Called when the first QUIC stream frame is received for incoming data. | |
| 105 void QuicDataTransport::OnDataReceived(net::QuicStreamId id, | |
| 106 const char* data, | |
| 107 size_t len) { | |
| 108 RTC_DCHECK(quic_stream_by_id_.find(id) != quic_stream_by_id_.end()); | |
| 109 cricket::ReliableQuicStream* stream = quic_stream_by_id_[id]; | |
|
pthatcher1
2016/04/29 21:08:36
It would be better to check for release builds als
mikescarlett
2016/04/29 21:46:11
Done.
| |
| 110 stream->SignalDataReceived.disconnect(this); | |
| 111 quic_stream_by_id_.erase(id); | |
| 112 // Read the data channel ID and message ID. | |
| 113 int data_channel_id; | |
| 114 uint64_t message_id; | |
| 115 size_t bytes_read; | |
| 116 if (!DecodeQuicHeader(data, len, &data_channel_id, &message_id, | |
|
pthatcher1
2016/04/29 21:08:36
I'd call this ParseQuicDataMessageHeader instead o
mikescarlett
2016/04/29 21:46:11
Done.
| |
| 117 &bytes_read)) { | |
| 118 LOG(LS_ERROR) << "Could not read QUIC message header from QUIC stream " | |
| 119 << id; | |
| 120 return; | |
| 121 } | |
| 122 data += bytes_read; | |
| 123 len -= bytes_read; | |
| 124 // Retrieve the data channel which will handle the message. | |
| 125 const auto& kv = data_channel_by_id_.find(data_channel_id); | |
| 126 if (kv == data_channel_by_id_.end()) { | |
| 127 // TODO(mikescarlett): Implement OPEN message to create a new | |
| 128 // QuicDataChannel when messages are received for a nonexistent ID. | |
| 129 LOG(LS_ERROR) << "Data was received for QUIC data channel " | |
| 130 << data_channel_id | |
| 131 << " but it is not registered to the QuicDataTransport."; | |
| 132 return; | |
| 133 } | |
| 134 QuicDataChannel* data_channel = kv->second; | |
| 135 QuicDataChannel::Message message; | |
| 136 message.id = message_id; | |
| 137 message.buffer = rtc::CopyOnWriteBuffer(data, len); | |
| 138 message.stream = stream; | |
| 139 data_channel->OnIncomingMessage(std::move(message)); | |
| 140 } | |
| 141 | |
| 142 } // namespace webrtc | |
| OLD | NEW |