| 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_generator.h" | 5 #include "net/quic/quic_packet_generator.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 QuicFramer* framer, | 39 QuicFramer* framer, |
| 40 QuicRandom* random_generator, | 40 QuicRandom* random_generator, |
| 41 DelegateInterface* delegate) | 41 DelegateInterface* delegate) |
| 42 : delegate_(delegate), | 42 : delegate_(delegate), |
| 43 debug_delegate_(nullptr), | 43 debug_delegate_(nullptr), |
| 44 packet_creator_(connection_id, framer, random_generator), | 44 packet_creator_(connection_id, framer, random_generator), |
| 45 batch_mode_(false), | 45 batch_mode_(false), |
| 46 fec_timeout_(QuicTime::Delta::Zero()), | 46 fec_timeout_(QuicTime::Delta::Zero()), |
| 47 should_fec_protect_(false), | 47 should_fec_protect_(false), |
| 48 should_send_ack_(false), | 48 should_send_ack_(false), |
| 49 should_send_stop_waiting_(false) { | 49 should_send_stop_waiting_(false), |
| 50 ack_queued_(false), |
| 51 stop_waiting_queued_(false) { |
| 50 } | 52 } |
| 51 | 53 |
| 52 QuicPacketGenerator::~QuicPacketGenerator() { | 54 QuicPacketGenerator::~QuicPacketGenerator() { |
| 53 for (QuicFrames::iterator it = queued_control_frames_.begin(); | 55 for (QuicFrames::iterator it = queued_control_frames_.begin(); |
| 54 it != queued_control_frames_.end(); ++it) { | 56 it != queued_control_frames_.end(); ++it) { |
| 55 switch (it->type) { | 57 switch (it->type) { |
| 56 case PADDING_FRAME: | 58 case PADDING_FRAME: |
| 57 delete it->padding_frame; | 59 delete it->padding_frame; |
| 58 break; | 60 break; |
| 59 case STREAM_FRAME: | 61 case STREAM_FRAME: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 packet_creator_.set_max_packets_per_fec_group( | 96 packet_creator_.set_max_packets_per_fec_group( |
| 95 static_cast<size_t>(kMaxPacketsInFlightMultiplierForFecGroupSize * | 97 static_cast<size_t>(kMaxPacketsInFlightMultiplierForFecGroupSize * |
| 96 max_packets_in_flight)); | 98 max_packets_in_flight)); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void QuicPacketGenerator::OnRttChange(QuicTime::Delta rtt) { | 101 void QuicPacketGenerator::OnRttChange(QuicTime::Delta rtt) { |
| 100 fec_timeout_ = rtt.Multiply(kRttMultiplierForFecTimeout); | 102 fec_timeout_ = rtt.Multiply(kRttMultiplierForFecTimeout); |
| 101 } | 103 } |
| 102 | 104 |
| 103 void QuicPacketGenerator::SetShouldSendAck(bool also_send_stop_waiting) { | 105 void QuicPacketGenerator::SetShouldSendAck(bool also_send_stop_waiting) { |
| 104 if (pending_ack_frame_ != nullptr) { | 106 if (ack_queued_) { |
| 105 // Ack already queued, nothing to do. | 107 // Ack already queued, nothing to do. |
| 106 return; | 108 return; |
| 107 } | 109 } |
| 108 | 110 |
| 109 if (also_send_stop_waiting && pending_stop_waiting_frame_ != nullptr) { | 111 if (also_send_stop_waiting && stop_waiting_queued_) { |
| 110 LOG(DFATAL) << "Should only ever be one pending stop waiting frame."; | 112 LOG(DFATAL) << "Should only ever be one pending stop waiting frame."; |
| 111 return; | 113 return; |
| 112 } | 114 } |
| 113 | 115 |
| 114 should_send_ack_ = true; | 116 should_send_ack_ = true; |
| 115 should_send_stop_waiting_ = also_send_stop_waiting; | 117 should_send_stop_waiting_ = also_send_stop_waiting; |
| 116 SendQueuedFrames(false); | 118 SendQueuedFrames(false); |
| 117 } | 119 } |
| 118 | 120 |
| 119 void QuicPacketGenerator::SetShouldSendStopWaiting() { | 121 void QuicPacketGenerator::SetShouldSendStopWaiting() { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 return packet_creator_.HasPendingFrames() || HasPendingFrames(); | 355 return packet_creator_.HasPendingFrames() || HasPendingFrames(); |
| 354 } | 356 } |
| 355 | 357 |
| 356 bool QuicPacketGenerator::HasPendingFrames() const { | 358 bool QuicPacketGenerator::HasPendingFrames() const { |
| 357 return should_send_ack_ || should_send_stop_waiting_ || | 359 return should_send_ack_ || should_send_stop_waiting_ || |
| 358 !queued_control_frames_.empty(); | 360 !queued_control_frames_.empty(); |
| 359 } | 361 } |
| 360 | 362 |
| 361 bool QuicPacketGenerator::AddNextPendingFrame() { | 363 bool QuicPacketGenerator::AddNextPendingFrame() { |
| 362 if (should_send_ack_) { | 364 if (should_send_ack_) { |
| 363 pending_ack_frame_.reset(delegate_->CreateAckFrame()); | 365 delegate_->PopulateAckFrame(&pending_ack_frame_); |
| 366 ack_queued_ = true; |
| 364 // If we can't this add the frame now, then we still need to do so later. | 367 // If we can't this add the frame now, then we still need to do so later. |
| 365 should_send_ack_ = !AddFrame(QuicFrame(pending_ack_frame_.get())); | 368 should_send_ack_ = !AddFrame(QuicFrame(&pending_ack_frame_)); |
| 366 // Return success if we have cleared out this flag (i.e., added the frame). | 369 // Return success if we have cleared out this flag (i.e., added the frame). |
| 367 // If we still need to send, then the frame is full, and we have failed. | 370 // If we still need to send, then the frame is full, and we have failed. |
| 368 return !should_send_ack_; | 371 return !should_send_ack_; |
| 369 } | 372 } |
| 370 | 373 |
| 371 if (should_send_stop_waiting_) { | 374 if (should_send_stop_waiting_) { |
| 372 pending_stop_waiting_frame_.reset(delegate_->CreateStopWaitingFrame()); | 375 delegate_->PopulateStopWaitingFrame(&pending_stop_waiting_frame_); |
| 376 stop_waiting_queued_ = true; |
| 373 // If we can't this add the frame now, then we still need to do so later. | 377 // If we can't this add the frame now, then we still need to do so later. |
| 374 should_send_stop_waiting_ = | 378 should_send_stop_waiting_ = |
| 375 !AddFrame(QuicFrame(pending_stop_waiting_frame_.get())); | 379 !AddFrame(QuicFrame(&pending_stop_waiting_frame_)); |
| 376 // Return success if we have cleared out this flag (i.e., added the frame). | 380 // Return success if we have cleared out this flag (i.e., added the frame). |
| 377 // If we still need to send, then the frame is full, and we have failed. | 381 // If we still need to send, then the frame is full, and we have failed. |
| 378 return !should_send_stop_waiting_; | 382 return !should_send_stop_waiting_; |
| 379 } | 383 } |
| 380 | 384 |
| 381 LOG_IF(DFATAL, queued_control_frames_.empty()) | 385 LOG_IF(DFATAL, queued_control_frames_.empty()) |
| 382 << "AddNextPendingFrame called with no queued control frames."; | 386 << "AddNextPendingFrame called with no queued control frames."; |
| 383 if (!AddFrame(queued_control_frames_.back())) { | 387 if (!AddFrame(queued_control_frames_.back())) { |
| 384 // Packet was full. | 388 // Packet was full. |
| 385 return false; | 389 return false; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 402 | 406 |
| 403 // There may be AckNotifiers interested in this packet. | 407 // There may be AckNotifiers interested in this packet. |
| 404 if (FLAGS_quic_attach_ack_notifiers_to_packets) { | 408 if (FLAGS_quic_attach_ack_notifiers_to_packets) { |
| 405 serialized_packet.notifiers.swap(ack_notifiers_); | 409 serialized_packet.notifiers.swap(ack_notifiers_); |
| 406 ack_notifiers_.clear(); | 410 ack_notifiers_.clear(); |
| 407 } | 411 } |
| 408 | 412 |
| 409 delegate_->OnSerializedPacket(serialized_packet); | 413 delegate_->OnSerializedPacket(serialized_packet); |
| 410 MaybeSendFecPacketAndCloseGroup(/*force=*/false); | 414 MaybeSendFecPacketAndCloseGroup(/*force=*/false); |
| 411 | 415 |
| 412 // The packet has now been serialized, safe to delete pending frames. | 416 // The packet has now been serialized, so the frames are no longer queued. |
| 413 pending_ack_frame_.reset(); | 417 ack_queued_ = false; |
| 414 pending_stop_waiting_frame_.reset(); | 418 stop_waiting_queued_ = false; |
| 415 } | 419 } |
| 416 | 420 |
| 417 void QuicPacketGenerator::StopSendingVersion() { | 421 void QuicPacketGenerator::StopSendingVersion() { |
| 418 packet_creator_.StopSendingVersion(); | 422 packet_creator_.StopSendingVersion(); |
| 419 } | 423 } |
| 420 | 424 |
| 421 QuicPacketSequenceNumber QuicPacketGenerator::sequence_number() const { | 425 QuicPacketSequenceNumber QuicPacketGenerator::sequence_number() const { |
| 422 return packet_creator_.sequence_number(); | 426 return packet_creator_.sequence_number(); |
| 423 } | 427 } |
| 424 | 428 |
| 425 QuicByteCount QuicPacketGenerator::max_packet_length() const { | 429 QuicByteCount QuicPacketGenerator::max_packet_length() const { |
| 426 return packet_creator_.max_packet_length(); | 430 return packet_creator_.max_packet_length(); |
| 427 } | 431 } |
| 428 | 432 |
| 429 void QuicPacketGenerator::set_max_packet_length(QuicByteCount length) { | 433 void QuicPacketGenerator::set_max_packet_length(QuicByteCount length) { |
| 430 packet_creator_.set_max_packet_length(length); | 434 packet_creator_.set_max_packet_length(length); |
| 431 } | 435 } |
| 432 | 436 |
| 433 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket( | 437 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket( |
| 434 const QuicVersionVector& supported_versions) { | 438 const QuicVersionVector& supported_versions) { |
| 435 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions); | 439 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions); |
| 436 } | 440 } |
| 437 | 441 |
| 438 SerializedPacket QuicPacketGenerator::ReserializeAllFrames( | 442 SerializedPacket QuicPacketGenerator::ReserializeAllFrames( |
| 439 const QuicFrames& frames, | 443 const RetransmittableFrames& frames, |
| 440 QuicSequenceNumberLength original_length) { | 444 QuicSequenceNumberLength original_length) { |
| 441 return packet_creator_.ReserializeAllFrames(frames, original_length); | 445 return packet_creator_.ReserializeAllFrames(frames, original_length); |
| 442 } | 446 } |
| 443 | 447 |
| 444 void QuicPacketGenerator::UpdateSequenceNumberLength( | 448 void QuicPacketGenerator::UpdateSequenceNumberLength( |
| 445 QuicPacketSequenceNumber least_packet_awaited_by_peer, | 449 QuicPacketSequenceNumber least_packet_awaited_by_peer, |
| 446 QuicPacketCount max_packets_in_flight) { | 450 QuicPacketCount max_packets_in_flight) { |
| 447 return packet_creator_.UpdateSequenceNumberLength( | 451 return packet_creator_.UpdateSequenceNumberLength( |
| 448 least_packet_awaited_by_peer, max_packets_in_flight); | 452 least_packet_awaited_by_peer, max_packets_in_flight); |
| 449 } | 453 } |
| 450 | 454 |
| 451 void QuicPacketGenerator::SetConnectionIdLength(uint32 length) { | 455 void QuicPacketGenerator::SetConnectionIdLength(uint32 length) { |
| 452 if (length == 0) { | 456 if (length == 0) { |
| 453 packet_creator_.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID); | 457 packet_creator_.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID); |
| 454 } else if (length == 1) { | 458 } else if (length == 1) { |
| 455 packet_creator_.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID); | 459 packet_creator_.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID); |
| 456 } else if (length <= 4) { | 460 } else if (length <= 4) { |
| 457 packet_creator_.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID); | 461 packet_creator_.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID); |
| 458 } else { | 462 } else { |
| 459 packet_creator_.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID); | 463 packet_creator_.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID); |
| 460 } | 464 } |
| 461 } | 465 } |
| 462 | 466 |
| 463 | 467 |
| 464 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) { | 468 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) { |
| 465 packet_creator_.set_encryption_level(level); | 469 packet_creator_.set_encryption_level(level); |
| 466 } | 470 } |
| 467 | 471 |
| 468 } // namespace net | 472 } // namespace net |
| OLD | NEW |