OLD | NEW |
| (Empty) |
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. | |
7 | |
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | |
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | |
10 | |
11 #include <list> | |
12 | |
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" | |
22 | |
23 namespace net { | |
24 | |
25 class EpollServer; | |
26 class QuicConfig; | |
27 class QuicCryptoServerConfig; | |
28 class QuicSession; | |
29 | |
30 namespace tools { | |
31 | |
32 class DeleteSessionsAlarm; | |
33 class QuicEpollConnectionHelper; | |
34 class QuicPacketWriterWrapper; | |
35 | |
36 namespace test { | |
37 class QuicDispatcherPeer; | |
38 } // namespace test | |
39 | |
40 class ProcessPacketInterface { | |
41 public: | |
42 virtual ~ProcessPacketInterface() {} | |
43 virtual void ProcessPacket(const IPEndPoint& server_address, | |
44 const IPEndPoint& client_address, | |
45 const QuicEncryptedPacket& packet) = 0; | |
46 }; | |
47 | |
48 class QuicDispatcher : public QuicServerSessionVisitor, | |
49 public ProcessPacketInterface { | |
50 public: | |
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 { | |
57 public: | |
58 virtual ~PacketWriterFactory() {} | |
59 | |
60 virtual QuicPacketWriter* Create(QuicPacketWriter* writer, | |
61 QuicConnection* connection) = 0; | |
62 }; | |
63 | |
64 // Creates ordinary QuicPerConnectionPacketWriter instances. | |
65 class DefaultPacketWriterFactory : public PacketWriterFactory { | |
66 public: | |
67 ~DefaultPacketWriterFactory() override {} | |
68 | |
69 QuicPacketWriter* Create(QuicPacketWriter* writer, | |
70 QuicConnection* connection) override; | |
71 }; | |
72 | |
73 // Ideally we'd have a linked_hash_set: the boolean is unused. | |
74 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; | |
75 | |
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); | |
85 | |
86 ~QuicDispatcher() override; | |
87 | |
88 virtual void Initialize(int fd); | |
89 | |
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; | |
95 | |
96 // Called when the socket becomes writable to allow queued writes to happen. | |
97 virtual void OnCanWrite(); | |
98 | |
99 // Returns true if there's anything in the blocked writer list. | |
100 virtual bool HasPendingWrites() const; | |
101 | |
102 // Sends ConnectionClose frames to all connected clients. | |
103 void Shutdown(); | |
104 | |
105 // QuicServerSessionVisitor interface implementation: | |
106 // Ensure that the closed connection is cleaned up asynchronously. | |
107 void OnConnectionClosed(QuicConnectionId connection_id, | |
108 QuicErrorCode error) override; | |
109 | |
110 // Queues the blocked writer for later resumption. | |
111 void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) override; | |
112 | |
113 // Called whenever the QuicTimeWaitListManager adds a new connection to the | |
114 // time-wait list. | |
115 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id) override; | |
116 | |
117 // Called whenever the QuicTimeWaitListManager removes an old connection from | |
118 // the time-wait list. | |
119 void OnConnectionRemovedFromTimeWaitList( | |
120 QuicConnectionId connection_id) override; | |
121 | |
122 typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap; | |
123 | |
124 // Deletes all sessions on the closed session list and clears the list. | |
125 void DeleteSessions(); | |
126 | |
127 const SessionMap& session_map() const { return session_map_; } | |
128 | |
129 protected: | |
130 // Instantiates a new low-level packet writer. Caller takes ownership of the | |
131 // returned object. | |
132 virtual QuicPacketWriter* CreateWriter(int fd); | |
133 | |
134 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, | |
135 const IPEndPoint& server_address, | |
136 const IPEndPoint& client_address); | |
137 | |
138 virtual QuicConnection* CreateQuicConnection( | |
139 QuicConnectionId connection_id, | |
140 const IPEndPoint& server_address, | |
141 const IPEndPoint& client_address); | |
142 | |
143 // Called by |framer_visitor_| when the public header has been parsed. | |
144 virtual bool OnUnauthenticatedPublicHeader( | |
145 const QuicPacketPublicHeader& header); | |
146 | |
147 // Create and return the time wait list manager for this dispatcher, which | |
148 // will be owned by the dispatcher as time_wait_list_manager_ | |
149 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); | |
150 | |
151 // Replaces the packet writer with |writer|. Takes ownership of |writer|. | |
152 void set_writer(QuicPacketWriter* writer) { | |
153 writer_.reset(writer); | |
154 } | |
155 | |
156 QuicTimeWaitListManager* time_wait_list_manager() { | |
157 return time_wait_list_manager_.get(); | |
158 } | |
159 | |
160 EpollServer* epoll_server() { return epoll_server_; } | |
161 | |
162 const QuicVersionVector& supported_versions() const { | |
163 return supported_versions_; | |
164 } | |
165 | |
166 const IPEndPoint& current_server_address() { | |
167 return current_server_address_; | |
168 } | |
169 const IPEndPoint& current_client_address() { | |
170 return current_client_address_; | |
171 } | |
172 const QuicEncryptedPacket& current_packet() { | |
173 return *current_packet_; | |
174 } | |
175 | |
176 const QuicConfig& config() const { return config_; } | |
177 | |
178 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } | |
179 | |
180 QuicFramer* framer() { return &framer_; } | |
181 | |
182 QuicEpollConnectionHelper* helper() { return helper_.get(); } | |
183 | |
184 QuicPacketWriter* writer() { return writer_.get(); } | |
185 | |
186 const QuicConnection::PacketWriterFactory& connection_writer_factory() { | |
187 return connection_writer_factory_; | |
188 } | |
189 | |
190 private: | |
191 class QuicFramerVisitor; | |
192 friend class net::tools::test::QuicDispatcherPeer; | |
193 | |
194 // An adapter that creates packet writers using the dispatcher's | |
195 // PacketWriterFactory and shared writer. Essentially, it just curries the | |
196 // writer argument away from QuicDispatcher::PacketWriterFactory. | |
197 class PacketWriterFactoryAdapter : | |
198 public QuicConnection::PacketWriterFactory { | |
199 public: | |
200 PacketWriterFactoryAdapter(QuicDispatcher* dispatcher); | |
201 ~PacketWriterFactoryAdapter() override; | |
202 | |
203 QuicPacketWriter* Create(QuicConnection* connection) const override; | |
204 | |
205 private: | |
206 QuicDispatcher* dispatcher_; | |
207 }; | |
208 | |
209 // Called by |framer_visitor_| when the private header has been parsed | |
210 // of a data packet that is destined for the time wait manager. | |
211 void OnUnauthenticatedHeader(const QuicPacketHeader& header); | |
212 | |
213 // Removes the session from the session map and write blocked list, and | |
214 // adds the ConnectionId to the time-wait list. | |
215 void CleanUpSession(SessionMap::iterator it); | |
216 | |
217 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); | |
218 | |
219 const QuicConfig& config_; | |
220 | |
221 const QuicCryptoServerConfig& crypto_config_; | |
222 | |
223 // The list of connections waiting to write. | |
224 WriteBlockedList write_blocked_list_; | |
225 | |
226 SessionMap session_map_; | |
227 | |
228 // Entity that manages connection_ids in time wait state. | |
229 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_; | |
230 | |
231 // An alarm which deletes closed sessions. | |
232 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_; | |
233 | |
234 // The list of closed but not-yet-deleted sessions. | |
235 std::list<QuicSession*> closed_session_list_; | |
236 | |
237 EpollServer* epoll_server_; // Owned by the server. | |
238 | |
239 // The helper used for all connections. | |
240 scoped_ptr<QuicEpollConnectionHelper> helper_; | |
241 | |
242 // The writer to write to the socket with. | |
243 scoped_ptr<QuicPacketWriter> writer_; | |
244 | |
245 // Used to create per-connection packet writers, not |writer_| itself. | |
246 scoped_ptr<PacketWriterFactory> packet_writer_factory_; | |
247 | |
248 // Passed in to QuicConnection for it to create the per-connection writers | |
249 PacketWriterFactoryAdapter connection_writer_factory_; | |
250 | |
251 // This vector contains QUIC versions which we currently support. | |
252 // This should be ordered such that the highest supported version is the first | |
253 // element, with subsequent elements in descending order (versions can be | |
254 // skipped as necessary). | |
255 const QuicVersionVector supported_versions_; | |
256 | |
257 // Information about the packet currently being handled. | |
258 IPEndPoint current_client_address_; | |
259 IPEndPoint current_server_address_; | |
260 const QuicEncryptedPacket* current_packet_; | |
261 | |
262 QuicFramer framer_; | |
263 scoped_ptr<QuicFramerVisitor> framer_visitor_; | |
264 | |
265 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); | |
266 }; | |
267 | |
268 } // namespace tools | |
269 } // namespace net | |
270 | |
271 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | |
OLD | NEW |