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 server side dispatcher which dispatches a given client's data to their
      6 // stream.
      7 
      8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
      9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
     10 
     11 #include <list>
     12 
     13 #include "base/containers/hash_tables.h"
     14 #include "net/base/ip_endpoint.h"
     15 #include "net/quic/blocked_list.h"
     16 #include "net/quic/quic_blocked_writer_interface.h"
     17 #include "net/quic/quic_protocol.h"
     18 #include "net/tools/flip_server/epoll_server.h"
     19 #include "net/tools/quic/quic_packet_writer.h"
     20 #include "net/tools/quic/quic_server_session.h"
     21 #include "net/tools/quic/quic_time_wait_list_manager.h"
     22 
     23 #if defined(COMPILER_GCC)
     24 namespace BASE_HASH_NAMESPACE {
     25 template<>
     26 struct hash<net::QuicBlockedWriterInterface*> {
     27   std::size_t operator()(
     28       const net::QuicBlockedWriterInterface* ptr) const {
     29     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
     30   }
     31 };
     32 }
     33 #endif
     34 
     35 namespace net {
     36 
     37 class EpollServer;
     38 class QuicConfig;
     39 class QuicCryptoServerConfig;
     40 class QuicSession;
     41 
     42 namespace tools {
     43 
     44 namespace test {
     45 class QuicDispatcherPeer;
     46 }  // namespace test
     47 
     48 class DeleteSessionsAlarm;
     49 class QuicDispatcher : public QuicPacketWriter, public QuicSessionOwner {
     50  public:
     51   typedef BlockedList<QuicBlockedWriterInterface*> WriteBlockedList;
     52 
     53   // Due to the way delete_sessions_closure_ is registered, the Dispatcher
     54   // must live until epoll_server Shutdown.
     55   QuicDispatcher(const QuicConfig& config,
     56                  const QuicCryptoServerConfig& crypto_config,
     57                  int fd,
     58                  EpollServer* epoll_server);
     59   virtual ~QuicDispatcher();
     60 
     61   // QuicPacketWriter
     62   virtual int WritePacket(const char* buffer, size_t buf_len,
     63                           const IPAddressNumber& self_address,
     64                           const IPEndPoint& peer_address,
     65                           QuicBlockedWriterInterface* writer,
     66                           int* error) OVERRIDE;
     67 
     68   virtual void ProcessPacket(const IPEndPoint& server_address,
     69                              const IPEndPoint& client_address,
     70                              QuicGuid guid,
     71                              const QuicEncryptedPacket& packet);
     72 
     73   // Called when the underyling connection becomes writable to allow
     74   // queued writes to happen.
     75   //
     76   // Returns true if more writes are possible, false otherwise.
     77   virtual bool OnCanWrite();
     78 
     79   // Sends ConnectionClose frames to all connected clients.
     80   void Shutdown();
     81 
     82   // Ensure that the closed connection is cleaned up asynchronously.
     83   virtual void OnConnectionClose(QuicGuid guid, QuicErrorCode error) OVERRIDE;
     84 
     85   int fd() { return fd_; }
     86   void set_fd(int fd) { fd_ = fd; }
     87 
     88   typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
     89 
     90   virtual QuicSession* CreateQuicSession(
     91       QuicGuid guid,
     92       const IPEndPoint& client_address,
     93       int fd,
     94       EpollServer* epoll_server);
     95 
     96   // Deletes all sessions on the closed session list and clears the list.
     97   void DeleteSessions();
     98 
     99   const SessionMap& session_map() const { return session_map_; }
    100 
    101   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
    102 
    103  protected:
    104   const QuicConfig& config_;
    105   const QuicCryptoServerConfig& crypto_config_;
    106 
    107   QuicTimeWaitListManager* time_wait_list_manager() {
    108     return time_wait_list_manager_.get();
    109   }
    110 
    111  private:
    112   friend class net::tools::test::QuicDispatcherPeer;
    113 
    114   // Removes the session from the session map and write blocked list, and
    115   // adds the GUID to the time-wait list.
    116   void CleanUpSession(SessionMap::iterator it);
    117 
    118   // The list of connections waiting to write.
    119   WriteBlockedList write_blocked_list_;
    120 
    121   SessionMap session_map_;
    122 
    123   // Entity that manages guids in time wait state.
    124   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
    125 
    126   // An alarm which deletes closed sessions.
    127   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
    128 
    129   // The list of closed but not-yet-deleted sessions.
    130   std::list<QuicSession*> closed_session_list_;
    131 
    132   EpollServer* epoll_server_;  // Owned by the server.
    133 
    134   // The connection for client-server communication
    135   int fd_;
    136 
    137   // True if the session is write blocked due to the socket returning EAGAIN.
    138   // False if we have gotten a call to OnCanWrite after the last failed write.
    139   bool write_blocked_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
    142 };
    143 
    144 }  // namespace tools
    145 }  // namespace net
    146 
    147 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
    148