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 const auto& quic_stream_kv = quic_stream_by_id_.find(id); |
| 109 if (quic_stream_kv == quic_stream_by_id_.end()) { |
| 110 RTC_DCHECK(false); |
| 111 return; |
| 112 } |
| 113 cricket::ReliableQuicStream* stream = quic_stream_kv->second; |
| 114 stream->SignalDataReceived.disconnect(this); |
| 115 quic_stream_by_id_.erase(id); |
| 116 // Read the data channel ID and message ID. |
| 117 int data_channel_id; |
| 118 uint64_t message_id; |
| 119 size_t bytes_read; |
| 120 if (!ParseQuicDataMessageHeader(data, len, &data_channel_id, &message_id, |
| 121 &bytes_read)) { |
| 122 LOG(LS_ERROR) << "Could not read QUIC message header from QUIC stream " |
| 123 << id; |
| 124 return; |
| 125 } |
| 126 data += bytes_read; |
| 127 len -= bytes_read; |
| 128 // Retrieve the data channel which will handle the message. |
| 129 const auto& data_channel_kv = data_channel_by_id_.find(data_channel_id); |
| 130 if (data_channel_kv == data_channel_by_id_.end()) { |
| 131 // TODO(mikescarlett): Implement OPEN message to create a new |
| 132 // QuicDataChannel when messages are received for a nonexistent ID. |
| 133 LOG(LS_ERROR) << "Data was received for QUIC data channel " |
| 134 << data_channel_id |
| 135 << " but it is not registered to the QuicDataTransport."; |
| 136 return; |
| 137 } |
| 138 QuicDataChannel* data_channel = data_channel_kv->second; |
| 139 QuicDataChannel::Message message; |
| 140 message.id = message_id; |
| 141 message.buffer = rtc::CopyOnWriteBuffer(data, len); |
| 142 message.stream = stream; |
| 143 data_channel->OnIncomingMessage(std::move(message)); |
| 144 } |
| 145 |
| 146 } // namespace webrtc |
OLD | NEW |