| 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_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 QuicConnection* connection_; | 155 QuicConnection* connection_; |
| 156 | 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(PingAlarm); | 157 DISALLOW_COPY_AND_ASSIGN(PingAlarm); |
| 158 }; | 158 }; |
| 159 | 159 |
| 160 } // namespace | 160 } // namespace |
| 161 | 161 |
| 162 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | 162 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, |
| 163 EncryptionLevel level, | 163 EncryptionLevel level) |
| 164 TransmissionType transmission_type) | |
| 165 : serialized_packet(packet), | 164 : serialized_packet(packet), |
| 166 encryption_level(level), | 165 encryption_level(level), |
| 167 transmission_type(transmission_type) { | 166 transmission_type(NOT_RETRANSMISSION), |
| 167 original_sequence_number(0) { |
| 168 } |
| 169 |
| 170 QuicConnection::QueuedPacket::QueuedPacket( |
| 171 SerializedPacket packet, |
| 172 EncryptionLevel level, |
| 173 TransmissionType transmission_type, |
| 174 QuicPacketSequenceNumber original_sequence_number) |
| 175 : serialized_packet(packet), |
| 176 encryption_level(level), |
| 177 transmission_type(transmission_type), |
| 178 original_sequence_number(original_sequence_number) { |
| 168 } | 179 } |
| 169 | 180 |
| 170 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 181 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 171 | 182 |
| 172 QuicConnection::QuicConnection(QuicConnectionId connection_id, | 183 QuicConnection::QuicConnection(QuicConnectionId connection_id, |
| 173 IPEndPoint address, | 184 IPEndPoint address, |
| 174 QuicConnectionHelperInterface* helper, | 185 QuicConnectionHelperInterface* helper, |
| 175 const PacketWriterFactory& writer_factory, | 186 const PacketWriterFactory& writer_factory, |
| 176 bool owns_writer, | 187 bool owns_writer, |
| 177 bool is_server, | 188 bool is_server, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 253 } |
| 243 | 254 |
| 244 QuicConnection::~QuicConnection() { | 255 QuicConnection::~QuicConnection() { |
| 245 if (owns_writer_) { | 256 if (owns_writer_) { |
| 246 delete writer_; | 257 delete writer_; |
| 247 } | 258 } |
| 248 STLDeleteElements(&undecryptable_packets_); | 259 STLDeleteElements(&undecryptable_packets_); |
| 249 STLDeleteValues(&group_map_); | 260 STLDeleteValues(&group_map_); |
| 250 for (QueuedPacketList::iterator it = queued_packets_.begin(); | 261 for (QueuedPacketList::iterator it = queued_packets_.begin(); |
| 251 it != queued_packets_.end(); ++it) { | 262 it != queued_packets_.end(); ++it) { |
| 263 delete it->serialized_packet.retransmittable_frames; |
| 252 delete it->serialized_packet.packet; | 264 delete it->serialized_packet.packet; |
| 253 } | 265 } |
| 254 } | 266 } |
| 255 | 267 |
| 256 void QuicConnection::SetFromConfig(const QuicConfig& config) { | 268 void QuicConnection::SetFromConfig(const QuicConfig& config) { |
| 257 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); | 269 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); |
| 258 sent_packet_manager_.SetFromConfig(config); | 270 sent_packet_manager_.SetFromConfig(config); |
| 259 } | 271 } |
| 260 | 272 |
| 261 bool QuicConnection::SelectMutualVersion( | 273 bool QuicConnection::SelectMutualVersion( |
| (...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1214 | 1226 |
| 1215 void QuicConnection::WriteQueuedPackets() { | 1227 void QuicConnection::WriteQueuedPackets() { |
| 1216 DCHECK(!writer_->IsWriteBlocked()); | 1228 DCHECK(!writer_->IsWriteBlocked()); |
| 1217 | 1229 |
| 1218 if (pending_version_negotiation_packet_) { | 1230 if (pending_version_negotiation_packet_) { |
| 1219 SendVersionNegotiationPacket(); | 1231 SendVersionNegotiationPacket(); |
| 1220 } | 1232 } |
| 1221 | 1233 |
| 1222 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); | 1234 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); |
| 1223 while (packet_iterator != queued_packets_.end() && | 1235 while (packet_iterator != queued_packets_.end() && |
| 1224 WritePacket(*packet_iterator)) { | 1236 WritePacket(&(*packet_iterator))) { |
| 1225 delete packet_iterator->serialized_packet.packet; | |
| 1226 packet_iterator = queued_packets_.erase(packet_iterator); | 1237 packet_iterator = queued_packets_.erase(packet_iterator); |
| 1227 } | 1238 } |
| 1228 } | 1239 } |
| 1229 | 1240 |
| 1230 void QuicConnection::WritePendingRetransmissions() { | 1241 void QuicConnection::WritePendingRetransmissions() { |
| 1231 // Keep writing as long as there's a pending retransmission which can be | 1242 // Keep writing as long as there's a pending retransmission which can be |
| 1232 // written. | 1243 // written. |
| 1233 while (sent_packet_manager_.HasPendingRetransmissions()) { | 1244 while (sent_packet_manager_.HasPendingRetransmissions()) { |
| 1234 const QuicSentPacketManager::PendingRetransmission pending = | 1245 const QuicSentPacketManager::PendingRetransmission pending = |
| 1235 sent_packet_manager_.NextPendingRetransmission(); | 1246 sent_packet_manager_.NextPendingRetransmission(); |
| 1236 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { | 1247 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 1237 break; | 1248 break; |
| 1238 } | 1249 } |
| 1239 | 1250 |
| 1240 // Re-packetize the frames with a new sequence number for retransmission. | 1251 // Re-packetize the frames with a new sequence number for retransmission. |
| 1241 // Retransmitted data packets do not use FEC, even when it's enabled. | 1252 // Retransmitted data packets do not use FEC, even when it's enabled. |
| 1242 // Retransmitted packets use the same sequence number length as the | 1253 // Retransmitted packets use the same sequence number length as the |
| 1243 // original. | 1254 // original. |
| 1244 // Flush the packet generator before making a new packet. | 1255 // Flush the packet generator before making a new packet. |
| 1245 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1256 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
| 1246 // does not require the creator to be flushed. | 1257 // does not require the creator to be flushed. |
| 1247 packet_generator_.FlushAllQueuedFrames(); | 1258 packet_generator_.FlushAllQueuedFrames(); |
| 1248 SerializedPacket serialized_packet = packet_generator_.ReserializeAllFrames( | 1259 SerializedPacket serialized_packet = packet_generator_.ReserializeAllFrames( |
| 1249 pending.retransmittable_frames.frames(), | 1260 pending.retransmittable_frames.frames(), |
| 1250 pending.sequence_number_length); | 1261 pending.sequence_number_length); |
| 1251 | 1262 |
| 1252 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number | 1263 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number |
| 1253 << " as " << serialized_packet.sequence_number; | 1264 << " as " << serialized_packet.sequence_number; |
| 1254 if (debug_visitor_.get() != NULL) { | 1265 SendOrQueuePacket( |
| 1255 debug_visitor_->OnPacketRetransmitted( | 1266 QueuedPacket(serialized_packet, |
| 1256 pending.sequence_number, serialized_packet.sequence_number); | 1267 pending.retransmittable_frames.encryption_level(), |
| 1257 } | 1268 pending.transmission_type, |
| 1258 sent_packet_manager_.OnRetransmittedPacket( | 1269 pending.sequence_number)); |
| 1259 pending.sequence_number, | |
| 1260 serialized_packet.sequence_number); | |
| 1261 | |
| 1262 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(), | |
| 1263 serialized_packet, | |
| 1264 pending.transmission_type); | |
| 1265 } | 1270 } |
| 1266 } | 1271 } |
| 1267 | 1272 |
| 1268 void QuicConnection::RetransmitUnackedPackets( | 1273 void QuicConnection::RetransmitUnackedPackets( |
| 1269 TransmissionType retransmission_type) { | 1274 TransmissionType retransmission_type) { |
| 1270 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); | 1275 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); |
| 1271 | 1276 |
| 1272 WriteIfNotBlocked(); | 1277 WriteIfNotBlocked(); |
| 1273 } | 1278 } |
| 1274 | 1279 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 // If the scheduler requires a delay, then we can not send this packet now. | 1319 // If the scheduler requires a delay, then we can not send this packet now. |
| 1315 if (!delay.IsZero()) { | 1320 if (!delay.IsZero()) { |
| 1316 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); | 1321 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); |
| 1317 DVLOG(1) << "Delaying sending."; | 1322 DVLOG(1) << "Delaying sending."; |
| 1318 return false; | 1323 return false; |
| 1319 } | 1324 } |
| 1320 send_alarm_->Cancel(); | 1325 send_alarm_->Cancel(); |
| 1321 return true; | 1326 return true; |
| 1322 } | 1327 } |
| 1323 | 1328 |
| 1324 bool QuicConnection::WritePacket(const QueuedPacket& packet) { | 1329 bool QuicConnection::WritePacket(QueuedPacket* packet) { |
| 1325 if (ShouldDiscardPacket(packet)) { | 1330 if (!WritePacketInner(packet)) { |
| 1331 return false; |
| 1332 } |
| 1333 delete packet->serialized_packet.retransmittable_frames; |
| 1334 delete packet->serialized_packet.packet; |
| 1335 packet->serialized_packet.retransmittable_frames = NULL; |
| 1336 packet->serialized_packet.packet = NULL; |
| 1337 return true; |
| 1338 } |
| 1339 |
| 1340 bool QuicConnection::WritePacketInner(QueuedPacket* packet) { |
| 1341 if (ShouldDiscardPacket(*packet)) { |
| 1326 ++stats_.packets_discarded; | 1342 ++stats_.packets_discarded; |
| 1327 return true; | 1343 return true; |
| 1328 } | 1344 } |
| 1329 // Connection close packets are encypted and saved, so don't exit early. | 1345 // Connection close packets are encrypted and saved, so don't exit early. |
| 1330 if (writer_->IsWriteBlocked() && !IsConnectionClose(packet)) { | 1346 if (writer_->IsWriteBlocked() && !IsConnectionClose(*packet)) { |
| 1331 return false; | 1347 return false; |
| 1332 } | 1348 } |
| 1333 | 1349 |
| 1334 QuicPacketSequenceNumber sequence_number = | 1350 QuicPacketSequenceNumber sequence_number = |
| 1335 packet.serialized_packet.sequence_number; | 1351 packet->serialized_packet.sequence_number; |
| 1336 // Some encryption algorithms require the packet sequence numbers not be | 1352 // Some encryption algorithms require the packet sequence numbers not be |
| 1337 // repeated. | 1353 // repeated. |
| 1338 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | 1354 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); |
| 1339 sequence_number_of_last_sent_packet_ = sequence_number; | 1355 sequence_number_of_last_sent_packet_ = sequence_number; |
| 1340 | 1356 |
| 1341 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( | 1357 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( |
| 1342 packet.encryption_level, | 1358 packet->encryption_level, |
| 1343 sequence_number, | 1359 sequence_number, |
| 1344 *packet.serialized_packet.packet); | 1360 *packet->serialized_packet.packet); |
| 1345 if (encrypted == NULL) { | 1361 if (encrypted == NULL) { |
| 1346 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " | 1362 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " |
| 1347 << sequence_number; | 1363 << sequence_number; |
| 1348 // CloseConnection does not send close packet, so no infinite loop here. | 1364 // CloseConnection does not send close packet, so no infinite loop here. |
| 1349 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1365 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
| 1350 return false; | 1366 return false; |
| 1351 } | 1367 } |
| 1352 | 1368 |
| 1353 // Connection close packets are eventually owned by TimeWaitListManager. | 1369 // Connection close packets are eventually owned by TimeWaitListManager. |
| 1354 // Others are deleted at the end of this call. | 1370 // Others are deleted at the end of this call. |
| 1355 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; | 1371 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; |
| 1356 if (IsConnectionClose(packet)) { | 1372 if (IsConnectionClose(*packet)) { |
| 1357 DCHECK(connection_close_packet_.get() == NULL); | 1373 DCHECK(connection_close_packet_.get() == NULL); |
| 1358 connection_close_packet_.reset(encrypted); | 1374 connection_close_packet_.reset(encrypted); |
| 1359 // This assures we won't try to write *forced* packets when blocked. | 1375 // This assures we won't try to write *forced* packets when blocked. |
| 1360 // Return true to stop processing. | 1376 // Return true to stop processing. |
| 1361 if (writer_->IsWriteBlocked()) { | 1377 if (writer_->IsWriteBlocked()) { |
| 1362 visitor_->OnWriteBlocked(); | 1378 visitor_->OnWriteBlocked(); |
| 1363 return true; | 1379 return true; |
| 1364 } | 1380 } |
| 1365 } else { | 1381 } else { |
| 1366 encrypted_deleter.reset(encrypted); | 1382 encrypted_deleter.reset(encrypted); |
| 1367 } | 1383 } |
| 1368 | 1384 |
| 1369 if (!FLAGS_quic_allow_oversized_packets_for_test) { | 1385 if (!FLAGS_quic_allow_oversized_packets_for_test) { |
| 1370 DCHECK_LE(encrypted->length(), kMaxPacketSize); | 1386 DCHECK_LE(encrypted->length(), kMaxPacketSize); |
| 1371 } | 1387 } |
| 1372 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); | 1388 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); |
| 1373 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " | 1389 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " |
| 1374 << (packet.serialized_packet.packet->is_fec_packet() ? "FEC " : | 1390 << (packet->serialized_packet.packet->is_fec_packet() ? "FEC " : |
| 1375 (IsRetransmittable(packet) == HAS_RETRANSMITTABLE_DATA | 1391 (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA |
| 1376 ? "data bearing " : " ack only ")) | 1392 ? "data bearing " : " ack only ")) |
| 1377 << ", encryption level: " | 1393 << ", encryption level: " |
| 1378 << QuicUtils::EncryptionLevelToString(packet.encryption_level) | 1394 << QuicUtils::EncryptionLevelToString(packet->encryption_level) |
| 1379 << ", length:" | 1395 << ", length:" |
| 1380 << packet.serialized_packet.packet->length() | 1396 << packet->serialized_packet.packet->length() |
| 1381 << ", encrypted length:" | 1397 << ", encrypted length:" |
| 1382 << encrypted->length(); | 1398 << encrypted->length(); |
| 1383 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1399 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
| 1384 << QuicUtils::StringToHexASCIIDump( | 1400 << QuicUtils::StringToHexASCIIDump( |
| 1385 packet.serialized_packet.packet->AsStringPiece()); | 1401 packet->serialized_packet.packet->AsStringPiece()); |
| 1386 | 1402 |
| 1387 WriteResult result = writer_->WritePacket(encrypted->data(), | 1403 WriteResult result = writer_->WritePacket(encrypted->data(), |
| 1388 encrypted->length(), | 1404 encrypted->length(), |
| 1389 self_address().address(), | 1405 self_address().address(), |
| 1390 peer_address()); | 1406 peer_address()); |
| 1391 if (result.error_code == ERR_IO_PENDING) { | 1407 if (result.error_code == ERR_IO_PENDING) { |
| 1392 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | 1408 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); |
| 1393 } | 1409 } |
| 1394 if (debug_visitor_.get() != NULL) { | 1410 if (debug_visitor_.get() != NULL) { |
| 1395 // Pass the write result to the visitor. | 1411 // Pass the write result to the visitor. |
| 1396 debug_visitor_->OnPacketSent(sequence_number, | 1412 debug_visitor_->OnPacketSent(sequence_number, |
| 1397 packet.encryption_level, | 1413 packet->encryption_level, |
| 1398 packet.transmission_type, | 1414 packet->transmission_type, |
| 1399 *encrypted, | 1415 *encrypted, |
| 1400 result); | 1416 result); |
| 1401 } | 1417 } |
| 1402 | 1418 |
| 1403 if (result.status == WRITE_STATUS_BLOCKED) { | 1419 if (result.status == WRITE_STATUS_BLOCKED) { |
| 1404 visitor_->OnWriteBlocked(); | 1420 visitor_->OnWriteBlocked(); |
| 1405 // If the socket buffers the the data, then the packet should not | 1421 // If the socket buffers the the data, then the packet should not |
| 1406 // be queued and sent again, which would result in an unnecessary | 1422 // be queued and sent again, which would result in an unnecessary |
| 1407 // duplicate packet being sent. The helper must call OnCanWrite | 1423 // duplicate packet being sent. The helper must call OnCanWrite |
| 1408 // when the write completes, and OnWriteError if an error occurs. | 1424 // when the write completes, and OnWriteError if an error occurs. |
| 1409 if (!writer_->IsWriteBlockedDataBuffered()) { | 1425 if (!writer_->IsWriteBlockedDataBuffered()) { |
| 1410 return false; | 1426 return false; |
| 1411 } | 1427 } |
| 1412 } | 1428 } |
| 1413 QuicTime now = clock_->Now(); | 1429 QuicTime now = clock_->Now(); |
| 1414 if (packet.transmission_type == NOT_RETRANSMISSION) { | 1430 if (packet->transmission_type == NOT_RETRANSMISSION) { |
| 1415 time_of_last_sent_new_packet_ = now; | 1431 time_of_last_sent_new_packet_ = now; |
| 1416 } | 1432 } |
| 1417 SetPingAlarm(); | 1433 SetPingAlarm(); |
| 1418 DVLOG(1) << ENDPOINT << "time of last sent packet: " | 1434 DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| 1419 << now.ToDebuggingValue(); | 1435 << now.ToDebuggingValue(); |
| 1420 | 1436 |
| 1421 // TODO(ianswett): Change the sequence number length and other packet creator | 1437 // TODO(ianswett): Change the sequence number length and other packet creator |
| 1422 // options by a more explicit API than setting a struct value directly, | 1438 // options by a more explicit API than setting a struct value directly, |
| 1423 // perhaps via the NetworkChangeVisitor. | 1439 // perhaps via the NetworkChangeVisitor. |
| 1424 packet_generator_.UpdateSequenceNumberLength( | 1440 packet_generator_.UpdateSequenceNumberLength( |
| 1425 sent_packet_manager_.least_packet_awaited_by_peer(), | 1441 sent_packet_manager_.least_packet_awaited_by_peer(), |
| 1426 sent_packet_manager_.GetCongestionWindow()); | 1442 sent_packet_manager_.GetCongestionWindow()); |
| 1427 | 1443 |
| 1444 if (packet->original_sequence_number == 0) { |
| 1445 sent_packet_manager_.OnSerializedPacket(packet->serialized_packet); |
| 1446 } else { |
| 1447 if (debug_visitor_.get() != NULL) { |
| 1448 debug_visitor_->OnPacketRetransmitted( |
| 1449 packet->original_sequence_number, sequence_number); |
| 1450 } |
| 1451 sent_packet_manager_.OnRetransmittedPacket(packet->original_sequence_number, |
| 1452 sequence_number); |
| 1453 } |
| 1428 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( | 1454 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( |
| 1429 sequence_number, | 1455 sequence_number, |
| 1430 now, | 1456 now, |
| 1431 encrypted->length(), | 1457 encrypted->length(), |
| 1432 packet.transmission_type, | 1458 packet->transmission_type, |
| 1433 IsRetransmittable(packet)); | 1459 IsRetransmittable(*packet)); |
| 1460 // The SentPacketManager now owns the retransmittable frames. |
| 1461 packet->serialized_packet.retransmittable_frames = NULL; |
| 1434 | 1462 |
| 1435 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | 1463 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
| 1436 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), | 1464 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), |
| 1437 QuicTime::Delta::FromMilliseconds(1)); | 1465 QuicTime::Delta::FromMilliseconds(1)); |
| 1438 } | 1466 } |
| 1439 | 1467 |
| 1440 stats_.bytes_sent += result.bytes_written; | 1468 stats_.bytes_sent += result.bytes_written; |
| 1441 ++stats_.packets_sent; | 1469 ++stats_.packets_sent; |
| 1442 if (packet.transmission_type != NOT_RETRANSMISSION) { | 1470 if (packet->transmission_type != NOT_RETRANSMISSION) { |
| 1443 stats_.bytes_retransmitted += result.bytes_written; | 1471 stats_.bytes_retransmitted += result.bytes_written; |
| 1444 ++stats_.packets_retransmitted; | 1472 ++stats_.packets_retransmitted; |
| 1445 } | 1473 } |
| 1446 | 1474 |
| 1447 if (result.status == WRITE_STATUS_ERROR) { | 1475 if (result.status == WRITE_STATUS_ERROR) { |
| 1448 OnWriteError(result.error_code); | 1476 OnWriteError(result.error_code); |
| 1449 return false; | 1477 return false; |
| 1450 } | 1478 } |
| 1451 | 1479 |
| 1452 return true; | 1480 return true; |
| 1453 } | 1481 } |
| 1454 | 1482 |
| 1455 bool QuicConnection::ShouldDiscardPacket(const QueuedPacket& packet) { | 1483 bool QuicConnection::ShouldDiscardPacket(const QueuedPacket& packet) { |
| 1456 if (!connected_) { | 1484 if (!connected_) { |
| 1457 DVLOG(1) << ENDPOINT | 1485 DVLOG(1) << ENDPOINT |
| 1458 << "Not sending packet as connection is disconnected."; | 1486 << "Not sending packet as connection is disconnected."; |
| 1459 return true; | 1487 return true; |
| 1460 } | 1488 } |
| 1461 | 1489 |
| 1462 QuicPacketSequenceNumber sequence_number = | 1490 QuicPacketSequenceNumber sequence_number = |
| 1463 packet.serialized_packet.sequence_number; | 1491 packet.serialized_packet.sequence_number; |
| 1464 // If the packet has been discarded before sending, don't send it. | |
| 1465 // This occurs if a packet gets serialized, queued, then discarded. | |
| 1466 if (!sent_packet_manager_.IsUnacked(sequence_number)) { | |
| 1467 DVLOG(1) << ENDPOINT << "Dropping packet before sending: " | |
| 1468 << sequence_number << " since it has already been discarded."; | |
| 1469 return true; | |
| 1470 } | |
| 1471 | |
| 1472 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && | 1492 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && |
| 1473 packet.encryption_level == ENCRYPTION_NONE) { | 1493 packet.encryption_level == ENCRYPTION_NONE) { |
| 1474 // Drop packets that are NULL encrypted since the peer won't accept them | 1494 // Drop packets that are NULL encrypted since the peer won't accept them |
| 1475 // anymore. | 1495 // anymore. |
| 1476 DVLOG(1) << ENDPOINT << "Dropping NULL encrypted packet: " | 1496 DVLOG(1) << ENDPOINT << "Dropping NULL encrypted packet: " |
| 1477 << sequence_number << " since the connection is forward secure."; | 1497 << sequence_number << " since the connection is forward secure."; |
| 1478 LOG_IF(DFATAL, | |
| 1479 sent_packet_manager_.HasRetransmittableFrames(sequence_number)) | |
| 1480 << "Once forward secure, all NULL encrypted packets should be " | |
| 1481 << "neutered."; | |
| 1482 return true; | 1498 return true; |
| 1483 } | 1499 } |
| 1484 | 1500 |
| 1501 // If a retransmission has been acked before sending, don't send it. |
| 1502 // This occurs if a packet gets serialized, queued, then discarded. |
| 1485 if (packet.transmission_type != NOT_RETRANSMISSION && | 1503 if (packet.transmission_type != NOT_RETRANSMISSION && |
| 1486 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1504 (!sent_packet_manager_.IsUnacked(packet.original_sequence_number) || |
| 1505 !sent_packet_manager_.HasRetransmittableFrames( |
| 1506 packet.original_sequence_number))) { |
| 1487 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number | 1507 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number |
| 1488 << " A previous transmission was acked while write blocked."; | 1508 << " A previous transmission was acked while write blocked."; |
| 1489 return true; | 1509 return true; |
| 1490 } | 1510 } |
| 1491 | 1511 |
| 1492 return false; | 1512 return false; |
| 1493 } | 1513 } |
| 1494 | 1514 |
| 1495 void QuicConnection::OnWriteError(int error_code) { | 1515 void QuicConnection::OnWriteError(int error_code) { |
| 1496 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code | 1516 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code |
| 1497 << " (" << ErrorToString(error_code) << ")"; | 1517 << " (" << ErrorToString(error_code) << ")"; |
| 1498 // We can't send an error as the socket is presumably borked. | 1518 // We can't send an error as the socket is presumably borked. |
| 1499 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1519 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
| 1500 } | 1520 } |
| 1501 | 1521 |
| 1502 void QuicConnection::OnSerializedPacket( | 1522 void QuicConnection::OnSerializedPacket( |
| 1503 const SerializedPacket& serialized_packet) { | 1523 const SerializedPacket& serialized_packet) { |
| 1504 if (serialized_packet.retransmittable_frames) { | 1524 if (serialized_packet.retransmittable_frames) { |
| 1505 serialized_packet.retransmittable_frames-> | 1525 serialized_packet.retransmittable_frames-> |
| 1506 set_encryption_level(encryption_level_); | 1526 set_encryption_level(encryption_level_); |
| 1507 } | 1527 } |
| 1508 sent_packet_manager_.OnSerializedPacket(serialized_packet); | 1528 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); |
| 1509 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions | |
| 1510 // serialize packets and invoke SendOrQueuePacket directly. | |
| 1511 SendOrQueuePacket(encryption_level_, serialized_packet, NOT_RETRANSMISSION); | |
| 1512 } | 1529 } |
| 1513 | 1530 |
| 1514 void QuicConnection::OnCongestionWindowChange(QuicByteCount congestion_window) { | 1531 void QuicConnection::OnCongestionWindowChange(QuicByteCount congestion_window) { |
| 1515 packet_generator_.OnCongestionWindowChange(congestion_window); | 1532 packet_generator_.OnCongestionWindowChange(congestion_window); |
| 1516 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); | 1533 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); |
| 1517 } | 1534 } |
| 1518 | 1535 |
| 1519 void QuicConnection::OnHandshakeComplete() { | 1536 void QuicConnection::OnHandshakeComplete() { |
| 1520 sent_packet_manager_.SetHandshakeConfirmed(); | 1537 sent_packet_manager_.SetHandshakeConfirmed(); |
| 1521 } | 1538 } |
| 1522 | 1539 |
| 1523 void QuicConnection::SendOrQueuePacket(EncryptionLevel level, | 1540 void QuicConnection::SendOrQueuePacket(QueuedPacket packet) { |
| 1524 const SerializedPacket& packet, | |
| 1525 TransmissionType transmission_type) { | |
| 1526 // The caller of this function is responsible for checking CanWrite(). | 1541 // The caller of this function is responsible for checking CanWrite(). |
| 1527 if (packet.packet == NULL) { | 1542 if (packet.serialized_packet.packet == NULL) { |
| 1528 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; | 1543 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; |
| 1529 return; | 1544 return; |
| 1530 } | 1545 } |
| 1531 | 1546 |
| 1532 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, | 1547 sent_entropy_manager_.RecordPacketEntropyHash( |
| 1533 packet.entropy_hash); | 1548 packet.serialized_packet.sequence_number, |
| 1534 QueuedPacket queued_packet(packet, level, transmission_type); | 1549 packet.serialized_packet.entropy_hash); |
| 1535 LOG_IF(DFATAL, !queued_packets_.empty() && !writer_->IsWriteBlocked()) | 1550 LOG_IF(DFATAL, !queued_packets_.empty() && !writer_->IsWriteBlocked()) |
| 1536 << "Packets should only be left queued if we're write blocked."; | 1551 << "Packets should only be left queued if we're write blocked."; |
| 1537 if (WritePacket(queued_packet)) { | 1552 if (!WritePacket(&packet)) { |
| 1538 delete packet.packet; | 1553 queued_packets_.push_back(packet); |
| 1539 } else { | |
| 1540 queued_packets_.push_back(queued_packet); | |
| 1541 } | 1554 } |
| 1542 } | 1555 } |
| 1543 | 1556 |
| 1544 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { | 1557 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { |
| 1545 stop_waiting->least_unacked = GetLeastUnacked(); | 1558 stop_waiting->least_unacked = GetLeastUnacked(); |
| 1546 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( | 1559 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( |
| 1547 stop_waiting->least_unacked - 1); | 1560 stop_waiting->least_unacked - 1); |
| 1548 } | 1561 } |
| 1549 | 1562 |
| 1550 void QuicConnection::SendPing() { | 1563 void QuicConnection::SendPing() { |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1886 } else { | 1899 } else { |
| 1887 overall_connection_timeout_ = timeout; | 1900 overall_connection_timeout_ = timeout; |
| 1888 } | 1901 } |
| 1889 } | 1902 } |
| 1890 | 1903 |
| 1891 bool QuicConnection::CheckForTimeout() { | 1904 bool QuicConnection::CheckForTimeout() { |
| 1892 QuicTime now = clock_->ApproximateNow(); | 1905 QuicTime now = clock_->ApproximateNow(); |
| 1893 QuicTime time_of_last_packet = max(time_of_last_received_packet_, | 1906 QuicTime time_of_last_packet = max(time_of_last_received_packet_, |
| 1894 time_of_last_sent_new_packet_); | 1907 time_of_last_sent_new_packet_); |
| 1895 | 1908 |
| 1896 // If no packets have been sent or recieved, then don't timeout. | 1909 // If no packets have been sent or received, then don't timeout. |
| 1897 if (FLAGS_quic_timeouts_require_activity && | 1910 if (FLAGS_quic_timeouts_require_activity && |
| 1898 !time_of_last_packet.IsInitialized()) { | 1911 !time_of_last_packet.IsInitialized()) { |
| 1899 timeout_alarm_->Cancel(); | 1912 timeout_alarm_->Cancel(); |
| 1900 timeout_alarm_->Set(now.Add(idle_network_timeout_)); | 1913 timeout_alarm_->Set(now.Add(idle_network_timeout_)); |
| 1901 return false; | 1914 return false; |
| 1902 } | 1915 } |
| 1903 | 1916 |
| 1904 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| | 1917 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
| 1905 // is accurate time. However, this should not change the behavior of | 1918 // is accurate time. However, this should not change the behavior of |
| 1906 // timeout handling. | 1919 // timeout handling. |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2019 } | 2032 } |
| 2020 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { | 2033 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { |
| 2021 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { | 2034 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { |
| 2022 return true; | 2035 return true; |
| 2023 } | 2036 } |
| 2024 } | 2037 } |
| 2025 return false; | 2038 return false; |
| 2026 } | 2039 } |
| 2027 | 2040 |
| 2028 } // namespace net | 2041 } // namespace net |
| OLD | NEW |