Index: webrtc/api/quicdatachannel.h |
diff --git a/webrtc/api/quicdatachannel.h b/webrtc/api/quicdatachannel.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e792362e03b4ccbde9e856f8629aac81733b217e |
--- /dev/null |
+++ b/webrtc/api/quicdatachannel.h |
@@ -0,0 +1,197 @@ |
+/* |
+ * 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. |
+ */ |
+ |
+#ifndef WEBRTC_API_QUICDATACHANNEL_H_ |
+#define WEBRTC_API_QUICDATACHANNEL_H_ |
+ |
+#include <string> |
+#include <unordered_map> |
+#include <unordered_set> |
+ |
+#include "webrtc/api/datachannelinterface.h" |
+#include "webrtc/base/asyncinvoker.h" |
+#include "webrtc/base/sigslot.h" |
+#include "webrtc/base/thread.h" |
+ |
+namespace cricket { |
+class QuicTransportChannel; |
+class ReliableQuicStream; |
+class TransportChannel; |
+} // namepsace cricket |
+ |
+namespace net { |
+typedef uint32_t QuicStreamId; |
pthatcher1
2016/04/12 00:58:39
Put a TODO to make this uint64_t. The QUIC guys s
mikescarlett
2016/04/13 16:53:38
Done.
|
+} // namespace net |
+ |
+namespace rtc { |
+class CopyOnWriteBuffer; |
+} // namespace rtc |
+ |
+namespace webrtc { |
+ |
+// QuicDataChannel is an implementation of DataChannelInterface based on the |
+// QUIC protocol. It uses a QuicTransportChannel to establish encryption and |
+// transfer data. Currently this class implements unordered, reliable delivery. |
+// |
+// Each time a message is sent: |
+// |
+// - The local peer uses a QuicTransportChannel to create a ReliableQuicStream. |
+// The message is prepended with the data channel id and message id, then the |
+// ReliableQuicStream sends it to the remote peer with a FIN. |
+// |
+// - The remote peer's QuicSession creates a ReliableQuicStream to receive the |
+// data. The ReliableQuicStream is assigned to a QuicDataChannel with the same |
+// id as the local peer's data channel. |
+// |
+// - The remote QuicDataChannel queues data from the ReliableQuicStream. Once |
+// it receives a QUIC stream frame with a FIN, it signals that a message has |
+// arrived to the DataChannelObserver. |
+// |
+// TODO(mikescarlett): Implement ordered delivery, unreliable delivery, and |
+// an OPEN message similar to the one for SCTP. |
+class QuicDataChannel : public rtc::RefCountedObject<DataChannelInterface>, |
+ public sigslot::has_slots<> { |
+ public: |
+ // MessageHelper is an abstract class that encodes messages sent by the |
+ // QuicDataChannel with the data channel id and message id. Once the encoded |
+ // message is sent by the outgoing QUIC stream, the remote peer's |
+ // MessageHelper parses the data channel id and message id, then assigning |
+ // the QUIC stream to the matching QuicDataChannel by calling Dispatch(). |
+ // |
+ // Subclasses are responsible for handling incoming QUIC streams from |
+ // QuicTransportChannel::SignalIncomingStream and reading data from |
+ // ReliableQuicStream::SignalDataReceived before assigning the QUIC stream |
+ // to the data channel. |
+ class MessageHelper { |
pthatcher1
2016/04/12 00:58:39
Why is this public?
mikescarlett
2016/04/13 16:53:38
I made it public so that my unit test and QuicData
|
+ public: |
+ virtual ~MessageHelper() {} |
+ // Encodes the message with the data channel id and message id before the |
+ // QuicDataChannel sends it to the remote peer's MessageHelper. |
+ virtual void Encode(const DataBuffer& message, |
+ int data_channel_id, |
+ uint64_t message_id, |
+ rtc::CopyOnWriteBuffer* payload) const = 0; |
+ protected: |
+ // Dispatches the incoming ReliableQuicStream to the corresponding |
+ // QuicDataChannel with the message id and the first bytes of the message, |
+ // once the first QUIC stream frame has been received. MessageHelper |
+ // should stop reading data from the ReliableQuicStream once this is called. |
+ void Dispatch(QuicDataChannel* data_channel, |
+ uint64_t message_id, |
+ const char* first_bytes, |
+ size_t len, |
+ cricket::ReliableQuicStream* stream) const { |
+ data_channel->OnIncomingStream(message_id, first_bytes, len, stream); |
+ } |
pthatcher1
2016/04/12 00:58:39
Why is this protected?
mikescarlett
2016/04/13 16:53:38
I made this protected so that only subclasses can
|
+ }; |
+ |
+ QuicDataChannel(rtc::Thread* signaling_thread, |
+ rtc::Thread* worker_thread, |
+ const std::string& label, |
+ const DataChannelInit* config, |
+ const MessageHelper& message_helper); |
+ ~QuicDataChannel() override; |
+ |
+ // DataChannelInterface overrides. |
+ std::string label() const override { return label_; } |
+ bool reliable() const override { return true; } |
+ bool ordered() const override { return false; } |
+ uint16_t maxRetransmitTime() const override { return -1; } |
+ uint16_t maxRetransmits() const override { return -1; } |
+ bool negotiated() const override { return false; } |
+ int id() const override { return id_; } |
+ DataState state() const override { return state_; } |
+ uint64_t buffered_amount() const override { return buffered_amount_; } |
+ std::string protocol() const override { return protocol_; } |
+ void RegisterObserver(DataChannelObserver* observer) override; |
+ void UnregisterObserver() override; |
+ void Close() override; |
+ bool Send(const DataBuffer& buffer) override; |
+ |
+ // Sets the QUIC transport channel that will be used for creating QUIC |
+ // streams and updating the data channel's writability state. |
+ void SetTransportChannel(cricket::QuicTransportChannel* channel); |
+ |
+ // Emitted when the state transitions to kClosed. |
+ sigslot::signal1<QuicDataChannel*> SignalClosed; |
+ |
+ protected: |
+ bool Send_w(const DataBuffer& buffer); |
+ bool WriteData_w(const char* data, size_t len); |
+ void OnMessage_s(const DataBuffer& received_data); |
pthatcher1
2016/04/12 00:58:39
Can you provide some comments on these? Why are t
mikescarlett
2016/04/13 16:53:38
I added comments. They should be private since Qui
|
+ void SetState(DataState state); |
+ |
+ private: |
+ // Message contains the incoming QUIC stream that is receiving a message from |
+ // the remote peer, and its buffered data before the FIN has arrived. |
+ struct Message { |
+ cricket::ReliableQuicStream* stream; |
+ rtc::CopyOnWriteBuffer buffer; |
+ }; |
+ |
+ // Callbacks for ReliableQuicStream. |
+ void OnDataReceived(net::QuicStreamId stream_id, |
+ const char* data, |
+ size_t len); |
+ void OnStreamClosed(net::QuicStreamId stream_id, int error); |
+ void OnQueuedBytesWritten(net::QuicStreamId stream_id, |
+ uint64_t queued_bytes_written); |
+ |
+ // Callbacks for |quic_transport_channel_|. |
+ void OnWritableState(cricket::TransportChannel* channel); |
+ void OnConnectionClosed(); |
+ |
+ // Callback for |message_helper_|. |
+ // Called when the first QUIC stream frame for an incoming ReliableQuicStream |
+ // has been received for this data channel. |
+ virtual void OnIncomingStream(uint64_t message_id, |
+ const char* first_bytes, |
+ size_t len, |
+ cricket::ReliableQuicStream* stream); |
+ |
+ // QUIC transport channel which owns the QUIC session. It is used to create |
+ // a QUIC stream each time a message is sent. |
+ cricket::QuicTransportChannel* quic_transport_channel_ = nullptr; |
+ // Signaling thread for DataChannelInterface methods. |
+ rtc::Thread* const signaling_thread_; |
+ // Worker thread for sending/receiving data. |
+ rtc::Thread* const worker_thread_; |
+ // Invokes asyncronous method calls. |
+ rtc::AsyncInvoker invoker_; |
+ // Helper for encoding messages with the data channel/message id and |
+ // dispatching them to the appropriate data channel. |
+ const MessageHelper& message_helper_; |
pthatcher1
2016/04/12 00:58:39
Hold a const ref???
mikescarlett
2016/04/13 16:53:38
I didn't see the need for QuicDataChannel to modif
|
+ // Map of QUIC stream ID => ReliableQuicStream* for each write blocked QUIC |
+ // stream. |
+ std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*> |
+ write_blocked_quic_streams_; |
+ // Map of QUIC stream ID => Message for each incoming QUIC stream. |
+ std::unordered_map<net::QuicStreamId, Message> incoming_quic_streams_; |
+ // Handles received data from the remote peer. |
+ DataChannelObserver* observer_; |
+ // Data channel id. |
+ int id_; |
+ // Connectivity state of the data channel. |
+ DataState state_; |
+ // Total bytes that are buffered among the QUIC streams. |
+ uint64_t buffered_amount_; |
+ // Counter for number of sent messages that is used for message ids. |
+ uint64_t num_sent_messages_; |
+ |
+ // Variables for application use. |
+ const std::string& label_; |
+ const std::string& protocol_; |
+ |
+ friend MessageHelper; |
+}; |
+ |
+} // namespace webrtc |
+ |
+#endif // WEBRTC_API_QUICDATACHANNEL_H_ |