Home | History | Annotate | Download | only in quic
      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.
      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_SERVER_H_
      9 #define NET_TOOLS_QUIC_QUIC_SERVER_H_
     10 
     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"
     18 
     19 namespace net {
     20 
     21 class QuicCryptoServerConfig;
     22 
     23 namespace tools {
     24 
     25 class QuicDispatcher;
     26 
     27 class QuicServer : public EpollCallbackInterface {
     28  public:
     29   QuicServer();
     30   explicit QuicServer(const QuicConfig& config);
     31 
     32   virtual ~QuicServer();
     33 
     34   // Start listening on the specified address.
     35   bool Listen(const IPEndPoint& address);
     36 
     37   // Wait up to 50ms, and handle any events which occur.
     38   void WaitForEvents();
     39 
     40   // Server deletion is imminent.  Start cleaning up the epoll server.
     41   void Shutdown();
     42 
     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 {}
     49 
     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
     52   // otherwise.
     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
     55   // dropped packets.
     56   static bool ReadAndDispatchSinglePacket(int fd, int port,
     57                                           QuicDispatcher* dispatcher,
     58                                           int* packets_dropped);
     59 
     60   virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
     61 
     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);
     69 
     70   bool overflow_supported() { return overflow_supported_; }
     71 
     72   int packets_dropped() { return packets_dropped_; }
     73 
     74   int port() { return port_; }
     75 
     76  private:
     77   // Initialize the internal state of the server.
     78   void Initialize();
     79 
     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_;
     84 
     85   // The port the server is listening on.
     86   int port_;
     87 
     88   // Listening connection.  Also used for outbound client communication.
     89   int fd_;
     90 
     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
     93   // are dropped.
     94   int packets_dropped_;
     95 
     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_;
     99 
    100   // If true, use recvmmsg for reading.
    101   bool use_recvmmsg_;
    102 
    103   // config_ contains non-crypto parameters that are negotiated in the crypto
    104   // handshake.
    105   QuicConfig config_;
    106   // crypto_config_ contains crypto parameters for the handshake.
    107   QuicCryptoServerConfig crypto_config_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(QuicServer);
    110 };
    111 
    112 }  // namespace tools
    113 }  // namespace net
    114 
    115 #endif  // NET_TOOLS_QUIC_QUIC_SERVER_H_
    116