Index: webrtc/api/quicdatatransport.cc |
diff --git a/webrtc/api/quicdatatransport.cc b/webrtc/api/quicdatatransport.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3d609fea698762a3ce4b460a2b02381e597ca549 |
--- /dev/null |
+++ b/webrtc/api/quicdatatransport.cc |
@@ -0,0 +1,130 @@ |
+/* |
+ * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/api/quicdatatransport.h" |
+ |
+#include "webrtc/base/bytebuffer.h" |
+#include "webrtc/base/logging.h" |
+#include "webrtc/p2p/quic/quictransportchannel.h" |
+#include "webrtc/p2p/quic/reliablequicstream.h" |
+ |
+namespace webrtc { |
+ |
+QuicDataTransport::QuicDataTransport() {} |
+ |
+QuicDataTransport::~QuicDataTransport() {} |
+ |
+void QuicDataTransport::SetTransportChannel( |
+ cricket::QuicTransportChannel* channel) { |
+ RTC_DCHECK(channel != nullptr); |
+ RTC_DCHECK(quic_transport_channel_ == nullptr); |
+ |
+ LOG(LS_INFO) << "Setting QuicTransportChannel for QuicDataTransport"; |
+ quic_transport_channel_ = channel; |
+ quic_transport_channel_->SignalIncomingStream.connect( |
+ this, &QuicDataTransport::OnIncomingStream); |
+ |
+ for (const auto& kv : data_channel_by_id_) { |
+ rtc::scoped_refptr<QuicDataChannel> data_channel = kv.second; |
+ data_channel->SetTransportChannel(quic_transport_channel_); |
+ } |
+} |
+ |
+rtc::scoped_refptr<DataChannelInterface> QuicDataTransport::CreateDataChannel( |
+ rtc::Thread* signaling_thread, |
+ rtc::Thread* worker_thread, |
+ const std::string& label, |
+ const DataChannelInit* config) { |
+ RTC_DCHECK(config != nullptr); |
+ if (data_channel_by_id_.find(config->id) != data_channel_by_id_.end()) { |
+ LOG(LS_ERROR) << "QUIC data channel already exists with id " << config->id; |
+ return nullptr; |
+ } |
+ rtc::scoped_refptr<QuicDataChannel> data_channel(new QuicDataChannel( |
+ signaling_thread, worker_thread, label, *config, *this)); |
+ if (quic_transport_channel_) { |
+ data_channel->SetTransportChannel(quic_transport_channel_); |
+ } |
+ |
+ data_channel_by_id_[data_channel->id()] = data_channel; |
+ return data_channel; |
+} |
+ |
+void QuicDataTransport::DestroyDataChannel(int id) { |
+ data_channel_by_id_.erase(id); |
+} |
+ |
+bool QuicDataTransport::HasDataChannel(int id) const { |
+ return data_channel_by_id_.find(id) != data_channel_by_id_.end(); |
+} |
+ |
+bool QuicDataTransport::HasDataChannels() const { |
+ return !data_channel_by_id_.empty(); |
+} |
+ |
+// Called when a QUIC stream is created for incoming data. |
+void QuicDataTransport::OnIncomingStream(cricket::ReliableQuicStream* stream) { |
+ RTC_DCHECK(stream != nullptr); |
+ quic_stream_by_id_[stream->id()] = stream; |
+ stream->SignalDataReceived.connect(this, &QuicDataTransport::OnDataReceived); |
+} |
+ |
+// Called when the first QUIC stream frame is received for incoming data. |
+void QuicDataTransport::OnDataReceived(net::QuicStreamId id, |
+ const char* data, |
+ size_t len) { |
+ RTC_DCHECK(quic_stream_by_id_.find(id) != quic_stream_by_id_.end()); |
+ cricket::ReliableQuicStream* stream = quic_stream_by_id_[id]; |
+ stream->SignalDataReceived.disconnect(this); |
+ quic_stream_by_id_.erase(id); |
+ // Read data channel id |
+ rtc::ByteBufferReader byte_buffer(data, len, rtc::ByteBuffer::ORDER_HOST); |
+ uint64_t data_channel_id; |
+ if (!byte_buffer.ReadUVarint(&data_channel_id)) { |
+ LOG(LS_ERROR) << "Could not read the data channel ID for QUIC stream " |
+ << id; |
+ return; |
+ } |
+ // Retrieve the data channel which will handle the message. |
+ const auto& kv = data_channel_by_id_.find(data_channel_id); |
+ if (kv == data_channel_by_id_.end()) { |
+ // TODO(mikescarlett): Implement OPEN message to create a new |
+ // QuicDataChannel when messages are received for a nonexistent ID. |
+ LOG(LS_ERROR) << "Data was received for QUIC data channel " |
+ << data_channel_id |
+ << " but it is not registered to the QuicDataTransport."; |
+ return; |
+ } |
+ QuicDataChannel* data_channel = kv->second; |
+ uint64_t message_id; |
+ if (!byte_buffer.ReadUVarint(&message_id)) { |
+ LOG(LS_ERROR) << "Could not read message ID for QUIC stream " << id |
+ << " destined for data channel " << data_channel_id; |
+ return; |
+ } |
+ QuicMessageDispatcher::Dispatch(data_channel, message_id, byte_buffer.Data(), |
+ byte_buffer.Length(), stream); |
+} |
+ |
+void QuicDataTransport::EncodeHeader(int data_channel_id, |
+ uint64_t message_id, |
+ rtc::CopyOnWriteBuffer* header) const { |
+ RTC_DCHECK(header != nullptr); |
+ // 64-bit varints require at most 10 bytes (7*10 == 70), and 32-bit varints |
+ // require at most 5 bytes (7*5 == 35). |
+ size_t max_length = 15; |
+ rtc::ByteBufferWriter byte_buffer(nullptr, max_length, |
+ rtc::ByteBuffer::ByteOrder::ORDER_HOST); |
+ byte_buffer.WriteUVarint(data_channel_id); |
+ byte_buffer.WriteUVarint(message_id); |
+ header->SetData(byte_buffer.Data(), byte_buffer.Length()); |
+} |
+ |
+} // namespace webrtc |