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.
5 // Accumulates frames for the next packet until more frames no longer fit or
6 // it's time to create a packet from them. Also provides packet creation of
7 // FEC packets based on previously created packets.
9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/quic/quic_fec_group.h"
18 #include "net/quic/quic_framer.h"
19 #include "net/quic/quic_protocol.h"
23 class QuicPacketCreatorPeer
;
26 class QuicAckNotifier
;
28 class QuicRandomBoolSource
;
30 class NET_EXPORT_PRIVATE QuicPacketCreator
: public QuicFecBuilderInterface
{
32 // QuicRandom* required for packet entropy.
33 QuicPacketCreator(QuicConnectionId connection_id
,
35 QuicRandom
* random_generator
);
37 virtual ~QuicPacketCreator();
39 // QuicFecBuilderInterface
40 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader
& header
,
41 base::StringPiece payload
) OVERRIDE
;
43 // Turn on FEC protection for subsequently created packets. FEC should be
44 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
45 // protection to start.
46 void StartFecProtectingPackets();
48 // Turn off FEC protection for subsequently created packets. If the creator
49 // has any open FEC group, call will fail. It is the caller's responsibility
50 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
51 // that there is no open FEC group.
52 void StopFecProtectingPackets();
54 // Checks if it's time to send an FEC packet. |force_close| forces this to
55 // return true if an FEC group is open.
56 bool ShouldSendFec(bool force_close
) const;
58 // Returns true if an FEC packet is under construction.
59 bool IsFecGroupOpen() const;
61 // Makes the framer not serialize the protocol version in sent packets.
62 void StopSendingVersion();
64 // Update the sequence number length to use in future packets as soon as it
65 // can be safely changed.
66 void UpdateSequenceNumberLength(
67 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
68 QuicByteCount congestion_window
);
70 // The overhead the framing will add for a packet with one frame.
71 static size_t StreamFramePacketOverhead(
73 QuicConnectionIdLength connection_id_length
,
75 QuicSequenceNumberLength sequence_number_length
,
76 QuicStreamOffset offset
,
77 InFecGroup is_in_fec_group
);
79 bool HasRoomForStreamFrame(QuicStreamId id
, QuicStreamOffset offset
) const;
81 // Converts a raw payload to a frame which fits into the currently open
82 // packet if there is one. Returns the number of bytes consumed from data.
83 // If data is empty and fin is true, the expected behavior is to consume the
85 size_t CreateStreamFrame(QuicStreamId id
,
87 QuicStreamOffset offset
,
91 // As above, but keeps track of an QuicAckNotifier that should be called when
92 // the packet that contains this stream frame is ACKed.
93 // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
95 size_t CreateStreamFrameWithNotifier(QuicStreamId id
,
97 QuicStreamOffset offset
,
99 QuicAckNotifier
* notifier
,
102 // Serializes all frames into a single packet. All frames must fit into a
103 // single packet. Also, sets the entropy hash of the serialized packet to a
104 // random bool and returns that value as a member of SerializedPacket.
105 // Never returns a RetransmittableFrames in SerializedPacket.
106 SerializedPacket
SerializeAllFrames(const QuicFrames
& frames
);
108 // Re-serializes frames with the original packet's sequence number length.
109 // Used for retransmitting packets to ensure they aren't too long.
110 // Caller must ensure that any open FEC group is closed before calling this
112 SerializedPacket
ReserializeAllFrames(
113 const QuicFrames
& frames
,
114 QuicSequenceNumberLength original_length
);
116 // Returns true if there are frames pending to be serialized.
117 bool HasPendingFrames() const;
119 // Returns true if there are retransmittable frames pending to be serialized.
120 bool HasPendingRetransmittableFrames() const;
122 // Returns whether FEC protection is currently enabled. Note: Enabled does not
123 // mean that an FEC group is currently active; i.e., IsFecProtected() may
124 // still return false.
125 bool IsFecEnabled() const;
127 // Returns true if subsequent packets will be FEC protected. Note: True does
128 // not mean that an FEC packet is currently under construction; i.e.,
129 // fec_group_.get() may still be NULL, until MaybeStartFec() is called.
130 bool IsFecProtected() const;
132 // Returns the number of bytes which are available to be used by additional
133 // frames in the packet. Since stream frames are slightly smaller when they
134 // are the last frame in a packet, this method will return a different
135 // value than max_packet_size - PacketSize(), in this case.
136 size_t BytesFree() const;
138 // Returns the number of bytes that the packet will expand by if a new frame
139 // is added to the packet. If the last frame was a stream frame, it will
140 // expand slightly when a new frame is added, and this method returns the
141 // amount of expected expansion. If the packet is in an FEC group, no
142 // expansion happens and this method always returns zero.
143 size_t ExpansionOnNewFrame() const;
145 // Returns the number of bytes in the current packet, including the header,
146 // if serialized with the current frames. Adding a frame to the packet
147 // may change the serialized length of existing frames, as per the comment
149 size_t PacketSize() const;
151 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
152 // if it is a stream frame, not other types of frames. Fix this API;
153 // add a AddNonSavedFrame method.
154 // Adds |frame| to the packet creator's list of frames to be serialized.
155 // Returns false if the frame doesn't fit into the current packet.
156 bool AddSavedFrame(const QuicFrame
& frame
);
158 // Serializes all frames which have been added and adds any which should be
159 // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
160 // fit into a single packet. Sets the entropy hash of the serialized
161 // packet to a random bool and returns that value as a member of
162 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
163 // to the corresponding RetransmittableFrames if any frames are to be
165 SerializedPacket
SerializePacket();
167 // Packetize FEC data. All frames must fit into a single packet. Also, sets
168 // the entropy hash of the serialized packet to a random bool and returns
169 // that value as a member of SerializedPacket.
170 SerializedPacket
SerializeFec();
172 // Creates a packet with connection close frame. Caller owns the created
173 // packet. Also, sets the entropy hash of the serialized packet to a random
174 // bool and returns that value as a member of SerializedPacket.
175 SerializedPacket
SerializeConnectionClose(
176 QuicConnectionCloseFrame
* close_frame
);
178 // Creates a version negotiation packet which supports |supported_versions|.
179 // Caller owns the created packet. Also, sets the entropy hash of the
180 // serialized packet to a random bool and returns that value as a member of
182 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
183 const QuicVersionVector
& supported_versions
);
185 // Sets the encryption level that will be applied to new packets.
186 void set_encryption_level(EncryptionLevel level
) {
187 encryption_level_
= level
;
190 // Sequence number of the last created packet, or 0 if no packets have been
192 QuicPacketSequenceNumber
sequence_number() const {
193 return sequence_number_
;
196 void set_sequence_number(QuicPacketSequenceNumber s
) {
197 sequence_number_
= s
;
200 QuicConnectionIdLength
connection_id_length() const {
201 return connection_id_length_
;
204 QuicSequenceNumberLength
next_sequence_number_length() const {
205 return next_sequence_number_length_
;
208 void set_next_sequence_number_length(QuicSequenceNumberLength length
) {
209 next_sequence_number_length_
= length
;
212 size_t max_packet_length() const {
213 return max_packet_length_
;
216 void set_max_packet_length(size_t length
) {
217 // |max_packet_length_| should not be changed mid-packet or mid-FEC group.
218 DCHECK(fec_group_
.get() == NULL
&& queued_frames_
.empty());
219 max_packet_length_
= length
;
222 // Returns current max number of packets covered by an FEC group.
223 size_t max_packets_per_fec_group() const {
224 return max_packets_per_fec_group_
;
227 // Sets creator's max number of packets covered by an FEC group.
228 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group
) {
229 // To turn off FEC protection, use StopFecProtectingPackets().
230 DCHECK_NE(0u, max_packets_per_fec_group
);
231 max_packets_per_fec_group_
= max_packets_per_fec_group
;
235 friend class test::QuicPacketCreatorPeer
;
237 static bool ShouldRetransmit(const QuicFrame
& frame
);
239 // Updates sequence number and max packet lengths on a packet or FEC group
241 void MaybeUpdateLengths();
243 // Updates lengths and also starts an FEC group if FEC protection is on and
244 // there is not already an FEC group open.
245 InFecGroup
MaybeUpdateLengthsAndStartFec();
247 void FillPacketHeader(QuicFecGroupNumber fec_group
,
249 QuicPacketHeader
* header
);
251 // Allows a frame to be added without creating retransmittable frames.
252 // Particularly useful for retransmits using SerializeAllFrames().
253 bool AddFrame(const QuicFrame
& frame
, bool save_retransmittable_frames
);
255 // Adds a padding frame to the current packet only if the current packet
256 // contains a handshake message, and there is sufficient room to fit a
258 void MaybeAddPadding();
260 QuicConnectionId connection_id_
;
261 EncryptionLevel encryption_level_
;
263 scoped_ptr
<QuicRandomBoolSource
> random_bool_source_
;
264 QuicPacketSequenceNumber sequence_number_
;
265 // If true, any created packets will be FEC protected.
266 bool should_fec_protect_
;
267 QuicFecGroupNumber fec_group_number_
;
268 scoped_ptr
<QuicFecGroup
> fec_group_
;
269 // Controls whether protocol version should be included while serializing the
271 bool send_version_in_packet_
;
272 // Maximum length including headers and encryption (UDP payload length.)
273 size_t max_packet_length_
;
274 // 0 indicates FEC is disabled.
275 size_t max_packets_per_fec_group_
;
276 // Length of connection_id to send over the wire.
277 QuicConnectionIdLength connection_id_length_
;
278 // Staging variable to hold next packet sequence number length. When sequence
279 // number length is to be changed, this variable holds the new length until
280 // a packet or FEC group boundary, when the creator's sequence_number_length_
281 // can be changed to this new value.
282 QuicSequenceNumberLength next_sequence_number_length_
;
283 // Sequence number length for the current packet and for the current FEC group
284 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
286 mutable QuicSequenceNumberLength sequence_number_length_
;
287 // packet_size_ is mutable because it's just a cache of the current size.
288 // packet_size should never be read directly, use PacketSize() instead.
289 mutable size_t packet_size_
;
290 QuicFrames queued_frames_
;
291 scoped_ptr
<RetransmittableFrames
> queued_retransmittable_frames_
;
293 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator
);
298 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_