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 1371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1382 << encrypted->length(); | 1382 << encrypted->length(); |
1383 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1383 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
1384 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); | 1384 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); |
1385 | 1385 |
1386 DCHECK(encrypted->length() <= kMaxPacketSize || | 1386 DCHECK(encrypted->length() <= kMaxPacketSize || |
1387 FLAGS_quic_allow_oversized_packets_for_test) | 1387 FLAGS_quic_allow_oversized_packets_for_test) |
1388 << "Packet " << sequence_number << " will not be read; too large: " | 1388 << "Packet " << sequence_number << " will not be read; too large: " |
1389 << packet.packet->length() << " " << encrypted->length() << " " | 1389 << packet.packet->length() << " " << encrypted->length() << " " |
1390 << " close: " << (packet.type == CONNECTION_CLOSE ? "yes" : "no"); | 1390 << " close: " << (packet.type == CONNECTION_CLOSE ? "yes" : "no"); |
1391 | 1391 |
1392 DCHECK(pending_write_.get() == NULL); | |
1393 pending_write_.reset(new QueuedPacket(packet)); | |
1394 | |
1395 WriteResult result = writer_->WritePacket(encrypted->data(), | 1392 WriteResult result = writer_->WritePacket(encrypted->data(), |
1396 encrypted->length(), | 1393 encrypted->length(), |
1397 self_address().address(), | 1394 self_address().address(), |
1398 peer_address()); | 1395 peer_address()); |
1399 if (result.error_code == ERR_IO_PENDING) { | 1396 if (result.error_code == ERR_IO_PENDING) { |
1400 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | 1397 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); |
1401 } | 1398 } |
1402 if (debug_visitor_.get() != NULL) { | 1399 if (debug_visitor_.get() != NULL) { |
1403 // Pass the write result to the visitor. | 1400 // Pass the write result to the visitor. |
1404 debug_visitor_->OnPacketSent(sequence_number, | 1401 debug_visitor_->OnPacketSent(sequence_number, |
1405 packet.encryption_level, | 1402 packet.encryption_level, |
1406 packet.transmission_type, | 1403 packet.transmission_type, |
1407 *encrypted, | 1404 *encrypted, |
1408 result); | 1405 result); |
1409 } | 1406 } |
| 1407 |
1410 if (result.status == WRITE_STATUS_BLOCKED) { | 1408 if (result.status == WRITE_STATUS_BLOCKED) { |
1411 visitor_->OnWriteBlocked(); | 1409 visitor_->OnWriteBlocked(); |
1412 // If the socket buffers the the data, then the packet should not | 1410 // If the socket buffers the the data, then the packet should not |
1413 // be queued and sent again, which would result in an unnecessary | 1411 // be queued and sent again, which would result in an unnecessary |
1414 // duplicate packet being sent. The helper must call OnPacketSent | 1412 // duplicate packet being sent. The helper must call OnPacketSent |
1415 // when the packet is actually sent. | 1413 // when the packet is actually sent. |
1416 if (writer_->IsWriteBlockedDataBuffered()) { | 1414 if (!writer_->IsWriteBlockedDataBuffered()) { |
1417 return true; | 1415 return false; |
1418 } | 1416 } |
1419 pending_write_.reset(); | 1417 } |
1420 return false; | 1418 QuicTime now = clock_->Now(); |
| 1419 if (packet.transmission_type == NOT_RETRANSMISSION) { |
| 1420 time_of_last_sent_new_packet_ = now; |
| 1421 } |
| 1422 SetPingAlarm(); |
| 1423 DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| 1424 << now.ToDebuggingValue(); |
| 1425 |
| 1426 // TODO(ianswett): Change the sequence number length and other packet creator |
| 1427 // options by a more explicit API than setting a struct value directly, |
| 1428 // perhaps via the NetworkChangeVisitor. |
| 1429 packet_generator_.UpdateSequenceNumberLength( |
| 1430 sent_packet_manager_.least_packet_awaited_by_peer(), |
| 1431 sent_packet_manager_.GetCongestionWindow()); |
| 1432 |
| 1433 bool reset_retransmission_alarm = |
| 1434 sent_packet_manager_.OnPacketSent(sequence_number, |
| 1435 now, |
| 1436 encrypted->length(), |
| 1437 packet.transmission_type, |
| 1438 packet.retransmittable); |
| 1439 |
| 1440 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
| 1441 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), |
| 1442 QuicTime::Delta::FromMilliseconds(1)); |
1421 } | 1443 } |
1422 | 1444 |
1423 if (OnPacketSent(result)) { | 1445 stats_.bytes_sent += result.bytes_written; |
1424 return true; | 1446 ++stats_.packets_sent; |
| 1447 if (packet.transmission_type != NOT_RETRANSMISSION) { |
| 1448 stats_.bytes_retransmitted += result.bytes_written; |
| 1449 ++stats_.packets_retransmitted; |
1425 } | 1450 } |
1426 return false; | 1451 |
| 1452 return OnPacketSent(result); |
1427 } | 1453 } |
1428 | 1454 |
1429 bool QuicConnection::ShouldDiscardPacket( | 1455 bool QuicConnection::ShouldDiscardPacket( |
1430 EncryptionLevel level, | 1456 EncryptionLevel level, |
1431 QuicPacketSequenceNumber sequence_number, | 1457 QuicPacketSequenceNumber sequence_number, |
1432 HasRetransmittableData retransmittable) { | 1458 HasRetransmittableData retransmittable) { |
1433 if (!connected_) { | 1459 if (!connected_) { |
1434 DVLOG(1) << ENDPOINT | 1460 DVLOG(1) << ENDPOINT |
1435 << "Not sending packet as connection is disconnected."; | 1461 << "Not sending packet as connection is disconnected."; |
1436 return true; | 1462 return true; |
(...skipping 24 matching lines...) Expand all Loading... |
1461 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1487 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { |
1462 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number | 1488 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number |
1463 << " A previous transmission was acked while write blocked."; | 1489 << " A previous transmission was acked while write blocked."; |
1464 return true; | 1490 return true; |
1465 } | 1491 } |
1466 | 1492 |
1467 return false; | 1493 return false; |
1468 } | 1494 } |
1469 | 1495 |
1470 bool QuicConnection::OnPacketSent(WriteResult result) { | 1496 bool QuicConnection::OnPacketSent(WriteResult result) { |
1471 DCHECK_NE(WRITE_STATUS_BLOCKED, result.status); | |
1472 if (pending_write_.get() == NULL) { | |
1473 LOG(DFATAL) << "OnPacketSent called without a pending write."; | |
1474 return false; | |
1475 } | |
1476 | |
1477 QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number; | |
1478 TransmissionType transmission_type = pending_write_->transmission_type; | |
1479 HasRetransmittableData retransmittable = pending_write_->retransmittable; | |
1480 size_t length = pending_write_->length; | |
1481 pending_write_.reset(); | |
1482 | |
1483 if (result.status == WRITE_STATUS_ERROR) { | 1497 if (result.status == WRITE_STATUS_ERROR) { |
1484 DVLOG(1) << ENDPOINT << "Write failed with error: " << result.error_code | 1498 DVLOG(1) << ENDPOINT << "Write failed with error: " << result.error_code |
1485 << " (" << ErrorToString(result.error_code) << ")"; | 1499 << " (" << ErrorToString(result.error_code) << ")"; |
1486 // We can't send an error as the socket is presumably borked. | 1500 // We can't send an error as the socket is presumably borked. |
1487 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1501 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
1488 return false; | 1502 return false; |
1489 } | 1503 } |
1490 | 1504 |
1491 QuicTime now = clock_->Now(); | |
1492 if (transmission_type == NOT_RETRANSMISSION) { | |
1493 time_of_last_sent_new_packet_ = now; | |
1494 } | |
1495 SetPingAlarm(); | |
1496 DVLOG(1) << ENDPOINT << "time of last sent packet: " | |
1497 << now.ToDebuggingValue(); | |
1498 | |
1499 // TODO(ianswett): Change the sequence number length and other packet creator | |
1500 // options by a more explicit API than setting a struct value directly. | |
1501 packet_generator_.UpdateSequenceNumberLength( | |
1502 sent_packet_manager_.least_packet_awaited_by_peer(), | |
1503 sent_packet_manager_.GetCongestionWindow()); | |
1504 | |
1505 bool reset_retransmission_alarm = | |
1506 sent_packet_manager_.OnPacketSent(sequence_number, now, length, | |
1507 transmission_type, retransmittable); | |
1508 | |
1509 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | |
1510 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | |
1511 retransmission_alarm_->Update(retransmission_time, | |
1512 QuicTime::Delta::FromMilliseconds(1)); | |
1513 } | |
1514 | |
1515 stats_.bytes_sent += result.bytes_written; | |
1516 ++stats_.packets_sent; | |
1517 | |
1518 if (transmission_type != NOT_RETRANSMISSION) { | |
1519 stats_.bytes_retransmitted += result.bytes_written; | |
1520 ++stats_.packets_retransmitted; | |
1521 } | |
1522 | |
1523 return true; | 1505 return true; |
1524 } | 1506 } |
1525 | 1507 |
1526 bool QuicConnection::OnSerializedPacket( | 1508 bool QuicConnection::OnSerializedPacket( |
1527 const SerializedPacket& serialized_packet) { | 1509 const SerializedPacket& serialized_packet) { |
1528 if (serialized_packet.retransmittable_frames) { | 1510 if (serialized_packet.retransmittable_frames) { |
1529 serialized_packet.retransmittable_frames-> | 1511 serialized_packet.retransmittable_frames-> |
1530 set_encryption_level(encryption_level_); | 1512 set_encryption_level(encryption_level_); |
1531 } | 1513 } |
1532 sent_packet_manager_.OnSerializedPacket(serialized_packet); | 1514 sent_packet_manager_.OnSerializedPacket(serialized_packet); |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2009 // If we changed the generator's batch state, restore original batch state. | 1991 // If we changed the generator's batch state, restore original batch state. |
2010 if (!already_in_batch_mode_) { | 1992 if (!already_in_batch_mode_) { |
2011 DVLOG(1) << "Leaving Batch Mode."; | 1993 DVLOG(1) << "Leaving Batch Mode."; |
2012 connection_->packet_generator_.FinishBatchOperations(); | 1994 connection_->packet_generator_.FinishBatchOperations(); |
2013 } | 1995 } |
2014 DCHECK_EQ(already_in_batch_mode_, | 1996 DCHECK_EQ(already_in_batch_mode_, |
2015 connection_->packet_generator_.InBatchMode()); | 1997 connection_->packet_generator_.InBatchMode()); |
2016 } | 1998 } |
2017 | 1999 |
2018 } // namespace net | 2000 } // namespace net |
OLD | NEW |