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 typedef base::hash_map
<QuicConnectionId
, QuicSession
*> SessionMap
;
113 // Deletes all sessions on the closed session list and clears the list.
114 void DeleteSessions();
116 const SessionMap
& session_map() const { return session_map_
; }
119 virtual QuicSession
* CreateQuicSession(QuicConnectionId connection_id
,
120 const IPEndPoint
& server_address
,
121 const IPEndPoint
& client_address
);
123 virtual QuicConnection
* CreateQuicConnection(
124 QuicConnectionId connection_id
,
125 const IPEndPoint
& server_address
,
126 const IPEndPoint
& client_address
);
128 // Called by |framer_visitor_| when the public header has been parsed.
129 virtual bool OnUnauthenticatedPublicHeader(
130 const QuicPacketPublicHeader
& header
);
132 // Create and return the time wait list manager for this dispatcher, which
133 // will be owned by the dispatcher as time_wait_list_manager_
134 virtual QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
136 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
137 void set_writer(QuicServerPacketWriter
* writer
) {
138 writer_
.reset(writer
);
141 QuicTimeWaitListManager
* time_wait_list_manager() {
142 return time_wait_list_manager_
.get();
145 const QuicVersionVector
& supported_versions() const {
146 return supported_versions_
;
149 const IPEndPoint
& current_server_address() {
150 return current_server_address_
;
152 const IPEndPoint
& current_client_address() {
153 return current_client_address_
;
155 const QuicEncryptedPacket
& current_packet() {
156 return *current_packet_
;
159 const QuicConfig
& config() const { return config_
; }
161 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
163 QuicFramer
* framer() { return &framer_
; }
165 QuicConnectionHelperInterface
* helper() { return helper_
; }
167 QuicServerPacketWriter
* writer() { return writer_
.get(); }
169 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
170 return connection_writer_factory_
;
174 class QuicFramerVisitor
;
175 friend class net::test::QuicDispatcherPeer
;
177 // An adapter that creates packet writers using the dispatcher's
178 // PacketWriterFactory and shared writer. Essentially, it just curries the
179 // writer argument away from QuicDispatcher::PacketWriterFactory.
180 class PacketWriterFactoryAdapter
:
181 public QuicConnection::PacketWriterFactory
{
183 PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
184 ~PacketWriterFactoryAdapter() override
;
186 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
189 QuicDispatcher
* dispatcher_
;
192 // Called by |framer_visitor_| when the private header has been parsed
193 // of a data packet that is destined for the time wait manager.
194 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
196 // Removes the session from the session map and write blocked list, and
197 // adds the ConnectionId to the time-wait list.
198 void CleanUpSession(SessionMap::iterator it
);
200 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
202 const QuicConfig
& config_
;
204 const QuicCryptoServerConfig
& crypto_config_
;
206 // The list of connections waiting to write.
207 WriteBlockedList write_blocked_list_
;
209 SessionMap session_map_
;
211 // Entity that manages connection_ids in time wait state.
212 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
214 // The helper used for all connections. Owned by the server.
215 QuicConnectionHelperInterface
* helper_
;
217 // An alarm which deletes closed sessions.
218 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
220 // The list of closed but not-yet-deleted sessions.
221 std::list
<QuicSession
*> closed_session_list_
;
223 // The writer to write to the socket with.
224 scoped_ptr
<QuicServerPacketWriter
> writer_
;
226 // Used to create per-connection packet writers, not |writer_| itself.
227 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
229 // Passed in to QuicConnection for it to create the per-connection writers
230 PacketWriterFactoryAdapter connection_writer_factory_
;
232 // This vector contains QUIC versions which we currently support.
233 // This should be ordered such that the highest supported version is the first
234 // element, with subsequent elements in descending order (versions can be
235 // skipped as necessary).
236 const QuicVersionVector supported_versions_
;
238 // Information about the packet currently being handled.
239 IPEndPoint current_client_address_
;
240 IPEndPoint current_server_address_
;
241 const QuicEncryptedPacket
* current_packet_
;
244 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
246 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
251 #endif // NET_QUIC_QUIC_DISPATCHER_H_