| 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/tools/quic/quic_simple_client.h" | 5 #include "net/tools/quic/quic_simple_client.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 IPEndPoint server_address, | 36 IPEndPoint server_address, |
| 37 const QuicServerId& server_id, | 37 const QuicServerId& server_id, |
| 38 const QuicVersionVector& supported_versions, | 38 const QuicVersionVector& supported_versions, |
| 39 std::unique_ptr<ProofVerifier> proof_verifier) | 39 std::unique_ptr<ProofVerifier> proof_verifier) |
| 40 : QuicClientBase(server_id, | 40 : QuicClientBase(server_id, |
| 41 supported_versions, | 41 supported_versions, |
| 42 QuicConfig(), | 42 QuicConfig(), |
| 43 CreateQuicConnectionHelper(), | 43 CreateQuicConnectionHelper(), |
| 44 CreateQuicAlarmFactory(), | 44 CreateQuicAlarmFactory(), |
| 45 std::move(proof_verifier)), | 45 std::move(proof_verifier)), |
| 46 server_address_(server_address), | |
| 47 local_port_(0), | |
| 48 initialized_(false), | 46 initialized_(false), |
| 49 packet_reader_started_(false), | 47 packet_reader_started_(false), |
| 50 weak_factory_(this) {} | 48 weak_factory_(this) { |
| 49 set_server_address(server_address); |
| 50 } |
| 51 | 51 |
| 52 QuicSimpleClient::QuicSimpleClient( | 52 QuicSimpleClient::QuicSimpleClient( |
| 53 IPEndPoint server_address, | 53 IPEndPoint server_address, |
| 54 const QuicServerId& server_id, | 54 const QuicServerId& server_id, |
| 55 const QuicVersionVector& supported_versions, | 55 const QuicVersionVector& supported_versions, |
| 56 const QuicConfig& config, | 56 const QuicConfig& config, |
| 57 std::unique_ptr<ProofVerifier> proof_verifier) | 57 std::unique_ptr<ProofVerifier> proof_verifier) |
| 58 : QuicClientBase(server_id, | 58 : QuicClientBase(server_id, |
| 59 supported_versions, | 59 supported_versions, |
| 60 config, | 60 config, |
| 61 CreateQuicConnectionHelper(), | 61 CreateQuicConnectionHelper(), |
| 62 CreateQuicAlarmFactory(), | 62 CreateQuicAlarmFactory(), |
| 63 std::move(proof_verifier)), | 63 std::move(proof_verifier)), |
| 64 server_address_(server_address), | |
| 65 local_port_(0), | |
| 66 initialized_(false), | 64 initialized_(false), |
| 67 packet_reader_started_(false), | 65 packet_reader_started_(false), |
| 68 weak_factory_(this) {} | 66 weak_factory_(this) { |
| 67 set_server_address(server_address); |
| 68 } |
| 69 | 69 |
| 70 QuicSimpleClient::~QuicSimpleClient() { | 70 QuicSimpleClient::~QuicSimpleClient() { |
| 71 if (connected()) { | 71 if (connected()) { |
| 72 session()->connection()->CloseConnection( | 72 session()->connection()->CloseConnection( |
| 73 QUIC_PEER_GOING_AWAY, "Shutting down", | 73 QUIC_PEER_GOING_AWAY, "Shutting down", |
| 74 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 74 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool QuicSimpleClient::Initialize() { | 78 bool QuicSimpleClient::Initialize() { |
| 79 DCHECK(!initialized_); | 79 DCHECK(!initialized_); |
| 80 | 80 |
| 81 QuicClientBase::Initialize(); | 81 QuicClientBase::Initialize(); |
| 82 | 82 |
| 83 if (!CreateUDPSocket()) { | 83 if (!CreateUDPSocket()) { |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 | 86 |
| 87 initialized_ = true; | 87 initialized_ = true; |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool QuicSimpleClient::CreateUDPSocket() { | 91 bool QuicSimpleClient::CreateUDPSocket() { |
| 92 std::unique_ptr<UDPClientSocket> socket( | 92 std::unique_ptr<UDPClientSocket> socket( |
| 93 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 93 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 94 &net_log_, NetLog::Source())); | 94 &net_log_, NetLog::Source())); |
| 95 | 95 |
| 96 int address_family = server_address_.GetSockAddrFamily(); | 96 int address_family = server_address().GetSockAddrFamily(); |
| 97 if (bind_to_address_.size() != 0) { | 97 if (bind_to_address().size() != 0) { |
| 98 client_address_ = IPEndPoint(bind_to_address_, local_port_); | 98 client_address_ = IPEndPoint(bind_to_address(), local_port()); |
| 99 } else if (address_family == AF_INET) { | 99 } else if (address_family == AF_INET) { |
| 100 client_address_ = IPEndPoint(IPAddress::IPv4AllZeros(), local_port_); | 100 client_address_ = IPEndPoint(IPAddress::IPv4AllZeros(), local_port()); |
| 101 } else { | 101 } else { |
| 102 client_address_ = IPEndPoint(IPAddress::IPv6AllZeros(), local_port_); | 102 client_address_ = IPEndPoint(IPAddress::IPv6AllZeros(), local_port()); |
| 103 } | 103 } |
| 104 | 104 |
| 105 int rc = socket->Connect(server_address_); | 105 int rc = socket->Connect(server_address()); |
| 106 if (rc != OK) { | 106 if (rc != OK) { |
| 107 LOG(ERROR) << "Connect failed: " << ErrorToShortString(rc); | 107 LOG(ERROR) << "Connect failed: " << ErrorToShortString(rc); |
| 108 return false; | 108 return false; |
| 109 } | 109 } |
| 110 | 110 |
| 111 rc = socket->SetReceiveBufferSize(kDefaultSocketReceiveBuffer); | 111 rc = socket->SetReceiveBufferSize(kDefaultSocketReceiveBuffer); |
| 112 if (rc != OK) { | 112 if (rc != OK) { |
| 113 LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToShortString(rc); | 113 LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToShortString(rc); |
| 114 return false; | 114 return false; |
| 115 } | 115 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // does not need to be resent. | 188 // does not need to be resent. |
| 189 if (session()->error() != QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) { | 189 if (session()->error() != QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) { |
| 190 ClearDataToResend(); | 190 ClearDataToResend(); |
| 191 } | 191 } |
| 192 // Before we destroy the last session and create a new one, gather its stats | 192 // Before we destroy the last session and create a new one, gather its stats |
| 193 // and update the stats for the overall connection. | 193 // and update the stats for the overall connection. |
| 194 UpdateStats(); | 194 UpdateStats(); |
| 195 } | 195 } |
| 196 | 196 |
| 197 CreateQuicClientSession(new QuicConnection( | 197 CreateQuicClientSession(new QuicConnection( |
| 198 GetNextConnectionId(), server_address_, helper(), alarm_factory(), | 198 GetNextConnectionId(), server_address(), helper(), alarm_factory(), |
| 199 writer(), | 199 writer(), |
| 200 /* owns_writer= */ false, Perspective::IS_CLIENT, supported_versions())); | 200 /* owns_writer= */ false, Perspective::IS_CLIENT, supported_versions())); |
| 201 | 201 |
| 202 session()->Initialize(); | 202 session()->Initialize(); |
| 203 session()->CryptoConnect(); | 203 session()->CryptoConnect(); |
| 204 set_connected_or_attempting_connect(true); | 204 set_connected_or_attempting_connect(true); |
| 205 } | 205 } |
| 206 | 206 |
| 207 void QuicSimpleClient::Disconnect() { | 207 void QuicSimpleClient::Disconnect() { |
| 208 DCHECK(initialized_); | 208 DCHECK(initialized_); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 } | 286 } |
| 287 | 287 |
| 288 return session()->num_active_requests() != 0; | 288 return session()->num_active_requests() != 0; |
| 289 } | 289 } |
| 290 | 290 |
| 291 bool QuicSimpleClient::MigrateSocket(const IPAddress& new_host) { | 291 bool QuicSimpleClient::MigrateSocket(const IPAddress& new_host) { |
| 292 if (!connected()) { | 292 if (!connected()) { |
| 293 return false; | 293 return false; |
| 294 } | 294 } |
| 295 | 295 |
| 296 bind_to_address_ = new_host; | 296 set_bind_to_address(new_host); |
| 297 if (!CreateUDPSocket()) { | 297 if (!CreateUDPSocket()) { |
| 298 return false; | 298 return false; |
| 299 } | 299 } |
| 300 | 300 |
| 301 session()->connection()->SetSelfAddress(client_address_); | 301 session()->connection()->SetSelfAddress(client_address_); |
| 302 | 302 |
| 303 QuicPacketWriter* writer = CreateQuicPacketWriter(); | 303 QuicPacketWriter* writer = CreateQuicPacketWriter(); |
| 304 set_writer(writer); | 304 set_writer(writer); |
| 305 session()->connection()->SetQuicPacketWriter(writer, false); | 305 session()->connection()->SetQuicPacketWriter(writer, false); |
| 306 | 306 |
| 307 return true; | 307 return true; |
| 308 } | 308 } |
| 309 | 309 |
| 310 void QuicSimpleClient::OnClose(QuicSpdyStream* stream) { | |
| 311 DCHECK(stream != nullptr); | |
| 312 QuicSpdyClientStream* client_stream = | |
| 313 static_cast<QuicSpdyClientStream*>(stream); | |
| 314 HttpResponseInfo response; | |
| 315 SpdyHeadersToHttpResponse(client_stream->response_headers(), &response); | |
| 316 if (response_listener_.get() != nullptr) { | |
| 317 response_listener_->OnCompleteResponse(stream->id(), *response.headers, | |
| 318 client_stream->data()); | |
| 319 } | |
| 320 | |
| 321 // Store response headers and body. | |
| 322 if (store_response_) { | |
| 323 latest_response_code_ = client_stream->response_code(); | |
| 324 response.headers->GetNormalizedHeaders(&latest_response_headers_); | |
| 325 latest_response_body_ = client_stream->data(); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 size_t QuicSimpleClient::latest_response_code() const { | |
| 330 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | |
| 331 return latest_response_code_; | |
| 332 } | |
| 333 | |
| 334 const string& QuicSimpleClient::latest_response_headers() const { | |
| 335 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | |
| 336 return latest_response_headers_; | |
| 337 } | |
| 338 | |
| 339 const string& QuicSimpleClient::latest_response_body() const { | |
| 340 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | |
| 341 return latest_response_body_; | |
| 342 } | |
| 343 | |
| 344 QuicConnectionId QuicSimpleClient::GenerateNewConnectionId() { | 310 QuicConnectionId QuicSimpleClient::GenerateNewConnectionId() { |
| 345 return helper()->GetRandomGenerator()->RandUint64(); | 311 return helper()->GetRandomGenerator()->RandUint64(); |
| 346 } | 312 } |
| 347 | 313 |
| 348 QuicChromiumConnectionHelper* QuicSimpleClient::CreateQuicConnectionHelper() { | 314 QuicChromiumConnectionHelper* QuicSimpleClient::CreateQuicConnectionHelper() { |
| 349 return new QuicChromiumConnectionHelper(&clock_, QuicRandom::GetInstance()); | 315 return new QuicChromiumConnectionHelper(&clock_, QuicRandom::GetInstance()); |
| 350 } | 316 } |
| 351 | 317 |
| 352 QuicChromiumAlarmFactory* QuicSimpleClient::CreateQuicAlarmFactory() { | 318 QuicChromiumAlarmFactory* QuicSimpleClient::CreateQuicAlarmFactory() { |
| 353 return new QuicChromiumAlarmFactory(base::ThreadTaskRunnerHandle::Get().get(), | 319 return new QuicChromiumAlarmFactory(base::ThreadTaskRunnerHandle::Get().get(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 370 session()->connection()->ProcessUdpPacket(local_address, peer_address, | 336 session()->connection()->ProcessUdpPacket(local_address, peer_address, |
| 371 packet); | 337 packet); |
| 372 if (!session()->connection()->connected()) { | 338 if (!session()->connection()->connected()) { |
| 373 return false; | 339 return false; |
| 374 } | 340 } |
| 375 | 341 |
| 376 return true; | 342 return true; |
| 377 } | 343 } |
| 378 | 344 |
| 379 } // namespace net | 345 } // namespace net |
| OLD | NEW |