Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: net/quic/quic_packet_creator.h

Issue 1459343009: Make QuicPacketCreator be able to serialize packet itself when it does not have room for next strea… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@107733506
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Accumulates frames for the next packet until more frames no longer fit or 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 6 // it's time to create a packet from them. Also provides packet creation of
7 // FEC packets based on previously created packets. 7 // FEC packets based on previously created packets.
8 8
9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ 9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_ 10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_
(...skipping 11 matching lines...) Expand all
22 namespace net { 22 namespace net {
23 namespace test { 23 namespace test {
24 class QuicPacketCreatorPeer; 24 class QuicPacketCreatorPeer;
25 } 25 }
26 26
27 class QuicRandom; 27 class QuicRandom;
28 class QuicRandomBoolSource; 28 class QuicRandomBoolSource;
29 29
30 class NET_EXPORT_PRIVATE QuicPacketCreator { 30 class NET_EXPORT_PRIVATE QuicPacketCreator {
31 public: 31 public:
32 // A delegate interface for further processing serialized packet.
33 class DelegateInterface {
34 public:
35 virtual ~DelegateInterface() {}
36 // Called when a packet is serialized. Delegate does not take the ownership
37 // of |serialized_packet|.
38 virtual void OnSerializedPacket(SerializedPacket* serialized_packet) = 0;
39 };
40
32 // QuicRandom* required for packet entropy. 41 // QuicRandom* required for packet entropy.
33 QuicPacketCreator(QuicConnectionId connection_id, 42 QuicPacketCreator(QuicConnectionId connection_id,
34 QuicFramer* framer, 43 QuicFramer* framer,
35 QuicRandom* random_generator); 44 QuicRandom* random_generator,
45 DelegateInterface* delegate);
36 46
37 ~QuicPacketCreator(); 47 ~QuicPacketCreator();
38 48
39 // Turn on FEC protection for subsequently created packets. FEC should be 49 // Turn on FEC protection for subsequently created packets. FEC should be
40 // enabled first (max_packets_per_fec_group should be non-zero) for FEC 50 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
41 // protection to start. 51 // protection to start.
42 void StartFecProtectingPackets(); 52 void StartFecProtectingPackets();
43 53
44 // Turn off FEC protection for subsequently created packets. If the creator 54 // Turn off FEC protection for subsequently created packets. If the creator
45 // has any open FEC group, call will fail. It is the caller's responsibility 55 // has any open FEC group, call will fail. It is the caller's responsibility
(...skipping 21 matching lines...) Expand all
67 QuicPacketCount max_packets_in_flight); 77 QuicPacketCount max_packets_in_flight);
68 78
69 // The overhead the framing will add for a packet with one frame. 79 // The overhead the framing will add for a packet with one frame.
70 static size_t StreamFramePacketOverhead( 80 static size_t StreamFramePacketOverhead(
71 QuicConnectionIdLength connection_id_length, 81 QuicConnectionIdLength connection_id_length,
72 bool include_version, 82 bool include_version,
73 QuicPacketNumberLength packet_number_length, 83 QuicPacketNumberLength packet_number_length,
74 QuicStreamOffset offset, 84 QuicStreamOffset offset,
75 InFecGroup is_in_fec_group); 85 InFecGroup is_in_fec_group);
76 86
77 // Converts a raw payload to a stream frame (which fits into the currently 87 // Returns false and flushes all pending frames if current open packet is
78 // open packet) and adds the frame to the packet. The payload begins at 88 // full.
79 // |iov_offset| into the |iov|. 89 // If current packet is not full, converts a raw payload into a stream frame
80 // Creates |frame| and returns true if succeeds, false otherwise. 90 // that fits into the open packet and adds it to the packet.
91 // The payload begins at |iov_offset| into the |iov|.
81 bool ConsumeData(QuicStreamId id, 92 bool ConsumeData(QuicStreamId id,
82 QuicIOVector iov, 93 QuicIOVector iov,
83 size_t iov_offset, 94 size_t iov_offset,
84 QuicStreamOffset offset, 95 QuicStreamOffset offset,
85 bool fin, 96 bool fin,
86 bool needs_padding, 97 bool needs_padding,
87 QuicFrame* frame); 98 QuicFrame* frame);
88 99
89 // Returns true if currently open packet can accommodate more stream frames of 100 // Returns true if current open packet can accommodate more stream frames of
90 // stream |id| at |offset|, false otherwise. 101 // stream |id| at |offset|, false otherwise.
91 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const; 102 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
92 103
93 // Serializes all frames into a single packet. All frames must fit into a 104 // Serializes all frames into a single packet. All frames must fit into a
94 // single packet. Also, sets the entropy hash of the serialized packet to a 105 // single packet. Also, sets the entropy hash of the serialized packet to a
95 // random bool and returns that value as a member of SerializedPacket. 106 // random bool and returns that value as a member of SerializedPacket.
96 // Never returns a RetransmittableFrames in SerializedPacket. 107 // Never returns a RetransmittableFrames in SerializedPacket.
97 SerializedPacket SerializeAllFrames(const QuicFrames& frames, 108 SerializedPacket SerializeAllFrames(const QuicFrames& frames,
98 char* buffer, 109 char* buffer,
99 size_t buffer_len); 110 size_t buffer_len);
100 111
101 // Re-serializes frames with the original packet's packet number length. 112 // Re-serializes frames with the original packet's packet number length.
102 // Used for retransmitting packets to ensure they aren't too long. 113 // Used for retransmitting packets to ensure they aren't too long.
103 // Caller must ensure that any open FEC group is closed before calling this 114 // Caller must ensure that any open FEC group is closed before calling this
104 // method. 115 // method.
105 SerializedPacket ReserializeAllFrames(const RetransmittableFrames& frames, 116 SerializedPacket ReserializeAllFrames(const RetransmittableFrames& frames,
106 QuicPacketNumberLength original_length, 117 QuicPacketNumberLength original_length,
107 char* buffer, 118 char* buffer,
108 size_t buffer_len); 119 size_t buffer_len);
109 120
121 // Serializes all added frames into a single packet and invokes the delegate_
122 // to further process the SerializedPacket.
123 void Flush();
124
110 // Returns true if there are frames pending to be serialized. 125 // Returns true if there are frames pending to be serialized.
111 bool HasPendingFrames() const; 126 bool HasPendingFrames() const;
112 127
113 // Returns true if there are retransmittable frames pending to be serialized. 128 // Returns true if there are retransmittable frames pending to be serialized.
114 bool HasPendingRetransmittableFrames() const; 129 bool HasPendingRetransmittableFrames() const;
115 130
116 // TODO(jri): Remove this method. 131 // TODO(jri): Remove this method.
117 // Returns whether FEC protection is currently enabled. Note: Enabled does not 132 // Returns whether FEC protection is currently enabled. Note: Enabled does not
118 // mean that an FEC group is currently active; i.e., IsFecProtected() may 133 // mean that an FEC group is currently active; i.e., IsFecProtected() may
119 // still return false. 134 // still return false.
(...skipping 19 matching lines...) Expand all
139 154
140 // Returns the number of bytes in the current packet, including the header, 155 // Returns the number of bytes in the current packet, including the header,
141 // if serialized with the current frames. Adding a frame to the packet 156 // if serialized with the current frames. Adding a frame to the packet
142 // may change the serialized length of existing frames, as per the comment 157 // may change the serialized length of existing frames, as per the comment
143 // in BytesFree. 158 // in BytesFree.
144 size_t PacketSize() const; 159 size_t PacketSize() const;
145 160
146 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame 161 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
147 // if it is a stream frame, not other types of frames. Fix this API; 162 // if it is a stream frame, not other types of frames. Fix this API;
148 // add a AddNonSavedFrame method. 163 // add a AddNonSavedFrame method.
149 // Adds |frame| to the packet creator's list of frames to be serialized. 164 // Tries to add |frame| to the packet creator's std::list of frames to be
150 // Returns false if the frame doesn't fit into the current packet. 165 // serialized. If the frame does not fit into the current packet, flushes the
166 // packet and returns false.
151 bool AddSavedFrame(const QuicFrame& frame); 167 bool AddSavedFrame(const QuicFrame& frame);
152 168
153 // Identical to AddSavedFrame, but takes ownership of the buffer. 169 // Identical to AddSavedFrame, but takes ownership of the buffer.
154 bool AddSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); 170 bool AddSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer);
155 171
156 // Identical to AddSavedFrame, but takes ownership of the buffer, and allows 172 // Identical to AddSavedFrame, but takes ownership of the buffer, and allows
157 // to cause the packet to be padded. 173 // to cause the packet to be padded.
158 bool AddPaddedSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); 174 bool AddPaddedSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer);
159 175
160 // Serializes all frames which have been added and adds any which should be
161 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
162 // must fit into a single packet. Sets the entropy hash of the serialized
163 // packet to a random bool and returns that value as a member of
164 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
165 // the corresponding RetransmittableFrames if any frames are to be
166 // retransmitted.
167 // Fails if |buffer_len| isn't long enough for the encrypted packet.
168 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len);
169
170 // Packetize FEC data. All frames must fit into a single packet. Also, sets 176 // Packetize FEC data. All frames must fit into a single packet. Also, sets
171 // the entropy hash of the serialized packet to a random bool and returns 177 // the entropy hash of the serialized packet to a random bool and returns
172 // that value as a member of SerializedPacket. 178 // that value as a member of SerializedPacket.
173 // Fails if |buffer_len| isn't long enough for the encrypted packet. 179 // Fails if |buffer_len| isn't long enough for the encrypted packet.
174 SerializedPacket SerializeFec(char* buffer, size_t buffer_len); 180 SerializedPacket SerializeFec(char* buffer, size_t buffer_len);
175 181
176 // Creates a version negotiation packet which supports |supported_versions|. 182 // Creates a version negotiation packet which supports |supported_versions|.
177 // Caller owns the created packet. Also, sets the entropy hash of the 183 // Caller owns the created packet. Also, sets the entropy hash of the
178 // serialized packet to a random bool and returns that value as a member of 184 // serialized packet to a random bool and returns that value as a member of
179 // SerializedPacket. 185 // SerializedPacket.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 size_t max_packets_per_fec_group() const { 225 size_t max_packets_per_fec_group() const {
220 return max_packets_per_fec_group_; 226 return max_packets_per_fec_group_;
221 } 227 }
222 228
223 // Sets creator's max number of packets covered by an FEC group. 229 // Sets creator's max number of packets covered by an FEC group.
224 // Note: While there are no constraints on |max_packets_per_fec_group|, 230 // Note: While there are no constraints on |max_packets_per_fec_group|,
225 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup. 231 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
226 // To turn off FEC protection, use StopFecProtectingPackets(). 232 // To turn off FEC protection, use StopFecProtectingPackets().
227 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group); 233 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group);
228 234
229 // Returns the currently open FEC group's number. Returns 0 when FEC is 235 // Returns the current open FEC group's number. Returns 0 when FEC is
230 // disabled or no FEC group is open. 236 // disabled or no FEC group is open.
231 QuicFecGroupNumber fec_group_number(); 237 QuicFecGroupNumber fec_group_number();
232 238
233 private: 239 private:
234 friend class test::QuicPacketCreatorPeer; 240 friend class test::QuicPacketCreatorPeer;
235 241
236 static bool ShouldRetransmit(const QuicFrame& frame); 242 static bool ShouldRetransmit(const QuicFrame& frame);
237 243
238 // Converts a raw payload to a frame which fits into the currently open 244 // Converts a raw payload to a frame which fits into the current open
239 // packet. The payload begins at |iov_offset| into the |iov|. 245 // packet. The payload begins at |iov_offset| into the |iov|.
240 // Returns the number of bytes consumed from data. 246 // Returns the number of bytes consumed from data.
241 // If data is empty and fin is true, the expected behavior is to consume the 247 // If data is empty and fin is true, the expected behavior is to consume the
242 // fin but return 0. If any data is consumed, it will be copied into a 248 // fin but return 0. If any data is consumed, it will be copied into a
243 // new buffer that |frame| will point to and will be stored in |buffer|. 249 // new buffer that |frame| will point to and will be stored in |buffer|.
244 size_t CreateStreamFrame(QuicStreamId id, 250 size_t CreateStreamFrame(QuicStreamId id,
245 QuicIOVector iov, 251 QuicIOVector iov,
246 size_t iov_offset, 252 size_t iov_offset,
247 QuicStreamOffset offset, 253 QuicStreamOffset offset,
248 bool fin, 254 bool fin,
(...skipping 16 matching lines...) Expand all
265 // |payload| is the non-encrypted FEC protected payload of the packet. 271 // |payload| is the non-encrypted FEC protected payload of the packet.
266 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, 272 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
267 base::StringPiece payload); 273 base::StringPiece payload);
268 274
269 void FillPacketHeader(QuicFecGroupNumber fec_group, 275 void FillPacketHeader(QuicFecGroupNumber fec_group,
270 bool fec_flag, 276 bool fec_flag,
271 QuicPacketHeader* header); 277 QuicPacketHeader* header);
272 278
273 // Allows a frame to be added without creating retransmittable frames. 279 // Allows a frame to be added without creating retransmittable frames.
274 // Particularly useful for retransmits using SerializeAllFrames(). 280 // Particularly useful for retransmits using SerializeAllFrames().
281 // If current open packet cannot accommodate |frame|, returns false and
282 // flushes all pending frames.
275 bool AddFrame(const QuicFrame& frame, 283 bool AddFrame(const QuicFrame& frame,
276 bool save_retransmittable_frames, 284 bool save_retransmittable_frames,
277 bool needs_padding, 285 bool needs_padding,
278 UniqueStreamBuffer buffer); 286 UniqueStreamBuffer buffer);
279 287
280 // Adds a padding frame to the current packet only if the current packet 288 // Adds a padding frame to the current packet only if the current packet
281 // contains a handshake message, and there is sufficient room to fit a 289 // contains a handshake message, and there is sufficient room to fit a
282 // padding frame. 290 // padding frame.
283 void MaybeAddPadding(); 291 void MaybeAddPadding();
284 292
293 // Serializes all frames which have been added and adds any which should be
294 // retransmitted to queued_retransmittable_frames_ if it's not nullptr. All
295 // frames must fit into a single packet. Sets the entropy hash of the
296 // serialized packet to a random bool and returns that value as a member of
297 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
298 // the corresponding RetransmittableFrames if any frames are to be
299 // retransmitted.
300 // Fails if |buffer_len| isn't long enough for the encrypted packet.
301 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len);
302
303 // Does not own this delegate.
304 DelegateInterface* delegate_;
285 QuicConnectionId connection_id_; 305 QuicConnectionId connection_id_;
286 EncryptionLevel encryption_level_; 306 EncryptionLevel encryption_level_;
287 QuicFramer* framer_; 307 QuicFramer* framer_;
288 scoped_ptr<QuicRandomBoolSource> random_bool_source_; 308 scoped_ptr<QuicRandomBoolSource> random_bool_source_;
289 QuicPacketNumber packet_number_; 309 QuicPacketNumber packet_number_;
290 // If true, any created packets will be FEC protected. 310 // If true, any created packets will be FEC protected.
291 bool should_fec_protect_; 311 bool should_fec_protect_;
292 scoped_ptr<QuicFecGroup> fec_group_; 312 scoped_ptr<QuicFecGroup> fec_group_;
293 // Controls whether protocol version should be included while serializing the 313 // Controls whether protocol version should be included while serializing the
294 // packet. 314 // packet.
(...skipping 21 matching lines...) Expand all
316 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_; 336 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
317 // If true, the packet will be padded up to |max_packet_length_|. 337 // If true, the packet will be padded up to |max_packet_length_|.
318 bool needs_padding_; 338 bool needs_padding_;
319 339
320 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); 340 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
321 }; 341 };
322 342
323 } // namespace net 343 } // namespace net
324 344
325 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ 345 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698