quic_simple_server.h 4.3 KB

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