pepper: add software vp9 encoder support to PPB_VideoEncoder
[chromium-blink-merge.git] / net / tools / quic / quic_dispatcher.h
bloba1befe67bd545991d91cfbcf77d8a4013a0d463e
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 time wait list.
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 time wait list manager adds a new connection to the
110 // time-wait list.
111 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id) override;
113 // Called whenever the time wait list manager 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 // The largest packet sequence number we expect to receive with a connection
126 // ID for a connection that is not established yet. The current design will
127 // send a handshake and then up to 50 or so data packets, and then it may
128 // resend the handshake packet up to 10 times. (Retransmitted packets are
129 // sent with unique sequence numbers.)
130 static const QuicPacketSequenceNumber kMaxReasonableInitialSequenceNumber =
131 100;
132 static_assert(kMaxReasonableInitialSequenceNumber >=
133 kInitialCongestionWindowSecure + 10,
134 "kMaxReasonableInitialSequenceNumber is unreasonably small "
135 "relative to kInitialCongestionWindowSecure.");
136 static_assert(kMaxReasonableInitialSequenceNumber >=
137 kInitialCongestionWindowInsecure + 10,
138 "kMaxReasonableInitialSequenceNumber is unreasonably small "
139 "relative to kInitialCongestionWindowInsecure.");
141 protected:
142 virtual QuicServerSession* CreateQuicSession(
143 QuicConnectionId connection_id,
144 const IPEndPoint& server_address,
145 const IPEndPoint& client_address);
147 // Called by |framer_visitor_| when the public header has been parsed.
148 virtual bool OnUnauthenticatedPublicHeader(
149 const QuicPacketPublicHeader& header);
151 // Values to be returned by ValidityChecks() to indicate what should be done
152 // with a packet. Fates with greater values are considered to be higher
153 // priority, in that if one validity check indicates a lower-valued fate and
154 // another validity check indicates a higher-valued fate, the higher-valued
155 // fate should be obeyed.
156 enum QuicPacketFate {
157 // Process the packet normally, which is usually to establish a connection.
158 kFateProcess,
159 // Put the connection ID into time-wait state and send a public reset.
160 kFateTimeWait,
161 // Drop the packet (ignore and give no response).
162 kFateDrop,
165 // This method is called by OnUnauthenticatedHeader on packets not associated
166 // with a known connection ID. It applies validity checks and returns a
167 // QuicPacketFate to tell what should be done with the packet.
168 virtual QuicPacketFate ValidityChecks(const QuicPacketHeader& header);
170 // Create and return the time wait list manager for this dispatcher, which
171 // will be owned by the dispatcher as time_wait_list_manager_
172 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
174 QuicTimeWaitListManager* time_wait_list_manager() {
175 return time_wait_list_manager_.get();
178 const QuicVersionVector& supported_versions() const {
179 return supported_versions_;
182 const IPEndPoint& current_server_address() {
183 return current_server_address_;
185 const IPEndPoint& current_client_address() {
186 return current_client_address_;
188 const QuicEncryptedPacket& current_packet() {
189 return *current_packet_;
192 const QuicConfig& config() const { return config_; }
194 const QuicCryptoServerConfig* crypto_config() const { return crypto_config_; }
196 QuicFramer* framer() { return &framer_; }
198 QuicConnectionHelperInterface* helper() { return helper_.get(); }
200 QuicPacketWriter* writer() { return writer_.get(); }
202 const QuicConnection::PacketWriterFactory& connection_writer_factory() {
203 return connection_writer_factory_;
206 void SetLastError(QuicErrorCode error);
208 private:
209 class QuicFramerVisitor;
210 friend class net::tools::test::QuicDispatcherPeer;
212 // An adapter that creates packet writers using the dispatcher's
213 // PacketWriterFactory and shared writer. Essentially, it just curries the
214 // writer argument away from QuicDispatcher::PacketWriterFactory.
215 class PacketWriterFactoryAdapter :
216 public QuicConnection::PacketWriterFactory {
217 public:
218 explicit PacketWriterFactoryAdapter(QuicDispatcher* dispatcher);
219 ~PacketWriterFactoryAdapter() override;
221 QuicPacketWriter* Create(QuicConnection* connection) const override;
223 private:
224 QuicDispatcher* dispatcher_;
227 // Called by |framer_visitor_| when the private header has been parsed
228 // of a data packet that is destined for the time wait manager.
229 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
231 // Removes the session from the session map and write blocked list, and adds
232 // the ConnectionId to the time-wait list. If |session_closed_statelessly| is
233 // true, any future packets for the ConnectionId will be black-holed.
234 void CleanUpSession(SessionMap::iterator it, bool session_closed_statelessly);
236 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
238 const QuicConfig& config_;
240 const QuicCryptoServerConfig* crypto_config_;
242 // The list of connections waiting to write.
243 WriteBlockedList write_blocked_list_;
245 SessionMap session_map_;
247 // Entity that manages connection_ids in time wait state.
248 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
250 // The list of closed but not-yet-deleted sessions.
251 std::list<QuicServerSession*> closed_session_list_;
253 // The helper used for all connections.
254 scoped_ptr<QuicConnectionHelperInterface> helper_;
256 // An alarm which deletes closed sessions.
257 scoped_ptr<QuicAlarm> delete_sessions_alarm_;
259 // The writer to write to the socket with.
260 scoped_ptr<QuicPacketWriter> writer_;
262 // A per-connection writer that is passed to the time wait list manager.
263 scoped_ptr<QuicPacketWriter> time_wait_list_writer_;
265 // Used to create per-connection packet writers, not |writer_| itself.
266 scoped_ptr<PacketWriterFactory> packet_writer_factory_;
268 // Passed in to QuicConnection for it to create the per-connection writers
269 PacketWriterFactoryAdapter connection_writer_factory_;
271 // This vector contains QUIC versions which we currently support.
272 // This should be ordered such that the highest supported version is the first
273 // element, with subsequent elements in descending order (versions can be
274 // skipped as necessary).
275 const QuicVersionVector supported_versions_;
277 // Information about the packet currently being handled.
278 IPEndPoint current_client_address_;
279 IPEndPoint current_server_address_;
280 const QuicEncryptedPacket* current_packet_;
282 QuicFramer framer_;
283 scoped_ptr<QuicFramerVisitor> framer_visitor_;
285 // The last error set by SetLastError(), which is called by
286 // framer_visitor_->OnError().
287 QuicErrorCode last_error_;
289 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
292 } // namespace tools
293 } // namespace net
295 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_