Update the project URL to DOM Distiller
[chromium-blink-merge.git] / net / quic / quic_dispatcher.h
blob8e2ce034c9cc7ad86cfbfec6ce8eeae89b0530d9
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.
4 //
5 // A server side dispatcher which dispatches a given client's data to their
6 // stream.
8 #ifndef NET_QUIC_QUIC_DISPATCHER_H_
9 #define NET_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_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 namespace net {
26 namespace test {
27 class QuicDispatcherPeer;
28 } // namespace test
30 class DeleteSessionsAlarm;
31 class QuicConfig;
32 class QuicCryptoServerConfig;
33 class QuicSession;
35 class ProcessPacketInterface {
36 public:
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 {
46 public:
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 {
53 public:
54 virtual ~PacketWriterFactory() {}
56 virtual QuicPacketWriter* Create(QuicServerPacketWriter* writer,
57 QuicConnection* connection) = 0;
60 // Creates ordinary QuicPerConnectionPacketWriter instances.
61 class DefaultPacketWriterFactory : public PacketWriterFactory {
62 public:
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.
97 void Shutdown();
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
112 // time-wait list.
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_; }
127 protected:
128 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
129 const IPEndPoint& server_address,
130 const IPEndPoint& client_address);
132 virtual QuicConnection* CreateQuicConnection(
133 QuicConnectionId connection_id,
134 const IPEndPoint& server_address,
135 const IPEndPoint& client_address);
137 // Called by |framer_visitor_| when the public header has been parsed.
138 virtual bool OnUnauthenticatedPublicHeader(
139 const QuicPacketPublicHeader& header);
141 // Create and return the time wait list manager for this dispatcher, which
142 // will be owned by the dispatcher as time_wait_list_manager_
143 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
145 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
146 void set_writer(QuicServerPacketWriter* writer) {
147 writer_.reset(writer);
150 QuicTimeWaitListManager* time_wait_list_manager() {
151 return time_wait_list_manager_.get();
154 const QuicVersionVector& supported_versions() const {
155 return supported_versions_;
158 const IPEndPoint& current_server_address() {
159 return current_server_address_;
161 const IPEndPoint& current_client_address() {
162 return current_client_address_;
164 const QuicEncryptedPacket& current_packet() {
165 return *current_packet_;
168 const QuicConfig& config() const { return config_; }
170 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
172 QuicFramer* framer() { return &framer_; }
174 QuicConnectionHelperInterface* helper() { return helper_; }
176 QuicServerPacketWriter* writer() { return writer_.get(); }
178 const QuicConnection::PacketWriterFactory& connection_writer_factory() {
179 return connection_writer_factory_;
182 private:
183 class QuicFramerVisitor;
184 friend class net::test::QuicDispatcherPeer;
186 // An adapter that creates packet writers using the dispatcher's
187 // PacketWriterFactory and shared writer. Essentially, it just curries the
188 // writer argument away from QuicDispatcher::PacketWriterFactory.
189 class PacketWriterFactoryAdapter :
190 public QuicConnection::PacketWriterFactory {
191 public:
192 PacketWriterFactoryAdapter(QuicDispatcher* dispatcher);
193 ~PacketWriterFactoryAdapter() override;
195 QuicPacketWriter* Create(QuicConnection* connection) const override;
197 private:
198 QuicDispatcher* dispatcher_;
201 // Called by |framer_visitor_| when the private header has been parsed
202 // of a data packet that is destined for the time wait manager.
203 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
205 // Removes the session from the session map and write blocked list, and
206 // adds the ConnectionId to the time-wait list.
207 void CleanUpSession(SessionMap::iterator it);
209 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
211 const QuicConfig& config_;
213 const QuicCryptoServerConfig& crypto_config_;
215 // The list of connections waiting to write.
216 WriteBlockedList write_blocked_list_;
218 SessionMap session_map_;
220 // Entity that manages connection_ids in time wait state.
221 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
223 // The helper used for all connections. Owned by the server.
224 QuicConnectionHelperInterface* helper_;
226 // An alarm which deletes closed sessions.
227 scoped_ptr<QuicAlarm> delete_sessions_alarm_;
229 // The list of closed but not-yet-deleted sessions.
230 std::list<QuicSession*> closed_session_list_;
232 // The writer to write to the socket with.
233 scoped_ptr<QuicServerPacketWriter> writer_;
235 // Used to create per-connection packet writers, not |writer_| itself.
236 scoped_ptr<PacketWriterFactory> packet_writer_factory_;
238 // Passed in to QuicConnection for it to create the per-connection writers
239 PacketWriterFactoryAdapter connection_writer_factory_;
241 // This vector contains QUIC versions which we currently support.
242 // This should be ordered such that the highest supported version is the first
243 // element, with subsequent elements in descending order (versions can be
244 // skipped as necessary).
245 const QuicVersionVector supported_versions_;
247 // Information about the packet currently being handled.
248 IPEndPoint current_client_address_;
249 IPEndPoint current_server_address_;
250 const QuicEncryptedPacket* current_packet_;
252 QuicFramer framer_;
253 scoped_ptr<QuicFramerVisitor> framer_visitor_;
255 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
258 } // namespace net
260 #endif // NET_QUIC_QUIC_DISPATCHER_H_