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/quic/quic_server_session.h"
21 #include "net/tools/quic/quic_time_wait_list_manager.h"
27 class QuicCryptoServerConfig
;
32 class DeleteSessionsAlarm
;
33 class QuicEpollConnectionHelper
;
34 class QuicPacketWriterWrapper
;
37 class QuicDispatcherPeer
;
40 class ProcessPacketInterface
{
42 virtual ~ProcessPacketInterface() {}
43 virtual void ProcessPacket(const IPEndPoint
& server_address
,
44 const IPEndPoint
& client_address
,
45 const QuicEncryptedPacket
& packet
) = 0;
48 class QuicDispatcher
: public QuicServerSessionVisitor
,
49 public ProcessPacketInterface
{
51 // Creates per-connection packet writers out of the QuicDispatcher's shared
52 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
53 // always be the same as the shared writer's IsWriteBlocked(), or else the
54 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
55 // cleaned up for bug 16950226.)
56 class PacketWriterFactory
{
58 virtual ~PacketWriterFactory() {}
60 virtual QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
61 QuicConnection
* connection
) = 0;
64 // Creates ordinary QuicPerConnectionPacketWriter instances.
65 class DefaultPacketWriterFactory
: public PacketWriterFactory
{
67 ~DefaultPacketWriterFactory() override
{}
69 QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
70 QuicConnection
* connection
) override
;
73 // Ideally we'd have a linked_hash_set: the boolean is unused.
74 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
76 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
77 // live until epoll_server Shutdown. |supported_versions| specifies the list
78 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
79 // which is used to create per-connection writers.
80 QuicDispatcher(const QuicConfig
& config
,
81 const QuicCryptoServerConfig
& crypto_config
,
82 const QuicVersionVector
& supported_versions
,
83 PacketWriterFactory
* packet_writer_factory
,
84 EpollServer
* epoll_server
);
86 ~QuicDispatcher() override
;
88 virtual void Initialize(int fd
);
90 // Process the incoming packet by creating a new session, passing it to
91 // an existing session, or passing it to the TimeWaitListManager.
92 void ProcessPacket(const IPEndPoint
& server_address
,
93 const IPEndPoint
& client_address
,
94 const QuicEncryptedPacket
& packet
) override
;
96 // Called when the socket becomes writable to allow queued writes to happen.
97 virtual void OnCanWrite();
99 // Returns true if there's anything in the blocked writer list.
100 virtual bool HasPendingWrites() const;
102 // Sends ConnectionClose frames to all connected clients.
105 // QuicServerSessionVisitor interface implementation:
106 // Ensure that the closed connection is cleaned up asynchronously.
107 void OnConnectionClosed(QuicConnectionId connection_id
,
108 QuicErrorCode error
) override
;
110 // Queues the blocked writer for later resumption.
111 void OnWriteBlocked(QuicBlockedWriterInterface
* blocked_writer
) override
;
113 typedef base::hash_map
<QuicConnectionId
, QuicSession
*> SessionMap
;
115 // Deletes all sessions on the closed session list and clears the list.
116 void DeleteSessions();
118 const SessionMap
& session_map() const { return session_map_
; }
121 // Instantiates a new low-level packet writer. Caller takes ownership of the
123 virtual QuicPacketWriter
* CreateWriter(int fd
);
125 virtual QuicSession
* CreateQuicSession(QuicConnectionId connection_id
,
126 const IPEndPoint
& server_address
,
127 const IPEndPoint
& client_address
);
129 virtual QuicConnection
* CreateQuicConnection(
130 QuicConnectionId connection_id
,
131 const IPEndPoint
& server_address
,
132 const IPEndPoint
& client_address
);
134 // Called by |framer_visitor_| when the public header has been parsed.
135 virtual bool OnUnauthenticatedPublicHeader(
136 const QuicPacketPublicHeader
& header
);
138 // Create and return the time wait list manager for this dispatcher, which
139 // will be owned by the dispatcher as time_wait_list_manager_
140 virtual QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
142 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
143 void set_writer(QuicPacketWriter
* writer
) {
144 writer_
.reset(writer
);
147 QuicTimeWaitListManager
* time_wait_list_manager() {
148 return time_wait_list_manager_
.get();
151 EpollServer
* epoll_server() { return epoll_server_
; }
153 const QuicVersionVector
& supported_versions() const {
154 return supported_versions_
;
157 const IPEndPoint
& current_server_address() {
158 return current_server_address_
;
160 const IPEndPoint
& current_client_address() {
161 return current_client_address_
;
163 const QuicEncryptedPacket
& current_packet() {
164 return *current_packet_
;
167 const QuicConfig
& config() const { return config_
; }
169 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
171 QuicFramer
* framer() { return &framer_
; }
173 QuicEpollConnectionHelper
* helper() { return helper_
.get(); }
175 QuicPacketWriter
* writer() { return writer_
.get(); }
177 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
178 return connection_writer_factory_
;
182 class QuicFramerVisitor
;
183 friend class net::tools::test::QuicDispatcherPeer
;
185 // An adapter that creates packet writers using the dispatcher's
186 // PacketWriterFactory and shared writer. Essentially, it just curries the
187 // writer argument away from QuicDispatcher::PacketWriterFactory.
188 class PacketWriterFactoryAdapter
:
189 public QuicConnection::PacketWriterFactory
{
191 PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
192 ~PacketWriterFactoryAdapter() override
;
194 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
197 QuicDispatcher
* dispatcher_
;
200 // Called by |framer_visitor_| when the private header has been parsed
201 // of a data packet that is destined for the time wait manager.
202 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
204 // Removes the session from the session map and write blocked list, and
205 // adds the ConnectionId to the time-wait list.
206 void CleanUpSession(SessionMap::iterator it
);
208 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
210 const QuicConfig
& config_
;
212 const QuicCryptoServerConfig
& crypto_config_
;
214 // The list of connections waiting to write.
215 WriteBlockedList write_blocked_list_
;
217 SessionMap session_map_
;
219 // Entity that manages connection_ids in time wait state.
220 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
222 // An alarm which deletes closed sessions.
223 scoped_ptr
<DeleteSessionsAlarm
> delete_sessions_alarm_
;
225 // The list of closed but not-yet-deleted sessions.
226 std::list
<QuicSession
*> closed_session_list_
;
228 EpollServer
* epoll_server_
; // Owned by the server.
230 // The helper used for all connections.
231 scoped_ptr
<QuicEpollConnectionHelper
> helper_
;
233 // The writer to write to the socket with.
234 scoped_ptr
<QuicPacketWriter
> writer_
;
236 // Used to create per-connection packet writers, not |writer_| itself.
237 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
239 // Passed in to QuicConnection for it to create the per-connection writers
240 PacketWriterFactoryAdapter connection_writer_factory_
;
242 // This vector contains QUIC versions which we currently support.
243 // This should be ordered such that the highest supported version is the first
244 // element, with subsequent elements in descending order (versions can be
245 // skipped as necessary).
246 const QuicVersionVector supported_versions_
;
248 // Information about the packet currently being handled.
249 IPEndPoint current_client_address_
;
250 IPEndPoint current_server_address_
;
251 const QuicEncryptedPacket
* current_packet_
;
254 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
256 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
262 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_