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.
5 // A toy server, which listens on a specified address for QUIC traffic and
6 // handles incoming responses.
8 #ifndef NET_TOOLS_QUIC_QUIC_SERVER_H_
9 #define NET_TOOLS_QUIC_QUIC_SERVER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/quic/crypto/crypto_server_config.h"
14 #include "net/quic/quic_config.h"
15 #include "net/quic/quic_framer.h"
16 #include "net/tools/flip_server/epoll_server.h"
17 #include "net/tools/quic/quic_dispatcher.h"
21 class QuicCryptoServerConfig
;
27 class QuicServer
: public EpollCallbackInterface
{
30 explicit QuicServer(const QuicConfig
& config
);
32 virtual ~QuicServer();
34 // Start listening on the specified address.
35 bool Listen(const IPEndPoint
& address
);
37 // Wait up to 50ms, and handle any events which occur.
40 // Server deletion is imminent. Start cleaning up the epoll server.
43 // From EpollCallbackInterface
44 virtual void OnRegistration(
45 EpollServer
* eps
, int fd
, int event_mask
) OVERRIDE
{}
46 virtual void OnModification(int fd
, int event_mask
) OVERRIDE
{}
47 virtual void OnEvent(int fd
, EpollEvent
* event
) OVERRIDE
;
48 virtual void OnUnregistration(int fd
, bool replaced
) OVERRIDE
{}
50 // Reads a packet from the given fd, and then passes it off to
51 // the QuicDispatcher. Returns true if a packet is read, false
53 // If packets_dropped is non-null, the socket is configured to track
54 // dropped packets, and some packets are read, it will be set to the number of
56 static bool ReadAndDispatchSinglePacket(int fd
, int port
,
57 QuicDispatcher
* dispatcher
,
58 int* packets_dropped
);
60 virtual void OnShutdown(EpollServer
* eps
, int fd
) OVERRIDE
{}
62 // Dispatches the given packet only if it looks like a valid QUIC packet.
63 // TODO(rjshade): Return a status describing why a packet was dropped, and log
64 // somehow. Maybe expose as a varz.
65 static void MaybeDispatchPacket(QuicDispatcher
* dispatcher
,
66 const QuicEncryptedPacket
& packet
,
67 const IPEndPoint
& server_address
,
68 const IPEndPoint
& client_address
);
70 bool overflow_supported() { return overflow_supported_
; }
72 int packets_dropped() { return packets_dropped_
; }
74 int port() { return port_
; }
77 // Initialize the internal state of the server.
80 // Accepts data from the framer and demuxes clients to sessions.
81 scoped_ptr
<QuicDispatcher
> dispatcher_
;
82 // Frames incoming packets and hands them to the dispatcher.
83 EpollServer epoll_server_
;
85 // The port the server is listening on.
88 // Listening connection. Also used for outbound client communication.
91 // If overflow_supported_ is true this will be the number of packets dropped
92 // during the lifetime of the server. This may overflow if enough packets
96 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
97 // because the socket would otherwise overflow.
98 bool overflow_supported_
;
100 // If true, use recvmmsg for reading.
103 // config_ contains non-crypto parameters that are negotiated in the crypto
106 // crypto_config_ contains crypto parameters for the handshake.
107 QuicCryptoServerConfig crypto_config_
;
109 DISALLOW_COPY_AND_ASSIGN(QuicServer
);
115 #endif // NET_TOOLS_QUIC_QUIC_SERVER_H_