OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A server side dispatcher which dispatches a given client's data to their | 5 // A server side dispatcher which dispatches a given client's data to their |
6 // stream. | 6 // stream. |
7 | 7 |
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | 8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ |
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | 9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
17 #include "net/base/linked_hash_map.h" | 17 #include "net/base/linked_hash_map.h" |
18 #include "net/quic/quic_blocked_writer_interface.h" | 18 #include "net/quic/quic_blocked_writer_interface.h" |
19 #include "net/quic/quic_protocol.h" | 19 #include "net/quic/quic_protocol.h" |
20 #include "net/tools/quic/quic_server_session.h" | 20 #include "net/tools/quic/quic_server_session.h" |
21 #include "net/tools/quic/quic_time_wait_list_manager.h" | 21 #include "net/tools/quic/quic_time_wait_list_manager.h" |
22 | 22 |
23 #if defined(COMPILER_GCC) | |
24 namespace BASE_HASH_NAMESPACE { | |
25 template<> | |
26 struct hash<net::QuicBlockedWriterInterface*> { | |
27 std::size_t operator()( | |
28 const net::QuicBlockedWriterInterface* ptr) const { | |
29 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
30 } | |
31 }; | |
32 } | |
33 #endif | |
34 | |
35 namespace net { | 23 namespace net { |
36 | 24 |
37 class EpollServer; | 25 class EpollServer; |
38 class QuicConfig; | 26 class QuicConfig; |
39 class QuicCryptoServerConfig; | 27 class QuicCryptoServerConfig; |
40 class QuicSession; | 28 class QuicSession; |
41 | 29 |
42 namespace tools { | 30 namespace tools { |
43 | 31 |
44 class QuicPacketWriterWrapper; | 32 class QuicPacketWriterWrapper; |
45 | 33 |
46 namespace test { | 34 namespace test { |
47 class QuicDispatcherPeer; | 35 class QuicDispatcherPeer; |
48 } // namespace test | 36 } // namespace test |
49 | 37 |
50 class DeleteSessionsAlarm; | 38 class DeleteSessionsAlarm; |
51 class QuicEpollConnectionHelper; | 39 class QuicEpollConnectionHelper; |
52 | 40 |
53 class ProcessPacketInterface { | 41 class ProcessPacketInterface { |
54 public: | 42 public: |
55 virtual ~ProcessPacketInterface() {} | 43 virtual ~ProcessPacketInterface() {} |
56 virtual void ProcessPacket(const IPEndPoint& server_address, | 44 virtual void ProcessPacket(const IPEndPoint& server_address, |
57 const IPEndPoint& client_address, | 45 const IPEndPoint& client_address, |
58 const QuicEncryptedPacket& packet) = 0; | 46 const QuicEncryptedPacket& packet) = 0; |
59 }; | 47 }; |
60 | 48 |
61 class QuicDispatcher : public QuicServerSessionVisitor, | 49 class QuicDispatcher : public QuicServerSessionVisitor, |
62 public ProcessPacketInterface { | 50 public ProcessPacketInterface { |
63 public: | 51 public: |
| 52 // Creates per-connection packet writers out of the QuicDispatcher's shared |
| 53 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must |
| 54 // always be the same as the shared writer's IsWriteBlocked(), or else the |
| 55 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be |
| 56 // cleaned up for bug 16950226.) |
| 57 class PacketWriterFactory { |
| 58 public: |
| 59 virtual ~PacketWriterFactory() {} |
| 60 |
| 61 virtual QuicPacketWriter* Create(QuicPacketWriter* writer, |
| 62 QuicConnection* connection) = 0; |
| 63 }; |
| 64 |
| 65 // Creates ordinary QuicPerConnectionPacketWriter instances. |
| 66 class DefaultPacketWriterFactory : public PacketWriterFactory { |
| 67 public: |
| 68 virtual ~DefaultPacketWriterFactory() {} |
| 69 |
| 70 virtual QuicPacketWriter* Create( |
| 71 QuicPacketWriter* writer, |
| 72 QuicConnection* connection) OVERRIDE; |
| 73 }; |
| 74 |
64 // Ideally we'd have a linked_hash_set: the boolean is unused. | 75 // Ideally we'd have a linked_hash_set: the boolean is unused. |
65 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; | 76 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; |
66 | 77 |
67 // Due to the way delete_sessions_closure_ is registered, the Dispatcher | 78 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must |
68 // must live until epoll_server Shutdown. |supported_versions| specifies the | 79 // live until epoll_server Shutdown. |supported_versions| specifies the list |
69 // list of supported QUIC versions. | 80 // of supported QUIC versions. Takes ownership of |packet_writer_factory|, |
| 81 // which is used to create per-connection writers. |
70 QuicDispatcher(const QuicConfig& config, | 82 QuicDispatcher(const QuicConfig& config, |
71 const QuicCryptoServerConfig& crypto_config, | 83 const QuicCryptoServerConfig& crypto_config, |
72 const QuicVersionVector& supported_versions, | 84 const QuicVersionVector& supported_versions, |
| 85 PacketWriterFactory* packet_writer_factory, |
73 EpollServer* epoll_server); | 86 EpollServer* epoll_server); |
74 | 87 |
75 virtual ~QuicDispatcher(); | 88 virtual ~QuicDispatcher(); |
76 | 89 |
77 virtual void Initialize(int fd); | 90 virtual void Initialize(int fd); |
78 | 91 |
79 // Process the incoming packet by creating a new session, passing it to | 92 // Process the incoming packet by creating a new session, passing it to |
80 // an existing session, or passing it to the TimeWaitListManager. | 93 // an existing session, or passing it to the TimeWaitListManager. |
81 virtual void ProcessPacket(const IPEndPoint& server_address, | 94 virtual void ProcessPacket(const IPEndPoint& server_address, |
82 const IPEndPoint& client_address, | 95 const IPEndPoint& client_address, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 const QuicConfig& config() const { return config_; } | 170 const QuicConfig& config() const { return config_; } |
158 | 171 |
159 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } | 172 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
160 | 173 |
161 QuicFramer* framer() { return &framer_; } | 174 QuicFramer* framer() { return &framer_; } |
162 | 175 |
163 QuicEpollConnectionHelper* helper() { return helper_.get(); } | 176 QuicEpollConnectionHelper* helper() { return helper_.get(); } |
164 | 177 |
165 QuicPacketWriter* writer() { return writer_.get(); } | 178 QuicPacketWriter* writer() { return writer_.get(); } |
166 | 179 |
| 180 const QuicConnection::PacketWriterFactory& connection_writer_factory() { |
| 181 return connection_writer_factory_; |
| 182 } |
| 183 |
167 private: | 184 private: |
168 class QuicFramerVisitor; | 185 class QuicFramerVisitor; |
169 friend class net::tools::test::QuicDispatcherPeer; | 186 friend class net::tools::test::QuicDispatcherPeer; |
170 | 187 |
| 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 PacketWriterFactoryAdapter(QuicDispatcher* dispatcher); |
| 195 virtual ~PacketWriterFactoryAdapter (); |
| 196 |
| 197 virtual QuicPacketWriter* Create(QuicConnection* connection) const OVERRIDE; |
| 198 |
| 199 private: |
| 200 QuicDispatcher* dispatcher_; |
| 201 }; |
| 202 |
171 // Called by |framer_visitor_| when the private header has been parsed | 203 // Called by |framer_visitor_| when the private header has been parsed |
172 // of a data packet that is destined for the time wait manager. | 204 // of a data packet that is destined for the time wait manager. |
173 void OnUnauthenticatedHeader(const QuicPacketHeader& header); | 205 void OnUnauthenticatedHeader(const QuicPacketHeader& header); |
174 | 206 |
175 // Removes the session from the session map and write blocked list, and | 207 // Removes the session from the session map and write blocked list, and |
176 // adds the ConnectionId to the time-wait list. | 208 // adds the ConnectionId to the time-wait list. |
177 void CleanUpSession(SessionMap::iterator it); | 209 void CleanUpSession(SessionMap::iterator it); |
178 | 210 |
179 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); | 211 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); |
180 | 212 |
(...skipping 16 matching lines...) Expand all Loading... |
197 std::list<QuicSession*> closed_session_list_; | 229 std::list<QuicSession*> closed_session_list_; |
198 | 230 |
199 EpollServer* epoll_server_; // Owned by the server. | 231 EpollServer* epoll_server_; // Owned by the server. |
200 | 232 |
201 // The helper used for all connections. | 233 // The helper used for all connections. |
202 scoped_ptr<QuicEpollConnectionHelper> helper_; | 234 scoped_ptr<QuicEpollConnectionHelper> helper_; |
203 | 235 |
204 // The writer to write to the socket with. | 236 // The writer to write to the socket with. |
205 scoped_ptr<QuicPacketWriter> writer_; | 237 scoped_ptr<QuicPacketWriter> writer_; |
206 | 238 |
| 239 // Used to create per-connection packet writers, not |writer_| itself. |
| 240 scoped_ptr<PacketWriterFactory> packet_writer_factory_; |
| 241 |
| 242 // Passed in to QuicConnection for it to create the per-connection writers |
| 243 PacketWriterFactoryAdapter connection_writer_factory_; |
| 244 |
207 // This vector contains QUIC versions which we currently support. | 245 // This vector contains QUIC versions which we currently support. |
208 // This should be ordered such that the highest supported version is the first | 246 // This should be ordered such that the highest supported version is the first |
209 // element, with subsequent elements in descending order (versions can be | 247 // element, with subsequent elements in descending order (versions can be |
210 // skipped as necessary). | 248 // skipped as necessary). |
211 const QuicVersionVector supported_versions_; | 249 const QuicVersionVector supported_versions_; |
212 | 250 |
213 // Information about the packet currently being handled. | 251 // Information about the packet currently being handled. |
214 IPEndPoint current_client_address_; | 252 IPEndPoint current_client_address_; |
215 IPEndPoint current_server_address_; | 253 IPEndPoint current_server_address_; |
216 const QuicEncryptedPacket* current_packet_; | 254 const QuicEncryptedPacket* current_packet_; |
217 | 255 |
218 QuicFramer framer_; | 256 QuicFramer framer_; |
219 scoped_ptr<QuicFramerVisitor> framer_visitor_; | 257 scoped_ptr<QuicFramerVisitor> framer_visitor_; |
220 | 258 |
221 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); | 259 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); |
222 }; | 260 }; |
223 | 261 |
224 } // namespace tools | 262 } // namespace tools |
225 } // namespace net | 263 } // namespace net |
226 | 264 |
227 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | 265 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ |
OLD | NEW |