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