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/bytebuffer.h" |
| 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/p2p/quic/quictransportchannel.h" |
| 16 #include "webrtc/p2p/quic/reliablequicstream.h" |
| 17 |
| 18 namespace webrtc { |
| 19 |
| 20 QuicDataTransport::QuicDataTransport() {} |
| 21 |
| 22 QuicDataTransport::~QuicDataTransport() {} |
| 23 |
| 24 void QuicDataTransport::SetTransportChannel( |
| 25 cricket::QuicTransportChannel* channel) { |
| 26 RTC_DCHECK(channel != nullptr); |
| 27 RTC_DCHECK(quic_transport_channel_ == nullptr); |
| 28 |
| 29 LOG(LS_INFO) << "Setting QuicTransportChannel for QuicDataTransport"; |
| 30 quic_transport_channel_ = channel; |
| 31 quic_transport_channel_->SignalIncomingStream.connect( |
| 32 this, &QuicDataTransport::OnIncomingStream); |
| 33 |
| 34 for (const auto& kv : data_channel_by_id_) { |
| 35 rtc::scoped_refptr<QuicDataChannel> data_channel = kv.second; |
| 36 data_channel->SetTransportChannel(quic_transport_channel_); |
| 37 } |
| 38 } |
| 39 |
| 40 rtc::scoped_refptr<DataChannelInterface> QuicDataTransport::CreateDataChannel( |
| 41 rtc::Thread* signaling_thread, |
| 42 rtc::Thread* worker_thread, |
| 43 const std::string& label, |
| 44 const DataChannelInit* config) { |
| 45 RTC_DCHECK(config != nullptr); |
| 46 if (data_channel_by_id_.find(config->id) != data_channel_by_id_.end()) { |
| 47 LOG(LS_ERROR) << "QUIC data channel already exists with id " << config->id; |
| 48 return nullptr; |
| 49 } |
| 50 rtc::scoped_refptr<QuicDataChannel> data_channel(new QuicDataChannel( |
| 51 signaling_thread, worker_thread, label, *config, *this)); |
| 52 if (quic_transport_channel_) { |
| 53 data_channel->SetTransportChannel(quic_transport_channel_); |
| 54 } |
| 55 |
| 56 data_channel_by_id_[data_channel->id()] = data_channel; |
| 57 return data_channel; |
| 58 } |
| 59 |
| 60 void QuicDataTransport::DestroyDataChannel(int id) { |
| 61 data_channel_by_id_.erase(id); |
| 62 } |
| 63 |
| 64 bool QuicDataTransport::HasDataChannel(int id) const { |
| 65 return data_channel_by_id_.find(id) != data_channel_by_id_.end(); |
| 66 } |
| 67 |
| 68 bool QuicDataTransport::HasDataChannels() const { |
| 69 return !data_channel_by_id_.empty(); |
| 70 } |
| 71 |
| 72 // Called when a QUIC stream is created for incoming data. |
| 73 void QuicDataTransport::OnIncomingStream(cricket::ReliableQuicStream* stream) { |
| 74 RTC_DCHECK(stream != nullptr); |
| 75 quic_stream_by_id_[stream->id()] = stream; |
| 76 stream->SignalDataReceived.connect(this, &QuicDataTransport::OnDataReceived); |
| 77 } |
| 78 |
| 79 // Called when the first QUIC stream frame is received for incoming data. |
| 80 void QuicDataTransport::OnDataReceived(net::QuicStreamId id, |
| 81 const char* data, |
| 82 size_t len) { |
| 83 RTC_DCHECK(quic_stream_by_id_.find(id) != quic_stream_by_id_.end()); |
| 84 cricket::ReliableQuicStream* stream = quic_stream_by_id_[id]; |
| 85 stream->SignalDataReceived.disconnect(this); |
| 86 quic_stream_by_id_.erase(id); |
| 87 // Read data channel id |
| 88 rtc::ByteBufferReader byte_buffer(data, len, rtc::ByteBuffer::ORDER_HOST); |
| 89 uint64_t data_channel_id; |
| 90 if (!byte_buffer.ReadUVarint(&data_channel_id)) { |
| 91 LOG(LS_ERROR) << "Could not read the data channel ID for QUIC stream " |
| 92 << id; |
| 93 return; |
| 94 } |
| 95 // Retrieve the data channel which will handle the message. |
| 96 const auto& kv = data_channel_by_id_.find(data_channel_id); |
| 97 if (kv == data_channel_by_id_.end()) { |
| 98 // TODO(mikescarlett): Implement OPEN message to create a new |
| 99 // QuicDataChannel when messages are received for a nonexistent ID. |
| 100 LOG(LS_ERROR) << "Data was received for QUIC data channel " |
| 101 << data_channel_id |
| 102 << " but it is not registered to the QuicDataTransport."; |
| 103 return; |
| 104 } |
| 105 QuicDataChannel* data_channel = kv->second; |
| 106 uint64_t message_id; |
| 107 if (!byte_buffer.ReadUVarint(&message_id)) { |
| 108 LOG(LS_ERROR) << "Could not read message ID for QUIC stream " << id |
| 109 << " destined for data channel " << data_channel_id; |
| 110 return; |
| 111 } |
| 112 QuicMessageDispatcher::Dispatch(data_channel, message_id, byte_buffer.Data(), |
| 113 byte_buffer.Length(), stream); |
| 114 } |
| 115 |
| 116 void QuicDataTransport::EncodeHeader(int data_channel_id, |
| 117 uint64_t message_id, |
| 118 rtc::CopyOnWriteBuffer* header) const { |
| 119 RTC_DCHECK(header != nullptr); |
| 120 // 64-bit varints require at most 10 bytes (7*10 == 70), and 32-bit varints |
| 121 // require at most 5 bytes (7*5 == 35). |
| 122 size_t max_length = 15; |
| 123 rtc::ByteBufferWriter byte_buffer(nullptr, max_length, |
| 124 rtc::ByteBuffer::ByteOrder::ORDER_HOST); |
| 125 byte_buffer.WriteUVarint(data_channel_id); |
| 126 byte_buffer.WriteUVarint(message_id); |
| 127 header->SetData(byte_buffer.Data(), byte_buffer.Length()); |
| 128 } |
| 129 |
| 130 } // namespace webrtc |
OLD | NEW |