Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / quic_packet_generator.h
blob762a79896fe958866fe5bcd95faa40af0cca3ed2
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Responsible for generating packets on behalf of a QuicConnection.
6 // Packets are serialized just-in-time. Control frames are queued.
7 // Ack and Feedback frames will be requested from the Connection
8 // just-in-time. When a packet needs to be sent, the Generator
9 // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket()
11 // The Generator's mode of operation is controlled by two conditions:
13 // 1) Is the Delegate writable?
15 // If the Delegate is not writable, then no operations will cause
16 // a packet to be serialized. In particular:
17 // * SetShouldSendAck will simply record that an ack is to be sent.
18 // * AddControlFrame will enqueue the control frame.
19 // * ConsumeData will do nothing.
21 // If the Delegate is writable, then the behavior depends on the second
22 // condition:
24 // 2) Is the Generator in batch mode?
26 // If the Generator is NOT in batch mode, then each call to a write
27 // operation will serialize one or more packets. The contents will
28 // include any previous queued frames. If an ack should be sent
29 // but has not been sent, then the Delegate will be asked to create
30 // an Ack frame which will then be included in the packet. When
31 // the write call completes, the current packet will be serialized
32 // and sent to the Delegate, even if it is not full.
34 // If the Generator is in batch mode, then each write operation will
35 // add data to the "current" packet. When the current packet becomes
36 // full, it will be serialized and sent to the packet. When batch
37 // mode is ended via |FinishBatchOperations|, the current packet
38 // will be serialzied, even if it is not full.
40 // FEC behavior also depends on batch mode. In batch mode, FEC packets
41 // will be sent after |max_packets_per_group| have been sent, as well
42 // as after batch operations are complete. When not in batch mode,
43 // an FEC packet will be sent after each write call completes.
45 // TODO(rch): This behavior should probably be tuned. When not in batch
46 // mode, we should probably set a timer so that several independent
47 // operations can be grouped into the same FEC group.
49 // When an FEC packet is generated, it will be send to the Delegate,
50 // even if the Delegate has become unwritable after handling the
51 // data packet immediately proceeding the FEC packet.
53 #ifndef NET_QUIC_QUIC_PACKET_GENERATOR_H_
54 #define NET_QUIC_QUIC_PACKET_GENERATOR_H_
56 #include <list>
58 #include "net/quic/quic_ack_notifier.h"
59 #include "net/quic/quic_packet_creator.h"
60 #include "net/quic/quic_sent_packet_manager.h"
61 #include "net/quic/quic_types.h"
63 namespace net {
65 namespace test {
66 class QuicPacketGeneratorPeer;
67 } // namespace test
69 class NET_EXPORT_PRIVATE QuicPacketGenerator {
70 public:
71 class NET_EXPORT_PRIVATE DelegateInterface {
72 public:
73 virtual ~DelegateInterface() {}
74 virtual bool ShouldGeneratePacket(TransmissionType transmission_type,
75 HasRetransmittableData retransmittable,
76 IsHandshake handshake) = 0;
77 virtual void PopulateAckFrame(QuicAckFrame* ack) = 0;
78 virtual void PopulateStopWaitingFrame(
79 QuicStopWaitingFrame* stop_waiting) = 0;
80 // Takes ownership of |packet.packet| and |packet.retransmittable_frames|.
81 virtual void OnSerializedPacket(const SerializedPacket& packet) = 0;
82 virtual void CloseConnection(QuicErrorCode error, bool from_peer) = 0;
85 // Interface which gets callbacks from the QuicPacketGenerator at interesting
86 // points. Implementations must not mutate the state of the generator
87 // as a result of these callbacks.
88 class NET_EXPORT_PRIVATE DebugDelegate {
89 public:
90 virtual ~DebugDelegate() {}
92 // Called when a frame has been added to the current packet.
93 virtual void OnFrameAddedToPacket(const QuicFrame& frame) {}
96 QuicPacketGenerator(QuicConnectionId connection_id,
97 QuicFramer* framer,
98 QuicRandom* random_generator,
99 DelegateInterface* delegate);
101 virtual ~QuicPacketGenerator();
103 // Called by the connection in the event of the congestion window changing.
104 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight);
106 // Called by the connection when the RTT may have changed.
107 void OnRttChange(QuicTime::Delta rtt);
109 // Indicates that an ACK frame should be sent.
110 // If |also_send_stop_waiting| is true, then it also indicates that a
111 // STOP_WAITING frame should be sent as well.
112 // The contents of the frame(s) will be generated via a call to the delegate
113 // CreateAckFrame() when the packet is serialized.
114 void SetShouldSendAck(bool also_send_stop_waiting);
116 void AddControlFrame(const QuicFrame& frame);
118 // Given some data, may consume part or all of it and pass it to the
119 // packet creator to be serialized into packets. If not in batch
120 // mode, these packets will also be sent during this call.
121 // |delegate| (if not nullptr) will be informed once all packets sent as a
122 // result of this call are ACKed by the peer.
123 QuicConsumedData ConsumeData(QuicStreamId id,
124 const IOVector& data,
125 QuicStreamOffset offset,
126 bool fin,
127 FecProtection fec_protection,
128 QuicAckNotifier::DelegateInterface* delegate);
130 // Indicates whether batch mode is currently enabled.
131 bool InBatchMode();
132 // Disables flushing.
133 void StartBatchOperations();
134 // Enables flushing and flushes queued data which can be sent.
135 void FinishBatchOperations();
137 // Flushes all queued frames, even frames which are not sendable.
138 void FlushAllQueuedFrames();
140 bool HasQueuedFrames() const;
142 // Makes the framer not serialize the protocol version in sent packets.
143 void StopSendingVersion();
145 // Creates a version negotiation packet which supports |supported_versions|.
146 // Caller owns the created packet. Also, sets the entropy hash of the
147 // serialized packet to a random bool and returns that value as a member of
148 // SerializedPacket.
149 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
150 const QuicVersionVector& supported_versions);
153 // Re-serializes frames with the original packet's sequence number length.
154 // Used for retransmitting packets to ensure they aren't too long.
155 // Caller must ensure that any open FEC group is closed before calling this
156 // method.
157 SerializedPacket ReserializeAllFrames(
158 const RetransmittableFrames& frames,
159 QuicSequenceNumberLength original_length);
161 // Update the sequence number length to use in future packets as soon as it
162 // can be safely changed.
163 void UpdateSequenceNumberLength(
164 QuicPacketSequenceNumber least_packet_awaited_by_peer,
165 QuicPacketCount max_packets_in_flight);
167 // Set the minimum number of bytes for the connection id length;
168 void SetConnectionIdLength(uint32 length);
170 // Called when the FEC alarm fires.
171 void OnFecTimeout();
173 // Called after sending |sequence_number| to determine whether an FEC alarm
174 // should be set for sending out an FEC packet. Returns a positive and finite
175 // timeout if an FEC alarm should be set, and infinite if no alarm should be
176 // set. OnFecTimeout should be called to send the FEC packet when the alarm
177 // fires.
178 QuicTime::Delta GetFecTimeout(QuicPacketSequenceNumber sequence_number);
180 // Sets the encrypter to use for the encryption level.
181 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
183 // Sets the encryption level that will be applied to new packets.
184 void set_encryption_level(EncryptionLevel level);
186 // Sequence number of the last created packet, or 0 if no packets have been
187 // created.
188 QuicPacketSequenceNumber sequence_number() const;
190 QuicByteCount max_packet_length() const;
192 void set_max_packet_length(QuicByteCount length);
194 void set_debug_delegate(DebugDelegate* debug_delegate) {
195 debug_delegate_ = debug_delegate;
198 private:
199 friend class test::QuicPacketGeneratorPeer;
201 // Turn on FEC protection for subsequent packets in the generator.
202 // If no FEC group is currently open in the creator, this method flushes any
203 // queued frames in the generator and in the creator, and it then turns FEC on
204 // in the creator. This method may be called with an open FEC group in the
205 // creator, in which case, only the generator's state is altered.
206 void MaybeStartFecProtection();
208 // Serializes and calls the delegate on an FEC packet if one was under
209 // construction in the creator. When |force| is false, it relies on the
210 // creator being ready to send an FEC packet, otherwise FEC packet is sent
211 // as long as one is under construction in the creator. Also tries to turn
212 // off FEC protection in the creator if it's off in the generator.
213 void MaybeSendFecPacketAndCloseGroup(bool force);
215 // Returns true if an FEC packet should be generated based on |force| and
216 // current state of the generator and the creator.
217 bool ShouldSendFecPacket(bool force);
219 void SendQueuedFrames(bool flush);
221 // Test to see if we have pending ack, or control frames.
222 bool HasPendingFrames() const;
223 // Test to see if the addition of a pending frame (which might be
224 // retransmittable) would still allow the resulting packet to be sent now.
225 bool CanSendWithNextPendingFrameAddition() const;
226 // Add exactly one pending frame, preferring ack frames over control frames.
227 bool AddNextPendingFrame();
229 bool AddFrame(const QuicFrame& frame);
231 void SerializeAndSendPacket();
233 DelegateInterface* delegate_;
234 DebugDelegate* debug_delegate_;
236 QuicPacketCreator packet_creator_;
237 QuicFrames queued_control_frames_;
239 // True if batch mode is currently enabled.
240 bool batch_mode_;
242 // Timeout used for FEC alarm. Can be set to zero initially or if the SRTT has
243 // not yet been set.
244 QuicTime::Delta fec_timeout_;
246 // True if FEC protection is on. The creator may have an open FEC group even
247 // if this variable is false.
248 bool should_fec_protect_;
250 // Flags to indicate the need for just-in-time construction of a frame.
251 bool should_send_ack_;
252 bool should_send_stop_waiting_;
253 // If we put a non-retransmittable frame (ack frame) in this packet, then we
254 // have to hold a reference to it until we flush (and serialize it).
255 // Retransmittable frames are referenced elsewhere so that they
256 // can later be (optionally) retransmitted.
257 QuicAckFrame pending_ack_frame_;
258 QuicStopWaitingFrame pending_stop_waiting_frame_;
259 // True if an ack or stop waiting frame is already queued, and should not be
260 // re-added.
261 bool ack_queued_;
262 bool stop_waiting_queued_;
264 // Stores notifiers that should be attached to the next serialized packet.
265 std::list<QuicAckNotifier*> ack_notifiers_;
267 DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator);
270 } // namespace net
272 #endif // NET_QUIC_QUIC_PACKET_GENERATOR_H_