| 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(QuicConnectionId connection_id, |
| 19 DebugDelegate* debug_delegate, | 19 QuicFramer* framer, |
| 20 QuicPacketCreator* creator) | 20 QuicRandom* random_generator, |
| 21 DelegateInterface* delegate) |
| 21 : delegate_(delegate), | 22 : delegate_(delegate), |
| 22 debug_delegate_(debug_delegate), | 23 debug_delegate_(NULL), |
| 23 packet_creator_(creator), | 24 packet_creator_(connection_id, framer, random_generator), |
| 24 batch_mode_(false), | 25 batch_mode_(false), |
| 25 should_fec_protect_(false), | 26 should_fec_protect_(false), |
| 26 should_send_ack_(false), | 27 should_send_ack_(false), |
| 27 should_send_feedback_(false), | 28 should_send_feedback_(false), |
| 28 should_send_stop_waiting_(false) { | 29 should_send_stop_waiting_(false) { |
| 29 } | 30 } |
| 30 | 31 |
| 31 QuicPacketGenerator::~QuicPacketGenerator() { | 32 QuicPacketGenerator::~QuicPacketGenerator() { |
| 32 for (QuicFrames::iterator it = queued_control_frames_.begin(); | 33 for (QuicFrames::iterator it = queued_control_frames_.begin(); |
| 33 it != queued_control_frames_.end(); ++it) { | 34 it != queued_control_frames_.end(); ++it) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 QuicStreamOffset offset, | 95 QuicStreamOffset offset, |
| 95 bool fin, | 96 bool fin, |
| 96 FecProtection fec_protection, | 97 FecProtection fec_protection, |
| 97 QuicAckNotifier* notifier) { | 98 QuicAckNotifier* notifier) { |
| 98 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE; | 99 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE; |
| 99 SendQueuedFrames(false); | 100 SendQueuedFrames(false); |
| 100 | 101 |
| 101 size_t total_bytes_consumed = 0; | 102 size_t total_bytes_consumed = 0; |
| 102 bool fin_consumed = false; | 103 bool fin_consumed = false; |
| 103 | 104 |
| 104 if (!packet_creator_->HasRoomForStreamFrame(id, offset)) { | 105 if (!packet_creator_.HasRoomForStreamFrame(id, offset)) { |
| 105 SerializeAndSendPacket(); | 106 SerializeAndSendPacket(); |
| 106 } | 107 } |
| 107 | 108 |
| 108 if (fec_protection == MUST_FEC_PROTECT) { | 109 if (fec_protection == MUST_FEC_PROTECT) { |
| 109 MaybeStartFecProtection(); | 110 MaybeStartFecProtection(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 IOVector data = data_to_write; | 113 IOVector data = data_to_write; |
| 113 size_t data_size = data.TotalBufferSize(); | 114 size_t data_size = data.TotalBufferSize(); |
| 114 while (delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, | 115 while (delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, |
| 115 HAS_RETRANSMITTABLE_DATA, handshake)) { | 116 HAS_RETRANSMITTABLE_DATA, handshake)) { |
| 116 QuicFrame frame; | 117 QuicFrame frame; |
| 117 size_t bytes_consumed; | 118 size_t bytes_consumed; |
| 118 if (notifier != NULL) { | 119 if (notifier != NULL) { |
| 119 // We want to track which packet this stream frame ends up in. | 120 // We want to track which packet this stream frame ends up in. |
| 120 bytes_consumed = packet_creator_->CreateStreamFrameWithNotifier( | 121 bytes_consumed = packet_creator_.CreateStreamFrameWithNotifier( |
| 121 id, data, offset + total_bytes_consumed, fin, notifier, &frame); | 122 id, data, offset + total_bytes_consumed, fin, notifier, &frame); |
| 122 } else { | 123 } else { |
| 123 bytes_consumed = packet_creator_->CreateStreamFrame( | 124 bytes_consumed = packet_creator_.CreateStreamFrame( |
| 124 id, data, offset + total_bytes_consumed, fin, &frame); | 125 id, data, offset + total_bytes_consumed, fin, &frame); |
| 125 } | 126 } |
| 126 if (!AddFrame(frame)) { | 127 if (!AddFrame(frame)) { |
| 127 LOG(DFATAL) << "Failed to add stream frame."; | 128 LOG(DFATAL) << "Failed to add stream frame."; |
| 128 // Inability to add a STREAM frame creates an unrecoverable hole in a | 129 // Inability to add a STREAM frame creates an unrecoverable hole in a |
| 129 // the stream, so it's best to close the connection. | 130 // the stream, so it's best to close the connection. |
| 130 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false); | 131 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false); |
| 131 return QuicConsumedData(0, false); | 132 return QuicConsumedData(0, false); |
| 132 } | 133 } |
| 133 | 134 |
| 134 total_bytes_consumed += bytes_consumed; | 135 total_bytes_consumed += bytes_consumed; |
| 135 fin_consumed = fin && total_bytes_consumed == data_size; | 136 fin_consumed = fin && total_bytes_consumed == data_size; |
| 136 data.Consume(bytes_consumed); | 137 data.Consume(bytes_consumed); |
| 137 DCHECK(data.Empty() || packet_creator_->BytesFree() == 0u); | 138 DCHECK(data.Empty() || packet_creator_.BytesFree() == 0u); |
| 138 | 139 |
| 139 // TODO(ianswett): Restore packet reordering. | 140 // TODO(ianswett): Restore packet reordering. |
| 140 if (!InBatchMode() || !packet_creator_->HasRoomForStreamFrame(id, offset)) { | 141 if (!InBatchMode() || !packet_creator_.HasRoomForStreamFrame(id, offset)) { |
| 141 SerializeAndSendPacket(); | 142 SerializeAndSendPacket(); |
| 142 } | 143 } |
| 143 | 144 |
| 144 if (data.Empty()) { | 145 if (data.Empty()) { |
| 145 // We're done writing the data. Exit the loop. | 146 // We're done writing the data. Exit the loop. |
| 146 // We don't make this a precondition because we could have 0 bytes of data | 147 // We don't make this a precondition because we could have 0 bytes of data |
| 147 // if we're simply writing a fin. | 148 // if we're simply writing a fin. |
| 148 if (fec_protection == MUST_FEC_PROTECT) { | 149 if (fec_protection == MUST_FEC_PROTECT) { |
| 149 // Turn off FEC protection when we're done writing protected data. | 150 // Turn off FEC protection when we're done writing protected data. |
| 150 DVLOG(1) << "Turning FEC protection OFF"; | 151 DVLOG(1) << "Turning FEC protection OFF"; |
| 151 should_fec_protect_ = false; | 152 should_fec_protect_ = false; |
| 152 } | 153 } |
| 153 break; | 154 break; |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 | 157 |
| 157 // Try to close FEC group since we've either run out of data to send or we're | 158 // Try to close FEC group since we've either run out of data to send or we're |
| 158 // blocked. If not in batch mode, force close the group. | 159 // blocked. If not in batch mode, force close the group. |
| 159 MaybeSendFecPacketAndCloseGroup(!InBatchMode()); | 160 MaybeSendFecPacketAndCloseGroup(!InBatchMode()); |
| 160 | 161 |
| 161 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); | 162 DCHECK(InBatchMode() || !packet_creator_.HasPendingFrames()); |
| 162 return QuicConsumedData(total_bytes_consumed, fin_consumed); | 163 return QuicConsumedData(total_bytes_consumed, fin_consumed); |
| 163 } | 164 } |
| 164 | 165 |
| 165 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { | 166 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { |
| 166 DCHECK(HasPendingFrames()); | 167 DCHECK(HasPendingFrames()); |
| 167 HasRetransmittableData retransmittable = | 168 HasRetransmittableData retransmittable = |
| 168 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) | 169 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) |
| 169 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; | 170 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; |
| 170 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { | 171 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
| 171 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. | 172 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. |
| 172 } | 173 } |
| 173 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, | 174 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, |
| 174 NOT_HANDSHAKE); | 175 NOT_HANDSHAKE); |
| 175 } | 176 } |
| 176 | 177 |
| 177 void QuicPacketGenerator::SendQueuedFrames(bool flush) { | 178 void QuicPacketGenerator::SendQueuedFrames(bool flush) { |
| 178 // Only add pending frames if we are SURE we can then send the whole packet. | 179 // Only add pending frames if we are SURE we can then send the whole packet. |
| 179 while (HasPendingFrames() && | 180 while (HasPendingFrames() && |
| 180 (flush || CanSendWithNextPendingFrameAddition())) { | 181 (flush || CanSendWithNextPendingFrameAddition())) { |
| 181 if (!AddNextPendingFrame()) { | 182 if (!AddNextPendingFrame()) { |
| 182 // Packet was full, so serialize and send it. | 183 // Packet was full, so serialize and send it. |
| 183 SerializeAndSendPacket(); | 184 SerializeAndSendPacket(); |
| 184 } | 185 } |
| 185 } | 186 } |
| 186 | 187 |
| 187 if (!InBatchMode() || flush) { | 188 if (!InBatchMode() || flush) { |
| 188 if (packet_creator_->HasPendingFrames()) { | 189 if (packet_creator_.HasPendingFrames()) { |
| 189 SerializeAndSendPacket(); | 190 SerializeAndSendPacket(); |
| 190 } | 191 } |
| 191 // Ensure the FEC group is closed at the end of this method unless other | 192 // Ensure the FEC group is closed at the end of this method unless other |
| 192 // writes are pending. | 193 // writes are pending. |
| 193 MaybeSendFecPacketAndCloseGroup(true); | 194 MaybeSendFecPacketAndCloseGroup(true); |
| 194 } | 195 } |
| 195 } | 196 } |
| 196 | 197 |
| 197 void QuicPacketGenerator::MaybeStartFecProtection() { | 198 void QuicPacketGenerator::MaybeStartFecProtection() { |
| 198 if (!packet_creator_->IsFecEnabled()) { | 199 if (!packet_creator_.IsFecEnabled()) { |
| 199 return; | 200 return; |
| 200 } | 201 } |
| 201 DVLOG(1) << "Turning FEC protection ON"; | 202 DVLOG(1) << "Turning FEC protection ON"; |
| 202 should_fec_protect_ = true; | 203 should_fec_protect_ = true; |
| 203 if (packet_creator_->IsFecProtected()) { | 204 if (packet_creator_.IsFecProtected()) { |
| 204 // Only start creator's FEC protection if not already on. | 205 // Only start creator's FEC protection if not already on. |
| 205 return; | 206 return; |
| 206 } | 207 } |
| 207 if (HasQueuedFrames()) { | 208 if (HasQueuedFrames()) { |
| 208 // TODO(jri): This currently requires that the generator flush out any | 209 // TODO(jri): This currently requires that the generator flush out any |
| 209 // pending frames when FEC protection is turned on. If current packet can be | 210 // pending frames when FEC protection is turned on. If current packet can be |
| 210 // converted to an FEC protected packet, do it. This will require the | 211 // converted to an FEC protected packet, do it. This will require the |
| 211 // generator to check if the resulting expansion still allows the incoming | 212 // generator to check if the resulting expansion still allows the incoming |
| 212 // frame to be added to the packet. | 213 // frame to be added to the packet. |
| 213 SendQueuedFrames(true); | 214 SendQueuedFrames(true); |
| 214 } | 215 } |
| 215 packet_creator_->StartFecProtectingPackets(); | 216 packet_creator_.StartFecProtectingPackets(); |
| 216 DCHECK(packet_creator_->IsFecProtected()); | 217 DCHECK(packet_creator_.IsFecProtected()); |
| 217 } | 218 } |
| 218 | 219 |
| 219 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) { | 220 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) { |
| 220 if (!packet_creator_->IsFecProtected() || | 221 if (!packet_creator_.IsFecProtected() || |
| 221 packet_creator_->HasPendingFrames()) { | 222 packet_creator_.HasPendingFrames()) { |
| 222 return; | 223 return; |
| 223 } | 224 } |
| 224 | 225 |
| 225 if (packet_creator_->ShouldSendFec(force)) { | 226 if (packet_creator_.ShouldSendFec(force)) { |
| 226 // TODO(jri): SerializeFec can return a NULL packet, and this should | 227 // TODO(jri): SerializeFec can return a NULL packet, and this should |
| 227 // cause an early return, with a call to | 228 // cause an early return, with a call to |
| 228 // delegate_->OnPacketGenerationError. | 229 // delegate_->OnPacketGenerationError. |
| 229 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | 230 SerializedPacket serialized_fec = packet_creator_.SerializeFec(); |
| 230 DCHECK(serialized_fec.packet); | 231 DCHECK(serialized_fec.packet); |
| 231 delegate_->OnSerializedPacket(serialized_fec); | 232 delegate_->OnSerializedPacket(serialized_fec); |
| 232 } | 233 } |
| 233 | 234 |
| 234 // Turn FEC protection off if the creator does not have an FEC group open. | 235 // Turn FEC protection off if the creator does not have an FEC group open. |
| 235 // Note: We only wait until the frames queued in the creator are flushed; | 236 // Note: We only wait until the frames queued in the creator are flushed; |
| 236 // pending frames in the generator will not keep us from turning FEC off. | 237 // pending frames in the generator will not keep us from turning FEC off. |
| 237 if (!should_fec_protect_ && !packet_creator_->IsFecGroupOpen()) { | 238 if (!should_fec_protect_ && !packet_creator_.IsFecGroupOpen()) { |
| 238 packet_creator_->StopFecProtectingPackets(); | 239 packet_creator_.StopFecProtectingPackets(); |
| 239 DCHECK(!packet_creator_->IsFecProtected()); | 240 DCHECK(!packet_creator_.IsFecProtected()); |
| 240 } | 241 } |
| 241 } | 242 } |
| 242 | 243 |
| 243 bool QuicPacketGenerator::InBatchMode() { | 244 bool QuicPacketGenerator::InBatchMode() { |
| 244 return batch_mode_; | 245 return batch_mode_; |
| 245 } | 246 } |
| 246 | 247 |
| 247 void QuicPacketGenerator::StartBatchOperations() { | 248 void QuicPacketGenerator::StartBatchOperations() { |
| 248 batch_mode_ = true; | 249 batch_mode_ = true; |
| 249 } | 250 } |
| 250 | 251 |
| 251 void QuicPacketGenerator::FinishBatchOperations() { | 252 void QuicPacketGenerator::FinishBatchOperations() { |
| 252 batch_mode_ = false; | 253 batch_mode_ = false; |
| 253 SendQueuedFrames(false); | 254 SendQueuedFrames(false); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void QuicPacketGenerator::FlushAllQueuedFrames() { | 257 void QuicPacketGenerator::FlushAllQueuedFrames() { |
| 257 SendQueuedFrames(true); | 258 SendQueuedFrames(true); |
| 258 } | 259 } |
| 259 | 260 |
| 260 bool QuicPacketGenerator::HasQueuedFrames() const { | 261 bool QuicPacketGenerator::HasQueuedFrames() const { |
| 261 return packet_creator_->HasPendingFrames() || HasPendingFrames(); | 262 return packet_creator_.HasPendingFrames() || HasPendingFrames(); |
| 262 } | 263 } |
| 263 | 264 |
| 264 bool QuicPacketGenerator::HasPendingFrames() const { | 265 bool QuicPacketGenerator::HasPendingFrames() const { |
| 265 return should_send_ack_ || should_send_feedback_ || | 266 return should_send_ack_ || should_send_feedback_ || |
| 266 should_send_stop_waiting_ || !queued_control_frames_.empty(); | 267 should_send_stop_waiting_ || !queued_control_frames_.empty(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 bool QuicPacketGenerator::AddNextPendingFrame() { | 270 bool QuicPacketGenerator::AddNextPendingFrame() { |
| 270 if (should_send_ack_) { | 271 if (should_send_ack_) { |
| 271 pending_ack_frame_.reset(delegate_->CreateAckFrame()); | 272 pending_ack_frame_.reset(delegate_->CreateAckFrame()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 299 << "AddNextPendingFrame called with no queued control frames."; | 300 << "AddNextPendingFrame called with no queued control frames."; |
| 300 if (!AddFrame(queued_control_frames_.back())) { | 301 if (!AddFrame(queued_control_frames_.back())) { |
| 301 // Packet was full. | 302 // Packet was full. |
| 302 return false; | 303 return false; |
| 303 } | 304 } |
| 304 queued_control_frames_.pop_back(); | 305 queued_control_frames_.pop_back(); |
| 305 return true; | 306 return true; |
| 306 } | 307 } |
| 307 | 308 |
| 308 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { | 309 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { |
| 309 bool success = packet_creator_->AddSavedFrame(frame); | 310 bool success = packet_creator_.AddSavedFrame(frame); |
| 310 if (success && debug_delegate_) { | 311 if (success && debug_delegate_) { |
| 311 debug_delegate_->OnFrameAddedToPacket(frame); | 312 debug_delegate_->OnFrameAddedToPacket(frame); |
| 312 } | 313 } |
| 313 return success; | 314 return success; |
| 314 } | 315 } |
| 315 | 316 |
| 316 void QuicPacketGenerator::SerializeAndSendPacket() { | 317 void QuicPacketGenerator::SerializeAndSendPacket() { |
| 317 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); | 318 SerializedPacket serialized_packet = packet_creator_.SerializePacket(); |
| 318 DCHECK(serialized_packet.packet); | 319 DCHECK(serialized_packet.packet); |
| 319 delegate_->OnSerializedPacket(serialized_packet); | 320 delegate_->OnSerializedPacket(serialized_packet); |
| 320 MaybeSendFecPacketAndCloseGroup(false); | 321 MaybeSendFecPacketAndCloseGroup(false); |
| 321 } | 322 } |
| 322 | 323 |
| 324 void QuicPacketGenerator::StopSendingVersion() { |
| 325 packet_creator_.StopSendingVersion(); |
| 326 } |
| 327 |
| 328 QuicPacketSequenceNumber QuicPacketGenerator::sequence_number() const { |
| 329 return packet_creator_.sequence_number(); |
| 330 } |
| 331 |
| 332 size_t QuicPacketGenerator::max_packet_length() const { |
| 333 return packet_creator_.max_packet_length(); |
| 334 } |
| 335 |
| 336 void QuicPacketGenerator::set_max_packet_length(size_t length) { |
| 337 packet_creator_.set_max_packet_length(length); |
| 338 } |
| 339 |
| 340 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket( |
| 341 const QuicVersionVector& supported_versions) { |
| 342 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions); |
| 343 } |
| 344 |
| 345 SerializedPacket QuicPacketGenerator::ReserializeAllFrames( |
| 346 const QuicFrames& frames, |
| 347 QuicSequenceNumberLength original_length) { |
| 348 return packet_creator_.ReserializeAllFrames(frames, original_length); |
| 349 } |
| 350 |
| 351 void QuicPacketGenerator::UpdateSequenceNumberLength( |
| 352 QuicPacketSequenceNumber least_packet_awaited_by_peer, |
| 353 QuicByteCount congestion_window) { |
| 354 return packet_creator_.UpdateSequenceNumberLength( |
| 355 least_packet_awaited_by_peer, congestion_window); |
| 356 } |
| 357 |
| 358 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) { |
| 359 packet_creator_.set_encryption_level(level); |
| 360 } |
| 361 |
| 323 } // namespace net | 362 } // namespace net |
| OLD | NEW |