base: Make it possible to replace the MessageLoop's task runner
[chromium-blink-merge.git] / net / tools / quic / quic_client.h
blob609ad44b8eccaf00e157861ce9cebe852b3cd578
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
3 // found in the LICENSE file.
4 //
5 // A toy client, which connects to a specified port and sends QUIC
6 // request to that endpoint.
8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_
9 #define NET_TOOLS_QUIC_QUIC_CLIENT_H_
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/quic/crypto/crypto_handshake.h"
19 #include "net/quic/quic_config.h"
20 #include "net/quic/quic_framer.h"
21 #include "net/quic/quic_packet_creator.h"
22 #include "net/tools/balsa/balsa_headers.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 namespace net {
29 class ProofVerifier;
30 class QuicServerId;
32 namespace tools {
34 class QuicEpollConnectionHelper;
36 namespace test {
37 class QuicClientPeer;
38 } // namespace test
40 class QuicClient : public EpollCallbackInterface,
41 public QuicDataStream::Visitor {
42 public:
43 class ResponseListener {
44 public:
45 ResponseListener() {}
46 virtual ~ResponseListener() {}
47 virtual void OnCompleteResponse(QuicStreamId id,
48 const BalsaHeaders& response_headers,
49 const std::string& response_body) = 0;
52 // A packet writer factory that always returns the same writer.
53 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory {
54 public:
55 explicit DummyPacketWriterFactory(QuicPacketWriter* writer);
56 ~DummyPacketWriterFactory() override;
58 QuicPacketWriter* Create(QuicConnection* connection) const override;
60 private:
61 QuicPacketWriter* writer_;
64 // Create a quic client, which will have events managed by an externally owned
65 // EpollServer.
66 QuicClient(IPEndPoint server_address,
67 const QuicServerId& server_id,
68 const QuicVersionVector& supported_versions,
69 EpollServer* epoll_server);
70 QuicClient(IPEndPoint server_address,
71 const QuicServerId& server_id,
72 const QuicVersionVector& supported_versions,
73 const QuicConfig& config,
74 EpollServer* epoll_server);
76 ~QuicClient() override;
78 // Initializes the client to create a connection. Should be called exactly
79 // once before calling StartConnect or Connect. Returns true if the
80 // initialization succeeds, false otherwise.
81 bool Initialize();
83 // "Connect" to the QUIC server, including performing synchronous crypto
84 // handshake.
85 bool Connect();
87 // Start the crypto handshake. This can be done in place of the synchronous
88 // Connect(), but callers are responsible for making sure the crypto handshake
89 // completes.
90 void StartConnect();
92 // Returns true if the crypto handshake has yet to establish encryption.
93 // Returns false if encryption is active (even if the server hasn't confirmed
94 // the handshake) or if the connection has been closed.
95 bool EncryptionBeingEstablished();
97 // Disconnects from the QUIC server.
98 void Disconnect();
100 // Sends an HTTP request and does not wait for response before returning.
101 void SendRequest(const BalsaHeaders& headers,
102 base::StringPiece body,
103 bool fin);
105 // Sends an HTTP request and waits for response before returning.
106 void SendRequestAndWaitForResponse(const BalsaHeaders& headers,
107 base::StringPiece body,
108 bool fin);
110 // Sends a request simple GET for each URL in |args|, and then waits for
111 // each to complete.
112 void SendRequestsAndWaitForResponse(
113 const std::vector<std::string>& url_list);
115 // Returns a newly created QuicSpdyClientStream, owned by the
116 // QuicClient.
117 QuicSpdyClientStream* CreateReliableClientStream();
119 // Wait for events until the stream with the given ID is closed.
120 void WaitForStreamToClose(QuicStreamId id);
122 // Wait for events until the handshake is confirmed.
123 void WaitForCryptoHandshakeConfirmed();
125 // Wait up to 50ms, and handle any events which occur.
126 // Returns true if there are any outstanding requests.
127 bool WaitForEvents();
129 // From EpollCallbackInterface
130 void OnRegistration(EpollServer* eps, int fd, int event_mask) override {}
131 void OnModification(int fd, int event_mask) override {}
132 void OnEvent(int fd, EpollEvent* event) override;
133 // |fd_| can be unregistered without the client being disconnected. This
134 // happens in b3m QuicProber where we unregister |fd_| to feed in events to
135 // the client from the SelectServer.
136 void OnUnregistration(int fd, bool replaced) override {}
137 void OnShutdown(EpollServer* eps, int fd) override {}
139 // QuicDataStream::Visitor
140 void OnClose(QuicDataStream* stream) override;
142 QuicClientSession* session() { return session_.get(); }
144 bool connected() const;
145 bool goaway_received() const;
147 void set_bind_to_address(IPAddressNumber address) {
148 bind_to_address_ = address;
151 IPAddressNumber bind_to_address() const { return bind_to_address_; }
153 void set_local_port(int local_port) { local_port_ = local_port; }
155 const IPEndPoint& server_address() const { return server_address_; }
157 const IPEndPoint& client_address() const { return client_address_; }
159 int fd() { return fd_; }
161 const QuicServerId& server_id() const { return server_id_; }
163 // This should only be set before the initial Connect()
164 void set_server_id(const QuicServerId& server_id) {
165 server_id_ = server_id;
168 void SetUserAgentID(const std::string& user_agent_id) {
169 crypto_config_.set_user_agent_id(user_agent_id);
172 // SetProofVerifier sets the ProofVerifier that will be used to verify the
173 // server's certificate and takes ownership of |verifier|.
174 void SetProofVerifier(ProofVerifier* verifier) {
175 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
176 crypto_config_.SetProofVerifier(verifier);
179 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
180 // server supports channel IDs, to obtain a channel ID for signing a message
181 // proving possession of the channel ID. This object takes ownership of
182 // |source|.
183 void SetChannelIDSource(ChannelIDSource* source) {
184 crypto_config_.SetChannelIDSource(source);
187 void SetSupportedVersions(const QuicVersionVector& versions) {
188 supported_versions_ = versions;
191 // Takes ownership of the listener.
192 void set_response_listener(ResponseListener* listener) {
193 response_listener_.reset(listener);
196 QuicConfig* config() { return &config_; }
198 void set_store_response(bool val) { store_response_ = val; }
200 size_t latest_response_code() const;
201 const std::string& latest_response_headers() const;
202 const std::string& latest_response_body() const;
204 protected:
205 virtual QuicConnectionId GenerateConnectionId();
206 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
207 virtual QuicPacketWriter* CreateQuicPacketWriter();
209 virtual int ReadPacket(char* buffer,
210 int buffer_len,
211 IPEndPoint* server_address,
212 IPAddressNumber* client_ip);
214 virtual QuicClientSession* CreateQuicClientSession(
215 const QuicConfig& config,
216 QuicConnection* connection,
217 const QuicServerId& server_id,
218 QuicCryptoClientConfig* crypto_config);
220 EpollServer* epoll_server() { return epoll_server_; }
222 // If the socket has been created, then unregister and close() the FD.
223 virtual void CleanUpUDPSocket();
225 private:
226 friend class net::tools::test::QuicClientPeer;
228 // Used during initialization: creates the UDP socket FD, sets socket options,
229 // and binds the socket to our address.
230 bool CreateUDPSocket();
232 // Actually clean up the socket.
233 void CleanUpUDPSocketImpl();
235 // Read a UDP packet and hand it to the framer.
236 bool ReadAndProcessPacket();
238 // Address of the server.
239 const IPEndPoint server_address_;
241 // |server_id_| is a tuple (hostname, port, is_https) of the server.
242 QuicServerId server_id_;
244 // config_ and crypto_config_ contain configuration and cached state about
245 // servers.
246 QuicConfig config_;
247 QuicCryptoClientConfig crypto_config_;
249 // Address of the client if the client is connected to the server.
250 IPEndPoint client_address_;
252 // If initialized, the address to bind to.
253 IPAddressNumber bind_to_address_;
254 // Local port to bind to. Initialize to 0.
255 int local_port_;
257 // Writer used to actually send packets to the wire. Needs to outlive
258 // |session_|.
259 scoped_ptr<QuicPacketWriter> writer_;
261 // Session which manages streams.
262 scoped_ptr<QuicClientSession> session_;
263 // Listens for events on the client socket.
264 EpollServer* epoll_server_;
265 // UDP socket.
266 int fd_;
268 // Helper to be used by created connections.
269 scoped_ptr<QuicEpollConnectionHelper> helper_;
271 // Listens for full responses.
272 scoped_ptr<ResponseListener> response_listener_;
274 // Tracks if the client is initialized to connect.
275 bool initialized_;
277 // If overflow_supported_ is true, this will be the number of packets dropped
278 // during the lifetime of the server.
279 QuicPacketCount packets_dropped_;
281 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
282 // because the socket would otherwise overflow.
283 bool overflow_supported_;
285 // This vector contains QUIC versions which we currently support.
286 // This should be ordered such that the highest supported version is the first
287 // element, with subsequent elements in descending order (versions can be
288 // skipped as necessary). We will always pick supported_versions_[0] as the
289 // initial version to use.
290 QuicVersionVector supported_versions_;
292 // If true, store the latest response code, headers, and body.
293 bool store_response_;
294 // HTTP response code from most recent response.
295 size_t latest_response_code_;
296 // HTTP headers from most recent response.
297 std::string latest_response_headers_;
298 // Body of most recent response.
299 std::string latest_response_body_;
301 DISALLOW_COPY_AND_ASSIGN(QuicClient);
304 } // namespace tools
305 } // namespace net
307 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_