Chromium Code Reviews| Index: webrtc/api/quicdatachannel.h |
| diff --git a/webrtc/api/quicdatachannel.h b/webrtc/api/quicdatachannel.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fbc8dba59b357081b5ac14de263f5246fd8353d8 |
| --- /dev/null |
| +++ b/webrtc/api/quicdatachannel.h |
| @@ -0,0 +1,223 @@ |
| +/* |
| + * 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 { |
| +// TODO(mikescarlett): Make this uint64_t once QUIC uses 64-bit ids. |
| +typedef uint32_t QuicStreamId; |
| +} // namespace net |
| + |
| +namespace rtc { |
| +class CopyOnWriteBuffer; |
| +} // namespace rtc |
| + |
| +namespace webrtc { |
| + |
| +class QuicMessageDispatcher; |
| + |
| +// QuicDataChannel is an implementation of DataChannelInterface based on the |
| +// QUIC protocol. It uses a QuicTransportChannel to establish encryption and |
| +// transfer data, and a QuicMessageDispatcher to receive incoming messages at |
| +// the correct data channel. Currently this class implements unordered, reliable |
| +// delivery and does not send an "open" message. |
| +// |
| +// Each time a message is sent: |
| +// |
| +// - The QuicDataChannel's QuicMessageDispatcher prepends it with the data |
| +// channel id and message id. The local peer's QuicTransportChannel creates a |
| +// ReliableQuicStream, then the ReliableQuicStream sends it with a FIN. |
| +// |
| +// - The remote QuicSession creates a ReliableQuicStream to receive the data. |
| +// The remote QuicMessageDispatcher dispatches the ReliableQuicStream to the |
| +// QuicDataChannel with the same id as this data channel. |
| +// |
| +// - The remote QuicDataChannel queues data from the ReliableQuicStream. Once |
| +// it receives a QUIC stream frame with a FIN, it provides the message 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: |
| + QuicDataChannel(rtc::Thread* signaling_thread, |
| + rtc::Thread* worker_thread, |
| + const std::string& label, |
| + const DataChannelInit& config, |
| + const QuicMessageDispatcher& message_dispatcher); |
| + ~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 send messages. The QUIC transport |
| + // channel must not already be set. |
|
pthatcher1
2016/04/14 17:21:56
Can you specify in the comment why we can't put th
mikescarlett
2016/04/14 22:16:51
Done.
|
| + void SetTransportChannel(cricket::QuicTransportChannel* channel); |
|
pthatcher1
2016/04/14 17:21:56
Should we return a bool for failure if it fails?
mikescarlett
2016/04/14 22:16:51
That's ok I changed it. Previously I was asserting
|
| + // Gets the number of outgoing QUIC streams with write blocked data that are |
| + // currently open for this data channel. |
| + size_t GetNumWriteBlockedStreams() const; |
|
pthatcher1
2016/04/14 17:21:57
What is this used for? Can you expand the comment
mikescarlett
2016/04/14 22:16:51
This makes it possible to test that write blocked
|
| + // Gets the number of incoming QUIC streams with buffered data that are |
| + // currently open for this data channel. |
| + size_t GetNumIncomingStreams() const; |
|
pthatcher1
2016/04/14 17:21:57
Same here
mikescarlett
2016/04/14 22:16:51
Done.
|
| + |
| + private: |
| + // Message contains the incoming QUIC stream being used to receive a message |
| + // from the remote peer, and buffered data from the QUIC stream. |
| + struct Message { |
| + cricket::ReliableQuicStream* stream; |
| + rtc::CopyOnWriteBuffer buffer; |
| + }; |
| + |
| + // Callbacks for ReliableQuicStream. |
| + void OnDataReceived(net::QuicStreamId stream_id, |
| + const char* data, |
| + size_t len); |
| + void OnWriteBlockedStreamClosed(net::QuicStreamId stream_id, int error); |
| + void OnIncomingQueuedStreamClosed(net::QuicStreamId stream_id, int error); |
| + void OnQueuedBytesWritten(net::QuicStreamId stream_id, |
| + uint64_t queued_bytes_written); |
| + |
| + // Callbacks for |quic_transport_channel_|. |
| + void OnReadyToSend(cricket::TransportChannel* channel); |
| + void OnConnectionClosed(); |
| + |
| + // Callback for |message_dispatcher_|. |
|
pthatcher1
2016/04/14 17:21:56
for message_dispatcher_ or *from* message_dispatch
mikescarlett
2016/04/14 22:16:51
Done.
|
| + // 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); |
| + |
| + // Worker thread methods. |
| + // Sends the data buffer to the remote peer using an outgoing QUIC stream. |
| + // Returns true if the data buffer can be successfully sent. |
| + bool Send_w(const DataBuffer& buffer); |
| + // Connects the |quic_transport_channel_| signals to this QuicDataChannel. |
| + void ConnectTransportChannel_w(); |
| + // Closes the QUIC streams associated with this QuicDataChannel. |
| + void Close_w(); |
| + |
| + // Signaling thread methods. |
| + // Triggers QuicDataChannelObserver::OnMessage when a message from the remote |
| + // peer is ready to be read. |
| + void OnMessage_s(const DataBuffer& received_data); |
| + // Triggers QuicDataChannel::OnStateChange if the state change is valid. |
| + // Otherwise does nothing if |state| == |state_| or |state| != kClosed when |
| + // the data channel is closing. |
| + void SetState_s(DataState state); |
| + // Triggers QuicDataChannelObserver::OnBufferedAmountChange when the total |
| + // buffered data changes for a QUIC stream. |
| + void OnBufferedAmountChange_s(uint64_t buffered_amount); |
| + |
| + // QUIC transport channel which owns the QUIC session. It is used to create |
| + // a QUIC stream for sending outgoing messages. |
| + cricket::QuicTransportChannel* quic_transport_channel_ = nullptr; |
| + // Signaling thread for DataChannelInterface methods. |
| + rtc::Thread* const signaling_thread_; |
| + // Worker thread for sending data and |quic_transport_channel_| callbacks. |
| + rtc::Thread* const worker_thread_; |
| + // Invokes asyncronous method calls. |
|
pthatcher1
2016/04/14 17:21:56
This comment isn't needed.
mikescarlett
2016/04/14 22:16:51
Done.
|
| + rtc::AsyncInvoker invoker_; |
| + // Helper for encoding messages with the data channel/message id so they can |
| + // be dispatched to the appropriate data channel. |
| + const QuicMessageDispatcher& message_dispatcher_; |
|
pthatcher1
2016/04/14 17:21:57
Why are we storing a const ref instead of a const
mikescarlett
2016/04/14 22:16:51
I'm removing this. See explanation below.
|
| + // Map of QUIC stream ID => ReliableQuicStream* for write blocked QUIC |
| + // streams. |
| + 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 and data channel state changes. |
| + DataChannelObserver* observer_ = nullptr; |
| + // QuicDataChannel id. |
| + int id_; |
| + // Connectivity state of the QuicDataChannel. |
| + 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 QuicMessageDispatcher; |
|
pthatcher1
2016/04/14 17:21:57
Why is this necessary?
mikescarlett
2016/04/14 22:16:51
This was for keeping OnIncomingStream private. See
|
| +}; |
| + |
| +// QuicMessageDispatcher is an abstract class that provides incoming QUIC |
| +// streams to the QuicDataChannel via Dispatch(). It also encodes messages sent |
| +// by the QuicDataChannel with the data channel id and message id, so that the |
| +// remote peer's QuicMessageDispatcher can decode these to dispatch the incoming |
| +// QUIC stream to the correct data channel. |
| +// |
| +// Subclasses are responsible for receiving incoming QUIC streams from the |
| +// QuicTransportChannel, and parsing the data channel ID and message ID from |
| +// the initial QUIC stream frame. |
| +class QuicMessageDispatcher { |
|
pthatcher1
2016/04/14 17:21:56
I don't quite understand the design of the QuicMes
mikescarlett
2016/04/14 22:16:50
I'm removing this. Its main purpose was to keep On
|
| + public: |
| + virtual ~QuicMessageDispatcher() {} |
| + // Encodes the message header with the data channel ID and message ID before |
| + // it is sent by a QUIC stream. |
| + virtual void EncodeHeader(int data_channel_id, |
| + uint64_t message_id, |
| + rtc::CopyOnWriteBuffer* header) const = 0; |
| + |
| + protected: |
| + // Dispatches the incoming ReliableQuicStream to the QuicDataChannel. The |
| + // QuicDataChannel must have this instance as its QuicMessageDispatcher. |
| + // |message_id| is the ID of the data channel's message, and |first_bytes| |
| + // contains |len| bytes from the first QUIC stream frame of the incoming |
| + // ReliableQuicStream. The QuicDataChannel is responsible for reading the |
| + // remaining data from the ReliableQuicStream. |
| + void Dispatch(QuicDataChannel* data_channel, |
| + uint64_t message_id, |
| + const char* first_bytes, |
| + size_t len, |
| + cricket::ReliableQuicStream* stream) const { |
| + RTC_DCHECK(&data_channel->message_dispatcher_ == this); |
| + data_channel->OnIncomingStream(message_id, first_bytes, len, stream); |
| + } |
| +}; |
| + |
| +} // namespace webrtc |
| + |
| +#endif // WEBRTC_API_QUICDATACHANNEL_H_ |