quic_client.h revision 868fa2fe829687343ffae624259930155e16dbd8
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.
7
8#ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_
9#define NET_TOOLS_QUIC_QUIC_CLIENT_H_
10
11#include <string>
12
13#include "base/command_line.h"
14#include "base/hash_tables.h"
15#include "base/memory/scoped_ptr.h"
16#include "net/base/ip_endpoint.h"
17#include "net/quic/crypto/crypto_handshake.h"
18#include "net/quic/quic_config.h"
19#include "net/quic/quic_framer.h"
20#include "net/quic/quic_packet_creator.h"
21#include "net/tools/flip_server/epoll_server.h"
22#include "net/tools/quic/quic_client_session.h"
23#include "net/tools/quic/quic_reliable_client_stream.h"
24
25namespace net {
26namespace tools {
27
28namespace test {
29class QuicClientPeer;
30}  // namespace test
31
32class QuicClient : public EpollCallbackInterface {
33 public:
34  QuicClient(IPEndPoint server_address, const std::string& server_hostname);
35  QuicClient(IPEndPoint server_address,
36             const std::string& server_hostname,
37             const QuicConfig& config);
38
39  virtual ~QuicClient();
40
41  // Initializes the client to create a connection. Should be called exactly
42  // once before calling StartConnect or Connect. Returns true if the
43  // initialization succeeds, false otherwise.
44  bool Initialize();
45
46  // "Connect" to the QUIC server, including performing synchronous crypto
47  // handshake.
48  bool Connect();
49
50  // Start the crypto handshake.  This can be done in place of the synchronous
51  // Connect(), but callers are responsible for making sure the crypto handshake
52  // completes.
53  bool StartConnect();
54
55  // Returns true if the crypto handshake has yet to establish encryption.
56  // Returns false if encryption is active (even if the server hasn't confirmed
57  // the handshake) or if the connection has been closed.
58  bool EncryptionBeingEstablished();
59
60  // Disconnects from the QUIC server.
61  void Disconnect();
62
63  // Sends a request simple GET for each URL in arg, and then waits for
64  // each to complete.
65  void SendRequestsAndWaitForResponse(const CommandLine::StringVector& args);
66
67  // Returns a newly created CreateReliableClientStream, owned by the
68  // QuicClient.
69  QuicReliableClientStream* CreateReliableClientStream();
70
71  // Wait for events until the stream with the given ID is closed.
72  void WaitForStreamToClose(QuicStreamId id);
73
74  // Wait up to 50ms, and handle any events which occur.
75  // Returns true if there are any outstanding requests.
76  bool WaitForEvents();
77
78  // From EpollCallbackInterface
79  virtual void OnRegistration(
80      EpollServer* eps, int fd, int event_mask) OVERRIDE {}
81  virtual void OnModification(int fd, int event_mask) OVERRIDE {}
82  virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
83  // |fd_| can be unregistered without the client being disconnected. This
84  // happens in b3m QuicProber where we unregister |fd_| to feed in events to
85  // the client from the SelectServer.
86  virtual void OnUnregistration(int fd, bool replaced) OVERRIDE {}
87  virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
88
89  QuicPacketCreator::Options* options();
90
91  QuicClientSession* session() { return session_.get(); }
92
93  bool connected() const;
94
95  int packets_dropped() { return packets_dropped_; }
96
97  void set_bind_to_address(IPAddressNumber address) {
98    bind_to_address_ = address;
99  }
100
101  IPAddressNumber bind_to_address() const { return bind_to_address_; }
102
103  void set_local_port(int local_port) { local_port_ = local_port; }
104
105  int local_port() { return local_port_; }
106
107  const IPEndPoint& server_address() const { return server_address_; }
108
109  const IPEndPoint& client_address() const { return client_address_; }
110
111  EpollServer* epoll_server() { return &epoll_server_; }
112
113  int fd() { return fd_; }
114
115  // This should only be set before the initial Connect()
116  void set_server_hostname(const string& hostname) {
117    server_hostname_ = hostname;
118  }
119
120 private:
121  friend class net::tools::test::QuicClientPeer;
122
123  // Read a UDP packet and hand it to the framer.
124  bool ReadAndProcessPacket();
125
126  // Set of streams created (and owned) by this client
127  base::hash_set<QuicReliableClientStream*> streams_;
128
129  // Address of the server.
130  const IPEndPoint server_address_;
131
132  // Hostname of the server. This may be a DNS name or an IP address literal.
133  std::string server_hostname_;
134
135  // config_ and crypto_config_ contain configuration and cached state about
136  // servers.
137  QuicConfig config_;
138  QuicCryptoClientConfig crypto_config_;
139
140  // Address of the client if the client is connected to the server.
141  IPEndPoint client_address_;
142
143  // If initialized, the address to bind to.
144  IPAddressNumber bind_to_address_;
145  // Local port to bind to. Initialize to 0.
146  int local_port_;
147
148  // Session which manages streams.
149  scoped_ptr<QuicClientSession> session_;
150  // Listens for events on the client socket.
151  EpollServer epoll_server_;
152  // UDP socket.
153  int fd_;
154
155  // Tracks if the client is initialized to connect.
156  bool initialized_;
157
158  // If overflow_supported_ is true, this will be the number of packets dropped
159  // during the lifetime of the server.  This may overflow if enough packets
160  // are dropped.
161  int packets_dropped_;
162
163  // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
164  // because the socket would otherwise overflow.
165  bool overflow_supported_;
166
167  DISALLOW_COPY_AND_ASSIGN(QuicClient);
168};
169
170}  // namespace tools
171}  // namespace net
172
173#endif  // NET_TOOLS_QUIC_QUIC_CLIENT_H_
174