| 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 16 matching lines...) Expand all Loading... |
| 27 #include "net/socket/udp_client_socket.h" | 27 #include "net/socket/udp_client_socket.h" |
| 28 #include "net/spdy/spdy_header_block.h" | 28 #include "net/spdy/spdy_header_block.h" |
| 29 #include "net/spdy/spdy_http_utils.h" | 29 #include "net/spdy/spdy_http_utils.h" |
| 30 | 30 |
| 31 using std::string; | 31 using std::string; |
| 32 using base::StringPiece; | 32 using base::StringPiece; |
| 33 | 33 |
| 34 namespace net { | 34 namespace net { |
| 35 | 35 |
| 36 QuicSimpleClient::QuicSimpleClient( | 36 QuicSimpleClient::QuicSimpleClient( |
| 37 IPEndPoint server_address, | 37 QuicSocketAddress server_address, |
| 38 const QuicServerId& server_id, | 38 const QuicServerId& server_id, |
| 39 const QuicVersionVector& supported_versions, | 39 const QuicVersionVector& supported_versions, |
| 40 std::unique_ptr<ProofVerifier> proof_verifier) | 40 std::unique_ptr<ProofVerifier> proof_verifier) |
| 41 : QuicSimpleClient(server_address, | |
| 42 server_id, | |
| 43 supported_versions, | |
| 44 QuicConfig(), | |
| 45 std::move(proof_verifier)) {} | |
| 46 | |
| 47 QuicSimpleClient::QuicSimpleClient( | |
| 48 IPEndPoint server_address, | |
| 49 const QuicServerId& server_id, | |
| 50 const QuicVersionVector& supported_versions, | |
| 51 const QuicConfig& config, | |
| 52 std::unique_ptr<ProofVerifier> proof_verifier) | |
| 53 : QuicClientBase(server_id, | 41 : QuicClientBase(server_id, |
| 54 supported_versions, | 42 supported_versions, |
| 55 config, | 43 QuicConfig(), |
| 56 CreateQuicConnectionHelper(), | 44 CreateQuicConnectionHelper(), |
| 57 CreateQuicAlarmFactory(), | 45 CreateQuicAlarmFactory(), |
| 58 std::move(proof_verifier)), | 46 std::move(proof_verifier)), |
| 59 initialized_(false), | 47 initialized_(false), |
| 60 packet_reader_started_(false), | 48 packet_reader_started_(false), |
| 61 weak_factory_(this) { | 49 weak_factory_(this) { |
| 62 set_server_address(QuicSocketAddress(QuicSocketAddressImpl(server_address))); | 50 set_server_address(server_address); |
| 63 } | 51 } |
| 64 | 52 |
| 65 QuicSimpleClient::~QuicSimpleClient() { | 53 QuicSimpleClient::~QuicSimpleClient() { |
| 66 if (connected()) { | 54 if (connected()) { |
| 67 session()->connection()->CloseConnection( | 55 session()->connection()->CloseConnection( |
| 68 QUIC_PEER_GOING_AWAY, "Shutting down", | 56 QUIC_PEER_GOING_AWAY, "Shutting down", |
| 69 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 57 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 70 } | 58 } |
| 71 } | 59 } |
| 72 | 60 |
| 73 bool QuicSimpleClient::CreateUDPSocketAndBind(QuicSocketAddress server_address, | 61 bool QuicSimpleClient::CreateUDPSocketAndBind(QuicSocketAddress server_address, |
| 74 QuicIpAddress bind_to_address, | 62 QuicIpAddress bind_to_address, |
| 75 int bind_to_port) { | 63 int bind_to_port) { |
| 76 std::unique_ptr<UDPClientSocket> socket( | 64 std::unique_ptr<UDPClientSocket> socket( |
| 77 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 65 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 78 &net_log_, NetLogSource())); | 66 &net_log_, NetLogSource())); |
| 79 | 67 |
| 80 int address_family = | 68 if (bind_to_address.IsInitialized()) { |
| 81 server_address.impl().socket_address().GetSockAddrFamily(); | 69 client_address_ = QuicSocketAddress(bind_to_address, local_port()); |
| 82 if (bind_to_address.impl().ip_address().size() != 0) { | 70 } else if (server_address.host().address_family() == IpAddressFamily::IP_V4) { |
| 83 client_address_ = | 71 client_address_ = QuicSocketAddress(QuicIpAddress::Any4(), bind_to_port); |
| 84 IPEndPoint(bind_to_address.impl().ip_address(), bind_to_port); | |
| 85 } else if (address_family == AF_INET) { | |
| 86 client_address_ = IPEndPoint(IPAddress::IPv4AllZeros(), bind_to_port); | |
| 87 } else { | 72 } else { |
| 88 client_address_ = IPEndPoint(IPAddress::IPv6AllZeros(), bind_to_port); | 73 client_address_ = QuicSocketAddress(QuicIpAddress::Any6(), bind_to_port); |
| 89 } | 74 } |
| 90 | 75 |
| 91 int rc = socket->Connect(server_address.impl().socket_address()); | 76 int rc = socket->Connect(server_address.impl().socket_address()); |
| 92 if (rc != OK) { | 77 if (rc != OK) { |
| 93 LOG(ERROR) << "Connect failed: " << ErrorToShortString(rc); | 78 LOG(ERROR) << "Connect failed: " << ErrorToShortString(rc); |
| 94 return false; | 79 return false; |
| 95 } | 80 } |
| 96 | 81 |
| 97 rc = socket->SetReceiveBufferSize(kDefaultSocketReceiveBuffer); | 82 rc = socket->SetReceiveBufferSize(kDefaultSocketReceiveBuffer); |
| 98 if (rc != OK) { | 83 if (rc != OK) { |
| 99 LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToShortString(rc); | 84 LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToShortString(rc); |
| 100 return false; | 85 return false; |
| 101 } | 86 } |
| 102 | 87 |
| 103 rc = socket->SetSendBufferSize(kDefaultSocketReceiveBuffer); | 88 rc = socket->SetSendBufferSize(kDefaultSocketReceiveBuffer); |
| 104 if (rc != OK) { | 89 if (rc != OK) { |
| 105 LOG(ERROR) << "SetSendBufferSize() failed: " << ErrorToShortString(rc); | 90 LOG(ERROR) << "SetSendBufferSize() failed: " << ErrorToShortString(rc); |
| 106 return false; | 91 return false; |
| 107 } | 92 } |
| 108 | 93 |
| 109 rc = socket->GetLocalAddress(&client_address_); | 94 IPEndPoint address; |
| 95 rc = socket->GetLocalAddress(&address); |
| 110 if (rc != OK) { | 96 if (rc != OK) { |
| 111 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToShortString(rc); | 97 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToShortString(rc); |
| 112 return false; | 98 return false; |
| 113 } | 99 } |
| 100 client_address_ = QuicSocketAddress(QuicSocketAddressImpl(address)); |
| 114 | 101 |
| 115 socket_.swap(socket); | 102 socket_.swap(socket); |
| 116 packet_reader_.reset(new QuicChromiumPacketReader( | 103 packet_reader_.reset(new QuicChromiumPacketReader( |
| 117 socket_.get(), &clock_, this, kQuicYieldAfterPacketsRead, | 104 socket_.get(), &clock_, this, kQuicYieldAfterPacketsRead, |
| 118 QuicTime::Delta::FromMilliseconds(kQuicYieldAfterDurationMilliseconds), | 105 QuicTime::Delta::FromMilliseconds(kQuicYieldAfterDurationMilliseconds), |
| 119 NetLogWithSource())); | 106 NetLogWithSource())); |
| 120 | 107 |
| 121 if (socket != nullptr) { | 108 if (socket != nullptr) { |
| 122 socket->Close(); | 109 socket->Close(); |
| 123 } | 110 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return new QuicChromiumPacketWriter(socket_.get()); | 144 return new QuicChromiumPacketWriter(socket_.get()); |
| 158 } | 145 } |
| 159 | 146 |
| 160 void QuicSimpleClient::OnReadError(int result, | 147 void QuicSimpleClient::OnReadError(int result, |
| 161 const DatagramClientSocket* socket) { | 148 const DatagramClientSocket* socket) { |
| 162 LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToShortString(result); | 149 LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToShortString(result); |
| 163 Disconnect(); | 150 Disconnect(); |
| 164 } | 151 } |
| 165 | 152 |
| 166 QuicSocketAddress QuicSimpleClient::GetLatestClientAddress() const { | 153 QuicSocketAddress QuicSimpleClient::GetLatestClientAddress() const { |
| 167 return QuicSocketAddress(QuicSocketAddressImpl(client_address_)); | 154 return client_address_; |
| 168 } | 155 } |
| 169 | 156 |
| 170 bool QuicSimpleClient::OnPacket(const QuicReceivedPacket& packet, | 157 bool QuicSimpleClient::OnPacket(const QuicReceivedPacket& packet, |
| 171 IPEndPoint local_address, | 158 IPEndPoint local_address, |
| 172 IPEndPoint peer_address) { | 159 IPEndPoint peer_address) { |
| 173 session()->connection()->ProcessUdpPacket( | 160 session()->connection()->ProcessUdpPacket( |
| 174 QuicSocketAddress(QuicSocketAddressImpl(local_address)), | 161 QuicSocketAddress(QuicSocketAddressImpl(local_address)), |
| 175 QuicSocketAddress(QuicSocketAddressImpl(peer_address)), packet); | 162 QuicSocketAddress(QuicSocketAddressImpl(peer_address)), packet); |
| 176 if (!session()->connection()->connected()) { | 163 if (!session()->connection()->connected()) { |
| 177 return false; | 164 return false; |
| 178 } | 165 } |
| 179 | 166 |
| 180 return true; | 167 return true; |
| 181 } | 168 } |
| 182 | 169 |
| 183 } // namespace net | 170 } // namespace net |
| OLD | NEW |