Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / net / quic / quic_packet_creator.h
blob4b5971c7709d72bc3d90d93349a74b6b97c131a6
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 // 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_
12 #include <string>
13 #include <utility>
14 #include <vector>
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "net/quic/quic_fec_group.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_protocol.h"
22 namespace net {
23 namespace test {
24 class QuicPacketCreatorPeer;
27 class QuicAckNotifier;
28 class QuicRandom;
29 class QuicRandomBoolSource;
31 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
32 public:
33 // QuicRandom* required for packet entropy.
34 QuicPacketCreator(QuicConnectionId connection_id,
35 QuicFramer* framer,
36 QuicRandom* random_generator);
38 ~QuicPacketCreator() override;
40 // QuicFecBuilderInterface
41 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
42 base::StringPiece payload) override;
44 // Turn on FEC protection for subsequently created packets. FEC should be
45 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
46 // protection to start.
47 void StartFecProtectingPackets();
49 // Turn off FEC protection for subsequently created packets. If the creator
50 // has any open FEC group, call will fail. It is the caller's responsibility
51 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
52 // that there is no open FEC group.
53 void StopFecProtectingPackets();
55 // Checks if it's time to send an FEC packet. |force_close| forces this to
56 // return true if an FEC group is open.
57 bool ShouldSendFec(bool force_close) const;
59 // Returns true if an FEC packet is under construction.
60 bool IsFecGroupOpen() const;
62 // Makes the framer not serialize the protocol version in sent packets.
63 void StopSendingVersion();
65 // Update the sequence number length to use in future packets as soon as it
66 // can be safely changed.
67 void UpdateSequenceNumberLength(
68 QuicPacketSequenceNumber least_packet_awaited_by_peer,
69 QuicPacketCount max_packets_in_flight);
71 // The overhead the framing will add for a packet with one frame.
72 static size_t StreamFramePacketOverhead(
73 QuicConnectionIdLength connection_id_length,
74 bool include_version,
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
84 // fin but return 0.
85 size_t CreateStreamFrame(QuicStreamId id,
86 const IOVector& data,
87 QuicStreamOffset offset,
88 bool fin,
89 QuicFrame* frame);
91 // Serializes all frames into a single packet. All frames must fit into a
92 // single packet. Also, sets the entropy hash of the serialized packet to a
93 // random bool and returns that value as a member of SerializedPacket.
94 // Never returns a RetransmittableFrames in SerializedPacket.
95 SerializedPacket SerializeAllFrames(const QuicFrames& frames);
97 // Re-serializes frames with the original packet's sequence number length.
98 // Used for retransmitting packets to ensure they aren't too long.
99 // Caller must ensure that any open FEC group is closed before calling this
100 // method.
101 SerializedPacket ReserializeAllFrames(
102 const RetransmittableFrames& frames,
103 QuicSequenceNumberLength original_length);
105 // Returns true if there are frames pending to be serialized.
106 bool HasPendingFrames() const;
108 // Returns true if there are retransmittable frames pending to be serialized.
109 bool HasPendingRetransmittableFrames() const;
111 // TODO(jri): Remove this method.
112 // Returns whether FEC protection is currently enabled. Note: Enabled does not
113 // mean that an FEC group is currently active; i.e., IsFecProtected() may
114 // still return false.
115 bool IsFecEnabled() const;
117 // Returns true if subsequent packets will be FEC protected. Note: True does
118 // not mean that an FEC packet is currently under construction; i.e.,
119 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
120 bool IsFecProtected() const;
122 // Returns the number of bytes which are available to be used by additional
123 // frames in the packet. Since stream frames are slightly smaller when they
124 // are the last frame in a packet, this method will return a different
125 // value than max_packet_size - PacketSize(), in this case.
126 size_t BytesFree() const;
128 // Returns the number of bytes that the packet will expand by if a new frame
129 // is added to the packet. If the last frame was a stream frame, it will
130 // expand slightly when a new frame is added, and this method returns the
131 // amount of expected expansion. If the packet is in an FEC group, no
132 // expansion happens and this method always returns zero.
133 size_t ExpansionOnNewFrame() const;
135 // Returns the number of bytes in the current packet, including the header,
136 // if serialized with the current frames. Adding a frame to the packet
137 // may change the serialized length of existing frames, as per the comment
138 // in BytesFree.
139 size_t PacketSize() const;
141 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
142 // if it is a stream frame, not other types of frames. Fix this API;
143 // add a AddNonSavedFrame method.
144 // Adds |frame| to the packet creator's list of frames to be serialized.
145 // Returns false if the frame doesn't fit into the current packet.
146 bool AddSavedFrame(const QuicFrame& frame);
148 // Serializes all frames which have been added and adds any which should be
149 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
150 // must fit into a single packet. Sets the entropy hash of the serialized
151 // packet to a random bool and returns that value as a member of
152 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
153 // the corresponding RetransmittableFrames if any frames are to be
154 // retransmitted.
155 SerializedPacket SerializePacket();
157 // Packetize FEC data. All frames must fit into a single packet. Also, sets
158 // the entropy hash of the serialized packet to a random bool and returns
159 // that value as a member of SerializedPacket.
160 SerializedPacket SerializeFec();
162 // Creates a version negotiation packet which supports |supported_versions|.
163 // Caller owns the created packet. Also, sets the entropy hash of the
164 // serialized packet to a random bool and returns that value as a member of
165 // SerializedPacket.
166 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
167 const QuicVersionVector& supported_versions);
169 // Returns a dummy packet that is valid but contains no useful information.
170 static SerializedPacket NoPacket();
172 // Sets the encryption level that will be applied to new packets.
173 void set_encryption_level(EncryptionLevel level) {
174 encryption_level_ = level;
177 // Sequence number of the last created packet, or 0 if no packets have been
178 // created.
179 QuicPacketSequenceNumber sequence_number() const {
180 return sequence_number_;
183 QuicConnectionIdLength connection_id_length() const {
184 return connection_id_length_;
187 void set_connection_id_length(QuicConnectionIdLength length) {
188 connection_id_length_ = length;
191 QuicByteCount max_packet_length() const {
192 return max_packet_length_;
195 // Sets the encrypter to use for the encryption level and updates the max
196 // plaintext size.
197 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
199 // Sets the maximum packet length.
200 void SetMaxPacketLength(QuicByteCount length);
202 // Returns current max number of packets covered by an FEC group.
203 size_t max_packets_per_fec_group() const {
204 return max_packets_per_fec_group_;
207 // Sets creator's max number of packets covered by an FEC group.
208 // Note: While there are no constraints on |max_packets_per_fec_group|,
209 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
210 // To turn off FEC protection, use StopFecProtectingPackets().
211 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group);
213 // Returns the currently open FEC group's number. If there isn't an open FEC
214 // group, returns the last closed FEC group number. Returns 0 when FEC is
215 // disabled or no FEC group has been created yet.
216 QuicFecGroupNumber fec_group_number() { return fec_group_number_; }
218 private:
219 friend class test::QuicPacketCreatorPeer;
221 static bool ShouldRetransmit(const QuicFrame& frame);
223 // Updates lengths and also starts an FEC group if FEC protection is on and
224 // there is not already an FEC group open.
225 InFecGroup MaybeUpdateLengthsAndStartFec();
227 void FillPacketHeader(QuicFecGroupNumber fec_group,
228 bool fec_flag,
229 QuicPacketHeader* header);
231 // Allows a frame to be added without creating retransmittable frames.
232 // Particularly useful for retransmits using SerializeAllFrames().
233 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
235 // Adds a padding frame to the current packet only if the current packet
236 // contains a handshake message, and there is sufficient room to fit a
237 // padding frame.
238 void MaybeAddPadding();
240 QuicConnectionId connection_id_;
241 EncryptionLevel encryption_level_;
242 QuicFramer* framer_;
243 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
244 QuicPacketSequenceNumber sequence_number_;
245 // If true, any created packets will be FEC protected.
246 bool should_fec_protect_;
247 QuicFecGroupNumber fec_group_number_;
248 scoped_ptr<QuicFecGroup> fec_group_;
249 // Controls whether protocol version should be included while serializing the
250 // packet.
251 bool send_version_in_packet_;
252 // Maximum length including headers and encryption (UDP payload length.)
253 QuicByteCount max_packet_length_;
254 // 0 indicates FEC is disabled.
255 size_t max_packets_per_fec_group_;
256 // Length of connection_id to send over the wire.
257 QuicConnectionIdLength connection_id_length_;
258 // Staging variable to hold next packet sequence number length. When sequence
259 // number length is to be changed, this variable holds the new length until
260 // a packet or FEC group boundary, when the creator's sequence_number_length_
261 // can be changed to this new value.
262 QuicSequenceNumberLength next_sequence_number_length_;
263 // Sequence number length for the current packet and for the current FEC group
264 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
265 // is empty.
266 mutable QuicSequenceNumberLength sequence_number_length_;
267 // packet_size_ is mutable because it's just a cache of the current size.
268 // packet_size should never be read directly, use PacketSize() instead.
269 mutable size_t packet_size_;
270 mutable size_t max_plaintext_size_;
271 QuicFrames queued_frames_;
272 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
274 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
277 } // namespace net
279 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_