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 server side dispatcher which dispatches a given client's data to their
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
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"
24 #if defined(COMPILER_GCC)
25 namespace BASE_HASH_NAMESPACE {
27 struct hash<net::QuicBlockedWriterInterface*> {
28 std::size_t operator()(
29 const net::QuicBlockedWriterInterface* ptr) const {
30 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
40 class QuicCryptoServerConfig;
45 class QuicPacketWriterWrapper;
48 class QuicDispatcherPeer;
51 class DeleteSessionsAlarm;
52 class QuicEpollConnectionHelper;
54 class QuicDispatcher : public QuicServerSessionVisitor {
56 // Ideally we'd have a linked_hash_set: the boolean is unused.
57 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
59 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
60 // must live until epoll_server Shutdown. |supported_versions| specifies the
61 // list of supported QUIC versions.
62 QuicDispatcher(const QuicConfig& config,
63 const QuicCryptoServerConfig& crypto_config,
64 const QuicVersionVector& supported_versions,
65 EpollServer* epoll_server,
66 uint32 initial_flow_control_window_bytes);
68 virtual ~QuicDispatcher();
70 virtual void Initialize(int fd);
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);
78 // Called when the socket becomes writable to allow queued writes to happen.
79 virtual void OnCanWrite();
81 // Returns true if there's anything in the blocked writer list.
82 virtual bool HasPendingWrites() const;
84 // Sends ConnectionClose frames to all connected clients.
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;
92 // Queues the blocked writer for later resumption.
93 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
95 typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
97 // Deletes all sessions on the closed session list and clears the list.
98 void DeleteSessions();
100 const SessionMap& session_map() const { return session_map_; }
102 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
105 // Instantiates a new low-level packet writer. Caller takes ownership of the
107 virtual QuicPacketWriter* CreateWriter(int fd);
109 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
110 const IPEndPoint& server_address,
111 const IPEndPoint& client_address);
113 virtual QuicConnection* CreateQuicConnection(
114 QuicConnectionId connection_id,
115 const IPEndPoint& server_address,
116 const IPEndPoint& client_address,
117 uint32 initial_flow_control_window);
119 // Called by |framer_visitor_| when the public header has been parsed.
120 virtual bool OnUnauthenticatedPublicHeader(
121 const QuicPacketPublicHeader& header);
123 // Create and return the time wait list manager for this dispatcher, which
124 // will be owned by the dispatcher as time_wait_list_manager_
125 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
127 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
128 void set_writer(QuicPacketWriter* writer) {
129 writer_.reset(writer);
132 QuicTimeWaitListManager* time_wait_list_manager() {
133 return time_wait_list_manager_.get();
136 EpollServer* epoll_server() { return epoll_server_; }
138 const QuicVersionVector& supported_versions() const {
139 return supported_versions_;
142 const QuicVersionVector& supported_versions_no_flow_control() const {
143 return supported_versions_no_flow_control_;
146 const QuicVersionVector& supported_versions_no_connection_flow_control()
148 return supported_versions_no_connection_flow_control_;
151 const IPEndPoint& current_server_address() {
152 return current_server_address_;
154 const IPEndPoint& current_client_address() {
155 return current_client_address_;
157 const QuicEncryptedPacket& current_packet() {
158 return *current_packet_;
161 const QuicConfig& config() const { return config_; }
163 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
165 QuicFramer* framer() { return &framer_; }
167 QuicEpollConnectionHelper* helper() { return helper_.get(); }
169 QuicPacketWriter* writer() { return writer_.get(); }
171 const uint32 initial_flow_control_window_bytes() const {
172 return initial_flow_control_window_bytes_;
176 class QuicFramerVisitor;
177 friend class net::tools::test::QuicDispatcherPeer;
179 // Called by |framer_visitor_| when the private header has been parsed
180 // of a data packet that is destined for the time wait manager.
181 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
183 // Removes the session from the session map and write blocked list, and
184 // adds the ConnectionId to the time-wait list.
185 void CleanUpSession(SessionMap::iterator it);
187 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
189 const QuicConfig& config_;
191 const QuicCryptoServerConfig& crypto_config_;
193 // The list of connections waiting to write.
194 WriteBlockedList write_blocked_list_;
196 SessionMap session_map_;
198 // Entity that manages connection_ids in time wait state.
199 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
201 // An alarm which deletes closed sessions.
202 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
204 // The list of closed but not-yet-deleted sessions.
205 std::list<QuicSession*> closed_session_list_;
207 EpollServer* epoll_server_; // Owned by the server.
209 // The helper used for all connections.
210 scoped_ptr<QuicEpollConnectionHelper> helper_;
212 // The writer to write to the socket with.
213 scoped_ptr<QuicPacketWriter> writer_;
215 // This vector contains QUIC versions which we currently support.
216 // This should be ordered such that the highest supported version is the first
217 // element, with subsequent elements in descending order (versions can be
218 // skipped as necessary).
219 const QuicVersionVector supported_versions_;
221 // Versions which do not support flow control (introduced in QUIC_VERSION_17).
222 // This is used to construct new QuicConnections when flow control is disabled
224 // TODO(rjshade): Remove this when
225 // FLAGS_enable_quic_stream_flow_control_2 is removed.
226 QuicVersionVector supported_versions_no_flow_control_;
227 // Versions which do not support *connection* flow control (introduced in
229 // This is used to construct new QuicConnections when connection flow control
230 // is disabled via flag.
231 // TODO(rjshade): Remove this when
232 // FLAGS_enable_quic_connection_flow_control is removed.
233 QuicVersionVector supported_versions_no_connection_flow_control_;
235 // Information about the packet currently being handled.
236 IPEndPoint current_client_address_;
237 IPEndPoint current_server_address_;
238 const QuicEncryptedPacket* current_packet_;
241 scoped_ptr<QuicFramerVisitor> framer_visitor_;
243 // Initial flow control window size to advertize to peer on newly created
245 const uint32 initial_flow_control_window_bytes_;
247 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
253 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_