• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
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 server, which listens on a specified address for QUIC traffic and
6 // handles incoming responses.
7 
8 #ifndef NET_TOOLS_QUIC_QUIC_SIMPLE_SERVER_H_
9 #define NET_TOOLS_QUIC_QUIC_SIMPLE_SERVER_H_
10 
11 #include <memory>
12 
13 #include "net/base/io_buffer.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/quic/platform/impl/quic_chromium_clock.h"
16 #include "net/quic/quic_chromium_alarm_factory.h"
17 #include "net/quic/quic_chromium_connection_helper.h"
18 #include "net/third_party/quiche/src/quiche/quic/core/crypto/quic_crypto_server_config.h"
19 #include "net/third_party/quiche/src/quiche/quic/core/deterministic_connection_id_generator.h"
20 #include "net/third_party/quiche/src/quiche/quic/core/quic_config.h"
21 #include "net/third_party/quiche/src/quiche/quic/core/quic_version_manager.h"
22 #include "net/third_party/quiche/src/quiche/quic/tools/quic_simple_server_backend.h"
23 #include "net/third_party/quiche/src/quiche/quic/tools/quic_spdy_server_base.h"
24 
25 namespace net {
26 
27 class UDPServerSocket;
28 
29 }  // namespace net
30 namespace quic {
31 class QuicDispatcher;
32 }  // namespace quic
33 namespace net {
34 
35 namespace test {
36 class QuicSimpleServerPeer;
37 }  // namespace test
38 
39 class QuicSimpleServer : public quic::QuicSpdyServerBase {
40  public:
41   QuicSimpleServer(
42       std::unique_ptr<quic::ProofSource> proof_source,
43       const quic::QuicConfig& config,
44       const quic::QuicCryptoServerConfig::ConfigOptions& crypto_config_options,
45       const quic::ParsedQuicVersionVector& supported_versions,
46       quic::QuicSimpleServerBackend* quic_simple_server_backend);
47 
48   QuicSimpleServer(const QuicSimpleServer&) = delete;
49   QuicSimpleServer& operator=(const QuicSimpleServer&) = delete;
50 
51   ~QuicSimpleServer() override;
52 
53   // QuicSpdyServerBase methods:
54   bool CreateUDPSocketAndListen(
55       const quic::QuicSocketAddress& address) override;
56   void HandleEventsForever() override;
57 
58   // Start listening on the specified address. Returns true on success.
59   bool Listen(const IPEndPoint& address);
60 
61   // Server deletion is imminent. Start cleaning up.
62   void Shutdown();
63 
64   // Start reading on the socket. On asynchronous reads, this registers
65   // OnReadComplete as the callback, which will then call StartReading again.
66   void StartReading();
67 
68   // Called on reads that complete asynchronously. Dispatches the packet and
69   // continues the read loop.
70   void OnReadComplete(int result);
71 
dispatcher()72   quic::QuicDispatcher* dispatcher() { return dispatcher_.get(); }
73 
server_address()74   IPEndPoint server_address() const { return server_address_; }
75 
76  private:
77   friend class test::QuicSimpleServerPeer;
78 
79   // Initialize the internal state of the server.
80   void Initialize();
81 
82   quic::QuicVersionManager version_manager_;
83 
84   // Accepts data from the framer and demuxes clients to sessions.
85   std::unique_ptr<quic::QuicDispatcher> dispatcher_;
86 
87   // Used by the helper_ to time alarms.
88   quic::QuicChromiumClock clock_;
89 
90   // Used to manage the message loop. Owned by dispatcher_.
91   QuicChromiumConnectionHelper* helper_;
92 
93   // Used to manage the message loop. Owned by dispatcher_.
94   QuicChromiumAlarmFactory* alarm_factory_;
95 
96   // Listening socket. Also used for outbound client communication.
97   std::unique_ptr<UDPServerSocket> socket_;
98 
99   // config_ contains non-crypto parameters that are negotiated in the crypto
100   // handshake.
101   quic::QuicConfig config_;
102   // crypto_config_ contains crypto parameters that are negotiated in the crypto
103   // handshake.
104   quic::QuicCryptoServerConfig::ConfigOptions crypto_config_options_;
105   // crypto_config_ contains crypto parameters for the handshake.
106   quic::QuicCryptoServerConfig crypto_config_;
107 
108   // The address that the server listens on.
109   IPEndPoint server_address_;
110 
111   // Keeps track of whether a read is currently in flight, after which
112   // OnReadComplete will be called.
113   bool read_pending_ = false;
114 
115   // The number of iterations of the read loop that have completed synchronously
116   // and without posting a new task to the message loop.
117   int synchronous_read_count_ = 0;
118 
119   // The target buffer of the current read.
120   scoped_refptr<IOBufferWithSize> read_buffer_;
121 
122   // The source address of the current read.
123   IPEndPoint client_address_;
124 
125   quic::QuicSimpleServerBackend* quic_simple_server_backend_;
126 
127   quic::DeterministicConnectionIdGenerator connection_id_generator_;
128 
129   base::WeakPtrFactory<QuicSimpleServer> weak_factory_{this};
130 };
131 
132 }  // namespace net
133 
134 #endif  // NET_TOOLS_QUIC_QUIC_SIMPLE_SERVER_H_
135