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 DebugDelegate* debug_delegate, | 19 DebugDelegate* 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_fec_protect_(false), |
25 should_send_ack_(false), | 26 should_send_ack_(false), |
26 should_send_feedback_(false), | 27 should_send_feedback_(false), |
27 should_send_stop_waiting_(false) { | 28 should_send_stop_waiting_(false) { |
28 } | 29 } |
29 | 30 |
30 QuicPacketGenerator::~QuicPacketGenerator() { | 31 QuicPacketGenerator::~QuicPacketGenerator() { |
31 for (QuicFrames::iterator it = queued_control_frames_.begin(); | 32 for (QuicFrames::iterator it = queued_control_frames_.begin(); |
32 it != queued_control_frames_.end(); ++it) { | 33 it != queued_control_frames_.end(); ++it) { |
33 switch (it->type) { | 34 switch (it->type) { |
34 case PADDING_FRAME: | 35 case PADDING_FRAME: |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 137 } |
137 | 138 |
138 if (data.Empty()) { | 139 if (data.Empty()) { |
139 // We're done writing the data. Exit the loop. | 140 // We're done writing the data. Exit the loop. |
140 // We don't make this a precondition because we could have 0 bytes of data | 141 // We don't make this a precondition because we could have 0 bytes of data |
141 // if we're simply writing a fin. | 142 // if we're simply writing a fin. |
142 break; | 143 break; |
143 } | 144 } |
144 } | 145 } |
145 | 146 |
146 // Ensure the FEC group is closed at the end of this method if not in batch | 147 // Try to close FEC group since we've either run out of data to send or we're |
147 // mode. | 148 // blocked. If not in batch mode, force close the group. |
148 if (!InBatchMode() && packet_creator_->ShouldSendFec(true)) { | 149 MaybeSendFecPacketAndCloseGroup(!InBatchMode()); |
149 // TODO(jri): SerializeFec can return a NULL packet, and this should | |
150 // cause an early return, with a call to delegate_->OnPacketGenerationError. | |
151 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | |
152 DCHECK(serialized_fec.packet); | |
153 delegate_->OnSerializedPacket(serialized_fec); | |
154 } | |
155 | 150 |
156 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); | 151 DCHECK(InBatchMode() || !packet_creator_->HasPendingFrames()); |
157 return QuicConsumedData(total_bytes_consumed, fin_consumed); | 152 return QuicConsumedData(total_bytes_consumed, fin_consumed); |
158 } | 153 } |
159 | 154 |
160 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { | 155 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { |
161 DCHECK(HasPendingFrames()); | 156 DCHECK(HasPendingFrames()); |
162 HasRetransmittableData retransmittable = | 157 HasRetransmittableData retransmittable = |
163 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) | 158 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_) |
164 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; | 159 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA; |
(...skipping 11 matching lines...) Expand all Loading... |
176 if (!AddNextPendingFrame()) { | 171 if (!AddNextPendingFrame()) { |
177 // Packet was full, so serialize and send it. | 172 // Packet was full, so serialize and send it. |
178 SerializeAndSendPacket(); | 173 SerializeAndSendPacket(); |
179 } | 174 } |
180 } | 175 } |
181 | 176 |
182 if (!InBatchMode() || flush) { | 177 if (!InBatchMode() || flush) { |
183 if (packet_creator_->HasPendingFrames()) { | 178 if (packet_creator_->HasPendingFrames()) { |
184 SerializeAndSendPacket(); | 179 SerializeAndSendPacket(); |
185 } | 180 } |
186 | |
187 // Ensure the FEC group is closed at the end of this method unless other | 181 // Ensure the FEC group is closed at the end of this method unless other |
188 // writes are pending. | 182 // writes are pending. |
189 if (packet_creator_->ShouldSendFec(true)) { | 183 MaybeSendFecPacketAndCloseGroup(true); |
190 // TODO(jri): SerializeFec can return a NULL packet, and this should | |
191 // cause an early return, with a call to | |
192 // delegate_->OnPacketGenerationError. | |
193 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | |
194 DCHECK(serialized_fec.packet); | |
195 delegate_->OnSerializedPacket(serialized_fec); | |
196 } | |
197 } | 184 } |
198 } | 185 } |
199 | 186 |
| 187 void QuicPacketGenerator::MaybeStartFecProtection() { |
| 188 if (!packet_creator_->IsFecEnabled()) { |
| 189 return; |
| 190 } |
| 191 DVLOG(1) << "Turning FEC protection ON"; |
| 192 should_fec_protect_ = true; |
| 193 if (packet_creator_->IsFecProtected()) { |
| 194 // Only start creator's FEC protection if not already on. |
| 195 return; |
| 196 } |
| 197 if (HasQueuedFrames()) { |
| 198 // 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 |
| 200 // converted to an FEC protected packet, do it. This will require the |
| 201 // generator to check if the resulting expansion still allows the incoming |
| 202 // frame to be added to the packet. |
| 203 SendQueuedFrames(true); |
| 204 } |
| 205 packet_creator_->StartFecProtectingPackets(); |
| 206 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 } |
| 215 |
| 216 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) { |
| 217 if (!packet_creator_->IsFecProtected() || |
| 218 packet_creator_->HasPendingFrames()) { |
| 219 return; |
| 220 } |
| 221 |
| 222 if (packet_creator_->ShouldSendFec(force)) { |
| 223 // TODO(jri): SerializeFec can return a NULL packet, and this should |
| 224 // cause an early return, with a call to |
| 225 // delegate_->OnPacketGenerationError. |
| 226 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); |
| 227 DCHECK(serialized_fec.packet); |
| 228 delegate_->OnSerializedPacket(serialized_fec); |
| 229 } |
| 230 |
| 231 // 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; |
| 233 // pending frames in the generator will not keep us from turning FEC off. |
| 234 if (!should_fec_protect_ && !packet_creator_->IsFecGroupOpen()) { |
| 235 packet_creator_->StopFecProtectingPackets(); |
| 236 DCHECK(!packet_creator_->IsFecProtected()); |
| 237 } |
| 238 } |
| 239 |
200 bool QuicPacketGenerator::InBatchMode() { | 240 bool QuicPacketGenerator::InBatchMode() { |
201 return batch_mode_; | 241 return batch_mode_; |
202 } | 242 } |
203 | 243 |
204 void QuicPacketGenerator::StartBatchOperations() { | 244 void QuicPacketGenerator::StartBatchOperations() { |
205 batch_mode_ = true; | 245 batch_mode_ = true; |
206 } | 246 } |
207 | 247 |
208 void QuicPacketGenerator::FinishBatchOperations() { | 248 void QuicPacketGenerator::FinishBatchOperations() { |
209 batch_mode_ = false; | 249 batch_mode_ = false; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 if (success && debug_delegate_) { | 307 if (success && debug_delegate_) { |
268 debug_delegate_->OnFrameAddedToPacket(frame); | 308 debug_delegate_->OnFrameAddedToPacket(frame); |
269 } | 309 } |
270 return success; | 310 return success; |
271 } | 311 } |
272 | 312 |
273 void QuicPacketGenerator::SerializeAndSendPacket() { | 313 void QuicPacketGenerator::SerializeAndSendPacket() { |
274 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); | 314 SerializedPacket serialized_packet = packet_creator_->SerializePacket(); |
275 DCHECK(serialized_packet.packet); | 315 DCHECK(serialized_packet.packet); |
276 delegate_->OnSerializedPacket(serialized_packet); | 316 delegate_->OnSerializedPacket(serialized_packet); |
277 | 317 MaybeSendFecPacketAndCloseGroup(false); |
278 if (packet_creator_->ShouldSendFec(false)) { | |
279 // TODO(jri): SerializeFec can return a NULL packet, and this should | |
280 // cause an early return, with a call to delegate_->OnPacketGenerationError. | |
281 SerializedPacket serialized_fec = packet_creator_->SerializeFec(); | |
282 DCHECK(serialized_fec.packet); | |
283 delegate_->OnSerializedPacket(serialized_fec); | |
284 } | |
285 } | 318 } |
286 | 319 |
287 } // namespace net | 320 } // namespace net |
OLD | NEW |