1 // Copyright 2014 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_QUIC_QUIC_DISPATCHER_H_
9 #define NET_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_connection_helper.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/quic/quic_server_packet_writer.h"
22 #include "net/quic/quic_server_session.h"
23 #include "net/quic/quic_time_wait_list_manager.h"
25 #if defined(COMPILER_GCC)
26 namespace BASE_HASH_NAMESPACE
{
28 struct hash
<net::QuicBlockedWriterInterface
*> {
29 std::size_t operator()(const net::QuicBlockedWriterInterface
* ptr
) const {
30 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
39 class QuicCryptoServerConfig
;
43 class QuicDispatcherPeer
;
46 class DeleteSessionsAlarm
;
48 class ProcessPacketInterface
{
50 virtual ~ProcessPacketInterface() {}
51 virtual void ProcessPacket(const IPEndPoint
& server_address
,
52 const IPEndPoint
& client_address
,
53 const QuicEncryptedPacket
& packet
) = 0;
56 class QuicDispatcher
: public QuicBlockedWriterInterface
,
57 public QuicServerSessionVisitor
,
58 public ProcessPacketInterface
{
60 // Ideally we'd have a linked_hash_set: the boolean is unused.
61 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
63 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
64 // must live until epoll_server Shutdown. |supported_versions| specifies the
65 // list of supported QUIC versions.
66 QuicDispatcher(const QuicConfig
& config
,
67 const QuicCryptoServerConfig
& crypto_config
,
68 const QuicVersionVector
& supported_versions
,
69 QuicConnectionHelperInterface
* helper
);
71 virtual ~QuicDispatcher();
73 // Takes ownership of the packet writer
74 virtual void Initialize(QuicServerPacketWriter
* writer
);
76 // Process the incoming packet by creating a new session, passing it to
77 // an existing session, or passing it to the TimeWaitListManager.
78 virtual void ProcessPacket(const IPEndPoint
& server_address
,
79 const IPEndPoint
& client_address
,
80 const QuicEncryptedPacket
& packet
) OVERRIDE
;
82 // Returns true if there's anything in the blocked writer list.
83 virtual bool HasPendingWrites() const;
85 // Sends ConnectionClose frames to all connected clients.
88 // QuicBlockedWriterInterface implementation:
89 // Called when the socket becomes writable to allow queued writes to happen.
90 virtual void OnCanWrite() OVERRIDE
;
92 // QuicServerSessionVisitor interface implementation:
93 // Ensure that the closed connection is cleaned up asynchronously.
94 virtual void OnConnectionClosed(QuicConnectionId connection_id
,
95 QuicErrorCode error
) OVERRIDE
;
97 // Queues the blocked writer for later resumption.
98 virtual void OnWriteBlocked(
99 QuicBlockedWriterInterface
* blocked_writer
) OVERRIDE
;
101 typedef base::hash_map
<QuicConnectionId
, QuicSession
*> SessionMap
;
103 // Deletes all sessions on the closed session list and clears the list.
104 void DeleteSessions();
106 const SessionMap
& session_map() const { return session_map_
; }
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 QuicPerConnectionPacketWriter
* writer
);
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(QuicServerPacketWriter
* writer
) {
129 writer_
.reset(writer
);
132 QuicTimeWaitListManager
* time_wait_list_manager() {
133 return time_wait_list_manager_
.get();
136 const QuicVersionVector
& supported_versions() const {
137 return supported_versions_
;
140 const QuicVersionVector
& supported_versions_no_connection_flow_control()
142 return supported_versions_no_connection_flow_control_
;
145 const IPEndPoint
& current_server_address() {
146 return current_server_address_
;
148 const IPEndPoint
& current_client_address() {
149 return current_client_address_
;
151 const QuicEncryptedPacket
& current_packet() {
152 return *current_packet_
;
155 const QuicConfig
& config() const { return config_
; }
157 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
159 QuicFramer
* framer() { return &framer_
; }
161 QuicConnectionHelperInterface
* helper() { return helper_
; }
163 QuicServerPacketWriter
* writer() { return writer_
.get(); }
166 class QuicFramerVisitor
;
167 friend class net::test::QuicDispatcherPeer
;
169 // Called by |framer_visitor_| when the private header has been parsed
170 // of a data packet that is destined for the time wait manager.
171 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
173 // Removes the session from the session map and write blocked list, and
174 // adds the ConnectionId to the time-wait list.
175 void CleanUpSession(SessionMap::iterator it
);
177 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
179 const QuicConfig
& config_
;
181 const QuicCryptoServerConfig
& crypto_config_
;
183 // The list of connections waiting to write.
184 WriteBlockedList write_blocked_list_
;
186 SessionMap session_map_
;
188 // Entity that manages connection_ids in time wait state.
189 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
191 // The helper used for all connections. Owned by the server.
192 QuicConnectionHelperInterface
* helper_
;
194 // An alarm which deletes closed sessions.
195 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
197 // The list of closed but not-yet-deleted sessions.
198 std::list
<QuicSession
*> closed_session_list_
;
200 // The writer to write to the socket with.
201 scoped_ptr
<QuicServerPacketWriter
> writer_
;
203 // This vector contains QUIC versions which we currently support.
204 // This should be ordered such that the highest supported version is the first
205 // element, with subsequent elements in descending order (versions can be
206 // skipped as necessary).
207 const QuicVersionVector supported_versions_
;
209 // Versions which do not support *connection* flow control (introduced in
211 // This is used to construct new QuicConnections when connection flow control
212 // is disabled via flag.
213 // TODO(rjshade): Remove this when
214 // FLAGS_enable_quic_connection_flow_control_2 is removed.
215 QuicVersionVector supported_versions_no_connection_flow_control_
;
217 // Information about the packet currently being handled.
218 IPEndPoint current_client_address_
;
219 IPEndPoint current_server_address_
;
220 const QuicEncryptedPacket
* current_packet_
;
223 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
225 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
230 #endif // NET_QUIC_QUIC_DISPATCHER_H_