| 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 "base/logging.h" | 7 #include "base/logging.h" | 
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" | 
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" | 
| 10 #include "net/http/http_request_info.h" | 10 #include "net/http/http_request_info.h" | 
| 11 #include "net/quic/crypto/quic_random.h" | 11 #include "net/quic/crypto/quic_random.h" | 
| 12 #include "net/quic/quic_connection.h" | 12 #include "net/quic/quic_connection.h" | 
| 13 #include "net/quic/quic_connection_helper.h" | 13 #include "net/quic/quic_connection_helper.h" | 
| 14 #include "net/quic/quic_default_packet_writer.h" | 14 #include "net/quic/quic_default_packet_writer.h" | 
| 15 #include "net/quic/quic_protocol.h" | 15 #include "net/quic/quic_protocol.h" | 
| 16 #include "net/quic/quic_server_id.h" | 16 #include "net/quic/quic_server_id.h" | 
| 17 #include "net/udp/udp_client_socket.h" | 17 #include "net/udp/udp_client_socket.h" | 
| 18 | 18 | 
| 19 using std::string; | 19 using std::string; | 
| 20 using std::vector; | 20 using std::vector; | 
| 21 | 21 | 
| 22 namespace net { | 22 namespace net { | 
| 23 namespace tools { | 23 namespace tools { | 
| 24 namespace { |  | 
| 25 |  | 
| 26 // Allocate some extra space so we can send an error if the server goes over |  | 
| 27 // the limit. |  | 
| 28 const int kReadBufferSize = 2 * kMaxPacketSize; |  | 
| 29 |  | 
| 30 }  // namespace |  | 
| 31 | 24 | 
| 32 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 25 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 
| 33                                    const QuicServerId& server_id, | 26                                    const QuicServerId& server_id, | 
| 34                                    const QuicVersionVector& supported_versions) | 27                                    const QuicVersionVector& supported_versions) | 
| 35     : server_address_(server_address), | 28     : server_address_(server_address), | 
| 36       server_id_(server_id), | 29       server_id_(server_id), | 
| 37       local_port_(0), | 30       local_port_(0), | 
| 38       helper_(CreateQuicConnectionHelper()), | 31       helper_(CreateQuicConnectionHelper()), | 
| 39       initialized_(false), | 32       initialized_(false), | 
| 40       supported_versions_(supported_versions), | 33       supported_versions_(supported_versions), | 
| 41       read_pending_(false), |  | 
| 42       synchronous_read_count_(0), |  | 
| 43       read_buffer_(new IOBufferWithSize(kReadBufferSize)), |  | 
| 44       weak_factory_(this) { | 34       weak_factory_(this) { | 
| 45 } | 35 } | 
| 46 | 36 | 
| 47 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 37 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 
| 48                                    const QuicServerId& server_id, | 38                                    const QuicServerId& server_id, | 
| 49                                    const QuicVersionVector& supported_versions, | 39                                    const QuicVersionVector& supported_versions, | 
| 50                                    const QuicConfig& config) | 40                                    const QuicConfig& config) | 
| 51     : server_address_(server_address), | 41     : server_address_(server_address), | 
| 52       server_id_(server_id), | 42       server_id_(server_id), | 
| 53       config_(config), | 43       config_(config), | 
| 54       local_port_(0), | 44       local_port_(0), | 
| 55       helper_(CreateQuicConnectionHelper()), | 45       helper_(CreateQuicConnectionHelper()), | 
| 56       initialized_(false), | 46       initialized_(false), | 
| 57       supported_versions_(supported_versions), | 47       supported_versions_(supported_versions), | 
| 58       read_pending_(false), |  | 
| 59       synchronous_read_count_(0), |  | 
| 60       read_buffer_(new IOBufferWithSize(kReadBufferSize)), |  | 
| 61       weak_factory_(this) { | 48       weak_factory_(this) { | 
| 62 } | 49 } | 
| 63 | 50 | 
| 64 QuicSimpleClient::~QuicSimpleClient() { | 51 QuicSimpleClient::~QuicSimpleClient() { | 
| 65   if (connected()) { | 52   if (connected()) { | 
| 66     session()->connection()->SendConnectionClosePacket( | 53     session()->connection()->SendConnectionClosePacket( | 
| 67         QUIC_PEER_GOING_AWAY, ""); | 54         QUIC_PEER_GOING_AWAY, ""); | 
| 68   } | 55   } | 
| 69 } | 56 } | 
| 70 | 57 | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 128     return false; | 115     return false; | 
| 129   } | 116   } | 
| 130 | 117 | 
| 131   rc = socket->GetLocalAddress(&client_address_); | 118   rc = socket->GetLocalAddress(&client_address_); | 
| 132   if (rc != OK) { | 119   if (rc != OK) { | 
| 133     LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(rc); | 120     LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(rc); | 
| 134     return false; | 121     return false; | 
| 135   } | 122   } | 
| 136 | 123 | 
| 137   socket_.swap(socket); | 124   socket_.swap(socket); | 
| 138 | 125   packet_reader_.reset(new QuicPacketReader(socket_.get(), this, | 
| 139   read_pending_ = false; | 126                                             BoundNetLog())); | 
| 140 | 127 | 
| 141   if (socket != nullptr) { | 128   if (socket != nullptr) { | 
| 142     socket->Close(); | 129     socket->Close(); | 
| 143   } | 130   } | 
| 144 | 131 | 
| 145   return true; | 132   return true; | 
| 146 } | 133 } | 
| 147 | 134 | 
| 148 bool QuicSimpleClient::Connect() { | 135 bool QuicSimpleClient::Connect() { | 
| 149   StartConnect(); | 136   StartConnect(); | 
| 150   StartReading(); | 137   packet_reader_->StartReading(); | 
| 151   while (EncryptionBeingEstablished()) { | 138   while (EncryptionBeingEstablished()) { | 
| 152     WaitForEvents(); | 139     WaitForEvents(); | 
| 153   } | 140   } | 
| 154   return session_->connection()->connected(); | 141   return session_->connection()->connected(); | 
| 155 } | 142 } | 
| 156 | 143 | 
| 157 void QuicSimpleClient::StartConnect() { | 144 void QuicSimpleClient::StartConnect() { | 
| 158   DCHECK(initialized_); | 145   DCHECK(initialized_); | 
| 159   DCHECK(!connected()); | 146   DCHECK(!connected()); | 
| 160 | 147 | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 178 } | 165 } | 
| 179 | 166 | 
| 180 void QuicSimpleClient::Disconnect() { | 167 void QuicSimpleClient::Disconnect() { | 
| 181   DCHECK(initialized_); | 168   DCHECK(initialized_); | 
| 182 | 169 | 
| 183   if (connected()) { | 170   if (connected()) { | 
| 184     session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 171     session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 
| 185   } | 172   } | 
| 186 | 173 | 
| 187   writer_.reset(); | 174   writer_.reset(); | 
| 188 | 175   packet_reader_.reset(); | 
| 189   read_pending_ = false; |  | 
| 190 | 176 | 
| 191   initialized_ = false; | 177   initialized_ = false; | 
| 192 } | 178 } | 
| 193 | 179 | 
| 194 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, | 180 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, | 
| 195                                    base::StringPiece body, | 181                                    base::StringPiece body, | 
| 196                                    bool fin) { | 182                                    bool fin) { | 
| 197   QuicSimpleClientStream* stream = CreateReliableClientStream(); | 183   QuicSimpleClientStream* stream = CreateReliableClientStream(); | 
| 198   if (stream == nullptr) { | 184   if (stream == nullptr) { | 
| 199     LOG(DFATAL) << "stream creation failed!"; | 185     LOG(DFATAL) << "stream creation failed!"; | 
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 302   return new QuicConnectionHelper( | 288   return new QuicConnectionHelper( | 
| 303       base::MessageLoop::current()->message_loop_proxy().get(), | 289       base::MessageLoop::current()->message_loop_proxy().get(), | 
| 304       &clock_, | 290       &clock_, | 
| 305       QuicRandom::GetInstance()); | 291       QuicRandom::GetInstance()); | 
| 306 } | 292 } | 
| 307 | 293 | 
| 308 QuicPacketWriter* QuicSimpleClient::CreateQuicPacketWriter() { | 294 QuicPacketWriter* QuicSimpleClient::CreateQuicPacketWriter() { | 
| 309   return new QuicDefaultPacketWriter(socket_.get()); | 295   return new QuicDefaultPacketWriter(socket_.get()); | 
| 310 } | 296 } | 
| 311 | 297 | 
| 312 void QuicSimpleClient::StartReading() { | 298 void QuicSimpleClient::OnReadError(int result) { | 
| 313   if (read_pending_) { | 299   LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToString(result); | 
| 314     return; | 300   Disconnect(); | 
| 315   } | 301 } | 
| 316   read_pending_ = true; |  | 
| 317 | 302 | 
| 318   int result = socket_->Read( | 303 bool QuicSimpleClient::OnPacket(const QuicEncryptedPacket& packet, | 
| 319       read_buffer_.get(), | 304                                 IPEndPoint local_address, | 
| 320       read_buffer_->size(), | 305                                 IPEndPoint peer_address) { | 
| 321       base::Bind(&QuicSimpleClient::OnReadComplete, | 306   session_->connection()->ProcessUdpPacket(local_address, peer_address, packet); | 
| 322                  weak_factory_.GetWeakPtr())); | 307   if (!session_->connection()->connected()) { | 
| 323 | 308     return false; | 
| 324   if (result == ERR_IO_PENDING) { |  | 
| 325     synchronous_read_count_ = 0; |  | 
| 326     return; |  | 
| 327   } | 309   } | 
| 328 | 310 | 
| 329   if (++synchronous_read_count_ > 32) { | 311   return true; | 
| 330     synchronous_read_count_ = 0; |  | 
| 331     // Schedule the processing through the message loop to 1) prevent infinite |  | 
| 332     // recursion and 2) avoid blocking the thread for too long. |  | 
| 333     base::MessageLoop::current()->PostTask( |  | 
| 334         FROM_HERE, |  | 
| 335         base::Bind(&QuicSimpleClient::OnReadComplete, |  | 
| 336                    weak_factory_.GetWeakPtr(), result)); |  | 
| 337   } else { |  | 
| 338     OnReadComplete(result); |  | 
| 339   } |  | 
| 340 } |  | 
| 341 |  | 
| 342 void QuicSimpleClient::OnReadComplete(int result) { |  | 
| 343   read_pending_ = false; |  | 
| 344   if (result == 0) |  | 
| 345     result = ERR_CONNECTION_CLOSED; |  | 
| 346 |  | 
| 347   if (result < 0) { |  | 
| 348     LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToString(result); |  | 
| 349     Disconnect(); |  | 
| 350     return; |  | 
| 351   } |  | 
| 352 |  | 
| 353   QuicEncryptedPacket packet(read_buffer_->data(), result); |  | 
| 354   IPEndPoint local_address; |  | 
| 355   IPEndPoint peer_address; |  | 
| 356   socket_->GetLocalAddress(&local_address); |  | 
| 357   socket_->GetPeerAddress(&peer_address); |  | 
| 358   session_->connection()->ProcessUdpPacket(local_address, peer_address, packet); |  | 
| 359   if (!session_->connection()->connected()) { |  | 
| 360     return; |  | 
| 361   } |  | 
| 362 |  | 
| 363   StartReading(); |  | 
| 364 } | 312 } | 
| 365 | 313 | 
| 366 }  // namespace tools | 314 }  // namespace tools | 
| 367 }  // namespace net | 315 }  // namespace net | 
| OLD | NEW | 
|---|