| 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 #include "net/quic/quic_packet_creator.h" | 5 #include "net/quic/quic_packet_creator.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "net/quic/crypto/quic_random.h" | 8 #include "net/quic/crypto/quic_random.h" |
| 9 #include "net/quic/quic_ack_notifier.h" | 9 #include "net/quic/quic_ack_notifier.h" |
| 10 #include "net/quic/quic_fec_group.h" | 10 #include "net/quic/quic_fec_group.h" |
| 11 #include "net/quic/quic_utils.h" | 11 #include "net/quic/quic_utils.h" |
| 12 | 12 |
| 13 using base::StringPiece; | 13 using base::StringPiece; |
| 14 using std::make_pair; | 14 using std::make_pair; |
| 15 using std::max; |
| 15 using std::min; | 16 using std::min; |
| 16 using std::pair; | 17 using std::pair; |
| 17 using std::vector; | 18 using std::vector; |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 QuicPacketCreator::QuicPacketCreator(QuicGuid guid, | 22 QuicPacketCreator::QuicPacketCreator(QuicGuid guid, |
| 22 QuicFramer* framer, | 23 QuicFramer* framer, |
| 23 QuicRandom* random_generator, | 24 QuicRandom* random_generator, |
| 24 bool is_server) | 25 bool is_server) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // maximum packet size if we stop sending version before it is serialized. | 65 // maximum packet size if we stop sending version before it is serialized. |
| 65 void QuicPacketCreator::StopSendingVersion() { | 66 void QuicPacketCreator::StopSendingVersion() { |
| 66 DCHECK(send_version_in_packet_); | 67 DCHECK(send_version_in_packet_); |
| 67 send_version_in_packet_ = false; | 68 send_version_in_packet_ = false; |
| 68 if (packet_size_ > 0) { | 69 if (packet_size_ > 0) { |
| 69 DCHECK_LT(kQuicVersionSize, packet_size_); | 70 DCHECK_LT(kQuicVersionSize, packet_size_); |
| 70 packet_size_ -= kQuicVersionSize; | 71 packet_size_ -= kQuicVersionSize; |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 75 void QuicPacketCreator::UpdateSequenceNumberLength( |
| 76 QuicPacketSequenceNumber least_packet_awaited_by_peer, |
| 77 QuicByteCount bytes_per_second) { |
| 78 DCHECK_LE(least_packet_awaited_by_peer, sequence_number_ + 1); |
| 79 // Since the packet creator will not change sequence number length mid FEC |
| 80 // group, include the size of an FEC group to be safe. |
| 81 const QuicPacketSequenceNumber current_delta = |
| 82 options_.max_packets_per_fec_group + sequence_number_ + 1 |
| 83 - least_packet_awaited_by_peer; |
| 84 const uint64 congestion_window = |
| 85 bytes_per_second / options_.max_packet_length; |
| 86 const uint64 delta = max(current_delta, congestion_window); |
| 87 |
| 88 if (delta < 1 << ((PACKET_1BYTE_SEQUENCE_NUMBER * 8) - 2)) { |
| 89 options_.send_sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER; |
| 90 } else if (delta < 1 << ((PACKET_2BYTE_SEQUENCE_NUMBER * 8) - 2)) { |
| 91 options_.send_sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER; |
| 92 } else if (delta < 1 << ((PACKET_4BYTE_SEQUENCE_NUMBER * 8) - 2)) { |
| 93 options_.send_sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER; |
| 94 } else { |
| 95 options_.send_sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER; |
| 96 } |
| 97 } |
| 98 |
| 74 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 99 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
| 75 QuicStreamOffset offset) const { | 100 QuicStreamOffset offset) const { |
| 76 return BytesFree() > | 101 return BytesFree() > |
| 77 QuicFramer::GetMinStreamFrameSize(framer_->version(), id, offset, true); | 102 QuicFramer::GetMinStreamFrameSize(framer_->version(), id, offset, true); |
| 78 } | 103 } |
| 79 | 104 |
| 80 // static | 105 // static |
| 81 size_t QuicPacketCreator::StreamFramePacketOverhead( | 106 size_t QuicPacketCreator::StreamFramePacketOverhead( |
| 82 QuicVersion version, | 107 QuicVersion version, |
| 83 QuicGuidLength guid_length, | 108 QuicGuidLength guid_length, |
| 84 bool include_version, | 109 bool include_version, |
| 85 QuicSequenceNumberLength sequence_number_length, | 110 QuicSequenceNumberLength sequence_number_length, |
| 86 InFecGroup is_in_fec_group) { | 111 InFecGroup is_in_fec_group) { |
| 87 return GetPacketHeaderSize(guid_length, include_version, | 112 return GetPacketHeaderSize(guid_length, include_version, |
| 88 sequence_number_length, is_in_fec_group) + | 113 sequence_number_length, is_in_fec_group) + |
| 89 // Assumes this is a stream with a single lone packet. | 114 // Assumes this is a stream with a single lone packet. |
| 90 QuicFramer::GetMinStreamFrameSize(version, 1u, 0u, true); | 115 QuicFramer::GetMinStreamFrameSize(version, 1u, 0u, true); |
| 91 } | 116 } |
| 92 | 117 |
| 93 size_t QuicPacketCreator::CreateStreamFrame(QuicStreamId id, | 118 size_t QuicPacketCreator::CreateStreamFrame(QuicStreamId id, |
| 94 StringPiece data, | 119 StringPiece data, |
| 95 QuicStreamOffset offset, | 120 QuicStreamOffset offset, |
| 96 bool fin, | 121 bool fin, |
| 97 QuicFrame* frame) { | 122 QuicFrame* frame) { |
| 98 DCHECK_GT(options_.max_packet_length, | 123 DCHECK_GT(options_.max_packet_length, |
| 99 StreamFramePacketOverhead( | 124 StreamFramePacketOverhead( |
| 100 framer_->version(), PACKET_8BYTE_GUID, kIncludeVersion, | 125 framer_->version(), PACKET_8BYTE_GUID, kIncludeVersion, |
| 101 PACKET_6BYTE_SEQUENCE_NUMBER, IN_FEC_GROUP)); | 126 PACKET_6BYTE_SEQUENCE_NUMBER, IN_FEC_GROUP)); |
| 102 DCHECK(HasRoomForStreamFrame(id, offset)); | 127 if (!HasRoomForStreamFrame(id, offset)) { |
| 128 LOG(DFATAL) << "No room for Stream frame, BytesFree: " << BytesFree() |
| 129 << " MinStreamFrameSize: " |
| 130 << QuicFramer::GetMinStreamFrameSize( |
| 131 framer_->version(), id, offset, true); |
| 132 } |
| 103 | 133 |
| 104 const size_t free_bytes = BytesFree(); | 134 const size_t free_bytes = BytesFree(); |
| 105 size_t bytes_consumed = 0; | 135 size_t bytes_consumed = 0; |
| 106 | 136 |
| 107 if (data.size() != 0) { | 137 if (data.size() != 0) { |
| 108 // When a STREAM frame is the last frame in a packet, it consumes two fewer | 138 // When a STREAM frame is the last frame in a packet, it consumes two fewer |
| 109 // bytes of framing overhead. | 139 // bytes of framing overhead. |
| 110 // Anytime more data is available than fits in with the extra two bytes, | 140 // Anytime more data is available than fits in with the extra two bytes, |
| 111 // the frame will be the last, and up to two extra bytes are consumed. | 141 // the frame will be the last, and up to two extra bytes are consumed. |
| 112 // TODO(ianswett): If QUIC pads, the 1 byte PADDING frame does not fit when | 142 // TODO(ianswett): If QUIC pads, the 1 byte PADDING frame does not fit when |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 queued_frames_.push_back( | 377 queued_frames_.push_back( |
| 348 queued_retransmittable_frames_->AddNonStreamFrame(frame)); | 378 queued_retransmittable_frames_->AddNonStreamFrame(frame)); |
| 349 } | 379 } |
| 350 } else { | 380 } else { |
| 351 queued_frames_.push_back(frame); | 381 queued_frames_.push_back(frame); |
| 352 } | 382 } |
| 353 return true; | 383 return true; |
| 354 } | 384 } |
| 355 | 385 |
| 356 } // namespace net | 386 } // namespace net |
| OLD | NEW |