rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / net / quic / quic_packet_creator.h
blob152085e1245973c310cb7a75564b15b42c26045d
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 <utility>
13 #include <vector>
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 namespace net {
22 namespace test {
23 class QuicPacketCreatorPeer;
26 class QuicAckNotifier;
27 class QuicRandom;
28 class QuicRandomBoolSource;
30 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
31 public:
32 // Options for controlling how packets are created.
33 struct Options {
34 Options()
35 : max_packet_length(kDefaultMaxPacketSize),
36 max_packets_per_fec_group(0),
37 send_guid_length(PACKET_8BYTE_GUID),
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 guid to send over the wire.
44 QuicGuidLength send_guid_length;
45 QuicSequenceNumberLength send_sequence_number_length;
48 // QuicRandom* required for packet entropy.
49 QuicPacketCreator(QuicGuid guid,
50 QuicFramer* framer,
51 QuicRandom* random_generator,
52 bool is_server);
54 virtual ~QuicPacketCreator();
56 // QuicFecBuilderInterface
57 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
58 base::StringPiece payload) OVERRIDE;
60 // Checks if it's time to send an FEC packet. |force_close| forces this to
61 // return true if an fec group is open.
62 bool ShouldSendFec(bool force_close) const;
64 // Makes the framer not serialize the protocol version in sent packets.
65 void StopSendingVersion();
67 // Update the sequence number length to use in future packets as soon as it
68 // can be safely changed.
69 void UpdateSequenceNumberLength(
70 QuicPacketSequenceNumber least_packet_awaited_by_peer,
71 QuicByteCount bytes_per_second);
73 // The overhead the framing will add for a packet with one frame.
74 static size_t StreamFramePacketOverhead(
75 QuicVersion version,
76 QuicGuidLength guid_length,
77 bool include_version,
78 QuicSequenceNumberLength sequence_number_length,
79 InFecGroup is_in_fec_group);
81 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
83 // Converts a raw payload to a frame which fits into the currently open
84 // packet if there is one. Returns the number of bytes consumed from data.
85 // If data is empty and fin is true, the expected behavior is to consume the
86 // fin but return 0.
87 size_t CreateStreamFrame(QuicStreamId id,
88 const IOVector& data,
89 QuicStreamOffset offset,
90 bool fin,
91 QuicFrame* frame);
93 // As above, but keeps track of an QuicAckNotifier that should be called when
94 // the packet that contains this stream frame is ACKed.
95 // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
96 // generated packet.
97 size_t CreateStreamFrameWithNotifier(QuicStreamId id,
98 const IOVector& data,
99 QuicStreamOffset offset,
100 bool fin,
101 QuicAckNotifier* notifier,
102 QuicFrame* frame);
104 // Serializes all frames into a single packet. All frames must fit into a
105 // single packet. Also, sets the entropy hash of the serialized packet to a
106 // random bool and returns that value as a member of SerializedPacket.
107 // Never returns a RetransmittableFrames in SerializedPacket.
108 SerializedPacket SerializeAllFrames(const QuicFrames& frames);
110 // Re-serializes frames with the original packet's sequence number length.
111 // Used for retransmitting packets to ensure they aren't too long.
112 SerializedPacket ReserializeAllFrames(
113 const QuicFrames& frames, QuicSequenceNumberLength original_length);
115 // Returns true if there are frames pending to be serialized.
116 bool HasPendingFrames();
118 // Returns the number of bytes which are available to be used by additional
119 // frames in the packet. Since stream frames are slightly smaller when they
120 // are the last frame in a packet, this method will return a different
121 // value than max_packet_size - PacketSize(), in this case.
122 size_t BytesFree() const;
124 // Returns the number of bytes in the current packet, including the header,
125 // if serialized with the current frames. Adding a frame to the packet
126 // may change the serialized length of existing frames, as per the comment
127 // in BytesFree.
128 size_t PacketSize() const;
130 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
131 // if it is a stream frame, not other types of frames. Fix this API;
132 // add a AddNonSavedFrame method.
133 // Adds |frame| to the packet creator's list of frames to be serialized.
134 // Returns false if the frame doesn't fit into the current packet.
135 bool AddSavedFrame(const QuicFrame& frame);
137 // Serializes all frames which have been added and adds any which should be
138 // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
139 // fit into a single packet. Sets the entropy hash of the serialized
140 // packet to a random bool and returns that value as a member of
141 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
142 // to the corresponding RetransmittableFrames if any frames are to be
143 // retransmitted.
144 SerializedPacket SerializePacket();
146 // Packetize FEC data. All frames must fit into a single packet. Also, sets
147 // the entropy hash of the serialized packet to a random bool and returns
148 // that value as a member of SerializedPacket.
149 SerializedPacket SerializeFec();
151 // Creates a packet with connection close frame. Caller owns the created
152 // packet. Also, sets the entropy hash of the serialized packet to a random
153 // bool and returns that value as a member of SerializedPacket.
154 SerializedPacket SerializeConnectionClose(
155 QuicConnectionCloseFrame* close_frame);
157 // Creates a version negotiation packet which supports |supported_versions|.
158 // Caller owns the created packet. Also, sets the entropy hash of the
159 // serialized packet to a random bool and returns that value as a member of
160 // SerializedPacket.
161 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
162 const QuicVersionVector& supported_versions);
164 // Sequence number of the last created packet, or 0 if no packets have been
165 // created.
166 QuicPacketSequenceNumber sequence_number() const {
167 return sequence_number_;
170 void set_sequence_number(QuicPacketSequenceNumber s) {
171 sequence_number_ = s;
174 Options* options() {
175 return &options_;
178 private:
179 friend class test::QuicPacketCreatorPeer;
181 static bool ShouldRetransmit(const QuicFrame& frame);
183 // Starts a new FEC group with the next serialized packet, if FEC is enabled
184 // and there is not already an FEC group open.
185 void MaybeStartFEC();
187 void FillPacketHeader(QuicFecGroupNumber fec_group,
188 bool fec_flag,
189 QuicPacketHeader* header);
191 // Allows a frame to be added without creating retransmittable frames.
192 // Particularly useful for retransmits using SerializeAllFrames().
193 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
195 // Adds a padding frame to the current packet only if the current packet
196 // contains a handshake message, and there is sufficient room to fit a
197 // padding frame.
198 void MaybeAddPadding();
200 Options options_;
201 QuicGuid guid_;
202 QuicFramer* framer_;
203 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
204 QuicPacketSequenceNumber sequence_number_;
205 QuicFecGroupNumber fec_group_number_;
206 scoped_ptr<QuicFecGroup> fec_group_;
207 // bool to keep track if this packet creator is being used the server.
208 bool is_server_;
209 // Controls whether protocol version should be included while serializing the
210 // packet.
211 bool send_version_in_packet_;
212 // The sequence number length for the current packet and the current FEC group
213 // if FEC is enabled.
214 // Mutable so PacketSize() can adjust it when the packet is empty.
215 mutable QuicSequenceNumberLength sequence_number_length_;
216 // packet_size_ is mutable because it's just a cache of the current size.
217 // packet_size should never be read directly, use PacketSize() instead.
218 mutable size_t packet_size_;
219 QuicFrames queued_frames_;
220 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
222 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
225 } // namespace net
227 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_