quic_dispatcher.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/basictypes.h"
14#include "base/containers/hash_tables.h"
15#include "base/memory/scoped_ptr.h"
16#include "net/base/ip_endpoint.h"
17#include "net/base/linked_hash_map.h"
18#include "net/quic/quic_blocked_writer_interface.h"
19#include "net/quic/quic_protocol.h"
20#include "net/tools/epoll_server/epoll_server.h"
21#include "net/tools/quic/quic_server_session.h"
22#include "net/tools/quic/quic_time_wait_list_manager.h"
23
24#if defined(COMPILER_GCC)
25namespace BASE_HASH_NAMESPACE {
26template<>
27struct hash<net::QuicBlockedWriterInterface*> {
28  std::size_t operator()(
29      const net::QuicBlockedWriterInterface* ptr) const {
30    return hash<size_t>()(reinterpret_cast<size_t>(ptr));
31  }
32};
33}
34#endif
35
36namespace net {
37
38class EpollServer;
39class QuicConfig;
40class QuicConnectionHelper;
41class QuicCryptoServerConfig;
42class QuicSession;
43
44namespace tools {
45
46class QuicPacketWriterWrapper;
47
48namespace test {
49class QuicDispatcherPeer;
50}  // namespace test
51
52class DeleteSessionsAlarm;
53class QuicEpollConnectionHelper;
54
55class QuicDispatcher : public QuicServerSessionVisitor {
56 public:
57  // Ideally we'd have a linked_hash_set: the  boolean is unused.
58  typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
59
60  // Due to the way delete_sessions_closure_ is registered, the Dispatcher
61  // must live until epoll_server Shutdown. |supported_versions| specifies the
62  // list of supported QUIC versions.
63  QuicDispatcher(const QuicConfig& config,
64                 const QuicCryptoServerConfig& crypto_config,
65                 const QuicVersionVector& supported_versions,
66                 EpollServer* epoll_server);
67
68  virtual ~QuicDispatcher();
69
70  void Initialize(int fd);
71
72  // Process the incoming packet by creating a new session, passing it to
73  // an existing session, or passing it to the TimeWaitListManager.
74  virtual void ProcessPacket(const IPEndPoint& server_address,
75                             const IPEndPoint& client_address,
76                             const QuicEncryptedPacket& packet);
77
78  // Called when the socket becomes writable to allow queued writes to happen.
79  virtual void OnCanWrite();
80
81  // Returns true if there's anything in the blocked writer list.
82  virtual bool HasPendingWrites() const;
83
84  // Sends ConnectionClose frames to all connected clients.
85  void Shutdown();
86
87  // QuicServerSessionVisitor interface implementation:
88  // Ensure that the closed connection is cleaned up asynchronously.
89  virtual void OnConnectionClosed(QuicConnectionId connection_id,
90                                  QuicErrorCode error) OVERRIDE;
91
92  // Queues the blocked writer for later resumption.
93  virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
94
95  typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
96
97  // Deletes all sessions on the closed session list and clears the list.
98  void DeleteSessions();
99
100  const SessionMap& session_map() const { return session_map_; }
101
102  WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
103
104 protected:
105  // Instantiates a new low-level packet writer. Caller takes ownership of the
106  // returned object.
107  QuicPacketWriter* CreateWriter(int fd);
108
109  // Instantiates a new top-level writer wrapper. Takes ownership of |writer|.
110  // Caller takes ownership of the returned object.
111  virtual QuicPacketWriterWrapper* CreateWriterWrapper(
112      QuicPacketWriter* writer);
113
114  virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
115                                         const IPEndPoint& server_address,
116                                         const IPEndPoint& client_address);
117
118  QuicConnection* CreateQuicConnection(QuicConnectionId connection_id,
119                                       const IPEndPoint& server_address,
120                                       const IPEndPoint& client_address);
121
122  // Replaces the packet writer with |writer|. Takes ownership of |writer|.
123  void set_writer(QuicPacketWriter* writer);
124
125  QuicTimeWaitListManager* time_wait_list_manager() {
126    return time_wait_list_manager_.get();
127  }
128
129  EpollServer* epoll_server() { return epoll_server_; }
130
131  const QuicVersionVector& supported_versions() const {
132    return supported_versions_;
133  }
134
135  // Called by |framer_visitor_| when the public header has been parsed.
136  virtual bool OnUnauthenticatedPublicHeader(
137      const QuicPacketPublicHeader& header);
138
139  const IPEndPoint& current_server_address() {
140    return current_server_address_;
141  }
142  const QuicEncryptedPacket& current_packet() {
143    return *current_packet_;
144  }
145
146  const QuicConfig& config() const { return config_; }
147
148  const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
149
150 private:
151  class QuicFramerVisitor;
152  friend class net::tools::test::QuicDispatcherPeer;
153
154  // Called by |framer_visitor_| when the private header has been parsed
155  // of a data packet that is destined for the time wait manager.
156  void OnUnauthenticatedHeader(const QuicPacketHeader& header);
157
158  // Removes the session from the session map and write blocked list, and
159  // adds the ConnectionId to the time-wait list.
160  void CleanUpSession(SessionMap::iterator it);
161
162  bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
163
164  const QuicConfig& config_;
165
166  const QuicCryptoServerConfig& crypto_config_;
167
168  // The list of connections waiting to write.
169  WriteBlockedList write_blocked_list_;
170
171  SessionMap session_map_;
172
173  // Entity that manages connection_ids in time wait state.
174  scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
175
176  // An alarm which deletes closed sessions.
177  scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
178
179  // The list of closed but not-yet-deleted sessions.
180  std::list<QuicSession*> closed_session_list_;
181
182  EpollServer* epoll_server_;  // Owned by the server.
183
184  // The helper used for all connections.
185  scoped_ptr<QuicEpollConnectionHelper> helper_;
186
187  // The writer to write to the socket with. We require a writer wrapper to
188  // allow replacing writer implementation without disturbing running
189  // connections.
190  scoped_ptr<QuicPacketWriterWrapper> writer_;
191
192  // This vector contains QUIC versions which we currently support.
193  // This should be ordered such that the highest supported version is the first
194  // element, with subsequent elements in descending order (versions can be
195  // skipped as necessary).
196  const QuicVersionVector supported_versions_;
197
198  // Information about the packet currently being handled.
199  IPEndPoint current_client_address_;
200  IPEndPoint current_server_address_;
201  const QuicEncryptedPacket* current_packet_;
202
203  QuicFramer framer_;
204  scoped_ptr<QuicFramerVisitor> framer_visitor_;
205
206  DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
207};
208
209}  // namespace tools
210}  // namespace net
211
212#endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
213