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_client.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/epoll.h> | 10 #include <sys/epoll.h> |
11 #include <sys/socket.h> | 11 #include <sys/socket.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "net/quic/congestion_control/tcp_receiver.h" | 15 #include "net/quic/congestion_control/tcp_receiver.h" |
16 #include "net/quic/crypto/quic_random.h" | 16 #include "net/quic/crypto/quic_random.h" |
17 #include "net/quic/quic_connection.h" | 17 #include "net/quic/quic_connection.h" |
18 #include "net/quic/quic_data_reader.h" | 18 #include "net/quic/quic_data_reader.h" |
19 #include "net/quic/quic_protocol.h" | 19 #include "net/quic/quic_protocol.h" |
20 #include "net/quic/quic_server_id.h" | 20 #include "net/quic/quic_server_id.h" |
21 #include "net/tools/balsa/balsa_headers.h" | 21 #include "net/tools/balsa/balsa_headers.h" |
| 22 #include "net/tools/epoll_server/epoll_server.h" |
22 #include "net/tools/quic/quic_epoll_connection_helper.h" | 23 #include "net/tools/quic/quic_epoll_connection_helper.h" |
23 #include "net/tools/quic/quic_socket_utils.h" | 24 #include "net/tools/quic/quic_socket_utils.h" |
24 #include "net/tools/quic/quic_spdy_client_stream.h" | 25 #include "net/tools/quic/quic_spdy_client_stream.h" |
25 | 26 |
26 #ifndef SO_RXQ_OVFL | 27 #ifndef SO_RXQ_OVFL |
27 #define SO_RXQ_OVFL 40 | 28 #define SO_RXQ_OVFL 40 |
28 #endif | 29 #endif |
29 | 30 |
30 namespace net { | 31 namespace net { |
31 namespace tools { | 32 namespace tools { |
32 | 33 |
33 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET; | 34 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET; |
34 | 35 |
35 QuicClient::QuicClient(IPEndPoint server_address, | 36 QuicClient::QuicClient(IPEndPoint server_address, |
36 const QuicServerId& server_id, | 37 const QuicServerId& server_id, |
37 const QuicVersionVector& supported_versions, | 38 const QuicVersionVector& supported_versions, |
38 bool print_response) | 39 bool print_response, |
| 40 EpollServer* epoll_server) |
39 : server_address_(server_address), | 41 : server_address_(server_address), |
40 server_id_(server_id), | 42 server_id_(server_id), |
41 local_port_(0), | 43 local_port_(0), |
| 44 epoll_server_(epoll_server), |
42 fd_(-1), | 45 fd_(-1), |
43 helper_(CreateQuicConnectionHelper()), | 46 helper_(CreateQuicConnectionHelper()), |
44 initialized_(false), | 47 initialized_(false), |
45 packets_dropped_(0), | 48 packets_dropped_(0), |
46 overflow_supported_(false), | 49 overflow_supported_(false), |
47 supported_versions_(supported_versions), | 50 supported_versions_(supported_versions), |
48 print_response_(print_response) { | 51 print_response_(print_response) { |
49 config_.SetDefaults(); | 52 config_.SetDefaults(); |
50 } | 53 } |
51 | 54 |
52 QuicClient::QuicClient(IPEndPoint server_address, | 55 QuicClient::QuicClient(IPEndPoint server_address, |
53 const QuicServerId& server_id, | 56 const QuicServerId& server_id, |
54 const QuicVersionVector& supported_versions, | 57 const QuicVersionVector& supported_versions, |
55 bool print_response, | 58 bool print_response, |
56 const QuicConfig& config) | 59 const QuicConfig& config, |
| 60 EpollServer* epoll_server) |
57 : server_address_(server_address), | 61 : server_address_(server_address), |
58 server_id_(server_id), | 62 server_id_(server_id), |
59 config_(config), | 63 config_(config), |
60 local_port_(0), | 64 local_port_(0), |
| 65 epoll_server_(epoll_server), |
61 fd_(-1), | 66 fd_(-1), |
62 helper_(CreateQuicConnectionHelper()), | 67 helper_(CreateQuicConnectionHelper()), |
63 initialized_(false), | 68 initialized_(false), |
64 packets_dropped_(0), | 69 packets_dropped_(0), |
65 overflow_supported_(false), | 70 overflow_supported_(false), |
66 supported_versions_(supported_versions), | 71 supported_versions_(supported_versions), |
67 print_response_(print_response) { | 72 print_response_(print_response) { |
68 } | 73 } |
69 | 74 |
70 QuicClient::~QuicClient() { | 75 QuicClient::~QuicClient() { |
71 if (connected()) { | 76 if (connected()) { |
72 session()->connection()->SendConnectionClosePacket( | 77 session()->connection()->SendConnectionClosePacket( |
73 QUIC_PEER_GOING_AWAY, ""); | 78 QUIC_PEER_GOING_AWAY, ""); |
74 } | 79 } |
| 80 if (fd_ > 0) { |
| 81 epoll_server_->UnregisterFD(fd_); |
| 82 } |
75 } | 83 } |
76 | 84 |
77 bool QuicClient::Initialize() { | 85 bool QuicClient::Initialize() { |
78 DCHECK(!initialized_); | 86 DCHECK(!initialized_); |
79 | 87 |
80 epoll_server_.set_timeout_in_us(50 * 1000); | 88 epoll_server_->set_timeout_in_us(50 * 1000); |
81 crypto_config_.SetDefaults(); | 89 crypto_config_.SetDefaults(); |
82 | 90 |
83 if (!CreateUDPSocket()) { | 91 if (!CreateUDPSocket()) { |
84 return false; | 92 return false; |
85 } | 93 } |
86 | 94 |
87 epoll_server_.RegisterFD(fd_, this, kEpollFlags); | 95 epoll_server_->RegisterFD(fd_, this, kEpollFlags); |
88 initialized_ = true; | 96 initialized_ = true; |
89 return true; | 97 return true; |
90 } | 98 } |
91 | 99 |
92 bool QuicClient::CreateUDPSocket() { | 100 bool QuicClient::CreateUDPSocket() { |
93 int address_family = server_address_.GetSockAddrFamily(); | 101 int address_family = server_address_.GetSockAddrFamily(); |
94 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); | 102 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); |
95 if (fd_ < 0) { | 103 if (fd_ < 0) { |
96 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno); | 104 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno); |
97 return false; | 105 return false; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return !session_->IsEncryptionEstablished() && | 203 return !session_->IsEncryptionEstablished() && |
196 session_->connection()->connected(); | 204 session_->connection()->connected(); |
197 } | 205 } |
198 | 206 |
199 void QuicClient::Disconnect() { | 207 void QuicClient::Disconnect() { |
200 DCHECK(initialized_); | 208 DCHECK(initialized_); |
201 | 209 |
202 if (connected()) { | 210 if (connected()) { |
203 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 211 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); |
204 } | 212 } |
205 epoll_server_.UnregisterFD(fd_); | 213 epoll_server_->UnregisterFD(fd_); |
206 close(fd_); | 214 close(fd_); |
207 fd_ = -1; | 215 fd_ = -1; |
208 initialized_ = false; | 216 initialized_ = false; |
209 } | 217 } |
210 | 218 |
211 void QuicClient::SendRequestsAndWaitForResponse( | 219 void QuicClient::SendRequestsAndWaitForResponse( |
212 const base::CommandLine::StringVector& args) { | 220 const base::CommandLine::StringVector& args) { |
213 for (size_t i = 0; i < args.size(); ++i) { | 221 for (size_t i = 0; i < args.size(); ++i) { |
214 BalsaHeaders headers; | 222 BalsaHeaders headers; |
215 headers.SetRequestFirstlineFromStringPieces("GET", args[i], "HTTP/1.1"); | 223 headers.SetRequestFirstlineFromStringPieces("GET", args[i], "HTTP/1.1"); |
(...skipping 11 matching lines...) Expand all Loading... |
227 return NULL; | 235 return NULL; |
228 } | 236 } |
229 | 237 |
230 return session_->CreateOutgoingDataStream(); | 238 return session_->CreateOutgoingDataStream(); |
231 } | 239 } |
232 | 240 |
233 void QuicClient::WaitForStreamToClose(QuicStreamId id) { | 241 void QuicClient::WaitForStreamToClose(QuicStreamId id) { |
234 DCHECK(connected()); | 242 DCHECK(connected()); |
235 | 243 |
236 while (connected() && !session_->IsClosedStream(id)) { | 244 while (connected() && !session_->IsClosedStream(id)) { |
237 epoll_server_.WaitForEventsAndExecuteCallbacks(); | 245 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
238 } | 246 } |
239 } | 247 } |
240 | 248 |
241 void QuicClient::WaitForCryptoHandshakeConfirmed() { | 249 void QuicClient::WaitForCryptoHandshakeConfirmed() { |
242 DCHECK(connected()); | 250 DCHECK(connected()); |
243 | 251 |
244 while (connected() && !session_->IsCryptoHandshakeConfirmed()) { | 252 while (connected() && !session_->IsCryptoHandshakeConfirmed()) { |
245 epoll_server_.WaitForEventsAndExecuteCallbacks(); | 253 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
246 } | 254 } |
247 } | 255 } |
248 | 256 |
249 bool QuicClient::WaitForEvents() { | 257 bool QuicClient::WaitForEvents() { |
250 DCHECK(connected()); | 258 DCHECK(connected()); |
251 | 259 |
252 epoll_server_.WaitForEventsAndExecuteCallbacks(); | 260 epoll_server_->WaitForEventsAndExecuteCallbacks(); |
253 return session_->num_active_requests() != 0; | 261 return session_->num_active_requests() != 0; |
254 } | 262 } |
255 | 263 |
256 void QuicClient::OnEvent(int fd, EpollEvent* event) { | 264 void QuicClient::OnEvent(int fd, EpollEvent* event) { |
257 DCHECK_EQ(fd, fd_); | 265 DCHECK_EQ(fd, fd_); |
258 | 266 |
259 if (event->in_events & EPOLLIN) { | 267 if (event->in_events & EPOLLIN) { |
260 while (connected() && ReadAndProcessPacket()) { | 268 while (connected() && ReadAndProcessPacket()) { |
261 } | 269 } |
262 } | 270 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 bool QuicClient::connected() const { | 303 bool QuicClient::connected() const { |
296 return session_.get() && session_->connection() && | 304 return session_.get() && session_->connection() && |
297 session_->connection()->connected(); | 305 session_->connection()->connected(); |
298 } | 306 } |
299 | 307 |
300 QuicConnectionId QuicClient::GenerateConnectionId() { | 308 QuicConnectionId QuicClient::GenerateConnectionId() { |
301 return QuicRandom::GetInstance()->RandUint64(); | 309 return QuicRandom::GetInstance()->RandUint64(); |
302 } | 310 } |
303 | 311 |
304 QuicEpollConnectionHelper* QuicClient::CreateQuicConnectionHelper() { | 312 QuicEpollConnectionHelper* QuicClient::CreateQuicConnectionHelper() { |
305 return new QuicEpollConnectionHelper(&epoll_server_); | 313 return new QuicEpollConnectionHelper(epoll_server_); |
306 } | 314 } |
307 | 315 |
308 QuicPacketWriter* QuicClient::CreateQuicPacketWriter() { | 316 QuicPacketWriter* QuicClient::CreateQuicPacketWriter() { |
309 return new QuicDefaultPacketWriter(fd_); | 317 return new QuicDefaultPacketWriter(fd_); |
310 } | 318 } |
311 | 319 |
312 int QuicClient::ReadPacket(char* buffer, | 320 int QuicClient::ReadPacket(char* buffer, |
313 int buffer_len, | 321 int buffer_len, |
314 IPEndPoint* server_address, | 322 IPEndPoint* server_address, |
315 IPAddressNumber* client_ip) { | 323 IPAddressNumber* client_ip) { |
(...skipping 19 matching lines...) Expand all Loading... |
335 QuicEncryptedPacket packet(buf, bytes_read, false); | 343 QuicEncryptedPacket packet(buf, bytes_read, false); |
336 | 344 |
337 IPEndPoint client_address(client_ip, client_address_.port()); | 345 IPEndPoint client_address(client_ip, client_address_.port()); |
338 session_->connection()->ProcessUdpPacket( | 346 session_->connection()->ProcessUdpPacket( |
339 client_address, server_address, packet); | 347 client_address, server_address, packet); |
340 return true; | 348 return true; |
341 } | 349 } |
342 | 350 |
343 } // namespace tools | 351 } // namespace tools |
344 } // namespace net | 352 } // namespace net |
OLD | NEW |