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 // A toy client, which connects to a specified port and sends QUIC | 5 // A toy client, which connects to a specified port and sends QUIC |
6 // request to that endpoint. | 6 // request to that endpoint. |
7 | 7 |
8 #ifndef NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ | 8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_ |
9 #define NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ | 9 #define NET_TOOLS_QUIC_QUIC_CLIENT_H_ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 #include "net/base/io_buffer.h" | |
18 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_log.h" | |
20 #include "net/http/http_response_headers.h" | |
21 #include "net/quic/crypto/crypto_handshake.h" | 18 #include "net/quic/crypto/crypto_handshake.h" |
22 #include "net/quic/quic_config.h" | 19 #include "net/quic/quic_config.h" |
23 #include "net/quic/quic_framer.h" | 20 #include "net/quic/quic_framer.h" |
24 #include "net/quic/quic_packet_creator.h" | 21 #include "net/quic/quic_packet_creator.h" |
25 #include "net/tools/quic/quic_simple_client_session.h" | 22 #include "net/tools/balsa/balsa_headers.h" |
26 #include "net/tools/quic/quic_simple_client_stream.h" | 23 #include "net/tools/epoll_server/epoll_server.h" |
| 24 #include "net/tools/quic/quic_client_session.h" |
| 25 #include "net/tools/quic/quic_spdy_client_stream.h" |
27 | 26 |
28 namespace net { | 27 namespace net { |
29 | 28 |
30 struct HttpRequestInfo; | |
31 class ProofVerifier; | 29 class ProofVerifier; |
32 class QuicServerId; | 30 class QuicServerId; |
33 class QuicConnectionHelper; | |
34 class UDPClientSocket; | |
35 | 31 |
36 namespace tools { | 32 namespace tools { |
37 | 33 |
| 34 class QuicEpollConnectionHelper; |
| 35 |
38 namespace test { | 36 namespace test { |
39 class QuicClientPeer; | 37 class QuicClientPeer; |
40 } // namespace test | 38 } // namespace test |
41 | 39 |
42 class QuicSimpleClient : public QuicDataStream::Visitor { | 40 class QuicClient : public EpollCallbackInterface, |
| 41 public QuicDataStream::Visitor { |
43 public: | 42 public: |
44 class ResponseListener { | 43 class ResponseListener { |
45 public: | 44 public: |
46 ResponseListener() {} | 45 ResponseListener() {} |
47 virtual ~ResponseListener() {} | 46 virtual ~ResponseListener() {} |
48 virtual void OnCompleteResponse(QuicStreamId id, | 47 virtual void OnCompleteResponse(QuicStreamId id, |
49 const HttpResponseHeaders& response_headers, | 48 const BalsaHeaders& response_headers, |
50 const std::string& response_body) = 0; | 49 const std::string& response_body) = 0; |
51 }; | 50 }; |
52 | 51 |
53 // Create a quic client, which will have events managed by an externally owned | 52 // Create a quic client, which will have events managed by an externally owned |
54 // EpollServer. | 53 // EpollServer. |
55 QuicSimpleClient(IPEndPoint server_address, | 54 QuicClient(IPEndPoint server_address, |
56 const QuicServerId& server_id, | 55 const QuicServerId& server_id, |
57 const QuicVersionVector& supported_versions); | 56 const QuicVersionVector& supported_versions, |
58 QuicSimpleClient(IPEndPoint server_address, | 57 EpollServer* epoll_server); |
59 const QuicServerId& server_id, | 58 QuicClient(IPEndPoint server_address, |
60 const QuicVersionVector& supported_versions, | 59 const QuicServerId& server_id, |
61 const QuicConfig& config); | 60 const QuicVersionVector& supported_versions, |
| 61 const QuicConfig& config, |
| 62 EpollServer* epoll_server); |
62 | 63 |
63 ~QuicSimpleClient() override; | 64 ~QuicClient() override; |
64 | 65 |
65 // Initializes the client to create a connection. Should be called exactly | 66 // Initializes the client to create a connection. Should be called exactly |
66 // once before calling StartConnect or Connect. Returns true if the | 67 // once before calling StartConnect or Connect. Returns true if the |
67 // initialization succeeds, false otherwise. | 68 // initialization succeeds, false otherwise. |
68 bool Initialize(); | 69 bool Initialize(); |
69 | 70 |
70 // "Connect" to the QUIC server, including performing synchronous crypto | 71 // "Connect" to the QUIC server, including performing synchronous crypto |
71 // handshake. | 72 // handshake. |
72 bool Connect(); | 73 bool Connect(); |
73 | 74 |
74 // Start the crypto handshake. This can be done in place of the synchronous | 75 // Start the crypto handshake. This can be done in place of the synchronous |
75 // Connect(), but callers are responsible for making sure the crypto handshake | 76 // Connect(), but callers are responsible for making sure the crypto handshake |
76 // completes. | 77 // completes. |
77 void StartConnect(); | 78 void StartConnect(); |
78 | 79 |
79 // Returns true if the crypto handshake has yet to establish encryption. | 80 // Returns true if the crypto handshake has yet to establish encryption. |
80 // Returns false if encryption is active (even if the server hasn't confirmed | 81 // Returns false if encryption is active (even if the server hasn't confirmed |
81 // the handshake) or if the connection has been closed. | 82 // the handshake) or if the connection has been closed. |
82 bool EncryptionBeingEstablished(); | 83 bool EncryptionBeingEstablished(); |
83 | 84 |
84 // Disconnects from the QUIC server. | 85 // Disconnects from the QUIC server. |
85 void Disconnect(); | 86 void Disconnect(); |
86 | 87 |
87 // Sends an HTTP request and does not wait for response before returning. | 88 // Sends an HTTP request and does not wait for response before returning. |
88 void SendRequest(const HttpRequestInfo& headers, | 89 void SendRequest(const BalsaHeaders& headers, |
89 base::StringPiece body, | 90 base::StringPiece body, |
90 bool fin); | 91 bool fin); |
91 | 92 |
92 // Sends an HTTP request and waits for response before returning. | 93 // Sends an HTTP request and waits for response before returning. |
93 void SendRequestAndWaitForResponse(const HttpRequestInfo& headers, | 94 void SendRequestAndWaitForResponse(const BalsaHeaders& headers, |
94 base::StringPiece body, | 95 base::StringPiece body, |
95 bool fin); | 96 bool fin); |
96 | 97 |
97 // Sends a request simple GET for each URL in |args|, and then waits for | 98 // Sends a request simple GET for each URL in |args|, and then waits for |
98 // each to complete. | 99 // each to complete. |
99 void SendRequestsAndWaitForResponse( | 100 void SendRequestsAndWaitForResponse(const |
100 const base::CommandLine::StringVector& url_list); | 101 base::CommandLine::StringVector& args); |
101 | 102 |
102 // Returns a newly created QuicSimpleClientStream, owned by the | 103 // Returns a newly created QuicSpdyClientStream, owned by the |
103 // QuicSimpleClient. | 104 // QuicClient. |
104 QuicSimpleClientStream* CreateReliableClientStream(); | 105 QuicSpdyClientStream* CreateReliableClientStream(); |
105 | 106 |
106 // Wait for events until the stream with the given ID is closed. | 107 // Wait for events until the stream with the given ID is closed. |
107 void WaitForStreamToClose(QuicStreamId id); | 108 void WaitForStreamToClose(QuicStreamId id); |
108 | 109 |
109 // Wait for events until the handshake is confirmed. | 110 // Wait for events until the handshake is confirmed. |
110 void WaitForCryptoHandshakeConfirmed(); | 111 void WaitForCryptoHandshakeConfirmed(); |
111 | 112 |
112 // Wait up to 50ms, and handle any events which occur. | 113 // Wait up to 50ms, and handle any events which occur. |
113 // Returns true if there are any outstanding requests. | 114 // Returns true if there are any outstanding requests. |
114 bool WaitForEvents(); | 115 bool WaitForEvents(); |
115 | 116 |
116 // Start the read loop on the socket. | 117 // From EpollCallbackInterface |
117 void StartReading(); | 118 void OnRegistration(EpollServer* eps, int fd, int event_mask) override {} |
118 | 119 void OnModification(int fd, int event_mask) override {} |
119 // Called on reads that complete asynchronously. Dispatches the packet and | 120 void OnEvent(int fd, EpollEvent* event) override; |
120 // calls StartReading() again. | 121 // |fd_| can be unregistered without the client being disconnected. This |
121 void OnReadComplete(int result); | 122 // happens in b3m QuicProber where we unregister |fd_| to feed in events to |
| 123 // the client from the SelectServer. |
| 124 void OnUnregistration(int fd, bool replaced) override {} |
| 125 void OnShutdown(EpollServer* eps, int fd) override {} |
122 | 126 |
123 // QuicDataStream::Visitor | 127 // QuicDataStream::Visitor |
124 void OnClose(QuicDataStream* stream) override; | 128 void OnClose(QuicDataStream* stream) override; |
125 | 129 |
126 QuicSimpleClientSession* session() { return session_.get(); } | 130 QuicClientSession* session() { return session_.get(); } |
127 | 131 |
128 bool connected() const; | 132 bool connected() const; |
129 bool goaway_received() const; | 133 bool goaway_received() const; |
130 | 134 |
131 void set_bind_to_address(IPAddressNumber address) { | 135 void set_bind_to_address(IPAddressNumber address) { |
132 bind_to_address_ = address; | 136 bind_to_address_ = address; |
133 } | 137 } |
134 | 138 |
135 IPAddressNumber bind_to_address() const { return bind_to_address_; } | 139 IPAddressNumber bind_to_address() const { return bind_to_address_; } |
136 | 140 |
137 void set_local_port(int local_port) { local_port_ = local_port; } | 141 void set_local_port(int local_port) { local_port_ = local_port; } |
138 | 142 |
139 const IPEndPoint& server_address() const { return server_address_; } | 143 const IPEndPoint& server_address() const { return server_address_; } |
140 | 144 |
141 const IPEndPoint& client_address() const { return client_address_; } | 145 const IPEndPoint& client_address() const { return client_address_; } |
142 | 146 |
| 147 int fd() { return fd_; } |
| 148 |
143 const QuicServerId& server_id() const { return server_id_; } | 149 const QuicServerId& server_id() const { return server_id_; } |
144 | 150 |
145 // This should only be set before the initial Connect() | 151 // This should only be set before the initial Connect() |
146 void set_server_id(const QuicServerId& server_id) { | 152 void set_server_id(const QuicServerId& server_id) { |
147 server_id_ = server_id; | 153 server_id_ = server_id; |
148 } | 154 } |
149 | 155 |
150 void SetUserAgentID(const std::string& user_agent_id) { | 156 void SetUserAgentID(const std::string& user_agent_id) { |
151 crypto_config_.set_user_agent_id(user_agent_id); | 157 crypto_config_.set_user_agent_id(user_agent_id); |
152 } | 158 } |
(...skipping 25 matching lines...) Expand all Loading... |
178 QuicConfig* config() { return &config_; } | 184 QuicConfig* config() { return &config_; } |
179 | 185 |
180 void set_store_response(bool val) { store_response_ = val; } | 186 void set_store_response(bool val) { store_response_ = val; } |
181 | 187 |
182 size_t latest_response_code() const; | 188 size_t latest_response_code() const; |
183 const std::string& latest_response_headers() const; | 189 const std::string& latest_response_headers() const; |
184 const std::string& latest_response_body() const; | 190 const std::string& latest_response_body() const; |
185 | 191 |
186 protected: | 192 protected: |
187 virtual QuicConnectionId GenerateConnectionId(); | 193 virtual QuicConnectionId GenerateConnectionId(); |
188 virtual QuicConnectionHelper* CreateQuicConnectionHelper(); | 194 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper(); |
189 virtual QuicPacketWriter* CreateQuicPacketWriter(); | 195 virtual QuicPacketWriter* CreateQuicPacketWriter(); |
190 | 196 |
| 197 virtual int ReadPacket(char* buffer, |
| 198 int buffer_len, |
| 199 IPEndPoint* server_address, |
| 200 IPAddressNumber* client_ip); |
| 201 |
| 202 EpollServer* epoll_server() { return epoll_server_; } |
| 203 |
191 private: | 204 private: |
192 friend class net::tools::test::QuicClientPeer; | 205 friend class net::tools::test::QuicClientPeer; |
193 | 206 |
194 // A packet writer factory that always returns the same writer | 207 // A packet writer factory that always returns the same writer |
195 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory { | 208 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory { |
196 public: | 209 public: |
197 DummyPacketWriterFactory(QuicPacketWriter* writer); | 210 DummyPacketWriterFactory(QuicPacketWriter* writer); |
198 ~DummyPacketWriterFactory() override; | 211 ~DummyPacketWriterFactory() override; |
199 | 212 |
200 QuicPacketWriter* Create(QuicConnection* connection) const override; | 213 QuicPacketWriter* Create(QuicConnection* connection) const override; |
201 | 214 |
202 private: | 215 private: |
203 QuicPacketWriter* writer_; | 216 QuicPacketWriter* writer_; |
204 }; | 217 }; |
205 | 218 |
206 // Used during initialization: creates the UDP socket FD, sets socket options, | 219 // Used during initialization: creates the UDP socket FD, sets socket options, |
207 // and binds the socket to our address. | 220 // and binds the socket to our address. |
208 bool CreateUDPSocket(); | 221 bool CreateUDPSocket(); |
209 | 222 |
| 223 // If the socket has been created, then unregister and close() the FD. |
| 224 void CleanUpUDPSocket(); |
| 225 |
210 // Read a UDP packet and hand it to the framer. | 226 // Read a UDP packet and hand it to the framer. |
211 bool ReadAndProcessPacket(); | 227 bool ReadAndProcessPacket(); |
212 | 228 |
213 // Used by |helper_| to time alarms. | |
214 QuicClock clock_; | |
215 | |
216 // Address of the server. | 229 // Address of the server. |
217 const IPEndPoint server_address_; | 230 const IPEndPoint server_address_; |
218 | 231 |
219 // |server_id_| is a tuple (hostname, port, is_https) of the server. | 232 // |server_id_| is a tuple (hostname, port, is_https) of the server. |
220 QuicServerId server_id_; | 233 QuicServerId server_id_; |
221 | 234 |
222 // config_ and crypto_config_ contain configuration and cached state about | 235 // config_ and crypto_config_ contain configuration and cached state about |
223 // servers. | 236 // servers. |
224 QuicConfig config_; | 237 QuicConfig config_; |
225 QuicCryptoClientConfig crypto_config_; | 238 QuicCryptoClientConfig crypto_config_; |
226 | 239 |
227 // Address of the client if the client is connected to the server. | 240 // Address of the client if the client is connected to the server. |
228 IPEndPoint client_address_; | 241 IPEndPoint client_address_; |
229 | 242 |
230 // If initialized, the address to bind to. | 243 // If initialized, the address to bind to. |
231 IPAddressNumber bind_to_address_; | 244 IPAddressNumber bind_to_address_; |
232 // Local port to bind to. Initialize to 0. | 245 // Local port to bind to. Initialize to 0. |
233 int local_port_; | 246 int local_port_; |
234 | 247 |
235 // Writer used to actually send packets to the wire. Needs to outlive | 248 // Writer used to actually send packets to the wire. Needs to outlive |
236 // |session_|. | 249 // |session_|. |
237 scoped_ptr<QuicPacketWriter> writer_; | 250 scoped_ptr<QuicPacketWriter> writer_; |
238 | 251 |
239 // Session which manages streams. | 252 // Session which manages streams. |
240 scoped_ptr<QuicSimpleClientSession> session_; | 253 scoped_ptr<QuicClientSession> session_; |
241 | 254 // Listens for events on the client socket. |
242 // UDP socket connected to the server. | 255 EpollServer* epoll_server_; |
243 scoped_ptr<UDPClientSocket> socket_; | 256 // UDP socket. |
244 | 257 int fd_; |
245 // Connection on the socket. Owned by |session_|. | |
246 QuicConnection* connection_; | |
247 | 258 |
248 // Helper to be used by created connections. | 259 // Helper to be used by created connections. |
249 scoped_ptr<QuicConnectionHelper> helper_; | 260 scoped_ptr<QuicEpollConnectionHelper> helper_; |
250 | 261 |
251 // Listens for full responses. | 262 // Listens for full responses. |
252 scoped_ptr<ResponseListener> response_listener_; | 263 scoped_ptr<ResponseListener> response_listener_; |
253 | 264 |
254 // Tracks if the client is initialized to connect. | 265 // Tracks if the client is initialized to connect. |
255 bool initialized_; | 266 bool initialized_; |
256 | 267 |
257 // If overflow_supported_ is true, this will be the number of packets dropped | 268 // If overflow_supported_ is true, this will be the number of packets dropped |
258 // during the lifetime of the server. | 269 // during the lifetime of the server. |
259 QuicPacketCount packets_dropped_; | 270 QuicPacketCount packets_dropped_; |
(...skipping 11 matching lines...) Expand all Loading... |
271 | 282 |
272 // If true, store the latest response code, headers, and body. | 283 // If true, store the latest response code, headers, and body. |
273 bool store_response_; | 284 bool store_response_; |
274 // HTTP response code from most recent response. | 285 // HTTP response code from most recent response. |
275 size_t latest_response_code_; | 286 size_t latest_response_code_; |
276 // HTTP headers from most recent response. | 287 // HTTP headers from most recent response. |
277 std::string latest_response_headers_; | 288 std::string latest_response_headers_; |
278 // Body of most recent response. | 289 // Body of most recent response. |
279 std::string latest_response_body_; | 290 std::string latest_response_body_; |
280 | 291 |
281 bool read_pending_; | 292 DISALLOW_COPY_AND_ASSIGN(QuicClient); |
282 | |
283 // The number of iterations of the read loop that have completed synchronously | |
284 // and without posting a new task to the message loop. | |
285 int synchronous_read_count_; | |
286 | |
287 // The target buffer of the current read. | |
288 scoped_refptr<IOBufferWithSize> read_buffer_; | |
289 | |
290 // The log used for the sockets. | |
291 NetLog net_log_; | |
292 | |
293 base::WeakPtrFactory<QuicSimpleClient> weak_factory_; | |
294 | |
295 DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient); | |
296 }; | 293 }; |
297 | 294 |
298 } // namespace tools | 295 } // namespace tools |
299 } // namespace net | 296 } // namespace net |
300 | 297 |
301 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ | 298 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_ |
OLD | NEW |