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 // Options for controlling how packets are created.
35 : max_packet_length(kDefaultMaxPacketSize
),
36 max_packets_per_fec_group(0),
37 send_connection_id_length(PACKET_8BYTE_CONNECTION_ID
),
38 send_sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
) {}
40 size_t max_packet_length
;
41 // 0 indicates fec is disabled.
42 size_t max_packets_per_fec_group
;
43 // Length of connection_id to send over the wire.
44 QuicConnectionIdLength send_connection_id_length
;
45 QuicSequenceNumberLength send_sequence_number_length
;
48 // QuicRandom* required for packet entropy.
49 QuicPacketCreator(QuicConnectionId connection_id
,
51 QuicRandom
* random_generator
,
54 virtual ~QuicPacketCreator();
56 // QuicFecBuilderInterface
57 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader
& header
,
58 base::StringPiece payload
) OVERRIDE
;
60 // Turn on FEC protection for subsequently created packets. FEC should
61 // be enabled first (set_max_packets_per_fec_group should be non-zero) for
62 // FEC protection to start.
63 void StartFecProtectingPackets();
65 // Turn off FEC protection for subsequently created packets. If the creator
66 // has any open fec group, call will fail. It is the caller's responsibility
67 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
68 // that there is no open FEC group.
69 void StopFecProtectingPackets();
71 // Checks if it's time to send an FEC packet. |force_close| forces this to
72 // return true if an fec group is open.
73 bool ShouldSendFec(bool force_close
) const;
75 // Returns current max number of packets covered by an FEC group.
76 size_t max_packets_per_fec_group() const;
78 // Sets creator's max number of packets covered by an FEC group.
79 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group
);
81 // Makes the framer not serialize the protocol version in sent packets.
82 void StopSendingVersion();
84 // Update the sequence number length to use in future packets as soon as it
85 // can be safely changed.
86 void UpdateSequenceNumberLength(
87 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
88 QuicByteCount congestion_window
);
90 // The overhead the framing will add for a packet with one frame.
91 static size_t StreamFramePacketOverhead(
93 QuicConnectionIdLength connection_id_length
,
95 QuicSequenceNumberLength sequence_number_length
,
96 InFecGroup is_in_fec_group
);
98 bool HasRoomForStreamFrame(QuicStreamId id
, QuicStreamOffset offset
) const;
100 // Converts a raw payload to a frame which fits into the currently open
101 // packet if there is one. Returns the number of bytes consumed from data.
102 // If data is empty and fin is true, the expected behavior is to consume the
104 size_t CreateStreamFrame(QuicStreamId id
,
105 const IOVector
& data
,
106 QuicStreamOffset offset
,
110 // As above, but keeps track of an QuicAckNotifier that should be called when
111 // the packet that contains this stream frame is ACKed.
112 // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
114 size_t CreateStreamFrameWithNotifier(QuicStreamId id
,
115 const IOVector
& data
,
116 QuicStreamOffset offset
,
118 QuicAckNotifier
* notifier
,
121 // Serializes all frames into a single packet. All frames must fit into a
122 // single packet. Also, sets the entropy hash of the serialized packet to a
123 // random bool and returns that value as a member of SerializedPacket.
124 // Never returns a RetransmittableFrames in SerializedPacket.
125 SerializedPacket
SerializeAllFrames(const QuicFrames
& frames
);
127 // Re-serializes frames with the original packet's sequence number length.
128 // Used for retransmitting packets to ensure they aren't too long.
129 // Caller must ensure that any open FEC group are closed before calling this
131 SerializedPacket
ReserializeAllFrames(
132 const QuicFrames
& frames
, QuicSequenceNumberLength original_length
);
134 // Returns true if there are frames pending to be serialized.
135 bool HasPendingFrames();
137 // Returns whether FEC protection is currently enabled. Note: Enabled does not
138 // mean that an FEC group is currently active; i.e., IsFecProtected() may
139 // still return NOT_IN_FEC_GROUP, and fec_group_.get() may still be NULL.
140 bool IsFecEnabled() const;
142 // Returns true if subsequent packets will be FEC protected. Note: True does
143 // not mean that an FEC packet is currently under construction; i.e.,
144 // fec_group_.get() may still be NULL, until MaybeStartFec() is called.
145 bool IsFecProtected() const;
147 // Returns the number of bytes which are available to be used by additional
148 // frames in the packet. Since stream frames are slightly smaller when they
149 // are the last frame in a packet, this method will return a different
150 // value than max_packet_size - PacketSize(), in this case.
151 size_t BytesFree() const;
153 // Returns the number of bytes that the packet will expand by if a new frame
154 // is added to the packet. If the last frame was a stream frame, it will
155 // expand slightly when a new frame is added, and this method returns the
156 // amount of expected expansion. If the packet is in an FEC group, no
157 // expansion happens and this method always returns zero.
158 size_t ExpansionOnNewFrame() const;
160 // Returns the number of bytes in the current packet, including the header,
161 // if serialized with the current frames. Adding a frame to the packet
162 // may change the serialized length of existing frames, as per the comment
164 size_t PacketSize() const;
166 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
167 // if it is a stream frame, not other types of frames. Fix this API;
168 // add a AddNonSavedFrame method.
169 // Adds |frame| to the packet creator's list of frames to be serialized.
170 // Returns false if the frame doesn't fit into the current packet.
171 bool AddSavedFrame(const QuicFrame
& frame
);
173 // Serializes all frames which have been added and adds any which should be
174 // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
175 // fit into a single packet. Sets the entropy hash of the serialized
176 // packet to a random bool and returns that value as a member of
177 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
178 // to the corresponding RetransmittableFrames if any frames are to be
180 SerializedPacket
SerializePacket();
182 // Packetize FEC data. All frames must fit into a single packet. Also, sets
183 // the entropy hash of the serialized packet to a random bool and returns
184 // that value as a member of SerializedPacket.
185 SerializedPacket
SerializeFec();
187 // Creates a packet with connection close frame. Caller owns the created
188 // packet. Also, sets the entropy hash of the serialized packet to a random
189 // bool and returns that value as a member of SerializedPacket.
190 SerializedPacket
SerializeConnectionClose(
191 QuicConnectionCloseFrame
* close_frame
);
193 // Creates a version negotiation packet which supports |supported_versions|.
194 // Caller owns the created packet. Also, sets the entropy hash of the
195 // serialized packet to a random bool and returns that value as a member of
197 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
198 const QuicVersionVector
& supported_versions
);
200 // Sets the encryption level that will be applied to new packets.
201 void set_encryption_level(EncryptionLevel level
) {
202 encryption_level_
= level
;
205 // Sequence number of the last created packet, or 0 if no packets have been
207 QuicPacketSequenceNumber
sequence_number() const {
208 return sequence_number_
;
211 void set_sequence_number(QuicPacketSequenceNumber s
) {
212 sequence_number_
= s
;
220 friend class test::QuicPacketCreatorPeer
;
222 static bool ShouldRetransmit(const QuicFrame
& frame
);
224 // Starts a new FEC group with the next serialized packet, if FEC is enabled
225 // and there is not already an FEC group open.
226 InFecGroup
MaybeStartFec();
228 void FillPacketHeader(QuicFecGroupNumber fec_group
,
230 QuicPacketHeader
* header
);
232 // Allows a frame to be added without creating retransmittable frames.
233 // Particularly useful for retransmits using SerializeAllFrames().
234 bool AddFrame(const QuicFrame
& frame
, bool save_retransmittable_frames
);
236 // Adds a padding frame to the current packet only if the current packet
237 // contains a handshake message, and there is sufficient room to fit a
239 void MaybeAddPadding();
242 QuicConnectionId connection_id_
;
243 EncryptionLevel encryption_level_
;
245 scoped_ptr
<QuicRandomBoolSource
> random_bool_source_
;
246 QuicPacketSequenceNumber sequence_number_
;
247 // If true, any created packets will be FEC protected.
248 bool should_fec_protect_
;
249 QuicFecGroupNumber fec_group_number_
;
250 scoped_ptr
<QuicFecGroup
> fec_group_
;
251 // bool to keep track if this packet creator is being used the server.
253 // Controls whether protocol version should be included while serializing the
255 bool send_version_in_packet_
;
256 // The sequence number length for the current packet and the current FEC group
257 // if FEC is enabled.
258 // Mutable so PacketSize() can adjust it when the packet is empty.
259 mutable QuicSequenceNumberLength sequence_number_length_
;
260 // packet_size_ is mutable because it's just a cache of the current size.
261 // packet_size should never be read directly, use PacketSize() instead.
262 mutable size_t packet_size_
;
263 QuicFrames queued_frames_
;
264 scoped_ptr
<RetransmittableFrames
> queued_retransmittable_frames_
;
266 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator
);
271 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_