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"
27 class QuicDispatcherPeer
;
30 class DeleteSessionsAlarm
;
32 class QuicCryptoServerConfig
;
35 class ProcessPacketInterface
{
37 virtual ~ProcessPacketInterface() {}
38 virtual void ProcessPacket(const IPEndPoint
& server_address
,
39 const IPEndPoint
& client_address
,
40 const QuicEncryptedPacket
& packet
) = 0;
43 class QuicDispatcher
: public QuicBlockedWriterInterface
,
44 public QuicServerSessionVisitor
,
45 public ProcessPacketInterface
{
47 // Creates per-connection packet writers out of the QuicDispatcher's shared
48 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
49 // always be the same as the shared writer's IsWriteBlocked(), or else the
50 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
51 // cleaned up for bug 16950226.)
52 class PacketWriterFactory
{
54 virtual ~PacketWriterFactory() {}
56 virtual QuicPacketWriter
* Create(QuicServerPacketWriter
* writer
,
57 QuicConnection
* connection
) = 0;
60 // Creates ordinary QuicPerConnectionPacketWriter instances.
61 class DefaultPacketWriterFactory
: public PacketWriterFactory
{
63 ~DefaultPacketWriterFactory() override
{}
65 QuicPacketWriter
* Create(QuicServerPacketWriter
* writer
,
66 QuicConnection
* connection
) override
;
69 // Ideally we'd have a linked_hash_set: the boolean is unused.
70 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
72 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
73 // live until epoll_server Shutdown. |supported_versions| specifies the list
74 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
75 // which is used to create per-connection writers.
76 QuicDispatcher(const QuicConfig
& config
,
77 const QuicCryptoServerConfig
& crypto_config
,
78 const QuicVersionVector
& supported_versions
,
79 PacketWriterFactory
* packet_writer_factory
,
80 QuicConnectionHelperInterface
* helper
);
82 ~QuicDispatcher() override
;
84 // Takes ownership of the packet writer.
85 virtual void Initialize(QuicServerPacketWriter
* writer
);
87 // Process the incoming packet by creating a new session, passing it to
88 // an existing session, or passing it to the TimeWaitListManager.
89 void ProcessPacket(const IPEndPoint
& server_address
,
90 const IPEndPoint
& client_address
,
91 const QuicEncryptedPacket
& packet
) override
;
93 // Returns true if there's anything in the blocked writer list.
94 virtual bool HasPendingWrites() const;
96 // Sends ConnectionClose frames to all connected clients.
99 // QuicBlockedWriterInterface implementation:
100 // Called when the socket becomes writable to allow queued writes to happen.
101 void OnCanWrite() override
;
103 // QuicServerSessionVisitor interface implementation:
104 // Ensure that the closed connection is cleaned up asynchronously.
105 void OnConnectionClosed(QuicConnectionId connection_id
,
106 QuicErrorCode error
) override
;
108 // Queues the blocked writer for later resumption.
109 void OnWriteBlocked(QuicBlockedWriterInterface
* blocked_writer
) override
;
111 // Called whenever the QuicTimeWaitListManager adds a new connection to the
113 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id
) override
;
115 // Called whenever the QuicTimeWaitListManager removes an old connection from
116 // the time-wait list.
117 void OnConnectionRemovedFromTimeWaitList(
118 QuicConnectionId connection_id
) override
;
120 typedef base::hash_map
<QuicConnectionId
, QuicSession
*> SessionMap
;
122 // Deletes all sessions on the closed session list and clears the list.
123 void DeleteSessions();
125 const SessionMap
& session_map() const { return session_map_
; }
128 virtual QuicSession
* CreateQuicSession(QuicConnectionId connection_id
,
129 const IPEndPoint
& server_address
,
130 const IPEndPoint
& client_address
);
132 // Called by |framer_visitor_| when the public header has been parsed.
133 virtual bool OnUnauthenticatedPublicHeader(
134 const QuicPacketPublicHeader
& header
);
136 // Create and return the time wait list manager for this dispatcher, which
137 // will be owned by the dispatcher as time_wait_list_manager_
138 virtual QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
140 QuicTimeWaitListManager
* time_wait_list_manager() {
141 return time_wait_list_manager_
.get();
144 const QuicVersionVector
& supported_versions() const {
145 return supported_versions_
;
148 const IPEndPoint
& current_server_address() {
149 return current_server_address_
;
151 const IPEndPoint
& current_client_address() {
152 return current_client_address_
;
154 const QuicEncryptedPacket
& current_packet() {
155 return *current_packet_
;
158 const QuicConfig
& config() const { return config_
; }
160 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
162 QuicFramer
* framer() { return &framer_
; }
164 QuicConnectionHelperInterface
* helper() { return helper_
; }
166 QuicServerPacketWriter
* writer() { return writer_
.get(); }
168 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
169 return connection_writer_factory_
;
173 class QuicFramerVisitor
;
174 friend class net::test::QuicDispatcherPeer
;
176 // An adapter that creates packet writers using the dispatcher's
177 // PacketWriterFactory and shared writer. Essentially, it just curries the
178 // writer argument away from QuicDispatcher::PacketWriterFactory.
179 class PacketWriterFactoryAdapter
:
180 public QuicConnection::PacketWriterFactory
{
182 PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
183 ~PacketWriterFactoryAdapter() override
;
185 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
188 QuicDispatcher
* dispatcher_
;
191 // Called by |framer_visitor_| when the private header has been parsed
192 // of a data packet that is destined for the time wait manager.
193 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
195 // Removes the session from the session map and write blocked list, and
196 // adds the ConnectionId to the time-wait list.
197 void CleanUpSession(SessionMap::iterator it
);
199 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
201 const QuicConfig
& config_
;
203 const QuicCryptoServerConfig
& crypto_config_
;
205 // The list of connections waiting to write.
206 WriteBlockedList write_blocked_list_
;
208 SessionMap session_map_
;
210 // Entity that manages connection_ids in time wait state.
211 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
213 // The helper used for all connections. Owned by the server.
214 QuicConnectionHelperInterface
* helper_
;
216 // An alarm which deletes closed sessions.
217 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
219 // The list of closed but not-yet-deleted sessions.
220 std::list
<QuicSession
*> closed_session_list_
;
222 // The writer to write to the socket with.
223 scoped_ptr
<QuicServerPacketWriter
> writer_
;
225 // Used to create per-connection packet writers, not |writer_| itself.
226 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
228 // Passed in to QuicConnection for it to create the per-connection writers
229 PacketWriterFactoryAdapter connection_writer_factory_
;
231 // This vector contains QUIC versions which we currently support.
232 // This should be ordered such that the highest supported version is the first
233 // element, with subsequent elements in descending order (versions can be
234 // skipped as necessary).
235 const QuicVersionVector supported_versions_
;
237 // Information about the packet currently being handled.
238 IPEndPoint current_client_address_
;
239 IPEndPoint current_server_address_
;
240 const QuicEncryptedPacket
* current_packet_
;
243 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
245 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
250 #endif // NET_QUIC_QUIC_DISPATCHER_H_