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
8#ifndef NET_QUIC_QUIC_SERVER_H_
9#define NET_QUIC_QUIC_SERVER_H_
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "net/base/io_buffer.h"
14#include "net/base/ip_endpoint.h"
15#include "net/base/net_log.h"
16#include "net/quic/crypto/quic_crypto_server_config.h"
17#include "net/quic/quic_clock.h"
18#include "net/quic/quic_config.h"
19#include "net/quic/quic_connection_helper.h"
20
21namespace net {
22
23
24namespace test {
25class QuicServerPeer;
26}  // namespace test
27
28class QuicDispatcher;
29class UDPServerSocket;
30
31class QuicServer {
32 public:
33  QuicServer(const QuicConfig& config,
34             const QuicVersionVector& supported_versions);
35
36  virtual ~QuicServer();
37
38  // Start listening on the specified address. Returns an error code.
39  int Listen(const IPEndPoint& address);
40
41  // Server deletion is imminent. Start cleaning up.
42  void Shutdown();
43
44  // Start reading on the socket. On asynchronous reads, this registers
45  // OnReadComplete as the callback, which will then call StartReading again.
46  void StartReading();
47
48  // Called on reads that complete asynchronously. Dispatches the packet and
49  // continues the read loop.
50  void OnReadComplete(int result);
51
52  void SetStrikeRegisterNoStartupPeriod() {
53    crypto_config_.set_strike_register_no_startup_period();
54  }
55
56  // SetProofSource sets the ProofSource that will be used to verify the
57  // server's certificate, and takes ownership of |source|.
58  void SetProofSource(ProofSource* source) {
59    crypto_config_.SetProofSource(source);
60  }
61
62 private:
63  friend class net::test::QuicServerPeer;
64
65  // Initialize the internal state of the server.
66  void Initialize();
67
68  // Accepts data from the framer and demuxes clients to sessions.
69  scoped_ptr<QuicDispatcher> dispatcher_;
70
71  // Used by the helper_ to time alarms.
72  QuicClock clock_;
73
74  // Used to manage the message loop.
75  QuicConnectionHelper helper_;
76
77  // Listening socket. Also used for outbound client communication.
78  scoped_ptr<UDPServerSocket> socket_;
79
80  // config_ contains non-crypto parameters that are negotiated in the crypto
81  // handshake.
82  QuicConfig config_;
83  // crypto_config_ contains crypto parameters for the handshake.
84  QuicCryptoServerConfig crypto_config_;
85
86  // This vector contains QUIC versions which we currently support.
87  // This should be ordered such that the highest supported version is the first
88  // element, with subsequent elements in descending order (versions can be
89  // skipped as necessary).
90  QuicVersionVector supported_versions_;
91
92  // The address that the server listens on.
93  IPEndPoint server_address_;
94
95  // Keeps track of whether a read is currently in flight, after which
96  // OnReadComplete will be called.
97  bool read_pending_;
98
99  // The number of iterations of the read loop that have completed synchronously
100  // and without posting a new task to the message loop.
101  int synchronous_read_count_;
102
103  // The target buffer of the current read.
104  scoped_refptr<IOBufferWithSize> read_buffer_;
105
106  // The source address of the current read.
107  IPEndPoint client_address_;
108
109  // The log to use for the socket.
110  NetLog net_log_;
111
112  base::WeakPtrFactory<QuicServer> weak_factory_;
113
114  DISALLOW_COPY_AND_ASSIGN(QuicServer);
115};
116
117}  // namespace net
118
119#endif  // NET_QUIC_QUIC_SERVER_H_
120