| 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 1349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1360 // This assures we won't try to write *forced* packets when blocked. | 1360 // This assures we won't try to write *forced* packets when blocked. |
| 1361 // Return true to stop processing. | 1361 // Return true to stop processing. |
| 1362 if (writer_->IsWriteBlocked()) { | 1362 if (writer_->IsWriteBlocked()) { |
| 1363 visitor_->OnWriteBlocked(); | 1363 visitor_->OnWriteBlocked(); |
| 1364 return true; | 1364 return true; |
| 1365 } | 1365 } |
| 1366 } else { | 1366 } else { |
| 1367 encrypted_deleter.reset(encrypted); | 1367 encrypted_deleter.reset(encrypted); |
| 1368 } | 1368 } |
| 1369 | 1369 |
| 1370 LOG_IF(DFATAL, encrypted->length() > | 1370 if (!FLAGS_quic_allow_oversized_packets_for_test) { |
| 1371 packet_generator_.max_packet_length()) | 1371 DCHECK_LE(encrypted->length(), kMaxPacketSize); |
| 1372 << "Writing an encrypted packet larger than max_packet_length:" | 1372 } |
| 1373 << packet_generator_.max_packet_length() << " encrypted length: " | 1373 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); |
| 1374 << encrypted->length(); | |
| 1375 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number | 1374 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number |
| 1376 << " : " << (packet.packet->is_fec_packet() ? "FEC " : | 1375 << " : " << (packet.packet->is_fec_packet() ? "FEC " : |
| 1377 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA | 1376 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA |
| 1378 ? "data bearing " : " ack only ")) | 1377 ? "data bearing " : " ack only ")) |
| 1379 << ", encryption level: " | 1378 << ", encryption level: " |
| 1380 << QuicUtils::EncryptionLevelToString(packet.encryption_level) | 1379 << QuicUtils::EncryptionLevelToString(packet.encryption_level) |
| 1381 << ", length:" << packet.packet->length() << ", encrypted length:" | 1380 << ", length:" << packet.packet->length() << ", encrypted length:" |
| 1382 << encrypted->length(); | 1381 << encrypted->length(); |
| 1383 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1382 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
| 1384 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); | 1383 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); |
| 1385 | 1384 |
| 1386 DCHECK(encrypted->length() <= kMaxPacketSize || | |
| 1387 FLAGS_quic_allow_oversized_packets_for_test) | |
| 1388 << "Packet " << sequence_number << " will not be read; too large: " | |
| 1389 << packet.packet->length() << " " << encrypted->length() << " " | |
| 1390 << " close: " << (packet.type == CONNECTION_CLOSE ? "yes" : "no"); | |
| 1391 | |
| 1392 WriteResult result = writer_->WritePacket(encrypted->data(), | 1385 WriteResult result = writer_->WritePacket(encrypted->data(), |
| 1393 encrypted->length(), | 1386 encrypted->length(), |
| 1394 self_address().address(), | 1387 self_address().address(), |
| 1395 peer_address()); | 1388 peer_address()); |
| 1396 if (result.error_code == ERR_IO_PENDING) { | 1389 if (result.error_code == ERR_IO_PENDING) { |
| 1397 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | 1390 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); |
| 1398 } | 1391 } |
| 1399 if (debug_visitor_.get() != NULL) { | 1392 if (debug_visitor_.get() != NULL) { |
| 1400 // Pass the write result to the visitor. | 1393 // Pass the write result to the visitor. |
| 1401 debug_visitor_->OnPacketSent(sequence_number, | 1394 debug_visitor_->OnPacketSent(sequence_number, |
| 1402 packet.encryption_level, | 1395 packet.encryption_level, |
| 1403 packet.transmission_type, | 1396 packet.transmission_type, |
| 1404 *encrypted, | 1397 *encrypted, |
| 1405 result); | 1398 result); |
| 1406 } | 1399 } |
| 1407 | 1400 |
| 1408 if (result.status == WRITE_STATUS_BLOCKED) { | 1401 if (result.status == WRITE_STATUS_BLOCKED) { |
| 1409 visitor_->OnWriteBlocked(); | 1402 visitor_->OnWriteBlocked(); |
| 1410 // If the socket buffers the the data, then the packet should not | 1403 // If the socket buffers the the data, then the packet should not |
| 1411 // be queued and sent again, which would result in an unnecessary | 1404 // be queued and sent again, which would result in an unnecessary |
| 1412 // duplicate packet being sent. The helper must call OnPacketSent | 1405 // duplicate packet being sent. The helper must call OnCanWrite |
| 1413 // when the packet is actually sent. | 1406 // when the write completes, and OnWriteError if an error occurs. |
| 1414 if (!writer_->IsWriteBlockedDataBuffered()) { | 1407 if (!writer_->IsWriteBlockedDataBuffered()) { |
| 1415 return false; | 1408 return false; |
| 1416 } | 1409 } |
| 1417 } | 1410 } |
| 1418 QuicTime now = clock_->Now(); | 1411 QuicTime now = clock_->Now(); |
| 1419 if (packet.transmission_type == NOT_RETRANSMISSION) { | 1412 if (packet.transmission_type == NOT_RETRANSMISSION) { |
| 1420 time_of_last_sent_new_packet_ = now; | 1413 time_of_last_sent_new_packet_ = now; |
| 1421 } | 1414 } |
| 1422 SetPingAlarm(); | 1415 SetPingAlarm(); |
| 1423 DVLOG(1) << ENDPOINT << "time of last sent packet: " | 1416 DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1442 QuicTime::Delta::FromMilliseconds(1)); | 1435 QuicTime::Delta::FromMilliseconds(1)); |
| 1443 } | 1436 } |
| 1444 | 1437 |
| 1445 stats_.bytes_sent += result.bytes_written; | 1438 stats_.bytes_sent += result.bytes_written; |
| 1446 ++stats_.packets_sent; | 1439 ++stats_.packets_sent; |
| 1447 if (packet.transmission_type != NOT_RETRANSMISSION) { | 1440 if (packet.transmission_type != NOT_RETRANSMISSION) { |
| 1448 stats_.bytes_retransmitted += result.bytes_written; | 1441 stats_.bytes_retransmitted += result.bytes_written; |
| 1449 ++stats_.packets_retransmitted; | 1442 ++stats_.packets_retransmitted; |
| 1450 } | 1443 } |
| 1451 | 1444 |
| 1452 return OnPacketSent(result); | 1445 if (result.status == WRITE_STATUS_ERROR) { |
| 1446 OnWriteError(result.error_code); |
| 1447 return false; |
| 1448 } |
| 1449 |
| 1450 return true; |
| 1453 } | 1451 } |
| 1454 | 1452 |
| 1455 bool QuicConnection::ShouldDiscardPacket( | 1453 bool QuicConnection::ShouldDiscardPacket( |
| 1456 EncryptionLevel level, | 1454 EncryptionLevel level, |
| 1457 QuicPacketSequenceNumber sequence_number, | 1455 QuicPacketSequenceNumber sequence_number, |
| 1458 HasRetransmittableData retransmittable) { | 1456 HasRetransmittableData retransmittable) { |
| 1459 if (!connected_) { | 1457 if (!connected_) { |
| 1460 DVLOG(1) << ENDPOINT | 1458 DVLOG(1) << ENDPOINT |
| 1461 << "Not sending packet as connection is disconnected."; | 1459 << "Not sending packet as connection is disconnected."; |
| 1462 return true; | 1460 return true; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1486 if (retransmittable == HAS_RETRANSMITTABLE_DATA && | 1484 if (retransmittable == HAS_RETRANSMITTABLE_DATA && |
| 1487 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1485 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { |
| 1488 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number | 1486 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number |
| 1489 << " A previous transmission was acked while write blocked."; | 1487 << " A previous transmission was acked while write blocked."; |
| 1490 return true; | 1488 return true; |
| 1491 } | 1489 } |
| 1492 | 1490 |
| 1493 return false; | 1491 return false; |
| 1494 } | 1492 } |
| 1495 | 1493 |
| 1496 bool QuicConnection::OnPacketSent(WriteResult result) { | 1494 void QuicConnection::OnWriteError(int error_code) { |
| 1497 if (result.status == WRITE_STATUS_ERROR) { | 1495 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code |
| 1498 DVLOG(1) << ENDPOINT << "Write failed with error: " << result.error_code | 1496 << " (" << ErrorToString(error_code) << ")"; |
| 1499 << " (" << ErrorToString(result.error_code) << ")"; | 1497 // 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. | 1498 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
| 1501 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | |
| 1502 return false; | |
| 1503 } | |
| 1504 | |
| 1505 return true; | |
| 1506 } | 1499 } |
| 1507 | 1500 |
| 1508 bool QuicConnection::OnSerializedPacket( | 1501 bool QuicConnection::OnSerializedPacket( |
| 1509 const SerializedPacket& serialized_packet) { | 1502 const SerializedPacket& serialized_packet) { |
| 1510 if (serialized_packet.retransmittable_frames) { | 1503 if (serialized_packet.retransmittable_frames) { |
| 1511 serialized_packet.retransmittable_frames-> | 1504 serialized_packet.retransmittable_frames-> |
| 1512 set_encryption_level(encryption_level_); | 1505 set_encryption_level(encryption_level_); |
| 1513 } | 1506 } |
| 1514 sent_packet_manager_.OnSerializedPacket(serialized_packet); | 1507 sent_packet_manager_.OnSerializedPacket(serialized_packet); |
| 1515 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions | 1508 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions |
| (...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1991 // If we changed the generator's batch state, restore original batch state. | 1984 // If we changed the generator's batch state, restore original batch state. |
| 1992 if (!already_in_batch_mode_) { | 1985 if (!already_in_batch_mode_) { |
| 1993 DVLOG(1) << "Leaving Batch Mode."; | 1986 DVLOG(1) << "Leaving Batch Mode."; |
| 1994 connection_->packet_generator_.FinishBatchOperations(); | 1987 connection_->packet_generator_.FinishBatchOperations(); |
| 1995 } | 1988 } |
| 1996 DCHECK_EQ(already_in_batch_mode_, | 1989 DCHECK_EQ(already_in_batch_mode_, |
| 1997 connection_->packet_generator_.InBatchMode()); | 1990 connection_->packet_generator_.InBatchMode()); |
| 1998 } | 1991 } |
| 1999 | 1992 |
| 2000 } // namespace net | 1993 } // namespace net |
| OLD | NEW |