| 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_fec_group.h" | 9 #include "net/quic/quic_fec_group.h" |
| 10 #include "net/quic/quic_utils.h" | 10 #include "net/quic/quic_utils.h" |
| 11 | 11 |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 class QuicAckNotifier; | 16 class QuicAckNotifier; |
| 17 | 17 |
| 18 QuicPacketGenerator::QuicPacketGenerator(DelegateInterface* delegate, | 18 QuicPacketGenerator::QuicPacketGenerator(DelegateInterface* delegate, |
| 19 DebugDelegateInterface* debug_delegate, | 19 DebugDelegateInterface* debug_delegate, |
| 20 QuicPacketCreator* creator) | 20 QuicPacketCreator* creator) |
| 21 : delegate_(delegate), | 21 : delegate_(delegate), |
| 22 debug_delegate_(debug_delegate), | 22 debug_delegate_(debug_delegate), |
| 23 packet_creator_(creator), | 23 packet_creator_(creator), |
| 24 batch_mode_(false), | 24 batch_mode_(false), |
| 25 should_send_ack_(false), | 25 should_send_ack_(false), |
| 26 should_send_feedback_(false) { | 26 should_send_feedback_(false), |
| 27 should_send_stop_waiting_(false) { |
| 27 } | 28 } |
| 28 | 29 |
| 29 QuicPacketGenerator::~QuicPacketGenerator() { | 30 QuicPacketGenerator::~QuicPacketGenerator() { |
| 30 for (QuicFrames::iterator it = queued_control_frames_.begin(); | 31 for (QuicFrames::iterator it = queued_control_frames_.begin(); |
| 31 it != queued_control_frames_.end(); ++it) { | 32 it != queued_control_frames_.end(); ++it) { |
| 32 switch (it->type) { | 33 switch (it->type) { |
| 33 case PADDING_FRAME: | 34 case PADDING_FRAME: |
| 34 delete it->padding_frame; | 35 delete it->padding_frame; |
| 35 break; | 36 break; |
| 36 case STREAM_FRAME: | 37 case STREAM_FRAME: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 50 break; | 51 break; |
| 51 case GOAWAY_FRAME: | 52 case GOAWAY_FRAME: |
| 52 delete it->goaway_frame; | 53 delete it->goaway_frame; |
| 53 break; | 54 break; |
| 54 case WINDOW_UPDATE_FRAME: | 55 case WINDOW_UPDATE_FRAME: |
| 55 delete it->window_update_frame; | 56 delete it->window_update_frame; |
| 56 break; | 57 break; |
| 57 case BLOCKED_FRAME: | 58 case BLOCKED_FRAME: |
| 58 delete it->blocked_frame; | 59 delete it->blocked_frame; |
| 59 break; | 60 break; |
| 61 case STOP_WAITING_FRAME: |
| 62 delete it->stop_waiting_frame; |
| 63 break; |
| 60 case NUM_FRAME_TYPES: | 64 case NUM_FRAME_TYPES: |
| 61 DCHECK(false) << "Cannot delete type: " << it->type; | 65 DCHECK(false) << "Cannot delete type: " << it->type; |
| 62 } | 66 } |
| 63 } | 67 } |
| 64 } | 68 } |
| 65 | 69 |
| 66 void QuicPacketGenerator::SetShouldSendAck(bool also_send_feedback) { | 70 void QuicPacketGenerator::SetShouldSendAck(bool also_send_feedback, |
| 71 bool also_send_stop_waiting) { |
| 67 should_send_ack_ = true; | 72 should_send_ack_ = true; |
| 68 should_send_feedback_ = also_send_feedback; | 73 should_send_feedback_ = also_send_feedback; |
| 74 should_send_stop_waiting_ = also_send_stop_waiting; |
| 69 SendQueuedFrames(false); | 75 SendQueuedFrames(false); |
| 70 } | 76 } |
| 71 | 77 |
| 78 void QuicPacketGenerator::SetShouldSendStopWaiting() { |
| 79 should_send_stop_waiting_ = true; |
| 80 SendQueuedFrames(false); |
| 81 } |
| 82 |
| 72 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) { | 83 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) { |
| 73 queued_control_frames_.push_back(frame); | 84 queued_control_frames_.push_back(frame); |
| 74 SendQueuedFrames(false); | 85 SendQueuedFrames(false); |
| 75 } | 86 } |
| 76 | 87 |
| 77 QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id, | 88 QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id, |
| 78 const IOVector& data_to_write, | 89 const IOVector& data_to_write, |
| 79 QuicStreamOffset offset, | 90 QuicStreamOffset offset, |
| 80 bool fin, | 91 bool fin, |
| 81 QuicAckNotifier* notifier) { | 92 QuicAckNotifier* notifier) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 delegate_->OnSerializedPacket(serialized_fec); | 151 delegate_->OnSerializedPacket(serialized_fec); |
| 141 } | 152 } |
| 142 | 153 |
| 143 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); | 154 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); |
| 144 return QuicConsumedData(total_bytes_consumed, fin_consumed); | 155 return QuicConsumedData(total_bytes_consumed, fin_consumed); |
| 145 } | 156 } |
| 146 | 157 |
| 147 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { | 158 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { |
| 148 DCHECK(HasPendingFrames()); | 159 DCHECK(HasPendingFrames()); |
| 149 HasRetransmittableData retransmittable = | 160 HasRetransmittableData retransmittable = |
| 150 (should_send_ack_ || should_send_feedback_) ? NO_RETRANSMITTABLE_DATA | 161 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) |
| 151 : HAS_RETRANSMITTABLE_DATA; | 162 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; |
| 152 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { | 163 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
| 153 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. | 164 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. |
| 154 } | 165 } |
| 155 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, | 166 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, |
| 156 NOT_HANDSHAKE); | 167 NOT_HANDSHAKE); |
| 157 } | 168 } |
| 158 | 169 |
| 159 void QuicPacketGenerator::SendQueuedFrames(bool flush) { | 170 void QuicPacketGenerator::SendQueuedFrames(bool flush) { |
| 160 // Only add pending frames if we are SURE we can then send the whole packet. | 171 // Only add pending frames if we are SURE we can then send the whole packet. |
| 161 while (HasPendingFrames() && | 172 while (HasPendingFrames() && |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 void QuicPacketGenerator::FlushAllQueuedFrames() { | 208 void QuicPacketGenerator::FlushAllQueuedFrames() { |
| 198 SendQueuedFrames(true); | 209 SendQueuedFrames(true); |
| 199 } | 210 } |
| 200 | 211 |
| 201 bool QuicPacketGenerator::HasQueuedFrames() const { | 212 bool QuicPacketGenerator::HasQueuedFrames() const { |
| 202 return packet_creator_->HasPendingFrames() || HasPendingFrames(); | 213 return packet_creator_->HasPendingFrames() || HasPendingFrames(); |
| 203 } | 214 } |
| 204 | 215 |
| 205 bool QuicPacketGenerator::HasPendingFrames() const { | 216 bool QuicPacketGenerator::HasPendingFrames() const { |
| 206 return should_send_ack_ || should_send_feedback_ || | 217 return should_send_ack_ || should_send_feedback_ || |
| 207 !queued_control_frames_.empty(); | 218 should_send_stop_waiting_ || !queued_control_frames_.empty(); |
| 208 } | 219 } |
| 209 | 220 |
| 210 bool QuicPacketGenerator::AddNextPendingFrame() { | 221 bool QuicPacketGenerator::AddNextPendingFrame() { |
| 211 if (should_send_ack_) { | 222 if (should_send_ack_) { |
| 212 pending_ack_frame_.reset(delegate_->CreateAckFrame()); | 223 pending_ack_frame_.reset(delegate_->CreateAckFrame()); |
| 213 // If we can't this add the frame now, then we still need to do so later. | 224 // If we can't this add the frame now, then we still need to do so later. |
| 214 should_send_ack_ = !AddFrame(QuicFrame(pending_ack_frame_.get())); | 225 should_send_ack_ = !AddFrame(QuicFrame(pending_ack_frame_.get())); |
| 215 // Return success if we have cleared out this flag (i.e., added the frame). | 226 // Return success if we have cleared out this flag (i.e., added the frame). |
| 216 // If we still need to send, then the frame is full, and we have failed. | 227 // If we still need to send, then the frame is full, and we have failed. |
| 217 return !should_send_ack_; | 228 return !should_send_ack_; |
| 218 } | 229 } |
| 219 | 230 |
| 220 if (should_send_feedback_) { | 231 if (should_send_feedback_) { |
| 221 pending_feedback_frame_.reset(delegate_->CreateFeedbackFrame()); | 232 pending_feedback_frame_.reset(delegate_->CreateFeedbackFrame()); |
| 222 // If we can't this add the frame now, then we still need to do so later. | 233 // If we can't this add the frame now, then we still need to do so later. |
| 223 should_send_feedback_ = !AddFrame(QuicFrame(pending_feedback_frame_.get())); | 234 should_send_feedback_ = !AddFrame(QuicFrame(pending_feedback_frame_.get())); |
| 224 // Return success if we have cleared out this flag (i.e., added the frame). | 235 // Return success if we have cleared out this flag (i.e., added the frame). |
| 225 // If we still need to send, then the frame is full, and we have failed. | 236 // If we still need to send, then the frame is full, and we have failed. |
| 226 return !should_send_feedback_; | 237 return !should_send_feedback_; |
| 227 } | 238 } |
| 228 | 239 |
| 229 if (queued_control_frames_.empty()) { | 240 if (should_send_stop_waiting_) { |
| 230 LOG(DFATAL) << "AddNextPendingFrame called with no queued control frames."; | 241 pending_stop_waiting_frame_.reset(delegate_->CreateStopWaitingFrame()); |
| 242 // If we can't this add the frame now, then we still need to do so later. |
| 243 should_send_stop_waiting_ = |
| 244 !AddFrame(QuicFrame(pending_stop_waiting_frame_.get())); |
| 245 // Return success if we have cleared out this flag (i.e., added the frame). |
| 246 // If we still need to send, then the frame is full, and we have failed. |
| 247 return !should_send_stop_waiting_; |
| 231 } | 248 } |
| 249 |
| 250 LOG_IF(DFATAL, queued_control_frames_.empty()) |
| 251 << "AddNextPendingFrame called with no queued control frames."; |
| 232 if (!AddFrame(queued_control_frames_.back())) { | 252 if (!AddFrame(queued_control_frames_.back())) { |
| 233 // Packet was full. | 253 // Packet was full. |
| 234 return false; | 254 return false; |
| 235 } | 255 } |
| 236 queued_control_frames_.pop_back(); | 256 queued_control_frames_.pop_back(); |
| 237 return true; | 257 return true; |
| 238 } | 258 } |
| 239 | 259 |
| 240 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { | 260 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { |
| 241 bool success = packet_creator_->AddSavedFrame(frame); | 261 bool success = packet_creator_->AddSavedFrame(frame); |
| 242 if (success && debug_delegate_) { | 262 if (success && debug_delegate_) { |
| 243 debug_delegate_->OnFrameAddedToPacket(frame); | 263 debug_delegate_->OnFrameAddedToPacket(frame); |
| 244 } | 264 } |
| 245 return success; | 265 return success; |
| 246 } | 266 } |
| 247 | 267 |
| 248 void QuicPacketGenerator::SerializeAndSendPacket() { | 268 void QuicPacketGenerator::SerializeAndSendPacket() { |
| 249 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); | 269 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); |
| 250 DCHECK(serialized_packet.packet); | 270 DCHECK(serialized_packet.packet); |
| 251 delegate_->OnSerializedPacket(serialized_packet); | 271 delegate_->OnSerializedPacket(serialized_packet); |
| 252 | 272 |
| 253 if (packet_creator_->ShouldSendFec(false)) { | 273 if (packet_creator_->ShouldSendFec(false)) { |
| 254 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | 274 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); |
| 255 DCHECK(serialized_fec.packet); | 275 DCHECK(serialized_fec.packet); |
| 256 delegate_->OnSerializedPacket(serialized_fec); | 276 delegate_->OnSerializedPacket(serialized_fec); |
| 257 } | 277 } |
| 258 } | 278 } |
| 259 | 279 |
| 260 } // namespace net | 280 } // namespace net |
| OLD | NEW |