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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 87 |
87 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) { | 88 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) { |
88 queued_control_frames_.push_back(frame); | 89 queued_control_frames_.push_back(frame); |
89 SendQueuedFrames(false); | 90 SendQueuedFrames(false); |
90 } | 91 } |
91 | 92 |
92 QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id, | 93 QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id, |
93 const IOVector& data_to_write, | 94 const IOVector& data_to_write, |
94 QuicStreamOffset offset, | 95 QuicStreamOffset offset, |
95 bool fin, | 96 bool fin, |
| 97 FecProtection fec_protection, |
96 QuicAckNotifier* notifier) { | 98 QuicAckNotifier* notifier) { |
97 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE; | 99 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE; |
98 SendQueuedFrames(false); | 100 SendQueuedFrames(false); |
99 | 101 |
100 size_t total_bytes_consumed = 0; | 102 size_t total_bytes_consumed = 0; |
101 bool fin_consumed = false; | 103 bool fin_consumed = false; |
102 | 104 |
103 if (!packet_creator_->HasRoomForStreamFrame(id, offset)) { | 105 if (!packet_creator_.HasRoomForStreamFrame(id, offset)) { |
104 SerializeAndSendPacket(); | 106 SerializeAndSendPacket(); |
105 } | 107 } |
106 | 108 |
| 109 if (fec_protection == MUST_FEC_PROTECT) { |
| 110 MaybeStartFecProtection(); |
| 111 } |
| 112 |
107 IOVector data = data_to_write; | 113 IOVector data = data_to_write; |
108 size_t data_size = data.TotalBufferSize(); | 114 size_t data_size = data.TotalBufferSize(); |
109 while (delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, | 115 while (delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, |
110 HAS_RETRANSMITTABLE_DATA, handshake)) { | 116 HAS_RETRANSMITTABLE_DATA, handshake)) { |
111 QuicFrame frame; | 117 QuicFrame frame; |
112 size_t bytes_consumed; | 118 size_t bytes_consumed; |
113 if (notifier != NULL) { | 119 if (notifier != NULL) { |
114 // 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. |
115 bytes_consumed = packet_creator_->CreateStreamFrameWithNotifier( | 121 bytes_consumed = packet_creator_.CreateStreamFrameWithNotifier( |
116 id, data, offset + total_bytes_consumed, fin, notifier, &frame); | 122 id, data, offset + total_bytes_consumed, fin, notifier, &frame); |
117 } else { | 123 } else { |
118 bytes_consumed = packet_creator_->CreateStreamFrame( | 124 bytes_consumed = packet_creator_.CreateStreamFrame( |
119 id, data, offset + total_bytes_consumed, fin, &frame); | 125 id, data, offset + total_bytes_consumed, fin, &frame); |
120 } | 126 } |
121 if (!AddFrame(frame)) { | 127 if (!AddFrame(frame)) { |
122 LOG(DFATAL) << "Failed to add stream frame."; | 128 LOG(DFATAL) << "Failed to add stream frame."; |
123 // 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 |
124 // the stream, so it's best to close the connection. | 130 // the stream, so it's best to close the connection. |
125 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false); | 131 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false); |
126 return QuicConsumedData(0, false); | 132 return QuicConsumedData(0, false); |
127 } | 133 } |
128 | 134 |
129 total_bytes_consumed += bytes_consumed; | 135 total_bytes_consumed += bytes_consumed; |
130 fin_consumed = fin && total_bytes_consumed == data_size; | 136 fin_consumed = fin && total_bytes_consumed == data_size; |
131 data.Consume(bytes_consumed); | 137 data.Consume(bytes_consumed); |
132 DCHECK(data.Empty() || packet_creator_->BytesFree() == 0u); | 138 DCHECK(data.Empty() || packet_creator_.BytesFree() == 0u); |
133 | 139 |
134 // TODO(ianswett): Restore packet reordering. | 140 // TODO(ianswett): Restore packet reordering. |
135 if (!InBatchMode() || !packet_creator_->HasRoomForStreamFrame(id, offset)) { | 141 if (!InBatchMode() || !packet_creator_.HasRoomForStreamFrame(id, offset)) { |
136 SerializeAndSendPacket(); | 142 SerializeAndSendPacket(); |
137 } | 143 } |
138 | 144 |
139 if (data.Empty()) { | 145 if (data.Empty()) { |
140 // We're done writing the data. Exit the loop. | 146 // We're done writing the data. Exit the loop. |
141 // 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 |
142 // if we're simply writing a fin. | 148 // if we're simply writing a fin. |
| 149 if (fec_protection == MUST_FEC_PROTECT) { |
| 150 // Turn off FEC protection when we're done writing protected data. |
| 151 DVLOG(1) << "Turning FEC protection OFF"; |
| 152 should_fec_protect_ = false; |
| 153 } |
143 break; | 154 break; |
144 } | 155 } |
145 } | 156 } |
146 | 157 |
147 // 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 |
148 // blocked. If not in batch mode, force close the group. | 159 // blocked. If not in batch mode, force close the group. |
149 MaybeSendFecPacketAndCloseGroup(!InBatchMode()); | 160 MaybeSendFecPacketAndCloseGroup(!InBatchMode()); |
150 | 161 |
151 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); | 162 DCHECK(InBatchMode() || !packet_creator_.HasPendingFrames()); |
152 return QuicConsumedData(total_bytes_consumed, fin_consumed); | 163 return QuicConsumedData(total_bytes_consumed, fin_consumed); |
153 } | 164 } |
154 | 165 |
155 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { | 166 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { |
156 DCHECK(HasPendingFrames()); | 167 DCHECK(HasPendingFrames()); |
157 HasRetransmittableData retransmittable = | 168 HasRetransmittableData retransmittable = |
158 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) | 169 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) |
159 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; | 170 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; |
160 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { | 171 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
161 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. | 172 DCHECK(!queued_control_frames_.empty()); // These are retransmittable. |
162 } | 173 } |
163 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, | 174 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable, |
164 NOT_HANDSHAKE); | 175 NOT_HANDSHAKE); |
165 } | 176 } |
166 | 177 |
167 void QuicPacketGenerator::SendQueuedFrames(bool flush) { | 178 void QuicPacketGenerator::SendQueuedFrames(bool flush) { |
168 // 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. |
169 while (HasPendingFrames() && | 180 while (HasPendingFrames() && |
170 (flush || CanSendWithNextPendingFrameAddition())) { | 181 (flush || CanSendWithNextPendingFrameAddition())) { |
171 if (!AddNextPendingFrame()) { | 182 if (!AddNextPendingFrame()) { |
172 // Packet was full, so serialize and send it. | 183 // Packet was full, so serialize and send it. |
173 SerializeAndSendPacket(); | 184 SerializeAndSendPacket(); |
174 } | 185 } |
175 } | 186 } |
176 | 187 |
177 if (!InBatchMode() || flush) { | 188 if (!InBatchMode() || flush) { |
178 if (packet_creator_->HasPendingFrames()) { | 189 if (packet_creator_.HasPendingFrames()) { |
179 SerializeAndSendPacket(); | 190 SerializeAndSendPacket(); |
180 } | 191 } |
181 // 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 |
182 // writes are pending. | 193 // writes are pending. |
183 MaybeSendFecPacketAndCloseGroup(true); | 194 MaybeSendFecPacketAndCloseGroup(true); |
184 } | 195 } |
185 } | 196 } |
186 | 197 |
187 void QuicPacketGenerator::MaybeStartFecProtection() { | 198 void QuicPacketGenerator::MaybeStartFecProtection() { |
188 if (!packet_creator_->IsFecEnabled()) { | 199 if (!packet_creator_.IsFecEnabled()) { |
189 return; | 200 return; |
190 } | 201 } |
191 DVLOG(1) << "Turning FEC protection ON"; | 202 DVLOG(1) << "Turning FEC protection ON"; |
192 should_fec_protect_ = true; | 203 should_fec_protect_ = true; |
193 if (packet_creator_->IsFecProtected()) { | 204 if (packet_creator_.IsFecProtected()) { |
194 // Only start creator's FEC protection if not already on. | 205 // Only start creator's FEC protection if not already on. |
195 return; | 206 return; |
196 } | 207 } |
197 if (HasQueuedFrames()) { | 208 if (HasQueuedFrames()) { |
198 // TODO(jri): This currently requires that the generator flush out any | 209 // TODO(jri): This currently requires that the generator flush out any |
199 // 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 |
200 // 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 |
201 // generator to check if the resulting expansion still allows the incoming | 212 // generator to check if the resulting expansion still allows the incoming |
202 // frame to be added to the packet. | 213 // frame to be added to the packet. |
203 SendQueuedFrames(true); | 214 SendQueuedFrames(true); |
204 } | 215 } |
205 packet_creator_->StartFecProtectingPackets(); | 216 packet_creator_.StartFecProtectingPackets(); |
206 DCHECK(packet_creator_->IsFecProtected()); | 217 DCHECK(packet_creator_.IsFecProtected()); |
207 } | |
208 | |
209 void QuicPacketGenerator::MaybeStopFecProtection(bool force) { | |
210 DVLOG(1) << "Turning FEC protection OFF"; | |
211 // FEC protection will stop after the next FEC packet is transmitted. | |
212 should_fec_protect_ = false; | |
213 MaybeSendFecPacketAndCloseGroup(force); | |
214 } | 218 } |
215 | 219 |
216 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) { | 220 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) { |
217 if (!packet_creator_->IsFecProtected() || | 221 if (!packet_creator_.IsFecProtected() || |
218 packet_creator_->HasPendingFrames()) { | 222 packet_creator_.HasPendingFrames()) { |
219 return; | 223 return; |
220 } | 224 } |
221 | 225 |
222 if (packet_creator_->ShouldSendFec(force)) { | 226 if (packet_creator_.ShouldSendFec(force)) { |
223 // TODO(jri): SerializeFec can return a NULL packet, and this should | 227 // TODO(jri): SerializeFec can return a NULL packet, and this should |
224 // cause an early return, with a call to | 228 // cause an early return, with a call to |
225 // delegate_->OnPacketGenerationError. | 229 // delegate_->OnPacketGenerationError. |
226 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | 230 SerializedPacket serialized_fec = packet_creator_.SerializeFec(); |
227 DCHECK(serialized_fec.packet); | 231 DCHECK(serialized_fec.packet); |
228 delegate_->OnSerializedPacket(serialized_fec); | 232 delegate_->OnSerializedPacket(serialized_fec); |
229 } | 233 } |
230 | 234 |
231 // 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. |
232 // 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; |
233 // 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. |
234 if (!should_fec_protect_ && !packet_creator_->IsFecGroupOpen()) { | 238 if (!should_fec_protect_ && !packet_creator_.IsFecGroupOpen()) { |
235 packet_creator_->StopFecProtectingPackets(); | 239 packet_creator_.StopFecProtectingPackets(); |
236 DCHECK(!packet_creator_->IsFecProtected()); | 240 DCHECK(!packet_creator_.IsFecProtected()); |
237 } | 241 } |
238 } | 242 } |
239 | 243 |
240 bool QuicPacketGenerator::InBatchMode() { | 244 bool QuicPacketGenerator::InBatchMode() { |
241 return batch_mode_; | 245 return batch_mode_; |
242 } | 246 } |
243 | 247 |
244 void QuicPacketGenerator::StartBatchOperations() { | 248 void QuicPacketGenerator::StartBatchOperations() { |
245 batch_mode_ = true; | 249 batch_mode_ = true; |
246 } | 250 } |
247 | 251 |
248 void QuicPacketGenerator::FinishBatchOperations() { | 252 void QuicPacketGenerator::FinishBatchOperations() { |
249 batch_mode_ = false; | 253 batch_mode_ = false; |
250 SendQueuedFrames(false); | 254 SendQueuedFrames(false); |
251 } | 255 } |
252 | 256 |
253 void QuicPacketGenerator::FlushAllQueuedFrames() { | 257 void QuicPacketGenerator::FlushAllQueuedFrames() { |
254 SendQueuedFrames(true); | 258 SendQueuedFrames(true); |
255 } | 259 } |
256 | 260 |
257 bool QuicPacketGenerator::HasQueuedFrames() const { | 261 bool QuicPacketGenerator::HasQueuedFrames() const { |
258 return packet_creator_->HasPendingFrames() || HasPendingFrames(); | 262 return packet_creator_.HasPendingFrames() || HasPendingFrames(); |
259 } | 263 } |
260 | 264 |
261 bool QuicPacketGenerator::HasPendingFrames() const { | 265 bool QuicPacketGenerator::HasPendingFrames() const { |
262 return should_send_ack_ || should_send_feedback_ || | 266 return should_send_ack_ || should_send_feedback_ || |
263 should_send_stop_waiting_ || !queued_control_frames_.empty(); | 267 should_send_stop_waiting_ || !queued_control_frames_.empty(); |
264 } | 268 } |
265 | 269 |
266 bool QuicPacketGenerator::AddNextPendingFrame() { | 270 bool QuicPacketGenerator::AddNextPendingFrame() { |
267 if (should_send_ack_) { | 271 if (should_send_ack_) { |
268 pending_ack_frame_.reset(delegate_->CreateAckFrame()); | 272 pending_ack_frame_.reset(delegate_->CreateAckFrame()); |
(...skipping 27 matching lines...) Expand all Loading... |
296 << "AddNextPendingFrame called with no queued control frames."; | 300 << "AddNextPendingFrame called with no queued control frames."; |
297 if (!AddFrame(queued_control_frames_.back())) { | 301 if (!AddFrame(queued_control_frames_.back())) { |
298 // Packet was full. | 302 // Packet was full. |
299 return false; | 303 return false; |
300 } | 304 } |
301 queued_control_frames_.pop_back(); | 305 queued_control_frames_.pop_back(); |
302 return true; | 306 return true; |
303 } | 307 } |
304 | 308 |
305 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { | 309 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) { |
306 bool success = packet_creator_->AddSavedFrame(frame); | 310 bool success = packet_creator_.AddSavedFrame(frame); |
307 if (success && debug_delegate_) { | 311 if (success && debug_delegate_) { |
308 debug_delegate_->OnFrameAddedToPacket(frame); | 312 debug_delegate_->OnFrameAddedToPacket(frame); |
309 } | 313 } |
310 return success; | 314 return success; |
311 } | 315 } |
312 | 316 |
313 void QuicPacketGenerator::SerializeAndSendPacket() { | 317 void QuicPacketGenerator::SerializeAndSendPacket() { |
314 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); | 318 SerializedPacket serialized_packet = packet_creator_.SerializePacket(); |
315 DCHECK(serialized_packet.packet); | 319 DCHECK(serialized_packet.packet); |
316 delegate_->OnSerializedPacket(serialized_packet); | 320 delegate_->OnSerializedPacket(serialized_packet); |
317 MaybeSendFecPacketAndCloseGroup(false); | 321 MaybeSendFecPacketAndCloseGroup(false); |
318 } | 322 } |
319 | 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 |
320 } // namespace net | 362 } // namespace net |
OLD | NEW |