| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // Stored random bits. | 62 // Stored random bits. |
| 63 uint64 bit_bucket_; | 63 uint64 bit_bucket_; |
| 64 // The next available bit has "1" in the mask. Zero means empty bucket. | 64 // The next available bit has "1" in the mask. Zero means empty bucket. |
| 65 uint64 bit_mask_; | 65 uint64 bit_mask_; |
| 66 | 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource); | 67 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource); |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 QuicPacketCreator::QuicPacketCreator(QuicConnectionId connection_id, | 70 QuicPacketCreator::QuicPacketCreator(QuicConnectionId connection_id, |
| 71 QuicFramer* framer, | 71 QuicFramer* framer, |
| 72 QuicRandom* random_generator) | 72 QuicRandom* random_generator, |
| 73 : connection_id_(connection_id), | 73 DelegateInterface* delegate) |
| 74 : delegate_(delegate), |
| 75 connection_id_(connection_id), |
| 74 encryption_level_(ENCRYPTION_NONE), | 76 encryption_level_(ENCRYPTION_NONE), |
| 75 framer_(framer), | 77 framer_(framer), |
| 76 random_bool_source_(new QuicRandomBoolSource(random_generator)), | 78 random_bool_source_(new QuicRandomBoolSource(random_generator)), |
| 77 packet_number_(0), | 79 packet_number_(0), |
| 78 should_fec_protect_(false), | 80 should_fec_protect_(false), |
| 79 send_version_in_packet_(framer->perspective() == Perspective::IS_CLIENT), | 81 send_version_in_packet_(framer->perspective() == Perspective::IS_CLIENT), |
| 80 max_packet_length_(0), | 82 max_packet_length_(0), |
| 81 max_packets_per_fec_group_(kDefaultMaxPacketsPerFecGroup), | 83 max_packets_per_fec_group_(kDefaultMaxPacketsPerFecGroup), |
| 82 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), | 84 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), |
| 83 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), | 85 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 // Since the packet creator will not change packet number length mid FEC | 232 // Since the packet creator will not change packet number length mid FEC |
| 231 // group, include the size of an FEC group to be safe. | 233 // group, include the size of an FEC group to be safe. |
| 232 const QuicPacketNumber current_delta = max_packets_per_fec_group_ + | 234 const QuicPacketNumber current_delta = max_packets_per_fec_group_ + |
| 233 packet_number_ + 1 - | 235 packet_number_ + 1 - |
| 234 least_packet_awaited_by_peer; | 236 least_packet_awaited_by_peer; |
| 235 const uint64 delta = max(current_delta, max_packets_in_flight); | 237 const uint64 delta = max(current_delta, max_packets_in_flight); |
| 236 next_packet_number_length_ = | 238 next_packet_number_length_ = |
| 237 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 239 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
| 238 } | 240 } |
| 239 | 241 |
| 242 bool QuicPacketCreator::ConsumeData(QuicStreamId id, |
| 243 QuicIOVector iov, |
| 244 size_t iov_offset, |
| 245 QuicStreamOffset offset, |
| 246 bool fin, |
| 247 bool needs_padding, |
| 248 QuicFrame* frame) { |
| 249 if (!HasRoomForStreamFrame(id, offset)) { |
| 250 Flush(); |
| 251 return false; |
| 252 } |
| 253 |
| 254 UniqueStreamBuffer buffer; |
| 255 CreateStreamFrame(id, iov, iov_offset, offset, fin, frame, &buffer); |
| 256 |
| 257 bool success = AddFrame(*frame, |
| 258 /*save_retransmittable_frames=*/true, needs_padding, |
| 259 std::move(buffer)); |
| 260 DCHECK(success); |
| 261 return true; |
| 262 } |
| 263 |
| 240 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 264 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
| 241 QuicStreamOffset offset) const { | 265 QuicStreamOffset offset) const { |
| 242 // TODO(jri): This is a simple safe decision for now, but make | 266 // TODO(jri): This is a simple safe decision for now, but make |
| 243 // is_in_fec_group a parameter. Same as with all public methods in | 267 // is_in_fec_group a parameter. Same as with all public methods in |
| 244 // QuicPacketCreator. | 268 // QuicPacketCreator. |
| 245 return BytesFree() > | 269 return BytesFree() > |
| 246 QuicFramer::GetMinStreamFrameSize(id, offset, true, | 270 QuicFramer::GetMinStreamFrameSize(id, offset, true, |
| 247 should_fec_protect_ ? IN_FEC_GROUP : | 271 should_fec_protect_ ? IN_FEC_GROUP : |
| 248 NOT_IN_FEC_GROUP); | 272 NOT_IN_FEC_GROUP); |
| 249 } | 273 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 << "Attempt to serialize empty packet"; | 430 << "Attempt to serialize empty packet"; |
| 407 for (const QuicFrame& frame : frames) { | 431 for (const QuicFrame& frame : frames) { |
| 408 bool success = AddFrame(frame, false, false, nullptr); | 432 bool success = AddFrame(frame, false, false, nullptr); |
| 409 DCHECK(success); | 433 DCHECK(success); |
| 410 } | 434 } |
| 411 SerializedPacket packet = SerializePacket(buffer, buffer_len); | 435 SerializedPacket packet = SerializePacket(buffer, buffer_len); |
| 412 DCHECK(packet.retransmittable_frames == nullptr); | 436 DCHECK(packet.retransmittable_frames == nullptr); |
| 413 return packet; | 437 return packet; |
| 414 } | 438 } |
| 415 | 439 |
| 440 void QuicPacketCreator::Flush() { |
| 441 if (!HasPendingFrames()) { |
| 442 return; |
| 443 } |
| 444 |
| 445 // TODO(rtenneti): Change the default 64 alignas value (used the default |
| 446 // value from CACHELINE_SIZE). |
| 447 ALIGNAS(64) char seralized_packet_buffer[kMaxPacketSize]; |
| 448 SerializedPacket serialized_packet = |
| 449 SerializePacket(seralized_packet_buffer, kMaxPacketSize); |
| 450 delegate_->OnSerializedPacket(&serialized_packet); |
| 451 } |
| 452 |
| 416 bool QuicPacketCreator::HasPendingFrames() const { | 453 bool QuicPacketCreator::HasPendingFrames() const { |
| 417 return !queued_frames_.empty(); | 454 return !queued_frames_.empty(); |
| 418 } | 455 } |
| 419 | 456 |
| 420 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { | 457 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { |
| 421 return queued_retransmittable_frames_.get() != nullptr && | 458 return queued_retransmittable_frames_.get() != nullptr && |
| 422 !queued_retransmittable_frames_->frames().empty(); | 459 !queued_retransmittable_frames_->frames().empty(); |
| 423 } | 460 } |
| 424 | 461 |
| 425 size_t QuicPacketCreator::ExpansionOnNewFrame() const { | 462 size_t QuicPacketCreator::ExpansionOnNewFrame() const { |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 bool save_retransmittable_frames, | 679 bool save_retransmittable_frames, |
| 643 bool needs_padding, | 680 bool needs_padding, |
| 644 UniqueStreamBuffer buffer) { | 681 UniqueStreamBuffer buffer) { |
| 645 DVLOG(1) << "Adding frame: " << frame; | 682 DVLOG(1) << "Adding frame: " << frame; |
| 646 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); | 683 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); |
| 647 | 684 |
| 648 size_t frame_len = framer_->GetSerializedFrameLength( | 685 size_t frame_len = framer_->GetSerializedFrameLength( |
| 649 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, | 686 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, |
| 650 packet_number_length_); | 687 packet_number_length_); |
| 651 if (frame_len == 0) { | 688 if (frame_len == 0) { |
| 689 // Current open packet is full. |
| 690 Flush(); |
| 652 return false; | 691 return false; |
| 653 } | 692 } |
| 654 DCHECK_LT(0u, packet_size_); | 693 DCHECK_LT(0u, packet_size_); |
| 655 packet_size_ += ExpansionOnNewFrame() + frame_len; | 694 packet_size_ += ExpansionOnNewFrame() + frame_len; |
| 656 | 695 |
| 657 if (save_retransmittable_frames && ShouldRetransmit(frame)) { | 696 if (save_retransmittable_frames && ShouldRetransmit(frame)) { |
| 658 if (queued_retransmittable_frames_.get() == nullptr) { | 697 if (queued_retransmittable_frames_.get() == nullptr) { |
| 659 queued_retransmittable_frames_.reset( | 698 queued_retransmittable_frames_.reset( |
| 660 new RetransmittableFrames(encryption_level_)); | 699 new RetransmittableFrames(encryption_level_)); |
| 661 } | 700 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 680 if (BytesFree() == 0) { | 719 if (BytesFree() == 0) { |
| 681 // Don't pad full packets. | 720 // Don't pad full packets. |
| 682 return; | 721 return; |
| 683 } | 722 } |
| 684 | 723 |
| 685 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); | 724 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); |
| 686 DCHECK(success); | 725 DCHECK(success); |
| 687 } | 726 } |
| 688 | 727 |
| 689 } // namespace net | 728 } // namespace net |
| OLD | NEW |