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"
21 NET_EXPORT_PRIVATE extern bool FLAGS_pad_quic_handshake_packets;
25 class QuicPacketCreatorPeer;
28 class QuicAckNotifier;
31 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
33 // Options for controlling how packets are created.
36 : max_packet_length(kDefaultMaxPacketSize),
37 max_packets_per_fec_group(0),
38 send_guid_length(PACKET_8BYTE_GUID),
39 send_sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER) {}
41 size_t max_packet_length;
42 // 0 indicates fec is disabled.
43 size_t max_packets_per_fec_group;
44 // Length of guid to send over the wire.
45 QuicGuidLength send_guid_length;
46 QuicSequenceNumberLength send_sequence_number_length;
49 // QuicRandom* required for packet entropy.
50 QuicPacketCreator(QuicGuid guid,
52 QuicRandom* random_generator,
55 virtual ~QuicPacketCreator();
57 // QuicFecBuilderInterface
58 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
59 base::StringPiece payload) OVERRIDE;
61 // Checks if it's time to send an FEC packet. |force_close| forces this to
62 // return true if an fec group is open.
63 bool ShouldSendFec(bool force_close) const;
65 // Makes the framer not serialize the protocol version in sent packets.
66 void StopSendingVersion();
68 // Update the sequence number length to use in future packets as soon as it
69 // can be safely changed.
70 void UpdateSequenceNumberLength(
71 QuicPacketSequenceNumber least_packet_awaited_by_peer,
72 QuicByteCount bytes_per_second);
74 // The overhead the framing will add for a packet with one frame.
75 static size_t StreamFramePacketOverhead(
77 QuicGuidLength guid_length,
79 QuicSequenceNumberLength sequence_number_length,
80 InFecGroup is_in_fec_group);
82 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
84 // Converts a raw payload to a frame which fits into the currently open
85 // packet if there is one. Returns the number of bytes consumed from data.
86 // If data is empty and fin is true, the expected behavior is to consume the
88 size_t CreateStreamFrame(QuicStreamId id,
89 base::StringPiece data,
90 QuicStreamOffset offset,
94 // As above, but keeps track of an QuicAckNotifier that should be called when
95 // the packet that contains this stream frame is ACKed.
96 // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
98 size_t CreateStreamFrameWithNotifier(QuicStreamId id,
99 base::StringPiece data,
100 QuicStreamOffset offset,
102 QuicAckNotifier* notifier,
105 // Serializes all frames into a single packet. All frames must fit into a
106 // single packet. Also, sets the entropy hash of the serialized packet to a
107 // random bool and returns that value as a member of SerializedPacket.
108 // Never returns a RetransmittableFrames in SerializedPacket.
109 SerializedPacket SerializeAllFrames(const QuicFrames& frames);
111 // Re-serializes frames with the original packet's sequence number length.
112 // Used for retransmitting packets to ensure they aren't too long.
113 SerializedPacket ReserializeAllFrames(
114 const QuicFrames& frames, QuicSequenceNumberLength original_length);
116 // Returns true if there are frames pending to be serialized.
117 bool HasPendingFrames();
119 // Returns the number of bytes which are available to be used by additional
120 // frames in the packet. Since stream frames are slightly smaller when they
121 // are the last frame in a packet, this method will return a different
122 // value than max_packet_size - PacketSize(), in this case.
123 size_t BytesFree() const;
125 // Returns the number of bytes in the current packet, including the header,
126 // if serialized with the current frames. Adding a frame to the packet
127 // may change the serialized length of existing frames, as per the comment
129 size_t PacketSize() const;
131 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
132 // if it is a stream frame, not other types of frames. Fix this API;
133 // add a AddNonSavedFrame method.
134 // Adds |frame| to the packet creator's list of frames to be serialized.
135 // Returns false if the frame doesn't fit into the current packet.
136 bool AddSavedFrame(const QuicFrame& frame);
138 // Serializes all frames which have been added and adds any which should be
139 // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
140 // fit into a single packet. Sets the entropy hash of the serialized
141 // packet to a random bool and returns that value as a member of
142 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
143 // to the corresponding RetransmittableFrames if any frames are to be
145 SerializedPacket SerializePacket();
147 // Packetize FEC data. All frames must fit into a single packet. Also, sets
148 // the entropy hash of the serialized packet to a random bool and returns
149 // that value as a member of SerializedPacket.
150 SerializedPacket SerializeFec();
152 // Creates a packet with connection close frame. Caller owns the created
153 // packet. Also, sets the entropy hash of the serialized packet to a random
154 // bool and returns that value as a member of SerializedPacket.
155 SerializedPacket SerializeConnectionClose(
156 QuicConnectionCloseFrame* close_frame);
158 // Creates a version negotiation packet which supports |supported_versions|.
159 // Caller owns the created packet. Also, sets the entropy hash of the
160 // serialized packet to a random bool and returns that value as a member of
162 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
163 const QuicVersionVector& supported_versions);
165 // Sequence number of the last created packet, or 0 if no packets have been
167 QuicPacketSequenceNumber sequence_number() const {
168 return sequence_number_;
171 void set_sequence_number(QuicPacketSequenceNumber s) {
172 sequence_number_ = s;
180 friend class test::QuicPacketCreatorPeer;
182 static bool ShouldRetransmit(const QuicFrame& frame);
184 // Starts a new FEC group with the next serialized packet, if FEC is enabled
185 // and there is not already an FEC group open.
186 void MaybeStartFEC();
188 void FillPacketHeader(QuicFecGroupNumber fec_group,
190 bool fec_entropy_flag,
191 QuicPacketHeader* header);
193 // Allows a frame to be added without creating retransmittable frames.
194 // Particularly useful for retransmits using SerializeAllFrames().
195 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
197 // Adds a padding frame to the current packet only if the current packet
198 // contains a handshake message, and there is sufficient room to fit a
200 void MaybeAddPadding();
205 QuicRandom* random_generator_;
206 QuicPacketSequenceNumber sequence_number_;
207 QuicFecGroupNumber fec_group_number_;
208 scoped_ptr<QuicFecGroup> fec_group_;
209 // bool to keep track if this packet creator is being used the server.
211 // Controls whether protocol version should be included while serializing the
213 bool send_version_in_packet_;
214 // The sequence number length for the current packet and the current FEC group
215 // if FEC is enabled.
216 // Mutable so PacketSize() can adjust it when the packet is empty.
217 mutable QuicSequenceNumberLength sequence_number_length_;
218 // packet_size_ is mutable because it's just a cache of the current size.
219 // packet_size should never be read directly, use PacketSize() instead.
220 mutable size_t packet_size_;
221 QuicFrames queued_frames_;
222 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
224 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
229 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_