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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 239 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
238 } | 240 } |
239 | 241 |
240 bool QuicPacketCreator::ConsumeData(QuicStreamId id, | 242 bool QuicPacketCreator::ConsumeData(QuicStreamId id, |
241 QuicIOVector iov, | 243 QuicIOVector iov, |
242 size_t iov_offset, | 244 size_t iov_offset, |
243 QuicStreamOffset offset, | 245 QuicStreamOffset offset, |
244 bool fin, | 246 bool fin, |
245 bool needs_padding, | 247 bool needs_padding, |
246 QuicFrame* frame) { | 248 QuicFrame* frame) { |
| 249 if (!HasRoomForStreamFrame(id, offset)) { |
| 250 Flush(); |
| 251 return false; |
| 252 } |
| 253 |
247 UniqueStreamBuffer buffer; | 254 UniqueStreamBuffer buffer; |
248 CreateStreamFrame(id, iov, iov_offset, offset, fin, frame, &buffer); | 255 CreateStreamFrame(id, iov, iov_offset, offset, fin, frame, &buffer); |
249 | 256 |
250 return AddFrame(*frame, | 257 bool success = AddFrame(*frame, |
251 /*save_retransmittable_frames=*/true, | 258 /*save_retransmittable_frames=*/true, |
252 needs_padding, | 259 needs_padding, |
253 std::move(buffer)); | 260 std::move(buffer)); |
| 261 DCHECK(success); |
| 262 return true; |
254 } | 263 } |
255 | 264 |
256 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 265 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
257 QuicStreamOffset offset) const { | 266 QuicStreamOffset offset) const { |
258 // 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 |
259 // 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 |
260 // QuicPacketCreator. | 269 // QuicPacketCreator. |
261 return BytesFree() > | 270 return BytesFree() > |
262 QuicFramer::GetMinStreamFrameSize(id, offset, true, | 271 QuicFramer::GetMinStreamFrameSize(id, offset, true, |
263 should_fec_protect_ ? IN_FEC_GROUP : | 272 should_fec_protect_ ? IN_FEC_GROUP : |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 << "Attempt to serialize empty packet"; | 431 << "Attempt to serialize empty packet"; |
423 for (const QuicFrame& frame : frames) { | 432 for (const QuicFrame& frame : frames) { |
424 bool success = AddFrame(frame, false, false, nullptr); | 433 bool success = AddFrame(frame, false, false, nullptr); |
425 DCHECK(success); | 434 DCHECK(success); |
426 } | 435 } |
427 SerializedPacket packet = SerializePacket(buffer, buffer_len); | 436 SerializedPacket packet = SerializePacket(buffer, buffer_len); |
428 DCHECK(packet.retransmittable_frames == nullptr); | 437 DCHECK(packet.retransmittable_frames == nullptr); |
429 return packet; | 438 return packet; |
430 } | 439 } |
431 | 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 |
432 bool QuicPacketCreator::HasPendingFrames() const { | 454 bool QuicPacketCreator::HasPendingFrames() const { |
433 return !queued_frames_.empty(); | 455 return !queued_frames_.empty(); |
434 } | 456 } |
435 | 457 |
436 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { | 458 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { |
437 return queued_retransmittable_frames_.get() != nullptr && | 459 return queued_retransmittable_frames_.get() != nullptr && |
438 !queued_retransmittable_frames_->frames().empty(); | 460 !queued_retransmittable_frames_->frames().empty(); |
439 } | 461 } |
440 | 462 |
441 size_t QuicPacketCreator::ExpansionOnNewFrame() const { | 463 size_t QuicPacketCreator::ExpansionOnNewFrame() const { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 bool save_retransmittable_frames, | 680 bool save_retransmittable_frames, |
659 bool needs_padding, | 681 bool needs_padding, |
660 UniqueStreamBuffer buffer) { | 682 UniqueStreamBuffer buffer) { |
661 DVLOG(1) << "Adding frame: " << frame; | 683 DVLOG(1) << "Adding frame: " << frame; |
662 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); | 684 InFecGroup is_in_fec_group = MaybeUpdateLengthsAndStartFec(); |
663 | 685 |
664 size_t frame_len = framer_->GetSerializedFrameLength( | 686 size_t frame_len = framer_->GetSerializedFrameLength( |
665 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, | 687 frame, BytesFree(), queued_frames_.empty(), true, is_in_fec_group, |
666 packet_number_length_); | 688 packet_number_length_); |
667 if (frame_len == 0) { | 689 if (frame_len == 0) { |
| 690 // Current open packet is full. |
| 691 Flush(); |
668 return false; | 692 return false; |
669 } | 693 } |
670 DCHECK_LT(0u, packet_size_); | 694 DCHECK_LT(0u, packet_size_); |
671 packet_size_ += ExpansionOnNewFrame() + frame_len; | 695 packet_size_ += ExpansionOnNewFrame() + frame_len; |
672 | 696 |
673 if (save_retransmittable_frames && ShouldRetransmit(frame)) { | 697 if (save_retransmittable_frames && ShouldRetransmit(frame)) { |
674 if (queued_retransmittable_frames_.get() == nullptr) { | 698 if (queued_retransmittable_frames_.get() == nullptr) { |
675 queued_retransmittable_frames_.reset( | 699 queued_retransmittable_frames_.reset( |
676 new RetransmittableFrames(encryption_level_)); | 700 new RetransmittableFrames(encryption_level_)); |
677 } | 701 } |
(...skipping 18 matching lines...) Expand all Loading... |
696 if (BytesFree() == 0) { | 720 if (BytesFree() == 0) { |
697 // Don't pad full packets. | 721 // Don't pad full packets. |
698 return; | 722 return; |
699 } | 723 } |
700 | 724 |
701 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); | 725 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false, false, nullptr); |
702 DCHECK(success); | 726 DCHECK(success); |
703 } | 727 } |
704 | 728 |
705 } // namespace net | 729 } // namespace net |
OLD | NEW |