Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / tools / quic / quic_dispatcher.h
blobfdce9e1a8c7518709e7497142b4ff181b7f5ea2f
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 extern int32 FLAGS_quic_session_map_threshold_for_stateless_rejects;
36 class ProcessPacketInterface {
37 public:
38 virtual ~ProcessPacketInterface() {}
39 virtual void ProcessPacket(const IPEndPoint& server_address,
40 const IPEndPoint& client_address,
41 const QuicEncryptedPacket& packet) = 0;
44 class QuicDispatcher : public QuicServerSessionVisitor,
45 public ProcessPacketInterface,
46 public QuicBlockedWriterInterface {
47 public:
48 // Creates per-connection packet writers out of the QuicDispatcher's shared
49 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
50 // always be the same as the shared writer's IsWriteBlocked(), or else the
51 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
52 // cleaned up for bug 16950226.)
53 class PacketWriterFactory {
54 public:
55 virtual ~PacketWriterFactory() {}
57 virtual QuicPacketWriter* Create(QuicPacketWriter* writer,
58 QuicConnection* connection) = 0;
61 // Creates ordinary QuicPerConnectionPacketWriter instances.
62 class DefaultPacketWriterFactory : public PacketWriterFactory {
63 public:
64 ~DefaultPacketWriterFactory() override {}
66 QuicPacketWriter* Create(QuicPacketWriter* writer,
67 QuicConnection* connection) override;
70 // Ideally we'd have a linked_hash_set: the boolean is unused.
71 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
73 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
74 // live until server Shutdown. |supported_versions| specifies the std::list
75 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
76 // which is used to create per-connection writers.
77 QuicDispatcher(const QuicConfig& config,
78 const QuicCryptoServerConfig* crypto_config,
79 const QuicVersionVector& supported_versions,
80 PacketWriterFactory* packet_writer_factory,
81 QuicConnectionHelperInterface* helper);
83 ~QuicDispatcher() override;
85 // Takes ownership of |writer|.
86 void InitializeWithWriter(QuicPacketWriter* writer);
88 // Process the incoming packet by creating a new session, passing it to
89 // an existing session, or passing it to the time wait list.
90 void ProcessPacket(const IPEndPoint& server_address,
91 const IPEndPoint& client_address,
92 const QuicEncryptedPacket& packet) override;
94 // Called when the socket becomes writable to allow queued writes to happen.
95 void OnCanWrite() override;
97 // Returns true if there's anything in the blocked writer list.
98 virtual bool HasPendingWrites() const;
100 // Sends ConnectionClose frames to all connected clients.
101 void Shutdown();
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 time wait list manager adds a new connection to the
112 // time-wait list.
113 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id) override;
115 // Called whenever the time wait list manager removes an old connection from
116 // the time-wait list.
117 void OnConnectionRemovedFromTimeWaitList(
118 QuicConnectionId connection_id) override;
120 typedef base::hash_map<QuicConnectionId, QuicServerSession*> SessionMap;
122 const SessionMap& session_map() const { return session_map_; }
124 // Deletes all sessions on the closed session list and clears the list.
125 void DeleteSessions();
127 // The largest packet number we expect to receive with a connection
128 // ID for a connection that is not established yet. The current design will
129 // send a handshake and then up to 50 or so data packets, and then it may
130 // resend the handshake packet up to 10 times. (Retransmitted packets are
131 // sent with unique packet numbers.)
132 static const QuicPacketNumber kMaxReasonableInitialPacketNumber = 100;
133 static_assert(kMaxReasonableInitialPacketNumber >=
134 kInitialCongestionWindowSecure + 10,
135 "kMaxReasonableInitialPacketNumber is unreasonably small "
136 "relative to kInitialCongestionWindowSecure.");
137 static_assert(kMaxReasonableInitialPacketNumber >=
138 kInitialCongestionWindowInsecure + 10,
139 "kMaxReasonableInitialPacketNumber is unreasonably small "
140 "relative to kInitialCongestionWindowInsecure.");
142 protected:
143 virtual QuicServerSession* CreateQuicSession(
144 QuicConnectionId connection_id,
145 const IPEndPoint& server_address,
146 const IPEndPoint& client_address);
148 // Called by |framer_visitor_| when the public header has been parsed.
149 virtual bool OnUnauthenticatedPublicHeader(
150 const QuicPacketPublicHeader& header);
152 // Values to be returned by ValidityChecks() to indicate what should be done
153 // with a packet. Fates with greater values are considered to be higher
154 // priority, in that if one validity check indicates a lower-valued fate and
155 // another validity check indicates a higher-valued fate, the higher-valued
156 // fate should be obeyed.
157 enum QuicPacketFate {
158 // Process the packet normally, which is usually to establish a connection.
159 kFateProcess,
160 // Put the connection ID into time-wait state and send a public reset.
161 kFateTimeWait,
162 // Drop the packet (ignore and give no response).
163 kFateDrop,
166 // This method is called by OnUnauthenticatedHeader on packets not associated
167 // with a known connection ID. It applies validity checks and returns a
168 // QuicPacketFate to tell what should be done with the packet.
169 virtual QuicPacketFate ValidityChecks(const QuicPacketHeader& header);
171 // Create and return the time wait list manager for this dispatcher, which
172 // will be owned by the dispatcher as time_wait_list_manager_
173 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
175 QuicTimeWaitListManager* time_wait_list_manager() {
176 return time_wait_list_manager_.get();
179 const QuicVersionVector& supported_versions() const {
180 return supported_versions_;
183 const IPEndPoint& current_server_address() {
184 return current_server_address_;
186 const IPEndPoint& current_client_address() {
187 return current_client_address_;
189 const QuicEncryptedPacket& current_packet() {
190 return *current_packet_;
193 const QuicConfig& config() const { return config_; }
195 const QuicCryptoServerConfig* crypto_config() const { return crypto_config_; }
197 QuicFramer* framer() { return &framer_; }
199 QuicConnectionHelperInterface* helper() { return helper_.get(); }
201 QuicPacketWriter* writer() { return writer_.get(); }
203 const QuicConnection::PacketWriterFactory& connection_writer_factory() {
204 return connection_writer_factory_;
207 void SetLastError(QuicErrorCode error);
209 private:
210 class QuicFramerVisitor;
211 friend class net::tools::test::QuicDispatcherPeer;
213 // An adapter that creates packet writers using the dispatcher's
214 // PacketWriterFactory and shared writer. Essentially, it just curries the
215 // writer argument away from QuicDispatcher::PacketWriterFactory.
216 class PacketWriterFactoryAdapter :
217 public QuicConnection::PacketWriterFactory {
218 public:
219 explicit PacketWriterFactoryAdapter(QuicDispatcher* dispatcher);
220 ~PacketWriterFactoryAdapter() override;
222 QuicPacketWriter* Create(QuicConnection* connection) const override;
224 private:
225 QuicDispatcher* dispatcher_;
228 // Called by |framer_visitor_| when the private header has been parsed
229 // of a data packet that is destined for the time wait manager.
230 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
232 // Removes the session from the session map and write blocked list, and adds
233 // the ConnectionId to the time-wait list. If |session_closed_statelessly| is
234 // true, any future packets for the ConnectionId will be black-holed.
235 void CleanUpSession(SessionMap::iterator it, bool session_closed_statelessly);
237 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
239 const QuicConfig& config_;
241 const QuicCryptoServerConfig* crypto_config_;
243 // The list of connections waiting to write.
244 WriteBlockedList write_blocked_list_;
246 SessionMap session_map_;
248 // Entity that manages connection_ids in time wait state.
249 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
251 // The list of closed but not-yet-deleted sessions.
252 std::list<QuicServerSession*> closed_session_list_;
254 // The helper used for all connections.
255 scoped_ptr<QuicConnectionHelperInterface> helper_;
257 // An alarm which deletes closed sessions.
258 scoped_ptr<QuicAlarm> delete_sessions_alarm_;
260 // The writer to write to the socket with.
261 scoped_ptr<QuicPacketWriter> writer_;
263 // A per-connection writer that is passed to the time wait list manager.
264 scoped_ptr<QuicPacketWriter> time_wait_list_writer_;
266 // Used to create per-connection packet writers, not |writer_| itself.
267 scoped_ptr<PacketWriterFactory> packet_writer_factory_;
269 // Passed in to QuicConnection for it to create the per-connection writers
270 PacketWriterFactoryAdapter connection_writer_factory_;
272 // This vector contains QUIC versions which we currently support.
273 // This should be ordered such that the highest supported version is the first
274 // element, with subsequent elements in descending order (versions can be
275 // skipped as necessary).
276 const QuicVersionVector supported_versions_;
278 // Information about the packet currently being handled.
279 IPEndPoint current_client_address_;
280 IPEndPoint current_server_address_;
281 const QuicEncryptedPacket* current_packet_;
283 QuicFramer framer_;
284 scoped_ptr<QuicFramerVisitor> framer_visitor_;
286 // The last error set by SetLastError(), which is called by
287 // framer_visitor_->OnError().
288 QuicErrorCode last_error_;
290 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
293 } // namespace tools
294 } // namespace net
296 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_