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 crypto_config()76 quic::QuicCryptoServerConfig* crypto_config() { return &crypto_config_; } 77 78 private: 79 friend class test::QuicSimpleServerPeer; 80 81 // Initialize the internal state of the server. 82 void Initialize(); 83 84 quic::QuicVersionManager version_manager_; 85 86 // Accepts data from the framer and demuxes clients to sessions. 87 std::unique_ptr<quic::QuicDispatcher> dispatcher_; 88 89 // Used by the helper_ to time alarms. 90 quic::QuicChromiumClock clock_; 91 92 // Used to manage the message loop. Owned by dispatcher_. 93 QuicChromiumConnectionHelper* helper_; 94 95 // Used to manage the message loop. Owned by dispatcher_. 96 QuicChromiumAlarmFactory* alarm_factory_; 97 98 // Listening socket. Also used for outbound client communication. 99 std::unique_ptr<UDPServerSocket> socket_; 100 101 // config_ contains non-crypto parameters that are negotiated in the crypto 102 // handshake. 103 quic::QuicConfig config_; 104 // crypto_config_ contains crypto parameters that are negotiated in the crypto 105 // handshake. 106 quic::QuicCryptoServerConfig::ConfigOptions crypto_config_options_; 107 // crypto_config_ contains crypto parameters for the handshake. 108 quic::QuicCryptoServerConfig crypto_config_; 109 110 // The address that the server listens on. 111 IPEndPoint server_address_; 112 113 // Keeps track of whether a read is currently in flight, after which 114 // OnReadComplete will be called. 115 bool read_pending_ = false; 116 117 // The number of iterations of the read loop that have completed synchronously 118 // and without posting a new task to the message loop. 119 int synchronous_read_count_ = 0; 120 121 // The target buffer of the current read. 122 scoped_refptr<IOBufferWithSize> read_buffer_; 123 124 // The source address of the current read. 125 IPEndPoint client_address_; 126 127 quic::QuicSimpleServerBackend* quic_simple_server_backend_; 128 129 quic::DeterministicConnectionIdGenerator connection_id_generator_; 130 131 base::WeakPtrFactory<QuicSimpleServer> weak_factory_{this}; 132 }; 133 134 } // namespace net 135 136 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_SERVER_H_ 137