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