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 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 received_packet_manager_.least_packet_awaited_by_peer() - 1); | 562 received_packet_manager_.least_packet_awaited_by_peer() - 1); |
563 | 563 |
564 sent_packet_manager_.OnIncomingAck(incoming_ack, | 564 sent_packet_manager_.OnIncomingAck(incoming_ack, |
565 time_of_last_received_packet_); | 565 time_of_last_received_packet_); |
566 if (sent_packet_manager_.HasPendingRetransmissions()) { | 566 if (sent_packet_manager_.HasPendingRetransmissions()) { |
567 WriteIfNotBlocked(); | 567 WriteIfNotBlocked(); |
568 } | 568 } |
569 | 569 |
570 // Always reset the retransmission alarm when an ack comes in, since we now | 570 // Always reset the retransmission alarm when an ack comes in, since we now |
571 // have a better estimate of the current rtt than when it was set. | 571 // have a better estimate of the current rtt than when it was set. |
572 retransmission_alarm_->Cancel(); | |
573 QuicTime retransmission_time = | 572 QuicTime retransmission_time = |
574 sent_packet_manager_.GetRetransmissionTime(); | 573 sent_packet_manager_.GetRetransmissionTime(); |
575 if (retransmission_time != QuicTime::Zero()) { | 574 retransmission_alarm_->Update(retransmission_time, |
576 retransmission_alarm_->Set(retransmission_time); | 575 QuicTime::Delta::FromMilliseconds(1)); |
577 } | |
578 } | 576 } |
579 | 577 |
580 void QuicConnection::ProcessStopWaitingFrame( | 578 void QuicConnection::ProcessStopWaitingFrame( |
581 const QuicStopWaitingFrame& stop_waiting) { | 579 const QuicStopWaitingFrame& stop_waiting) { |
582 largest_seen_packet_with_stop_waiting_ = last_header_.packet_sequence_number; | 580 largest_seen_packet_with_stop_waiting_ = last_header_.packet_sequence_number; |
583 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); | 581 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); |
584 // Possibly close any FecGroups which are now irrelevant. | 582 // Possibly close any FecGroups which are now irrelevant. |
585 CloseFecGroupsBefore(stop_waiting.least_unacked + 1); | 583 CloseFecGroupsBefore(stop_waiting.least_unacked + 1); |
586 } | 584 } |
587 | 585 |
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 void QuicConnection::RetransmitUnackedPackets( | 1269 void QuicConnection::RetransmitUnackedPackets( |
1272 RetransmissionType retransmission_type) { | 1270 RetransmissionType retransmission_type) { |
1273 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); | 1271 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); |
1274 | 1272 |
1275 WriteIfNotBlocked(); | 1273 WriteIfNotBlocked(); |
1276 } | 1274 } |
1277 | 1275 |
1278 void QuicConnection::NeuterUnencryptedPackets() { | 1276 void QuicConnection::NeuterUnencryptedPackets() { |
1279 sent_packet_manager_.NeuterUnencryptedPackets(); | 1277 sent_packet_manager_.NeuterUnencryptedPackets(); |
1280 // This may have changed the retransmission timer, so re-arm it. | 1278 // This may have changed the retransmission timer, so re-arm it. |
1281 retransmission_alarm_->Cancel(); | |
1282 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | 1279 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); |
1283 if (retransmission_time != QuicTime::Zero()) { | 1280 retransmission_alarm_->Update(retransmission_time, |
1284 retransmission_alarm_->Set(retransmission_time); | 1281 QuicTime::Delta::FromMilliseconds(1)); |
1285 } | |
1286 } | 1282 } |
1287 | 1283 |
1288 bool QuicConnection::ShouldGeneratePacket( | 1284 bool QuicConnection::ShouldGeneratePacket( |
1289 TransmissionType transmission_type, | 1285 TransmissionType transmission_type, |
1290 HasRetransmittableData retransmittable, | 1286 HasRetransmittableData retransmittable, |
1291 IsHandshake handshake) { | 1287 IsHandshake handshake) { |
1292 // We should serialize handshake packets immediately to ensure that they | 1288 // We should serialize handshake packets immediately to ensure that they |
1293 // end up sent at the right encryption level. | 1289 // end up sent at the right encryption level. |
1294 if (handshake == IS_HANDSHAKE) { | 1290 if (handshake == IS_HANDSHAKE) { |
1295 return true; | 1291 return true; |
1296 } | 1292 } |
1297 | 1293 |
1298 return CanWrite(retransmittable); | 1294 return CanWrite(retransmittable); |
1299 } | 1295 } |
1300 | 1296 |
1301 bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) { | 1297 bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) { |
1302 if (writer_->IsWriteBlocked()) { | 1298 if (writer_->IsWriteBlocked()) { |
1303 visitor_->OnWriteBlocked(); | 1299 visitor_->OnWriteBlocked(); |
1304 return false; | 1300 return false; |
1305 } | 1301 } |
1306 | 1302 |
1307 send_alarm_->Cancel(); | |
1308 QuicTime now = clock_->Now(); | 1303 QuicTime now = clock_->Now(); |
1309 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( | 1304 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( |
1310 now, retransmittable); | 1305 now, retransmittable); |
1311 if (delay.IsInfinite()) { | 1306 if (delay.IsInfinite()) { |
| 1307 send_alarm_->Cancel(); |
1312 return false; | 1308 return false; |
1313 } | 1309 } |
1314 | 1310 |
1315 // If the scheduler requires a delay, then we can not send this packet now. | 1311 // If the scheduler requires a delay, then we can not send this packet now. |
1316 if (!delay.IsZero()) { | 1312 if (!delay.IsZero()) { |
1317 send_alarm_->Set(now.Add(delay)); | 1313 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); |
1318 DVLOG(1) << "Delaying sending."; | 1314 DVLOG(1) << "Delaying sending."; |
1319 return false; | 1315 return false; |
1320 } | 1316 } |
| 1317 send_alarm_->Cancel(); |
1321 return true; | 1318 return true; |
1322 } | 1319 } |
1323 | 1320 |
1324 bool QuicConnection::WritePacket(QueuedPacket packet) { | 1321 bool QuicConnection::WritePacket(QueuedPacket packet) { |
1325 QuicPacketSequenceNumber sequence_number = packet.sequence_number; | 1322 QuicPacketSequenceNumber sequence_number = packet.sequence_number; |
1326 if (ShouldDiscardPacket(packet.encryption_level, | 1323 if (ShouldDiscardPacket(packet.encryption_level, |
1327 sequence_number, | 1324 sequence_number, |
1328 packet.retransmittable)) { | 1325 packet.retransmittable)) { |
1329 ++stats_.packets_discarded; | 1326 ++stats_.packets_discarded; |
1330 return true; | 1327 return true; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 // options by a more explicit API than setting a struct value directly. | 1501 // options by a more explicit API than setting a struct value directly. |
1505 packet_generator_.UpdateSequenceNumberLength( | 1502 packet_generator_.UpdateSequenceNumberLength( |
1506 received_packet_manager_.least_packet_awaited_by_peer(), | 1503 received_packet_manager_.least_packet_awaited_by_peer(), |
1507 sent_packet_manager_.GetCongestionWindow()); | 1504 sent_packet_manager_.GetCongestionWindow()); |
1508 | 1505 |
1509 bool reset_retransmission_alarm = | 1506 bool reset_retransmission_alarm = |
1510 sent_packet_manager_.OnPacketSent(sequence_number, now, length, | 1507 sent_packet_manager_.OnPacketSent(sequence_number, now, length, |
1511 transmission_type, retransmittable); | 1508 transmission_type, retransmittable); |
1512 | 1509 |
1513 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | 1510 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
1514 retransmission_alarm_->Cancel(); | |
1515 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | 1511 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); |
1516 if (retransmission_time != QuicTime::Zero()) { | 1512 retransmission_alarm_->Update(retransmission_time, |
1517 retransmission_alarm_->Set(retransmission_time); | 1513 QuicTime::Delta::FromMilliseconds(1)); |
1518 } | |
1519 } | 1514 } |
1520 | 1515 |
1521 stats_.bytes_sent += result.bytes_written; | 1516 stats_.bytes_sent += result.bytes_written; |
1522 ++stats_.packets_sent; | 1517 ++stats_.packets_sent; |
1523 | 1518 |
1524 if (transmission_type != NOT_RETRANSMISSION) { | 1519 if (transmission_type != NOT_RETRANSMISSION) { |
1525 stats_.bytes_retransmitted += result.bytes_written; | 1520 stats_.bytes_retransmitted += result.bytes_written; |
1526 ++stats_.packets_retransmitted; | 1521 ++stats_.packets_retransmitted; |
1527 } | 1522 } |
1528 | 1523 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1625 // to send new data before retransmitting. | 1620 // to send new data before retransmitting. |
1626 if (sent_packet_manager_.MaybeRetransmitTailLossProbe()) { | 1621 if (sent_packet_manager_.MaybeRetransmitTailLossProbe()) { |
1627 // Send the pending retransmission now that it's been queued. | 1622 // Send the pending retransmission now that it's been queued. |
1628 WriteIfNotBlocked(); | 1623 WriteIfNotBlocked(); |
1629 } | 1624 } |
1630 | 1625 |
1631 // Ensure the retransmission alarm is always set if there are unacked packets | 1626 // Ensure the retransmission alarm is always set if there are unacked packets |
1632 // and nothing waiting to be sent. | 1627 // and nothing waiting to be sent. |
1633 if (!HasQueuedData() && !retransmission_alarm_->IsSet()) { | 1628 if (!HasQueuedData() && !retransmission_alarm_->IsSet()) { |
1634 QuicTime rto_timeout = sent_packet_manager_.GetRetransmissionTime(); | 1629 QuicTime rto_timeout = sent_packet_manager_.GetRetransmissionTime(); |
1635 if (rto_timeout != QuicTime::Zero()) { | 1630 if (rto_timeout.IsInitialized()) { |
1636 retransmission_alarm_->Set(rto_timeout); | 1631 retransmission_alarm_->Set(rto_timeout); |
1637 } | 1632 } |
1638 } | 1633 } |
1639 } | 1634 } |
1640 | 1635 |
1641 void QuicConnection::SetEncrypter(EncryptionLevel level, | 1636 void QuicConnection::SetEncrypter(EncryptionLevel level, |
1642 QuicEncrypter* encrypter) { | 1637 QuicEncrypter* encrypter) { |
1643 framer_.SetEncrypter(level, encrypter); | 1638 framer_.SetEncrypter(level, encrypter); |
1644 } | 1639 } |
1645 | 1640 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1875 IS_HANDSHAKE : NOT_HANDSHAKE; | 1870 IS_HANDSHAKE : NOT_HANDSHAKE; |
1876 // Sending queued packets may have caused the socket to become write blocked, | 1871 // Sending queued packets may have caused the socket to become write blocked, |
1877 // or the congestion manager to prohibit sending. If we've sent everything | 1872 // or the congestion manager to prohibit sending. If we've sent everything |
1878 // we had queued and we're still not blocked, let the visitor know it can | 1873 // we had queued and we're still not blocked, let the visitor know it can |
1879 // write more. | 1874 // write more. |
1880 return ShouldGeneratePacket(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | 1875 return ShouldGeneratePacket(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, |
1881 pending_handshake); | 1876 pending_handshake); |
1882 } | 1877 } |
1883 | 1878 |
1884 void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) { | 1879 void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) { |
| 1880 // Adjust the idle timeout on client and server to prevent clients from |
| 1881 // sending requests to servers which have already closed the connection. |
| 1882 if (is_server_) { |
| 1883 timeout = timeout.Add(QuicTime::Delta::FromSeconds(1)); |
| 1884 } else if (timeout > QuicTime::Delta::FromSeconds(1)) { |
| 1885 timeout = timeout.Subtract(QuicTime::Delta::FromSeconds(1)); |
| 1886 } |
| 1887 |
1885 if (timeout < idle_network_timeout_) { | 1888 if (timeout < idle_network_timeout_) { |
1886 idle_network_timeout_ = timeout; | 1889 idle_network_timeout_ = timeout; |
1887 CheckForTimeout(); | 1890 CheckForTimeout(); |
1888 } else { | 1891 } else { |
1889 idle_network_timeout_ = timeout; | 1892 idle_network_timeout_ = timeout; |
1890 } | 1893 } |
1891 } | 1894 } |
1892 | 1895 |
1893 void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) { | 1896 void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) { |
1894 if (timeout < overall_connection_timeout_) { | 1897 if (timeout < overall_connection_timeout_) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1937 | 1940 |
1938 // Take the min timeout. | 1941 // Take the min timeout. |
1939 QuicTime::Delta connection_timeout = | 1942 QuicTime::Delta connection_timeout = |
1940 overall_connection_timeout_.Subtract(connected_time); | 1943 overall_connection_timeout_.Subtract(connected_time); |
1941 if (connection_timeout < timeout) { | 1944 if (connection_timeout < timeout) { |
1942 timeout = connection_timeout; | 1945 timeout = connection_timeout; |
1943 } | 1946 } |
1944 } | 1947 } |
1945 | 1948 |
1946 timeout_alarm_->Cancel(); | 1949 timeout_alarm_->Cancel(); |
1947 timeout_alarm_->Set(clock_->ApproximateNow().Add(timeout)); | 1950 timeout_alarm_->Set(now.Add(timeout)); |
1948 return false; | 1951 return false; |
1949 } | 1952 } |
1950 | 1953 |
1951 void QuicConnection::SetPingAlarm() { | 1954 void QuicConnection::SetPingAlarm() { |
1952 if (is_server_) { | 1955 if (is_server_) { |
1953 // Only clients send pings. | 1956 // Only clients send pings. |
1954 return; | 1957 return; |
1955 } | 1958 } |
1956 ping_alarm_->Cancel(); | |
1957 if (!visitor_->HasOpenDataStreams()) { | 1959 if (!visitor_->HasOpenDataStreams()) { |
| 1960 ping_alarm_->Cancel(); |
1958 // Don't send a ping unless there are open streams. | 1961 // Don't send a ping unless there are open streams. |
1959 return; | 1962 return; |
1960 } | 1963 } |
1961 QuicTime::Delta ping_timeout = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); | 1964 QuicTime::Delta ping_timeout = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
1962 ping_alarm_->Set(clock_->ApproximateNow().Add(ping_timeout)); | 1965 ping_alarm_->Update(clock_->ApproximateNow().Add(ping_timeout), |
| 1966 QuicTime::Delta::FromSeconds(1)); |
1963 } | 1967 } |
1964 | 1968 |
1965 QuicConnection::ScopedPacketBundler::ScopedPacketBundler( | 1969 QuicConnection::ScopedPacketBundler::ScopedPacketBundler( |
1966 QuicConnection* connection, | 1970 QuicConnection* connection, |
1967 AckBundling send_ack) | 1971 AckBundling send_ack) |
1968 : connection_(connection), | 1972 : connection_(connection), |
1969 already_in_batch_mode_(connection != NULL && | 1973 already_in_batch_mode_(connection != NULL && |
1970 connection->packet_generator_.InBatchMode()) { | 1974 connection->packet_generator_.InBatchMode()) { |
1971 if (connection_ == NULL) { | 1975 if (connection_ == NULL) { |
1972 return; | 1976 return; |
(...skipping 21 matching lines...) Expand all Loading... |
1994 // If we changed the generator's batch state, restore original batch state. | 1998 // If we changed the generator's batch state, restore original batch state. |
1995 if (!already_in_batch_mode_) { | 1999 if (!already_in_batch_mode_) { |
1996 DVLOG(1) << "Leaving Batch Mode."; | 2000 DVLOG(1) << "Leaving Batch Mode."; |
1997 connection_->packet_generator_.FinishBatchOperations(); | 2001 connection_->packet_generator_.FinishBatchOperations(); |
1998 } | 2002 } |
1999 DCHECK_EQ(already_in_batch_mode_, | 2003 DCHECK_EQ(already_in_batch_mode_, |
2000 connection_->packet_generator_.InBatchMode()); | 2004 connection_->packet_generator_.InBatchMode()); |
2001 } | 2005 } |
2002 | 2006 |
2003 } // namespace net | 2007 } // namespace net |
OLD | NEW |