Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / tools / quic / quic_dispatcher.h
blobb19d5c545054156c15c315c9e9a7694e9bd8aa1b
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.
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
11 #include <list>
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 {
26 template<>
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));
34 #endif
36 namespace net {
38 class EpollServer;
39 class QuicConfig;
40 class QuicCryptoServerConfig;
41 class QuicSession;
43 namespace tools {
45 class QuicPacketWriterWrapper;
47 namespace test {
48 class QuicDispatcherPeer;
49 } // namespace test
51 class DeleteSessionsAlarm;
52 class QuicEpollConnectionHelper;
54 class QuicDispatcher : public QuicServerSessionVisitor {
55 public:
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.
85 void Shutdown();
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_; }
104 protected:
105 // Instantiates a new low-level packet writer. Caller takes ownership of the
106 // returned object.
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);
118 // Called by |framer_visitor_| when the public header has been parsed.
119 virtual bool OnUnauthenticatedPublicHeader(
120 const QuicPacketPublicHeader& header);
122 // Create and return the time wait list manager for this dispatcher, which
123 // will be owned by the dispatcher as time_wait_list_manager_
124 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
126 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
127 void set_writer(QuicPacketWriter* writer) {
128 writer_.reset(writer);
131 QuicTimeWaitListManager* time_wait_list_manager() {
132 return time_wait_list_manager_.get();
135 EpollServer* epoll_server() { return epoll_server_; }
137 const QuicVersionVector& supported_versions() const {
138 return supported_versions_;
141 const QuicVersionVector& supported_versions_no_flow_control() const {
142 return supported_versions_no_flow_control_;
145 const QuicVersionVector& supported_versions_no_connection_flow_control()
146 const {
147 return supported_versions_no_connection_flow_control_;
150 const IPEndPoint& current_server_address() {
151 return current_server_address_;
153 const IPEndPoint& current_client_address() {
154 return current_client_address_;
156 const QuicEncryptedPacket& current_packet() {
157 return *current_packet_;
160 const QuicConfig& config() const { return config_; }
162 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
164 QuicFramer* framer() { return &framer_; }
166 QuicEpollConnectionHelper* helper() { return helper_.get(); }
168 QuicPacketWriter* writer() { return writer_.get(); }
170 const uint32 initial_flow_control_window_bytes() const {
171 return initial_flow_control_window_bytes_;
174 private:
175 class QuicFramerVisitor;
176 friend class net::tools::test::QuicDispatcherPeer;
178 // Called by |framer_visitor_| when the private header has been parsed
179 // of a data packet that is destined for the time wait manager.
180 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
182 // Removes the session from the session map and write blocked list, and
183 // adds the ConnectionId to the time-wait list.
184 void CleanUpSession(SessionMap::iterator it);
186 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
188 const QuicConfig& config_;
190 const QuicCryptoServerConfig& crypto_config_;
192 // The list of connections waiting to write.
193 WriteBlockedList write_blocked_list_;
195 SessionMap session_map_;
197 // Entity that manages connection_ids in time wait state.
198 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
200 // An alarm which deletes closed sessions.
201 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
203 // The list of closed but not-yet-deleted sessions.
204 std::list<QuicSession*> closed_session_list_;
206 EpollServer* epoll_server_; // Owned by the server.
208 // The helper used for all connections.
209 scoped_ptr<QuicEpollConnectionHelper> helper_;
211 // The writer to write to the socket with.
212 scoped_ptr<QuicPacketWriter> writer_;
214 // This vector contains QUIC versions which we currently support.
215 // This should be ordered such that the highest supported version is the first
216 // element, with subsequent elements in descending order (versions can be
217 // skipped as necessary).
218 const QuicVersionVector supported_versions_;
220 // Versions which do not support flow control (introduced in QUIC_VERSION_17).
221 // This is used to construct new QuicConnections when flow control is disabled
222 // via flag.
223 // TODO(rjshade): Remove this when
224 // FLAGS_enable_quic_stream_flow_control_2 is removed.
225 QuicVersionVector supported_versions_no_flow_control_;
226 // Versions which do not support *connection* flow control (introduced in
227 // QUIC_VERSION_19).
228 // This is used to construct new QuicConnections when connection flow control
229 // is disabled via flag.
230 // TODO(rjshade): Remove this when
231 // FLAGS_enable_quic_connection_flow_control is removed.
232 QuicVersionVector supported_versions_no_connection_flow_control_;
234 // Information about the packet currently being handled.
235 IPEndPoint current_client_address_;
236 IPEndPoint current_server_address_;
237 const QuicEncryptedPacket* current_packet_;
239 QuicFramer framer_;
240 scoped_ptr<QuicFramerVisitor> framer_visitor_;
242 // Initial flow control window size to advertize to peer on newly created
243 // connections.
244 const uint32 initial_flow_control_window_bytes_;
246 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
249 } // namespace tools
250 } // namespace net
252 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_