| 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_client.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <netinet/in.h> |
| 9 #include <string.h> |
| 10 #include <sys/socket.h> |
| 11 #include <unistd.h> |
| 6 | 12 |
| 7 #include "base/logging.h" | 13 #include "base/logging.h" |
| 8 #include "base/run_loop.h" | |
| 9 #include "net/base/net_errors.h" | |
| 10 #include "net/http/http_request_info.h" | |
| 11 #include "net/quic/crypto/quic_random.h" | 14 #include "net/quic/crypto/quic_random.h" |
| 12 #include "net/quic/quic_connection.h" | 15 #include "net/quic/quic_connection.h" |
| 13 #include "net/quic/quic_connection_helper.h" | 16 #include "net/quic/quic_data_reader.h" |
| 14 #include "net/quic/quic_default_packet_writer.h" | |
| 15 #include "net/quic/quic_protocol.h" | 17 #include "net/quic/quic_protocol.h" |
| 16 #include "net/quic/quic_server_id.h" | 18 #include "net/quic/quic_server_id.h" |
| 17 #include "net/udp/udp_client_socket.h" | 19 #include "net/tools/balsa/balsa_headers.h" |
| 20 #include "net/tools/epoll_server/epoll_server.h" |
| 21 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 22 #include "net/tools/quic/quic_socket_utils.h" |
| 23 #include "net/tools/quic/quic_spdy_client_stream.h" |
| 24 |
| 25 #ifndef SO_RXQ_OVFL |
| 26 #define SO_RXQ_OVFL 40 |
| 27 #endif |
| 18 | 28 |
| 19 using std::string; | 29 using std::string; |
| 20 using std::vector; | |
| 21 | 30 |
| 22 namespace net { | 31 namespace net { |
| 23 namespace tools { | 32 namespace tools { |
| 24 namespace { | |
| 25 | 33 |
| 26 // Allocate some extra space so we can send an error if the server goes over | 34 const PollBits kEpollFlags = PollBits(NET_POLLIN | NET_POLLOUT | NET_POLLET); |
| 27 // the limit. | |
| 28 const int kReadBufferSize = 2 * kMaxPacketSize; | |
| 29 | 35 |
| 30 } // namespace | 36 QuicClient::QuicClient(IPEndPoint server_address, |
| 31 | 37 const QuicServerId& server_id, |
| 32 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 38 const QuicVersionVector& supported_versions, |
| 33 const QuicServerId& server_id, | 39 EpollServer* epoll_server) |
| 34 const QuicVersionVector& supported_versions) | |
| 35 : server_address_(server_address), | 40 : server_address_(server_address), |
| 36 server_id_(server_id), | 41 server_id_(server_id), |
| 37 local_port_(0), | 42 local_port_(0), |
| 43 epoll_server_(epoll_server), |
| 44 fd_(-1), |
| 38 helper_(CreateQuicConnectionHelper()), | 45 helper_(CreateQuicConnectionHelper()), |
| 39 initialized_(false), | 46 initialized_(false), |
| 47 packets_dropped_(0), |
| 48 overflow_supported_(false), |
| 40 supported_versions_(supported_versions), | 49 supported_versions_(supported_versions), |
| 41 read_pending_(false), | 50 store_response_(false), |
| 42 synchronous_read_count_(0), | 51 latest_response_code_(-1) { |
| 43 read_buffer_(new IOBufferWithSize(kReadBufferSize)), | |
| 44 weak_factory_(this) { | |
| 45 } | 52 } |
| 46 | 53 |
| 47 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 54 QuicClient::QuicClient(IPEndPoint server_address, |
| 48 const QuicServerId& server_id, | 55 const QuicServerId& server_id, |
| 49 const QuicVersionVector& supported_versions, | 56 const QuicVersionVector& supported_versions, |
| 50 const QuicConfig& config) | 57 const QuicConfig& config, |
| 58 EpollServer* epoll_server) |
| 51 : server_address_(server_address), | 59 : server_address_(server_address), |
| 52 server_id_(server_id), | 60 server_id_(server_id), |
| 53 config_(config), | 61 config_(config), |
| 54 local_port_(0), | 62 local_port_(0), |
| 63 epoll_server_(epoll_server), |
| 64 fd_(-1), |
| 55 helper_(CreateQuicConnectionHelper()), | 65 helper_(CreateQuicConnectionHelper()), |
| 56 initialized_(false), | 66 initialized_(false), |
| 67 packets_dropped_(0), |
| 68 overflow_supported_(false), |
| 57 supported_versions_(supported_versions), | 69 supported_versions_(supported_versions), |
| 58 read_pending_(false), | 70 store_response_(false), |
| 59 synchronous_read_count_(0), | 71 latest_response_code_(-1) { |
| 60 read_buffer_(new IOBufferWithSize(kReadBufferSize)), | |
| 61 weak_factory_(this) { | |
| 62 } | 72 } |
| 63 | 73 |
| 64 QuicSimpleClient::~QuicSimpleClient() { | 74 QuicClient::~QuicClient() { |
| 65 if (connected()) { | 75 if (connected()) { |
| 66 session()->connection()->SendConnectionClosePacket( | 76 session()->connection()->SendConnectionClosePacket( |
| 67 QUIC_PEER_GOING_AWAY, ""); | 77 QUIC_PEER_GOING_AWAY, ""); |
| 68 } | 78 } |
| 79 |
| 80 CleanUpUDPSocket(); |
| 69 } | 81 } |
| 70 | 82 |
| 71 bool QuicSimpleClient::Initialize() { | 83 bool QuicClient::Initialize() { |
| 72 DCHECK(!initialized_); | 84 DCHECK(!initialized_); |
| 73 | 85 |
| 86 // If an initial flow control window has not explicitly been set, then use the |
| 87 // same value that Chrome uses: 10 Mb. |
| 88 const uint32 kInitialFlowControlWindow = 10 * 1024 * 1024; // 10 Mb |
| 89 if (config_.GetInitialStreamFlowControlWindowToSend() == |
| 90 kMinimumFlowControlSendWindow) { |
| 91 config_.SetInitialStreamFlowControlWindowToSend(kInitialFlowControlWindow); |
| 92 } |
| 93 if (config_.GetInitialSessionFlowControlWindowToSend() == |
| 94 kMinimumFlowControlSendWindow) { |
| 95 config_.SetInitialSessionFlowControlWindowToSend(kInitialFlowControlWindow); |
| 96 } |
| 97 |
| 98 epoll_server_->set_timeout_in_us(50 * 1000); |
| 99 |
| 74 if (!CreateUDPSocket()) { | 100 if (!CreateUDPSocket()) { |
| 75 return false; | 101 return false; |
| 76 } | 102 } |
| 77 | 103 |
| 104 epoll_server_->RegisterFD(fd_, this, kEpollFlags); |
| 78 initialized_ = true; | 105 initialized_ = true; |
| 79 return true; | 106 return true; |
| 80 } | 107 } |
| 81 | 108 |
| 82 QuicSimpleClient::DummyPacketWriterFactory::DummyPacketWriterFactory( | 109 QuicClient::DummyPacketWriterFactory::DummyPacketWriterFactory( |
| 83 QuicPacketWriter* writer) | 110 QuicPacketWriter* writer) |
| 84 : writer_(writer) {} | 111 : writer_(writer) {} |
| 85 | 112 |
| 86 QuicSimpleClient::DummyPacketWriterFactory::~DummyPacketWriterFactory() {} | 113 QuicClient::DummyPacketWriterFactory::~DummyPacketWriterFactory() {} |
| 87 | 114 |
| 88 QuicPacketWriter* QuicSimpleClient::DummyPacketWriterFactory::Create( | 115 QuicPacketWriter* QuicClient::DummyPacketWriterFactory::Create( |
| 89 QuicConnection* /*connection*/) const { | 116 QuicConnection* /*connection*/) const { |
| 90 return writer_; | 117 return writer_; |
| 91 } | 118 } |
| 92 | 119 |
| 93 bool QuicSimpleClient::CreateUDPSocket() { | |
| 94 scoped_ptr<UDPClientSocket> socket( | |
| 95 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | |
| 96 RandIntCallback(), | |
| 97 &net_log_, | |
| 98 NetLog::Source())); | |
| 99 | 120 |
| 121 bool QuicClient::CreateUDPSocket() { |
| 100 int address_family = server_address_.GetSockAddrFamily(); | 122 int address_family = server_address_.GetSockAddrFamily(); |
| 123 fd_ = QuicSocketUtils::CreateNonBlockingSocket(address_family, SOCK_DGRAM, |
| 124 IPPROTO_UDP); |
| 125 if (fd_ < 0) { |
| 126 return false; // failure already logged |
| 127 } |
| 128 |
| 129 int get_overflow = 1; |
| 130 int rc = setsockopt(fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow, |
| 131 sizeof(get_overflow)); |
| 132 if (rc < 0) { |
| 133 DLOG(WARNING) << "Socket overflow detection not supported"; |
| 134 } else { |
| 135 overflow_supported_ = true; |
| 136 } |
| 137 |
| 138 if (!QuicSocketUtils::SetReceiveBufferSize(fd_, |
| 139 kDefaultSocketReceiveBuffer)) { |
| 140 return false; |
| 141 } |
| 142 |
| 143 if (!QuicSocketUtils::SetSendBufferSize(fd_, kDefaultSocketReceiveBuffer)) { |
| 144 return false; |
| 145 } |
| 146 |
| 147 rc = QuicSocketUtils::SetGetAddressInfo(fd_, address_family); |
| 148 if (rc < 0) { |
| 149 LOG(ERROR) << "IP detection not supported" << strerror(errno); |
| 150 return false; |
| 151 } |
| 152 |
| 101 if (bind_to_address_.size() != 0) { | 153 if (bind_to_address_.size() != 0) { |
| 102 client_address_ = IPEndPoint(bind_to_address_, local_port_); | 154 client_address_ = IPEndPoint(bind_to_address_, local_port_); |
| 103 } else if (address_family == AF_INET) { | 155 } else if (address_family == AF_INET) { |
| 104 IPAddressNumber any4; | 156 IPAddressNumber any4; |
| 105 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 157 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 106 client_address_ = IPEndPoint(any4, local_port_); | 158 client_address_ = IPEndPoint(any4, local_port_); |
| 107 } else { | 159 } else { |
| 108 IPAddressNumber any6; | 160 IPAddressNumber any6; |
| 109 CHECK(net::ParseIPLiteralToNumber("::", &any6)); | 161 CHECK(net::ParseIPLiteralToNumber("::", &any6)); |
| 110 client_address_ = IPEndPoint(any6, local_port_); | 162 client_address_ = IPEndPoint(any6, local_port_); |
| 111 } | 163 } |
| 112 | 164 |
| 113 int rc = socket->Connect(server_address_); | 165 sockaddr_storage raw_addr; |
| 114 if (rc != OK) { | 166 socklen_t raw_addr_len = sizeof(raw_addr); |
| 115 LOG(ERROR) << "Connect failed: " << ErrorToString(rc); | 167 CHECK(client_address_.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr), |
| 168 &raw_addr_len)); |
| 169 rc = bind(fd_, |
| 170 reinterpret_cast<const sockaddr*>(&raw_addr), |
| 171 sizeof(raw_addr)); |
| 172 if (rc < 0) { |
| 173 LOG(ERROR) << "Bind failed: " << strerror(errno); |
| 116 return false; | 174 return false; |
| 117 } | 175 } |
| 118 | 176 |
| 119 rc = socket->SetReceiveBufferSize(kDefaultSocketReceiveBuffer); | 177 SockaddrStorage storage; |
| 120 if (rc != OK) { | 178 if (getsockname(fd_, storage.addr, &storage.addr_len) != 0 || |
| 121 LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToString(rc); | 179 !client_address_.FromSockAddr(storage.addr, storage.addr_len)) { |
| 122 return false; | 180 LOG(ERROR) << "Unable to get self address. Error: " << strerror(errno); |
| 123 } | |
| 124 | |
| 125 rc = socket->SetSendBufferSize(kDefaultSocketReceiveBuffer); | |
| 126 if (rc != OK) { | |
| 127 LOG(ERROR) << "SetSendBufferSize() failed: " << ErrorToString(rc); | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 rc = socket->GetLocalAddress(&client_address_); | |
| 132 if (rc != OK) { | |
| 133 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(rc); | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 socket_.swap(socket); | |
| 138 | |
| 139 read_pending_ = false; | |
| 140 | |
| 141 if (socket != nullptr) { | |
| 142 socket->Close(); | |
| 143 } | 181 } |
| 144 | 182 |
| 145 return true; | 183 return true; |
| 146 } | 184 } |
| 147 | 185 |
| 148 bool QuicSimpleClient::Connect() { | 186 bool QuicClient::Connect() { |
| 149 StartConnect(); | 187 StartConnect(); |
| 150 StartReading(); | |
| 151 while (EncryptionBeingEstablished()) { | 188 while (EncryptionBeingEstablished()) { |
| 152 WaitForEvents(); | 189 WaitForEvents(); |
| 153 } | 190 } |
| 154 return session_->connection()->connected(); | 191 return session_->connection()->connected(); |
| 155 } | 192 } |
| 156 | 193 |
| 157 void QuicSimpleClient::StartConnect() { | 194 void QuicClient::StartConnect() { |
| 158 DCHECK(initialized_); | 195 DCHECK(initialized_); |
| 159 DCHECK(!connected()); | 196 DCHECK(!connected()); |
| 160 | 197 |
| 161 writer_.reset(CreateQuicPacketWriter()); | 198 QuicPacketWriter* writer = CreateQuicPacketWriter(); |
| 162 connection_ = new QuicConnection(GenerateConnectionId(), | 199 |
| 163 server_address_, | 200 DummyPacketWriterFactory factory(writer); |
| 164 helper_.get(), | 201 |
| 165 DummyPacketWriterFactory(writer_.get()), | 202 session_.reset(new QuicClientSession( |
| 166 /* owns_writer= */ false, | 203 config_, |
| 167 Perspective::IS_CLIENT, | 204 new QuicConnection(GenerateConnectionId(), server_address_, helper_.get(), |
| 168 server_id_.is_https(), | 205 factory, |
| 169 supported_versions_); | 206 /* owns_writer= */ false, Perspective::IS_CLIENT, |
| 170 session_.reset(new QuicSimpleClientSession(config_, connection_)); | 207 server_id_.is_https(), supported_versions_))); |
| 208 |
| 209 // Reset |writer_| after |session_| so that the old writer outlives the old |
| 210 // session. |
| 211 if (writer_.get() != writer) { |
| 212 writer_.reset(writer); |
| 213 } |
| 171 session_->InitializeSession(server_id_, &crypto_config_); | 214 session_->InitializeSession(server_id_, &crypto_config_); |
| 172 session_->CryptoConnect(); | 215 session_->CryptoConnect(); |
| 173 } | 216 } |
| 174 | 217 |
| 175 bool QuicSimpleClient::EncryptionBeingEstablished() { | 218 bool QuicClient::EncryptionBeingEstablished() { |
| 176 return !session_->IsEncryptionEstablished() && | 219 return !session_->IsEncryptionEstablished() && |
| 177 session_->connection()->connected(); | 220 session_->connection()->connected(); |
| 178 } | 221 } |
| 179 | 222 |
| 180 void QuicSimpleClient::Disconnect() { | 223 void QuicClient::Disconnect() { |
| 181 DCHECK(initialized_); | 224 DCHECK(initialized_); |
| 182 | 225 |
| 183 if (connected()) { | 226 if (connected()) { |
| 184 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 227 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); |
| 185 } | 228 } |
| 186 | 229 |
| 187 writer_.reset(); | 230 CleanUpUDPSocket(); |
| 188 | |
| 189 read_pending_ = false; | |
| 190 | 231 |
| 191 initialized_ = false; | 232 initialized_ = false; |
| 192 } | 233 } |
| 193 | 234 |
| 194 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, | 235 void QuicClient::CleanUpUDPSocket() { |
| 195 base::StringPiece body, | 236 if (fd_ > -1) { |
| 196 bool fin) { | 237 epoll_server_->UnregisterFD(fd_); |
| 197 QuicSimpleClientStream* stream = CreateReliableClientStream(); | 238 close(fd_); |
| 239 fd_ = -1; |
| 240 } |
| 241 } |
| 242 |
| 243 void QuicClient::SendRequest(const BalsaHeaders& headers, |
| 244 StringPiece body, |
| 245 bool fin) { |
| 246 QuicSpdyClientStream* stream = CreateReliableClientStream(); |
| 198 if (stream == nullptr) { | 247 if (stream == nullptr) { |
| 199 LOG(DFATAL) << "stream creation failed!"; | 248 LOG(DFATAL) << "stream creation failed!"; |
| 200 return; | 249 return; |
| 201 } | 250 } |
| 202 stream->SendRequest(headers, body, fin); | 251 stream->SendRequest(headers, body, fin); |
| 203 stream->set_visitor(this); | 252 stream->set_visitor(this); |
| 204 } | 253 } |
| 205 | 254 |
| 206 void QuicSimpleClient::SendRequestAndWaitForResponse( | 255 void QuicClient::SendRequestAndWaitForResponse(const BalsaHeaders& headers, |
| 207 const HttpRequestInfo& request, | 256 StringPiece body, |
| 208 base::StringPiece body, | 257 bool fin) { |
| 209 bool fin) { | 258 SendRequest(headers, "", true); |
| 210 SendRequest(request, body, fin); | 259 while (WaitForEvents()) { |
| 260 } |
| 261 } |
| 262 |
| 263 void QuicClient::SendRequestsAndWaitForResponse( |
| 264 const base::CommandLine::StringVector& args) { |
| 265 for (size_t i = 0; i < args.size(); ++i) { |
| 266 BalsaHeaders headers; |
| 267 headers.SetRequestFirstlineFromStringPieces("GET", args[i], "HTTP/1.1"); |
| 268 SendRequest(headers, "", true); |
| 269 } |
| 211 while (WaitForEvents()) {} | 270 while (WaitForEvents()) {} |
| 212 } | 271 } |
| 213 | 272 |
| 214 void QuicSimpleClient::SendRequestsAndWaitForResponse( | 273 QuicSpdyClientStream* QuicClient::CreateReliableClientStream() { |
| 215 const base::CommandLine::StringVector& url_list) { | |
| 216 for (size_t i = 0; i < url_list.size(); ++i) { | |
| 217 HttpRequestInfo request; | |
| 218 request.method = "GET"; | |
| 219 request.url = GURL(url_list[i]); | |
| 220 SendRequest(request, "", true); | |
| 221 } | |
| 222 | |
| 223 while (WaitForEvents()) {} | |
| 224 } | |
| 225 | |
| 226 QuicSimpleClientStream* QuicSimpleClient::CreateReliableClientStream() { | |
| 227 if (!connected()) { | 274 if (!connected()) { |
| 228 return nullptr; | 275 return nullptr; |
| 229 } | 276 } |
| 230 | 277 |
| 231 return session_->CreateOutgoingDataStream(); | 278 return session_->CreateOutgoingDataStream(); |
| 232 } | 279 } |
| 233 | 280 |
| 234 void QuicSimpleClient::WaitForStreamToClose(QuicStreamId id) { | 281 void QuicClient::WaitForStreamToClose(QuicStreamId id) { |
| 235 DCHECK(connected()); | 282 DCHECK(connected()); |
| 236 | 283 |
| 237 while (connected() && !session_->IsClosedStream(id)) { | 284 while (connected() && !session_->IsClosedStream(id)) { |
| 238 WaitForEvents(); | 285 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
| 239 } | 286 } |
| 240 } | 287 } |
| 241 | 288 |
| 242 void QuicSimpleClient::WaitForCryptoHandshakeConfirmed() { | 289 void QuicClient::WaitForCryptoHandshakeConfirmed() { |
| 243 DCHECK(connected()); | 290 DCHECK(connected()); |
| 244 | 291 |
| 245 while (connected() && !session_->IsCryptoHandshakeConfirmed()) { | 292 while (connected() && !session_->IsCryptoHandshakeConfirmed()) { |
| 246 WaitForEvents(); | 293 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
| 247 } | 294 } |
| 248 } | 295 } |
| 249 | 296 |
| 250 bool QuicSimpleClient::WaitForEvents() { | 297 bool QuicClient::WaitForEvents() { |
| 251 DCHECK(connected()); | 298 DCHECK(connected()); |
| 252 | 299 |
| 253 base::RunLoop().RunUntilIdle(); | 300 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
| 254 return session_->num_active_requests() != 0; | 301 return session_->num_active_requests() != 0; |
| 255 } | 302 } |
| 256 | 303 |
| 257 void QuicSimpleClient::OnClose(QuicDataStream* stream) { | 304 void QuicClient::OnEvent(int fd, EpollEvent* event) { |
| 258 QuicSimpleClientStream* client_stream = | 305 DCHECK_EQ(fd, fd_); |
| 259 static_cast<QuicSimpleClientStream*>(stream); | 306 |
| 307 if (event->in_events & NET_POLLIN) { |
| 308 while (connected() && ReadAndProcessPacket()) { |
| 309 } |
| 310 } |
| 311 if (connected() && (event->in_events & NET_POLLOUT)) { |
| 312 writer_->SetWritable(); |
| 313 session_->connection()->OnCanWrite(); |
| 314 } |
| 315 if (event->in_events & NET_POLLERR) { |
| 316 DVLOG(1) << "NET_POLLERR"; |
| 317 } |
| 318 } |
| 319 |
| 320 void QuicClient::OnClose(QuicDataStream* stream) { |
| 321 QuicSpdyClientStream* client_stream = |
| 322 static_cast<QuicSpdyClientStream*>(stream); |
| 260 if (response_listener_.get() != nullptr) { | 323 if (response_listener_.get() != nullptr) { |
| 261 response_listener_->OnCompleteResponse( | 324 response_listener_->OnCompleteResponse( |
| 262 stream->id(), *client_stream->headers(), client_stream->data()); | 325 stream->id(), client_stream->headers(), client_stream->data()); |
| 263 } | 326 } |
| 264 | 327 |
| 265 // Store response headers and body. | 328 // Store response headers and body. |
| 266 if (store_response_) { | 329 if (store_response_) { |
| 267 latest_response_code_ = client_stream->headers()->response_code(); | 330 latest_response_code_ = client_stream->headers().parsed_response_code(); |
| 268 client_stream->headers()->GetNormalizedHeaders(&latest_response_headers_); | 331 client_stream->headers().DumpHeadersToString(&latest_response_headers_); |
| 269 latest_response_body_ = client_stream->data(); | 332 latest_response_body_ = client_stream->data(); |
| 270 } | 333 } |
| 271 } | 334 } |
| 272 | 335 |
| 273 bool QuicSimpleClient::connected() const { | 336 bool QuicClient::connected() const { |
| 274 return session_.get() && session_->connection() && | 337 return session_.get() && session_->connection() && |
| 275 session_->connection()->connected(); | 338 session_->connection()->connected(); |
| 276 } | 339 } |
| 277 | 340 |
| 278 bool QuicSimpleClient::goaway_received() const { | 341 bool QuicClient::goaway_received() const { |
| 279 return session_ != nullptr && session_->goaway_received(); | 342 return session_ != nullptr && session_->goaway_received(); |
| 280 } | 343 } |
| 281 | 344 |
| 282 size_t QuicSimpleClient::latest_response_code() const { | 345 size_t QuicClient::latest_response_code() const { |
| 283 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | 346 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; |
| 284 return latest_response_code_; | 347 return latest_response_code_; |
| 285 } | 348 } |
| 286 | 349 |
| 287 const string& QuicSimpleClient::latest_response_headers() const { | 350 const string& QuicClient::latest_response_headers() const { |
| 288 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | 351 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; |
| 289 return latest_response_headers_; | 352 return latest_response_headers_; |
| 290 } | 353 } |
| 291 | 354 |
| 292 const string& QuicSimpleClient::latest_response_body() const { | 355 const string& QuicClient::latest_response_body() const { |
| 293 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; | 356 LOG_IF(DFATAL, !store_response_) << "Response not stored!"; |
| 294 return latest_response_body_; | 357 return latest_response_body_; |
| 295 } | 358 } |
| 296 | 359 |
| 297 QuicConnectionId QuicSimpleClient::GenerateConnectionId() { | 360 QuicConnectionId QuicClient::GenerateConnectionId() { |
| 298 return helper_->GetRandomGenerator()->RandUint64(); | 361 return QuicRandom::GetInstance()->RandUint64(); |
| 299 } | 362 } |
| 300 | 363 |
| 301 QuicConnectionHelper* QuicSimpleClient::CreateQuicConnectionHelper() { | 364 QuicEpollConnectionHelper* QuicClient::CreateQuicConnectionHelper() { |
| 302 return new QuicConnectionHelper( | 365 return new QuicEpollConnectionHelper(epoll_server_); |
| 303 base::MessageLoop::current()->message_loop_proxy().get(), | |
| 304 &clock_, | |
| 305 QuicRandom::GetInstance()); | |
| 306 } | 366 } |
| 307 | 367 |
| 308 QuicPacketWriter* QuicSimpleClient::CreateQuicPacketWriter() { | 368 QuicPacketWriter* QuicClient::CreateQuicPacketWriter() { |
| 309 return new QuicDefaultPacketWriter(socket_.get()); | 369 return new QuicDefaultPacketWriter(fd_); |
| 310 } | 370 } |
| 311 | 371 |
| 312 void QuicSimpleClient::StartReading() { | 372 int QuicClient::ReadPacket(char* buffer, |
| 313 if (read_pending_) { | 373 int buffer_len, |
| 314 return; | 374 IPEndPoint* server_address, |
| 315 } | 375 IPAddressNumber* client_ip) { |
| 316 read_pending_ = true; | 376 return QuicSocketUtils::ReadPacket( |
| 377 fd_, buffer, buffer_len, |
| 378 overflow_supported_ ? &packets_dropped_ : nullptr, client_ip, |
| 379 server_address); |
| 380 } |
| 317 | 381 |
| 318 int result = socket_->Read( | 382 bool QuicClient::ReadAndProcessPacket() { |
| 319 read_buffer_.get(), | 383 // Allocate some extra space so we can send an error if the server goes over |
| 320 read_buffer_->size(), | 384 // the limit. |
| 321 base::Bind(&QuicSimpleClient::OnReadComplete, | 385 char buf[2 * kMaxPacketSize]; |
| 322 weak_factory_.GetWeakPtr())); | |
| 323 | 386 |
| 324 if (result == ERR_IO_PENDING) { | 387 IPEndPoint server_address; |
| 325 synchronous_read_count_ = 0; | 388 IPAddressNumber client_ip; |
| 326 return; | 389 |
| 390 int bytes_read = ReadPacket(buf, arraysize(buf), &server_address, &client_ip); |
| 391 |
| 392 if (bytes_read < 0) { |
| 393 return false; |
| 327 } | 394 } |
| 328 | 395 |
| 329 if (++synchronous_read_count_ > 32) { | 396 QuicEncryptedPacket packet(buf, bytes_read, false); |
| 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 | 397 |
| 342 void QuicSimpleClient::OnReadComplete(int result) { | 398 IPEndPoint client_address(client_ip, client_address_.port()); |
| 343 read_pending_ = false; | 399 session_->connection()->ProcessUdpPacket( |
| 344 if (result == 0) | 400 client_address, server_address, packet); |
| 345 result = ERR_CONNECTION_CLOSED; | 401 return true; |
| 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 } | 402 } |
| 365 | 403 |
| 366 } // namespace tools | 404 } // namespace tools |
| 367 } // namespace net | 405 } // namespace net |
| OLD | NEW |