OLD | NEW |
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. If multipath enabled, only | 7 // FEC packets based on previously created packets. If multipath enabled, only |
8 // creates packets on one path at the same time. Currently, next packet number | 8 // creates packets on one path at the same time. Currently, next packet number |
9 // is tracked per-path. | 9 // is tracked per-path. |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 QuicIOVector iov, | 120 QuicIOVector iov, |
121 size_t iov_offset, | 121 size_t iov_offset, |
122 QuicStreamOffset offset, | 122 QuicStreamOffset offset, |
123 bool fin, | 123 bool fin, |
124 bool needs_padding, | 124 bool needs_padding, |
125 QuicFrame* frame, | 125 QuicFrame* frame, |
126 FecProtection fec_protection); | 126 FecProtection fec_protection); |
127 | 127 |
128 // Returns true if current open packet can accommodate more stream frames of | 128 // Returns true if current open packet can accommodate more stream frames of |
129 // stream |id| at |offset|, false otherwise. | 129 // stream |id| at |offset|, false otherwise. |
130 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const; | 130 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset); |
131 | 131 |
132 // Re-serializes frames with the original packet's packet number length. | 132 // Re-serializes frames with the original packet's packet number length. |
133 // Used for retransmitting packets to ensure they aren't too long. | 133 // Used for retransmitting packets to ensure they aren't too long. |
134 // Caller must ensure that any open FEC group is closed before calling this | 134 // Caller must ensure that any open FEC group is closed before calling this |
135 // method. | 135 // method. |
136 SerializedPacket ReserializeAllFrames( | 136 SerializedPacket ReserializeAllFrames( |
137 const PendingRetransmission& retransmission, | 137 const PendingRetransmission& retransmission, |
138 char* buffer, | 138 char* buffer, |
139 size_t buffer_len); | 139 size_t buffer_len); |
140 | 140 |
141 // Serializes all added frames into a single packet and invokes the delegate_ | 141 // Serializes all added frames into a single packet and invokes the delegate_ |
142 // to further process the SerializedPacket. | 142 // to further process the SerializedPacket. |
143 void Flush(); | 143 void Flush(); |
144 | 144 |
145 // Returns true if there are frames pending to be serialized. | 145 // Returns true if there are frames pending to be serialized. |
146 bool HasPendingFrames() const; | 146 bool HasPendingFrames() const; |
147 | 147 |
148 // Returns true if there are retransmittable frames pending to be serialized. | 148 // Returns true if there are retransmittable frames pending to be serialized. |
149 bool HasPendingRetransmittableFrames() const; | 149 bool HasPendingRetransmittableFrames() const; |
150 | 150 |
151 // Returns the number of bytes which are available to be used by additional | 151 // Returns the number of bytes which are available to be used by additional |
152 // frames in the packet. Since stream frames are slightly smaller when they | 152 // frames in the packet. Since stream frames are slightly smaller when they |
153 // are the last frame in a packet, this method will return a different | 153 // are the last frame in a packet, this method will return a different |
154 // value than max_packet_size - PacketSize(), in this case. | 154 // value than max_packet_size - PacketSize(), in this case. |
155 size_t BytesFree() const; | 155 size_t BytesFree(); |
156 | 156 |
157 // Returns the number of bytes that the packet will expand by if a new frame | 157 // Returns the number of bytes that the packet will expand by if a new frame |
158 // is added to the packet. If the last frame was a stream frame, it will | 158 // is added to the packet. If the last frame was a stream frame, it will |
159 // expand slightly when a new frame is added, and this method returns the | 159 // expand slightly when a new frame is added, and this method returns the |
160 // amount of expected expansion. If the packet is in an FEC group, no | 160 // amount of expected expansion. If the packet is in an FEC group, no |
161 // expansion happens and this method always returns zero. | 161 // expansion happens and this method always returns zero. |
162 size_t ExpansionOnNewFrame() const; | 162 size_t ExpansionOnNewFrame() const; |
163 | 163 |
164 // Returns the number of bytes in the current packet, including the header, | 164 // Returns the number of bytes in the current packet, including the header, |
165 // if serialized with the current frames. Adding a frame to the packet | 165 // if serialized with the current frames. Adding a frame to the packet |
166 // may change the serialized length of existing frames, as per the comment | 166 // may change the serialized length of existing frames, as per the comment |
167 // in BytesFree. | 167 // in BytesFree. |
168 size_t PacketSize() const; | 168 size_t PacketSize(); |
169 | 169 |
170 // Tries to add |frame| to the packet creator's list of frames to be | 170 // Tries to add |frame| to the packet creator's list of frames to be |
171 // serialized. If the frame does not fit into the current packet, flushes the | 171 // serialized. If the frame does not fit into the current packet, flushes the |
172 // packet and returns false. | 172 // packet and returns false. |
173 bool AddSavedFrame(const QuicFrame& frame); | 173 bool AddSavedFrame(const QuicFrame& frame); |
174 | 174 |
175 // Identical to AddSavedFrame, but allows the frame to be padded. | 175 // Identical to AddSavedFrame, but allows the frame to be padded. |
176 bool AddPaddedSavedFrame(const QuicFrame& frame); | 176 bool AddPaddedSavedFrame(const QuicFrame& frame); |
177 | 177 |
178 // Adds |listener| to the next serialized packet and notifies the | 178 // Adds |listener| to the next serialized packet and notifies the |
(...skipping 13 matching lines...) Expand all Loading... |
192 static SerializedPacket NoPacket(); | 192 static SerializedPacket NoPacket(); |
193 | 193 |
194 // Called when the congestion window has changed. | 194 // Called when the congestion window has changed. |
195 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight); | 195 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight); |
196 | 196 |
197 // Called when the RTT may have changed. | 197 // Called when the RTT may have changed. |
198 void OnRttChange(QuicTime::Delta rtt); | 198 void OnRttChange(QuicTime::Delta rtt); |
199 | 199 |
200 // Sets the encryption level that will be applied to new packets. | 200 // Sets the encryption level that will be applied to new packets. |
201 void set_encryption_level(EncryptionLevel level) { | 201 void set_encryption_level(EncryptionLevel level) { |
202 encryption_level_ = level; | 202 packet_.encryption_level = level; |
203 } | 203 } |
204 | 204 |
205 // packet number of the last created packet, or 0 if no packets have been | 205 // packet number of the last created packet, or 0 if no packets have been |
206 // created. | 206 // created. |
207 QuicPacketNumber packet_number() const { return packet_number_; } | 207 QuicPacketNumber packet_number() const { return packet_.packet_number; } |
208 | 208 |
209 QuicConnectionIdLength connection_id_length() const { | 209 QuicConnectionIdLength connection_id_length() const { |
210 return connection_id_length_; | 210 return connection_id_length_; |
211 } | 211 } |
212 | 212 |
213 void set_connection_id_length(QuicConnectionIdLength length) { | 213 void set_connection_id_length(QuicConnectionIdLength length) { |
214 connection_id_length_ = length; | 214 connection_id_length_ = length; |
215 } | 215 } |
216 | 216 |
217 QuicByteCount max_packet_length() const { return max_packet_length_; } | 217 QuicByteCount max_packet_length() const { return max_packet_length_; } |
218 | 218 |
219 bool has_ack() const { return has_ack_; } | 219 bool has_ack() const { return packet_.has_ack; } |
220 | 220 |
221 bool has_stop_waiting() const { return has_stop_waiting_; } | 221 bool has_stop_waiting() const { return packet_.has_stop_waiting; } |
222 | 222 |
223 // Sets the encrypter to use for the encryption level and updates the max | 223 // Sets the encrypter to use for the encryption level and updates the max |
224 // plaintext size. | 224 // plaintext size. |
225 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); | 225 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); |
226 | 226 |
227 // Indicates whether the packet creator is in a state where it can change | 227 // Indicates whether the packet creator is in a state where it can change |
228 // current maximum packet length. | 228 // current maximum packet length. |
229 bool CanSetMaxPacketLength() const; | 229 bool CanSetMaxPacketLength() const; |
230 | 230 |
231 // Sets the maximum packet length. | 231 // Sets the maximum packet length. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 // frames if there isn't room. If |save_retransmittable_frames| is true, | 309 // frames if there isn't room. If |save_retransmittable_frames| is true, |
310 // saves the |frame| in the next SerializedPacket. | 310 // saves the |frame| in the next SerializedPacket. |
311 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); | 311 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); |
312 | 312 |
313 // Adds a padding frame to the current packet only if the current packet | 313 // Adds a padding frame to the current packet only if the current packet |
314 // contains a handshake message, and there is sufficient room to fit a | 314 // contains a handshake message, and there is sufficient room to fit a |
315 // padding frame. | 315 // padding frame. |
316 void MaybeAddPadding(); | 316 void MaybeAddPadding(); |
317 | 317 |
318 // Serializes all frames which have been added and adds any which should be | 318 // Serializes all frames which have been added and adds any which should be |
319 // retransmitted to queued_retransmittable_frames_ if it's not nullptr. All | 319 // retransmitted to packet_.retransmittable_frames. All frames must fit into |
320 // frames must fit into a single packet. Sets the entropy hash of the | 320 // a single packet. Sets the entropy hash of the serialized packet to a |
321 // serialized packet to a random bool and returns that value as a member of | 321 // random bool. |
322 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to | |
323 // the corresponding RetransmittableFrames if any frames are to be | |
324 // retransmitted. | |
325 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 322 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
326 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len); | 323 void SerializePacket(char* encrypted_buffer, size_t buffer_len); |
327 | 324 |
328 // Called after a new SerialiedPacket is created to call the delegate's | 325 // Called after a new SerialiedPacket is created to call the delegate's |
329 // OnSerializedPacket, reset state, and potentially flush FEC groups. | 326 // OnSerializedPacket, reset state, and potentially flush FEC groups. |
330 void OnSerializedPacket(SerializedPacket* packet); | 327 void OnSerializedPacket(); |
| 328 |
| 329 // Clears all fields of packet_ that should be cleared between serializations. |
| 330 void ClearPacket(); |
331 | 331 |
332 // Turn on FEC protection for subsequent packets. If no FEC group is currently | 332 // Turn on FEC protection for subsequent packets. If no FEC group is currently |
333 // open, this method flushes current open packet and then turns FEC on. | 333 // open, this method flushes current open packet and then turns FEC on. |
334 void MaybeStartFecProtection(); | 334 void MaybeStartFecProtection(); |
335 | 335 |
336 // Turn on FEC protection for subsequently created packets. FEC should be | 336 // Turn on FEC protection for subsequently created packets. FEC should be |
337 // enabled first (max_packets_per_fec_group should be non-zero) for FEC | 337 // enabled first (max_packets_per_fec_group should be non-zero) for FEC |
338 // protection to start. | 338 // protection to start. |
339 void StartFecProtectingPackets(); | 339 void StartFecProtectingPackets(); |
340 | 340 |
341 // Turn off FEC protection for subsequently created packets. If the creator | 341 // Turn off FEC protection for subsequently created packets. If the creator |
342 // has any open FEC group, call will fail. It is the caller's responsibility | 342 // has any open FEC group, call will fail. It is the caller's responsibility |
343 // to flush out FEC packets in generation, and to verify with ShouldSendFec() | 343 // to flush out FEC packets in generation, and to verify with ShouldSendFec() |
344 // that there is no open FEC group. | 344 // that there is no open FEC group. |
345 void StopFecProtectingPackets(); | 345 void StopFecProtectingPackets(); |
346 | 346 |
347 // Resets (closes) the FEC group. This method should only be called on a | 347 // Resets (closes) the FEC group. This method should only be called on a |
348 // packet boundary. | 348 // packet boundary. |
349 void ResetFecGroup(); | 349 void ResetFecGroup(); |
350 | 350 |
351 // Packetize FEC data. All frames must fit into a single packet. Also, sets | 351 // Packetize FEC data. Sets the entropy hash of the serialized packet to a |
352 // the entropy hash of the serialized packet to a random bool and returns | 352 // random bool. |
353 // that value as a member of SerializedPacket. | |
354 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 353 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
355 SerializedPacket SerializeFec(char* buffer, size_t buffer_len); | 354 void SerializeFec(char* buffer, size_t buffer_len); |
356 | 355 |
357 // Does not own these delegates or the framer. | 356 // Does not own these delegates or the framer. |
358 DelegateInterface* delegate_; | 357 DelegateInterface* delegate_; |
359 DebugDelegate* debug_delegate_; | 358 DebugDelegate* debug_delegate_; |
360 QuicFramer* framer_; | 359 QuicFramer* framer_; |
361 | 360 |
362 scoped_ptr<QuicRandomBoolSource> random_bool_source_; | 361 scoped_ptr<QuicRandomBoolSource> random_bool_source_; |
363 QuicBufferAllocator* const buffer_allocator_; | 362 QuicBufferAllocator* const buffer_allocator_; |
364 | 363 |
365 // Controls whether version should be included while serializing the packet. | 364 // Controls whether version should be included while serializing the packet. |
366 bool send_version_in_packet_; | 365 bool send_version_in_packet_; |
367 // Controls whether path id should be included while serializing the packet. | 366 // Controls whether path id should be included while serializing the packet. |
368 bool send_path_id_in_packet_; | 367 bool send_path_id_in_packet_; |
369 // Staging variable to hold next packet number length. When sequence | 368 // Staging variable to hold next packet number length. When sequence |
370 // number length is to be changed, this variable holds the new length until | 369 // number length is to be changed, this variable holds the new length until |
371 // a packet or FEC group boundary, when the creator's packet_number_length_ | 370 // a packet or FEC group boundary, when the creator's packet_number_length_ |
372 // can be changed to this new value. | 371 // can be changed to this new value. |
373 QuicPacketNumberLength next_packet_number_length_; | 372 QuicPacketNumberLength next_packet_number_length_; |
374 // Maximum length including headers and encryption (UDP payload length.) | 373 // Maximum length including headers and encryption (UDP payload length.) |
375 QuicByteCount max_packet_length_; | 374 QuicByteCount max_packet_length_; |
376 mutable size_t max_plaintext_size_; | 375 size_t max_plaintext_size_; |
| 376 // Length of connection_id to send over the wire. |
| 377 QuicConnectionIdLength connection_id_length_; |
377 | 378 |
378 // Frames to be added to the next SerializedPacket | 379 // Frames to be added to the next SerializedPacket |
379 QuicFrames queued_frames_; | 380 QuicFrames queued_frames_; |
380 | 381 |
381 // Fields used to populate SerializedPacket. | 382 // packet_size should never be read directly, use PacketSize() instead. |
| 383 // TODO(ianswett): Move packet_size_ into SerializedPacket once |
| 384 // QuicEncryptedPacket has been flattened into SerializedPacket. |
| 385 size_t packet_size_; |
382 QuicConnectionId connection_id_; | 386 QuicConnectionId connection_id_; |
383 EncryptionLevel encryption_level_; | 387 |
384 // True if an ack is queued in queued_frames_. | 388 // Packet used to invoke OnSerializedPacket. |
385 bool has_ack_; | 389 SerializedPacket packet_; |
386 // True if a stop waiting frame is queued in queued_frames_. | |
387 bool has_stop_waiting_; | |
388 // The path which current constructed packet will be sent on. | |
389 QuicPathId current_path_; | |
390 QuicPacketNumber packet_number_; | |
391 // Length of connection_id to send over the wire. | |
392 QuicConnectionIdLength connection_id_length_; | |
393 // packet number length for the current packet and for the current FEC group | |
394 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet | |
395 // is empty. | |
396 mutable QuicPacketNumberLength packet_number_length_; | |
397 // packet_size_ is mutable because it's just a cache of the current size. | |
398 // packet_size should never be read directly, use PacketSize() instead. | |
399 mutable size_t packet_size_; | |
400 scoped_ptr<QuicFrames> queued_retransmittable_frames_; | |
401 // If true, the packet will be padded up to |max_packet_length_|. | |
402 bool needs_padding_; | |
403 IsHandshake has_crypto_handshake_; | |
404 // Stores ack std::listeners that should be attached to the next packet. | |
405 std::list<AckListenerWrapper> ack_listeners_; | |
406 | 390 |
407 // Map mapping path_id to last sent packet number on the path. | 391 // Map mapping path_id to last sent packet number on the path. |
408 hash_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; | 392 hash_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; |
409 | 393 |
410 // FEC related fields. | 394 // FEC related fields. |
411 // True when creator is requested to turn on FEC protection. False otherwise. | 395 // True when creator is requested to turn on FEC protection. False otherwise. |
412 // There is a time difference between should_fec_protect_next_packet_ is | 396 // There is a time difference between should_fec_protect_next_packet_ is |
413 // true/false and FEC is actually turned on/off (e.g., The creator may have an | 397 // true/false and FEC is actually turned on/off (e.g., The creator may have an |
414 // open FEC group even if this variable is false). | 398 // open FEC group even if this variable is false). |
415 bool should_fec_protect_next_packet_; | 399 bool should_fec_protect_next_packet_; |
(...skipping 12 matching lines...) Expand all Loading... |
428 // The multiplication factor for FEC timeout based on RTT. | 412 // The multiplication factor for FEC timeout based on RTT. |
429 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment. | 413 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment. |
430 float rtt_multiplier_for_fec_timeout_; | 414 float rtt_multiplier_for_fec_timeout_; |
431 | 415 |
432 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); | 416 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); |
433 }; | 417 }; |
434 | 418 |
435 } // namespace net | 419 } // namespace net |
436 | 420 |
437 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ | 421 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ |
OLD | NEW |