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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "net/quic/crypto/quic_decrypter.h" | 11 #include "net/quic/crypto/quic_decrypter.h" |
12 #include "net/quic/crypto/quic_encrypter.h" | 12 #include "net/quic/crypto/quic_encrypter.h" |
13 #include "net/quic/quic_utils.h" | 13 #include "net/quic/quic_utils.h" |
14 | 14 |
15 using base::hash_map; | 15 using base::hash_map; |
16 using base::hash_set; | 16 using base::hash_set; |
17 using base::StringPiece; | 17 using base::StringPiece; |
18 using std::list; | 18 using std::list; |
19 using std::make_pair; | 19 using std::make_pair; |
20 using std::min; | 20 using std::min; |
21 using std::max; | 21 using std::max; |
22 using std::vector; | 22 using std::vector; |
23 using std::set; | 23 using std::set; |
24 using std::string; | 24 using std::string; |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | |
28 // TODO(pwestin): kDefaultTimeoutUs is in int64. | |
29 int32 kNegotiatedTimeoutUs = kDefaultTimeoutUs; | |
30 | |
31 namespace { | 27 namespace { |
32 | 28 |
33 // The largest gap in packets we'll accept without closing the connection. | 29 // The largest gap in packets we'll accept without closing the connection. |
34 // This will likely have to be tuned. | 30 // This will likely have to be tuned. |
35 const QuicPacketSequenceNumber kMaxPacketGap = 5000; | 31 const QuicPacketSequenceNumber kMaxPacketGap = 5000; |
36 | 32 |
37 // The maximum number of nacks which can be transmitted in a single ack packet | 33 // The maximum number of nacks which can be transmitted in a single ack packet |
38 // without exceeding kMaxPacketSize. | 34 // without exceeding kMaxPacketSize. |
39 // TODO(satyamshekhar): Get rid of magic numbers and move this to protocol.h | 35 // TODO(satyamshekhar): Get rid of magic numbers and move this to protocol.h |
40 // 16 - Min ack frame size. | 36 // 16 - Min ack frame size. |
(...skipping 29 matching lines...) Expand all Loading... |
70 } | 66 } |
71 | 67 |
72 } // namespace | 68 } // namespace |
73 | 69 |
74 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 70 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
75 | 71 |
76 QuicConnection::QuicConnection(QuicGuid guid, | 72 QuicConnection::QuicConnection(QuicGuid guid, |
77 IPEndPoint address, | 73 IPEndPoint address, |
78 QuicConnectionHelperInterface* helper, | 74 QuicConnectionHelperInterface* helper, |
79 bool is_server) | 75 bool is_server) |
80 : helper_(helper), | 76 : framer_(kQuicVersion1, |
81 framer_(kQuicVersion1, | |
82 helper->GetClock()->ApproximateNow(), | 77 helper->GetClock()->ApproximateNow(), |
83 is_server), | 78 is_server), |
| 79 helper_(helper), |
84 encryption_level_(ENCRYPTION_NONE), | 80 encryption_level_(ENCRYPTION_NONE), |
85 clock_(helper->GetClock()), | 81 clock_(helper->GetClock()), |
86 random_generator_(helper->GetRandomGenerator()), | 82 random_generator_(helper->GetRandomGenerator()), |
87 guid_(guid), | 83 guid_(guid), |
88 peer_address_(address), | 84 peer_address_(address), |
89 largest_seen_packet_with_ack_(0), | 85 largest_seen_packet_with_ack_(0), |
90 peer_largest_observed_packet_(0), | 86 peer_largest_observed_packet_(0), |
91 least_packet_awaited_by_peer_(1), | 87 least_packet_awaited_by_peer_(1), |
92 peer_least_packet_awaiting_ack_(0), | 88 peer_least_packet_awaiting_ack_(0), |
93 handling_retransmission_timeout_(false), | 89 handling_retransmission_timeout_(false), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 QuicConnection::~QuicConnection() { | 125 QuicConnection::~QuicConnection() { |
130 STLDeleteValues(&unacked_packets_); | 126 STLDeleteValues(&unacked_packets_); |
131 STLDeleteValues(&group_map_); | 127 STLDeleteValues(&group_map_); |
132 for (QueuedPacketList::iterator it = queued_packets_.begin(); | 128 for (QueuedPacketList::iterator it = queued_packets_.begin(); |
133 it != queued_packets_.end(); ++it) { | 129 it != queued_packets_.end(); ++it) { |
134 delete it->packet; | 130 delete it->packet; |
135 } | 131 } |
136 } | 132 } |
137 | 133 |
138 bool QuicConnection::SelectMutualVersion( | 134 bool QuicConnection::SelectMutualVersion( |
139 const QuicVersionTagList& available_versions) { | 135 const QuicTagVector& available_versions) { |
140 // TODO(satyamshekhar): Make this generic. | 136 // TODO(satyamshekhar): Make this generic. |
141 if (std::find(available_versions.begin(), available_versions.end(), | 137 if (std::find(available_versions.begin(), available_versions.end(), |
142 kQuicVersion1) == available_versions.end()) { | 138 kQuicVersion1) == available_versions.end()) { |
143 return false; | 139 return false; |
144 } | 140 } |
145 | 141 |
146 // Right now we only support kQuicVersion1 so it's okay not to | 142 // Right now we only support kQuicVersion1 so it's okay not to |
147 // update the framer and quic_version_. When start supporting more | 143 // update the framer and quic_version_. When start supporting more |
148 // versions please update both. | 144 // versions please update both. |
149 return true; | 145 return true; |
(...skipping 21 matching lines...) Expand all Loading... |
171 } | 167 } |
172 | 168 |
173 void QuicConnection::OnPublicResetPacket( | 169 void QuicConnection::OnPublicResetPacket( |
174 const QuicPublicResetPacket& packet) { | 170 const QuicPublicResetPacket& packet) { |
175 if (debug_visitor_) { | 171 if (debug_visitor_) { |
176 debug_visitor_->OnPublicResetPacket(packet); | 172 debug_visitor_->OnPublicResetPacket(packet); |
177 } | 173 } |
178 CloseConnection(QUIC_PUBLIC_RESET, true); | 174 CloseConnection(QUIC_PUBLIC_RESET, true); |
179 } | 175 } |
180 | 176 |
181 bool QuicConnection::OnProtocolVersionMismatch( | 177 bool QuicConnection::OnProtocolVersionMismatch(QuicTag received_version) { |
182 QuicVersionTag received_version) { | |
183 // TODO(satyamshekhar): Implement no server state in this mode. | 178 // TODO(satyamshekhar): Implement no server state in this mode. |
184 if (!is_server_) { | 179 if (!is_server_) { |
185 LOG(DFATAL) << "Framer called OnProtocolVersionMismatch for server. " | 180 LOG(DFATAL) << "Framer called OnProtocolVersionMismatch for server. " |
186 << "Closing connection."; | 181 << "Closing connection."; |
187 CloseConnection(QUIC_INTERNAL_ERROR, false); | 182 CloseConnection(QUIC_INTERNAL_ERROR, false); |
188 return false; | 183 return false; |
189 } | 184 } |
190 DCHECK_NE(quic_version_, received_version); | 185 DCHECK_NE(quic_version_, received_version); |
191 | 186 |
192 if (debug_visitor_) { | 187 if (debug_visitor_) { |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 SendAck(); | 668 SendAck(); |
674 } else if (!last_stream_frames_.empty()) { | 669 } else if (!last_stream_frames_.empty()) { |
675 // TODO(alyssar) this case should really be "if the packet contained any | 670 // TODO(alyssar) this case should really be "if the packet contained any |
676 // non-ack frame", rather than "if the packet contained a stream frame" | 671 // non-ack frame", rather than "if the packet contained a stream frame" |
677 helper_->SetAckAlarm(congestion_manager_.DefaultRetransmissionTime()); | 672 helper_->SetAckAlarm(congestion_manager_.DefaultRetransmissionTime()); |
678 } | 673 } |
679 send_ack_in_response_to_packet_ = !send_ack_in_response_to_packet_; | 674 send_ack_in_response_to_packet_ = !send_ack_in_response_to_packet_; |
680 } | 675 } |
681 | 676 |
682 void QuicConnection::SendVersionNegotiationPacket() { | 677 void QuicConnection::SendVersionNegotiationPacket() { |
683 QuicVersionTagList supported_versions; | 678 QuicTagVector supported_versions; |
684 supported_versions.push_back(kQuicVersion1); | 679 supported_versions.push_back(kQuicVersion1); |
685 QuicEncryptedPacket* encrypted = | 680 QuicEncryptedPacket* encrypted = |
686 packet_creator_.SerializeVersionNegotiationPacket(supported_versions); | 681 packet_creator_.SerializeVersionNegotiationPacket(supported_versions); |
687 // TODO(satyamshekhar): implement zero server state negotiation. | 682 // TODO(satyamshekhar): implement zero server state negotiation. |
688 int error; | 683 int error; |
689 helper_->WritePacketToWire(*encrypted, &error); | 684 helper_->WritePacketToWire(*encrypted, &error); |
690 delete encrypted; | 685 delete encrypted; |
691 } | 686 } |
692 | 687 |
693 QuicConsumedData QuicConnection::SendStreamData(QuicStreamId id, | 688 QuicConsumedData QuicConnection::SendStreamData(QuicStreamId id, |
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 group_map_.erase(it); | 1282 group_map_.erase(it); |
1288 delete fec_group; | 1283 delete fec_group; |
1289 it = next; | 1284 it = next; |
1290 } | 1285 } |
1291 } | 1286 } |
1292 | 1287 |
1293 bool QuicConnection::HasQueuedData() const { | 1288 bool QuicConnection::HasQueuedData() const { |
1294 return !queued_packets_.empty() || packet_generator_.HasQueuedData(); | 1289 return !queued_packets_.empty() || packet_generator_.HasQueuedData(); |
1295 } | 1290 } |
1296 | 1291 |
| 1292 void QuicConnection::SetConnectionTimeout(QuicTime::Delta timeout) { |
| 1293 timeout_ = timeout; |
| 1294 CheckForTimeout(); |
| 1295 } |
| 1296 |
1297 bool QuicConnection::CheckForTimeout() { | 1297 bool QuicConnection::CheckForTimeout() { |
1298 QuicTime now = clock_->ApproximateNow(); | 1298 QuicTime now = clock_->ApproximateNow(); |
1299 QuicTime time_of_last_packet = std::max(time_of_last_received_packet_, | 1299 QuicTime time_of_last_packet = std::max(time_of_last_received_packet_, |
1300 time_of_last_sent_packet_); | 1300 time_of_last_sent_packet_); |
1301 | 1301 |
1302 QuicTime::Delta delta = now.Subtract(time_of_last_packet); | 1302 QuicTime::Delta delta = now.Subtract(time_of_last_packet); |
1303 DVLOG(1) << "last packet " << time_of_last_packet.ToDebuggingValue() | 1303 DVLOG(1) << "last packet " << time_of_last_packet.ToDebuggingValue() |
1304 << " now:" << now.ToDebuggingValue() | 1304 << " now:" << now.ToDebuggingValue() |
1305 << " delta:" << delta.ToMicroseconds(); | 1305 << " delta:" << delta.ToMicroseconds(); |
1306 if (delta >= timeout_) { | 1306 if (delta >= timeout_) { |
1307 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); | 1307 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); |
1308 return true; | 1308 return true; |
1309 } | 1309 } |
1310 helper_->SetTimeoutAlarm(timeout_.Subtract(delta)); | 1310 helper_->SetTimeoutAlarm(timeout_.Subtract(delta)); |
1311 return false; | 1311 return false; |
1312 } | 1312 } |
1313 | 1313 |
1314 } // namespace net | 1314 } // namespace net |
OLD | NEW |