Land Recent QUIC Changes until 03/27/2015
[chromium-blink-merge.git] / net / tools / quic / quic_dispatcher.h
blob89b8e7d8674e69e691392d094199b1497c992ce6
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.
4 //
5 // A server side dispatcher which dispatches a given client's data to their
6 // stream.
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_blocked_writer_interface.h"
17 #include "net/quic/quic_connection.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/tools/quic/quic_server_session.h"
20 #include "net/tools/quic/quic_time_wait_list_manager.h"
22 namespace net {
24 class QuicConfig;
25 class QuicCryptoServerConfig;
26 class QuicServerSession;
28 namespace tools {
30 namespace test {
31 class QuicDispatcherPeer;
32 } // namespace test
34 class ProcessPacketInterface {
35 public:
36 virtual ~ProcessPacketInterface() {}
37 virtual void ProcessPacket(const IPEndPoint& server_address,
38 const IPEndPoint& client_address,
39 const QuicEncryptedPacket& packet) = 0;
42 class QuicDispatcher : public QuicServerSessionVisitor,
43 public ProcessPacketInterface,
44 public QuicBlockedWriterInterface {
45 public:
46 // Creates per-connection packet writers out of the QuicDispatcher's shared
47 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
48 // always be the same as the shared writer's IsWriteBlocked(), or else the
49 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
50 // cleaned up for bug 16950226.)
51 class PacketWriterFactory {
52 public:
53 virtual ~PacketWriterFactory() {}
55 virtual QuicPacketWriter* Create(QuicPacketWriter* writer,
56 QuicConnection* connection) = 0;
59 // Creates ordinary QuicPerConnectionPacketWriter instances.
60 class DefaultPacketWriterFactory : public PacketWriterFactory {
61 public:
62 ~DefaultPacketWriterFactory() override {}
64 QuicPacketWriter* Create(QuicPacketWriter* writer,
65 QuicConnection* connection) override;
68 // Ideally we'd have a linked_hash_set: the boolean is unused.
69 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
71 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
72 // live until server Shutdown. |supported_versions| specifies the std::list
73 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
74 // which is used to create per-connection writers.
75 QuicDispatcher(const QuicConfig& config,
76 const QuicCryptoServerConfig* crypto_config,
77 const QuicVersionVector& supported_versions,
78 PacketWriterFactory* packet_writer_factory,
79 QuicConnectionHelperInterface* helper);
81 ~QuicDispatcher() override;
83 // Takes ownership of |writer|.
84 void InitializeWithWriter(QuicPacketWriter* writer);
86 // Process the incoming packet by creating a new session, passing it to
87 // an existing session, or passing it to the TimeWaitListManager.
88 void ProcessPacket(const IPEndPoint& server_address,
89 const IPEndPoint& client_address,
90 const QuicEncryptedPacket& packet) override;
92 // Called when the socket becomes writable to allow queued writes to happen.
93 void OnCanWrite() override;
95 // Returns true if there's anything in the blocked writer list.
96 virtual bool HasPendingWrites() const;
98 // Sends ConnectionClose frames to all connected clients.
99 void Shutdown();
101 // QuicServerSessionVisitor interface implementation:
102 // Ensure that the closed connection is cleaned up asynchronously.
103 void OnConnectionClosed(QuicConnectionId connection_id,
104 QuicErrorCode error) override;
106 // Queues the blocked writer for later resumption.
107 void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) override;
109 // Called whenever the QuicTimeWaitListManager adds a new connection to the
110 // time-wait list.
111 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id) override;
113 // Called whenever the QuicTimeWaitListManager removes an old connection from
114 // the time-wait list.
115 void OnConnectionRemovedFromTimeWaitList(
116 QuicConnectionId connection_id) override;
118 typedef base::hash_map<QuicConnectionId, QuicServerSession*> SessionMap;
120 const SessionMap& session_map() const { return session_map_; }
122 // Deletes all sessions on the closed session list and clears the list.
123 void DeleteSessions();
125 protected:
126 virtual QuicServerSession* CreateQuicSession(
127 QuicConnectionId connection_id,
128 const IPEndPoint& server_address,
129 const IPEndPoint& client_address);
131 // Called by |framer_visitor_| when the public header has been parsed.
132 virtual bool OnUnauthenticatedPublicHeader(
133 const QuicPacketPublicHeader& header);
135 // Called by OnUnauthenticatedPublicHeader when the packet is not for a
136 // connection that the dispatcher has a record of, but is not handled by
137 // certain simple processing rules. This method may apply validity checks to
138 // reject stray packets. If the packet appears to be valid, it calls
139 // CreateQuicSession to create a new session for the packet. Returns the
140 // QuicServerSession that was created, or nullptr if the packet failed the
141 // validity checks.
142 virtual QuicServerSession* AdditionalValidityChecksThenCreateSession(
143 const QuicPacketPublicHeader& header,
144 QuicConnectionId connection_id);
146 // Create and return the time wait list manager for this dispatcher, which
147 // will be owned by the dispatcher as time_wait_list_manager_
148 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
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 {
171 return crypto_config_;
174 QuicFramer* framer() { return &framer_; }
176 QuicConnectionHelperInterface* helper() { return helper_.get(); }
178 QuicPacketWriter* writer() { return writer_.get(); }
180 const QuicConnection::PacketWriterFactory& connection_writer_factory() {
181 return connection_writer_factory_;
184 private:
185 class QuicFramerVisitor;
186 friend class net::tools::test::QuicDispatcherPeer;
188 // An adapter that creates packet writers using the dispatcher's
189 // PacketWriterFactory and shared writer. Essentially, it just curries the
190 // writer argument away from QuicDispatcher::PacketWriterFactory.
191 class PacketWriterFactoryAdapter :
192 public QuicConnection::PacketWriterFactory {
193 public:
194 explicit PacketWriterFactoryAdapter(QuicDispatcher* dispatcher);
195 ~PacketWriterFactoryAdapter() override;
197 QuicPacketWriter* Create(QuicConnection* connection) const override;
199 private:
200 QuicDispatcher* dispatcher_;
203 // Called by |framer_visitor_| when the private header has been parsed
204 // of a data packet that is destined for the time wait manager.
205 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
207 // Removes the session from the session map and write blocked list, and
208 // adds the ConnectionId to the time-wait list.
209 void CleanUpSession(SessionMap::iterator it);
211 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
213 const QuicConfig& config_;
215 const QuicCryptoServerConfig* crypto_config_;
217 // The list of connections waiting to write.
218 WriteBlockedList write_blocked_list_;
220 SessionMap session_map_;
222 // Entity that manages connection_ids in time wait state.
223 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
225 // The list of closed but not-yet-deleted sessions.
226 std::list<QuicServerSession*> closed_session_list_;
228 // The helper used for all connections.
229 scoped_ptr<QuicConnectionHelperInterface> helper_;
231 // An alarm which deletes closed sessions.
232 scoped_ptr<QuicAlarm> delete_sessions_alarm_;
234 // The writer to write to the socket with.
235 scoped_ptr<QuicPacketWriter> writer_;
237 // Used to create per-connection packet writers, not |writer_| itself.
238 scoped_ptr<PacketWriterFactory> packet_writer_factory_;
240 // Passed in to QuicConnection for it to create the per-connection writers
241 PacketWriterFactoryAdapter connection_writer_factory_;
243 // This vector contains QUIC versions which we currently support.
244 // This should be ordered such that the highest supported version is the first
245 // element, with subsequent elements in descending order (versions can be
246 // skipped as necessary).
247 const QuicVersionVector supported_versions_;
249 // Information about the packet currently being handled.
250 IPEndPoint current_client_address_;
251 IPEndPoint current_server_address_;
252 const QuicEncryptedPacket* current_packet_;
254 QuicFramer framer_;
255 scoped_ptr<QuicFramerVisitor> framer_visitor_;
257 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
260 } // namespace tools
261 } // namespace net
263 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_