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, |
| 259 needs_padding, |
| 260 std::move(buffer)); |
| 261 DCHECK(success); |
| 262 return true; |
| 263 } |
| 264 |
240 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 265 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
241 QuicStreamOffset offset) const { | 266 QuicStreamOffset offset) const { |
242 // TODO(jri): This is a simple safe decision for now, but make | 267 // 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 | 268 // is_in_fec_group a parameter. Same as with all public methods in |
244 // QuicPacketCreator. | 269 // QuicPacketCreator. |
245 return BytesFree() > | 270 return BytesFree() > |
246 QuicFramer::GetMinStreamFrameSize(id, offset, true, | 271 QuicFramer::GetMinStreamFrameSize(id, offset, true, |
247 should_fec_protect_ ? IN_FEC_GROUP : | 272 should_fec_protect_ ? IN_FEC_GROUP : |
248 NOT_IN_FEC_GROUP); | 273 NOT_IN_FEC_GROUP); |
249 } | 274 } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 << "Attempt to serialize empty packet"; | 431 << "Attempt to serialize empty packet"; |
407 for (const QuicFrame& frame : frames) { | 432 for (const QuicFrame& frame : frames) { |
408 bool success = AddFrame(frame, false, false, nullptr); | 433 bool success = AddFrame(frame, false, false, nullptr); |
409 DCHECK(success); | 434 DCHECK(success); |
410 } | 435 } |
411 SerializedPacket packet = SerializePacket(buffer, buffer_len); | 436 SerializedPacket packet = SerializePacket(buffer, buffer_len); |
412 DCHECK(packet.retransmittable_frames == nullptr); | 437 DCHECK(packet.retransmittable_frames == nullptr); |
413 return packet; | 438 return packet; |
414 } | 439 } |
415 | 440 |
| 441 void QuicPacketCreator::Flush() { |
| 442 if (!HasPendingFrames()) { |
| 443 return; |
| 444 } |
| 445 |
| 446 // TODO(rtenneti): Change the default 64 alignas value (used the default |
| 447 // value from CACHELINE_SIZE). |
| 448 ALIGNAS(64) char seralized_packet_buffer[kMaxPacketSize]; |
| 449 SerializedPacket serialized_packet = |
| 450 SerializePacket(seralized_packet_buffer, kMaxPacketSize); |
| 451 delegate_->OnSerializedPacket(&serialized_packet); |
| 452 } |
| 453 |
416 bool QuicPacketCreator::HasPendingFrames() const { | 454 bool QuicPacketCreator::HasPendingFrames() const { |
417 return !queued_frames_.empty(); | 455 return !queued_frames_.empty(); |
418 } | 456 } |
419 | 457 |
420 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { | 458 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { |
421 return queued_retransmittable_frames_.get() != nullptr && | 459 return queued_retransmittable_frames_.get() != nullptr && |
422 !queued_retransmittable_frames_->frames().empty(); | 460 !queued_retransmittable_frames_->frames().empty(); |
423 } | 461 } |
424 | 462 |
425 size_t QuicPacketCreator::ExpansionOnNewFrame() const { | 463 size_t QuicPacketCreator::ExpansionOnNewFrame() const { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 bool save_retransmittable_frames, | 680 bool save_retransmittable_frames, |
643 bool needs_padding, | 681 bool needs_padding, |
644 UniqueStreamBuffer buffer) { | 682 UniqueStreamBuffer buffer) { |
645 DVLOG(1) << "Adding frame: " << frame; | 683 DVLOG(1) << "Adding frame: " << frame; |
646 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); | 684 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); |
647 | 685 |
648 size_t frame_len = framer_->GetSerializedFrameLength( | 686 size_t frame_len = framer_->GetSerializedFrameLength( |
649 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, | 687 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, |
650 packet_number_length_); | 688 packet_number_length_); |
651 if (frame_len == 0) { | 689 if (frame_len == 0) { |
| 690 // Current open packet is full. |
| 691 Flush(); |
652 return false; | 692 return false; |
653 } | 693 } |
654 DCHECK_LT(0u, packet_size_); | 694 DCHECK_LT(0u, packet_size_); |
655 packet_size_ += ExpansionOnNewFrame() + frame_len; | 695 packet_size_ += ExpansionOnNewFrame() + frame_len; |
656 | 696 |
657 if (save_retransmittable_frames && ShouldRetransmit(frame)) { | 697 if (save_retransmittable_frames && ShouldRetransmit(frame)) { |
658 if (queued_retransmittable_frames_.get() == nullptr) { | 698 if (queued_retransmittable_frames_.get() == nullptr) { |
659 queued_retransmittable_frames_.reset( | 699 queued_retransmittable_frames_.reset( |
660 new RetransmittableFrames(encryption_level_)); | 700 new RetransmittableFrames(encryption_level_)); |
661 } | 701 } |
(...skipping 18 matching lines...) Expand all Loading... |
680 if (BytesFree() == 0) { | 720 if (BytesFree() == 0) { |
681 // Don't pad full packets. | 721 // Don't pad full packets. |
682 return; | 722 return; |
683 } | 723 } |
684 | 724 |
685 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); | 725 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); |
686 DCHECK(success); | 726 DCHECK(success); |
687 } | 727 } |
688 | 728 |
689 } // namespace net | 729 } // namespace net |
OLD | NEW |