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..b1f525c931928d9212184af8441cf7105ab46230 | 
| --- /dev/null | 
| +++ b/webrtc/api/quicdatachannel.h | 
| @@ -0,0 +1,160 @@ | 
| +/* | 
| + * 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/api/proxy.h" | 
| +#include "webrtc/base/copyonwritebuffer.h" | 
| +#include "webrtc/base/bytebuffer.h" | 
| +#include "webrtc/base/scoped_ref_ptr.h" | 
| 
 
pthatcher1
2016/03/30 20:34:48
If these aren't used in the .h, please move them t
 
mikescarlett
2016/04/05 19:58:50
Done.
 
 | 
| +#include "webrtc/base/sigslot.h" | 
| +#include "webrtc/base/thread.h" | 
| + | 
| +namespace net { | 
| +typedef uint32_t QuicStreamId; | 
| +} // namespace net | 
| + | 
| +namespace cricket { | 
| +class QuicTransportChannel; | 
| +class ReliableQuicStream; | 
| +class TransportChannel; | 
| +} // namepsace cricket | 
| + | 
| +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 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 | 
| 
 
pthatcher1
2016/03/30 20:34:48
Funny formatting here.
 
mikescarlett
2016/04/05 19:58:50
I'll fix the formatting
 
 | 
| +// 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 until | 
| +// the QUIC stream receives a frame with a FIN. | 
| +// | 
| +// TODO(mikescarlett): Write blocked? closed? thread safety? | 
| +// TODO(mikescarlett): Allow peers to negotiate QUIC flow control window size, | 
| +// so that peers can set the maximum size of a message. Currently QUIC | 
| +// defaults to 16KB per stream and session. | 
| +// TODO(mikescarlett): Jitter buffer | 
| +class QuicDataChannel : public rtc::RefCountedObject<DataChannelInterface>, | 
| + public sigslot::has_slots<> { | 
| + public: | 
| + QuicDataChannel(cricket::QuicTransportChannel* quic_transport_channel, | 
| + rtc::Thread* signaling_thread, | 
| + rtc::Thread* worker_thread, | 
| + const std::string& label, | 
| + const DataChannelInit* config); | 
| + ~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; | 
| + // Registers a new QUIC stream to this data channel. | 
| + virtual void OnIncomingStream(cricket::ReliableQuicStream* stream, | 
| + rtc::ByteBuffer* remaining_bytes); | 
| 
 
Taylor Brandstetter
2016/04/01 23:23:41
Maybe make this private and declare "friend QuicTr
 
mikescarlett
2016/04/05 19:58:50
Is this to prevent the method from being exposed d
 
Taylor Brandstetter
2016/04/05 22:31:17
Peter's suggestion sounds good. But even then, mak
 
mikescarlett
2016/04/13 16:53:36
I made this method private. For testing I have a f
 
 | 
| + rtc::Thread* worker_thread() const { return worker_thread_; } | 
| + | 
| + bool has_incoming_quic_stream(net::QuicStreamId id) const { | 
| + return incoming_quic_streams_.find(id) != incoming_quic_streams_.end(); | 
| + } | 
| 
 
Taylor Brandstetter
2016/04/01 23:23:41
What is this used for, and does it need to be publ
 
mikescarlett
2016/04/05 19:58:50
This isn't needed. I removed it.
 
 | 
| + | 
| + // Emitted when the state transitions to kClosed. | 
| + sigslot::signal1<QuicDataChannel*> SignalClosed; | 
| + | 
| + protected: | 
| + virtual bool Send_w(const DataBuffer& buffer); | 
| + virtual bool WriteData_w(const char* data, size_t len); | 
| + virtual void OnMessage_s(const DataBuffer& received_data); | 
| + // Sends the data channel OPEN message. | 
| + // TODO(mikescarlett): Implement this method. | 
| + virtual void SendOpenMessage_w(); | 
| + virtual void SetState(DataState state); | 
| + | 
| + rtc::Thread* signaling_thread() { return signaling_thread_; } | 
| + rtc::Thread* worker_thread() { return worker_thread_; } | 
| + | 
| + private: | 
| + // Callbacks for ReliableQuicStream. | 
| + void OnDataReceived(net::QuicStreamId stream_id, | 
| + const char* data, | 
| + size_t len); | 
| + void OnStreamClosed(net::QuicStreamId stream_id, int error); | 
| + | 
| + // Callbacks for |quic_transport_channel_|. | 
| + // Called when the transport chennel becomes writable or unwritable. | 
| + void OnWritableState(cricket::TransportChannel* channel); | 
| + // Called when the QUIC session closes the connection due to handshake failure | 
| + // or explicit | 
| + // termination. | 
| + void OnConnectionClosed(); | 
| + | 
| + // QUIC transport channel which owns the QUIC session. | 
| + cricket::QuicTransportChannel* const quic_transport_channel_; | 
| + // Signaling thread for DataChannelInterface methods. | 
| + rtc::Thread* const signaling_thread_; | 
| + // Worker thread for sending/receiving data. | 
| + rtc::Thread* const worker_thread_; | 
| + // QUIC streams which have queued incoming data from the remote peer. | 
| + std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*> | 
| + incoming_quic_streams_; | 
| + // Queued data for each incoming QUIC stream. | 
| + std::unordered_map<net::QuicStreamId, rtc::CopyOnWriteBuffer> queued_data_; | 
| + // 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_; | 
| + | 
| + // True if the data channel sends an OPEN message to the remote peer once | 
| + // this data channel is open. | 
| + bool send_open_message_; | 
| +}; | 
| + | 
| +} // namespace webrtc | 
| + | 
| +#endif // WEBRTC_API_QUICDATACHANNEL_H_ |