Chromium Code Reviews| 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 #ifndef WEBRTC_API_QUICDATACHANNEL_H_ | |
| 12 #define WEBRTC_API_QUICDATACHANNEL_H_ | |
| 13 | |
| 14 #include <string> | |
| 15 #include <unordered_map> | |
| 16 #include <unordered_set> | |
| 17 | |
| 18 #include "webrtc/api/datachannelinterface.h" | |
| 19 #include "webrtc/base/asyncinvoker.h" | |
| 20 #include "webrtc/base/sigslot.h" | |
| 21 #include "webrtc/base/thread.h" | |
| 22 | |
| 23 namespace cricket { | |
| 24 class QuicTransportChannel; | |
| 25 class ReliableQuicStream; | |
| 26 class TransportChannel; | |
| 27 } // namepsace cricket | |
| 28 | |
| 29 namespace net { | |
| 30 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.
| |
| 31 } // namespace net | |
| 32 | |
| 33 namespace rtc { | |
| 34 class CopyOnWriteBuffer; | |
| 35 } // namespace rtc | |
| 36 | |
| 37 namespace webrtc { | |
| 38 | |
| 39 // QuicDataChannel is an implementation of DataChannelInterface based on the | |
| 40 // QUIC protocol. It uses a QuicTransportChannel to establish encryption and | |
| 41 // transfer data. Currently this class implements unordered, reliable delivery. | |
| 42 // | |
| 43 // Each time a message is sent: | |
| 44 // | |
| 45 // - The local peer uses a QuicTransportChannel to create a ReliableQuicStream. | |
| 46 // The message is prepended with the data channel id and message id, then the | |
| 47 // ReliableQuicStream sends it to the remote peer with a FIN. | |
| 48 // | |
| 49 // - The remote peer's QuicSession creates a ReliableQuicStream to receive the | |
| 50 // data. The ReliableQuicStream is assigned to a QuicDataChannel with the same | |
| 51 // id as the local peer's data channel. | |
| 52 // | |
| 53 // - The remote QuicDataChannel queues data from the ReliableQuicStream. Once | |
| 54 // it receives a QUIC stream frame with a FIN, it signals that a message has | |
| 55 // arrived to the DataChannelObserver. | |
| 56 // | |
| 57 // TODO(mikescarlett): Implement ordered delivery, unreliable delivery, and | |
| 58 // an OPEN message similar to the one for SCTP. | |
| 59 class QuicDataChannel : public rtc::RefCountedObject<DataChannelInterface>, | |
| 60 public sigslot::has_slots<> { | |
| 61 public: | |
| 62 // MessageHelper is an abstract class that encodes messages sent by the | |
| 63 // QuicDataChannel with the data channel id and message id. Once the encoded | |
| 64 // message is sent by the outgoing QUIC stream, the remote peer's | |
| 65 // MessageHelper parses the data channel id and message id, then assigning | |
| 66 // the QUIC stream to the matching QuicDataChannel by calling Dispatch(). | |
| 67 // | |
| 68 // Subclasses are responsible for handling incoming QUIC streams from | |
| 69 // QuicTransportChannel::SignalIncomingStream and reading data from | |
| 70 // ReliableQuicStream::SignalDataReceived before assigning the QUIC stream | |
| 71 // to the data channel. | |
| 72 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
| |
| 73 public: | |
| 74 virtual ~MessageHelper() {} | |
| 75 // Encodes the message with the data channel id and message id before the | |
| 76 // QuicDataChannel sends it to the remote peer's MessageHelper. | |
| 77 virtual void Encode(const DataBuffer& message, | |
| 78 int data_channel_id, | |
| 79 uint64_t message_id, | |
| 80 rtc::CopyOnWriteBuffer* payload) const = 0; | |
| 81 protected: | |
| 82 // Dispatches the incoming ReliableQuicStream to the corresponding | |
| 83 // QuicDataChannel with the message id and the first bytes of the message, | |
| 84 // once the first QUIC stream frame has been received. MessageHelper | |
| 85 // should stop reading data from the ReliableQuicStream once this is called. | |
| 86 void Dispatch(QuicDataChannel* data_channel, | |
| 87 uint64_t message_id, | |
| 88 const char* first_bytes, | |
| 89 size_t len, | |
| 90 cricket::ReliableQuicStream* stream) const { | |
| 91 data_channel->OnIncomingStream(message_id, first_bytes, len, stream); | |
| 92 } | |
|
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
| |
| 93 }; | |
| 94 | |
| 95 QuicDataChannel(rtc::Thread* signaling_thread, | |
| 96 rtc::Thread* worker_thread, | |
| 97 const std::string& label, | |
| 98 const DataChannelInit* config, | |
| 99 const MessageHelper& message_helper); | |
| 100 ~QuicDataChannel() override; | |
| 101 | |
| 102 // DataChannelInterface overrides. | |
| 103 std::string label() const override { return label_; } | |
| 104 bool reliable() const override { return true; } | |
| 105 bool ordered() const override { return false; } | |
| 106 uint16_t maxRetransmitTime() const override { return -1; } | |
| 107 uint16_t maxRetransmits() const override { return -1; } | |
| 108 bool negotiated() const override { return false; } | |
| 109 int id() const override { return id_; } | |
| 110 DataState state() const override { return state_; } | |
| 111 uint64_t buffered_amount() const override { return buffered_amount_; } | |
| 112 std::string protocol() const override { return protocol_; } | |
| 113 void RegisterObserver(DataChannelObserver* observer) override; | |
| 114 void UnregisterObserver() override; | |
| 115 void Close() override; | |
| 116 bool Send(const DataBuffer& buffer) override; | |
| 117 | |
| 118 // Sets the QUIC transport channel that will be used for creating QUIC | |
| 119 // streams and updating the data channel's writability state. | |
| 120 void SetTransportChannel(cricket::QuicTransportChannel* channel); | |
| 121 | |
| 122 // Emitted when the state transitions to kClosed. | |
| 123 sigslot::signal1<QuicDataChannel*> SignalClosed; | |
| 124 | |
| 125 protected: | |
| 126 bool Send_w(const DataBuffer& buffer); | |
| 127 bool WriteData_w(const char* data, size_t len); | |
| 128 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
| |
| 129 void SetState(DataState state); | |
| 130 | |
| 131 private: | |
| 132 // Message contains the incoming QUIC stream that is receiving a message from | |
| 133 // the remote peer, and its buffered data before the FIN has arrived. | |
| 134 struct Message { | |
| 135 cricket::ReliableQuicStream* stream; | |
| 136 rtc::CopyOnWriteBuffer buffer; | |
| 137 }; | |
| 138 | |
| 139 // Callbacks for ReliableQuicStream. | |
| 140 void OnDataReceived(net::QuicStreamId stream_id, | |
| 141 const char* data, | |
| 142 size_t len); | |
| 143 void OnStreamClosed(net::QuicStreamId stream_id, int error); | |
| 144 void OnQueuedBytesWritten(net::QuicStreamId stream_id, | |
| 145 uint64_t queued_bytes_written); | |
| 146 | |
| 147 // Callbacks for |quic_transport_channel_|. | |
| 148 void OnWritableState(cricket::TransportChannel* channel); | |
| 149 void OnConnectionClosed(); | |
| 150 | |
| 151 // Callback for |message_helper_|. | |
| 152 // Called when the first QUIC stream frame for an incoming ReliableQuicStream | |
| 153 // has been received for this data channel. | |
| 154 virtual void OnIncomingStream(uint64_t message_id, | |
| 155 const char* first_bytes, | |
| 156 size_t len, | |
| 157 cricket::ReliableQuicStream* stream); | |
| 158 | |
| 159 // QUIC transport channel which owns the QUIC session. It is used to create | |
| 160 // a QUIC stream each time a message is sent. | |
| 161 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr; | |
| 162 // Signaling thread for DataChannelInterface methods. | |
| 163 rtc::Thread* const signaling_thread_; | |
| 164 // Worker thread for sending/receiving data. | |
| 165 rtc::Thread* const worker_thread_; | |
| 166 // Invokes asyncronous method calls. | |
| 167 rtc::AsyncInvoker invoker_; | |
| 168 // Helper for encoding messages with the data channel/message id and | |
| 169 // dispatching them to the appropriate data channel. | |
| 170 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
| |
| 171 // Map of QUIC stream ID => ReliableQuicStream* for each write blocked QUIC | |
| 172 // stream. | |
| 173 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*> | |
| 174 write_blocked_quic_streams_; | |
| 175 // Map of QUIC stream ID => Message for each incoming QUIC stream. | |
| 176 std::unordered_map<net::QuicStreamId, Message> incoming_quic_streams_; | |
| 177 // Handles received data from the remote peer. | |
| 178 DataChannelObserver* observer_; | |
| 179 // Data channel id. | |
| 180 int id_; | |
| 181 // Connectivity state of the data channel. | |
| 182 DataState state_; | |
| 183 // Total bytes that are buffered among the QUIC streams. | |
| 184 uint64_t buffered_amount_; | |
| 185 // Counter for number of sent messages that is used for message ids. | |
| 186 uint64_t num_sent_messages_; | |
| 187 | |
| 188 // Variables for application use. | |
| 189 const std::string& label_; | |
| 190 const std::string& protocol_; | |
| 191 | |
| 192 friend MessageHelper; | |
| 193 }; | |
| 194 | |
| 195 } // namespace webrtc | |
| 196 | |
| 197 #endif // WEBRTC_API_QUICDATACHANNEL_H_ | |
| OLD | NEW |