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