OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_QUIC_QUIC_DISPATCHER_H_ | 8 #ifndef NET_QUIC_QUIC_DISPATCHER_H_ |
9 #define NET_QUIC_QUIC_DISPATCHER_H_ | 9 #define NET_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_connection_helper.h" | 19 #include "net/quic/quic_connection_helper.h" |
20 #include "net/quic/quic_protocol.h" | 20 #include "net/quic/quic_protocol.h" |
21 #include "net/quic/quic_server_packet_writer.h" | 21 #include "net/quic/quic_server_packet_writer.h" |
22 #include "net/quic/quic_server_session.h" | 22 #include "net/quic/quic_server_session.h" |
23 #include "net/quic/quic_time_wait_list_manager.h" | 23 #include "net/quic/quic_time_wait_list_manager.h" |
24 | 24 |
25 #if defined(COMPILER_GCC) | 25 #if defined(COMPILER_GCC) && !defined(QUIC_BLOCKED_WRITER_INTERFACE_HASH) |
26 #define QUIC_BLOCKED_WRITER_INTERFACE_HASH | |
wtc
2014/08/13 21:01:34
IMPORTANT: this change is not ideal. We should try
dmz
2014/08/14 17:22:42
Done.
| |
26 namespace BASE_HASH_NAMESPACE { | 27 namespace BASE_HASH_NAMESPACE { |
27 template <> | 28 template <> |
28 struct hash<net::QuicBlockedWriterInterface*> { | 29 struct hash<net::QuicBlockedWriterInterface*> { |
29 std::size_t operator()(const net::QuicBlockedWriterInterface* ptr) const { | 30 std::size_t operator()(const net::QuicBlockedWriterInterface* ptr) const { |
30 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 31 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
wtc
2014/08/13 21:01:34
Nit: we may want to switch to this implementation
dmz
2014/08/14 17:22:41
Done.
| |
31 } | 32 } |
32 }; | 33 }; |
33 } | 34 } |
34 #endif | 35 #endif |
35 | 36 |
36 namespace net { | 37 namespace net { |
37 | 38 |
38 class QuicConfig; | 39 class QuicConfig; |
39 class QuicCryptoServerConfig; | 40 class QuicCryptoServerConfig; |
40 class QuicSession; | 41 class QuicSession; |
41 | 42 |
42 namespace test { | 43 namespace test { |
43 class QuicDispatcherPeer; | 44 class QuicDispatcherPeer; |
44 } // namespace test | 45 } // namespace test |
45 | 46 |
46 class DeleteSessionsAlarm; | 47 class DeleteSessionsAlarm; |
47 | 48 |
48 class ProcessPacketInterface { | 49 class ProcessPacketInterface { |
49 public: | 50 public: |
50 virtual ~ProcessPacketInterface() {} | 51 virtual ~ProcessPacketInterface() {} |
51 virtual void ProcessPacket(const IPEndPoint& server_address, | 52 virtual void ProcessPacket(const IPEndPoint& server_address, |
52 const IPEndPoint& client_address, | 53 const IPEndPoint& client_address, |
53 const QuicEncryptedPacket& packet) = 0; | 54 const QuicEncryptedPacket& packet) = 0; |
54 }; | 55 }; |
55 | 56 |
56 class QuicDispatcher : public QuicBlockedWriterInterface, | 57 class QuicDispatcher : public QuicBlockedWriterInterface, |
57 public QuicServerSessionVisitor, | 58 public QuicServerSessionVisitor, |
58 public ProcessPacketInterface { | 59 public ProcessPacketInterface { |
59 public: | 60 public: |
61 // Creates per-connection packet writers out of the QuicDispatcher's shared | |
62 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must | |
63 // always be the same as the shared writer's IsWriteBlocked(), or else the | |
64 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be | |
65 // cleaned up for bug 16950226.) | |
66 class PacketWriterFactory { | |
67 public: | |
68 virtual ~PacketWriterFactory() {} | |
69 | |
70 virtual QuicPacketWriter* Create(QuicServerPacketWriter* writer, | |
wtc
2014/08/13 21:01:34
IMPORTANT: please confirm that it is correct for t
dmz
2014/08/14 17:22:42
Yes, that's right.
| |
71 QuicConnection* connection) = 0; | |
72 }; | |
73 | |
74 // Creates ordinary QuicPerConnectionPacketWriter instances. | |
75 class DefaultPacketWriterFactory : public PacketWriterFactory { | |
76 public: | |
77 virtual ~DefaultPacketWriterFactory() {} | |
78 | |
79 virtual QuicPacketWriter* Create( | |
80 QuicServerPacketWriter* writer, | |
81 QuicConnection* connection) OVERRIDE; | |
82 }; | |
83 | |
60 // Ideally we'd have a linked_hash_set: the boolean is unused. | 84 // Ideally we'd have a linked_hash_set: the boolean is unused. |
61 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; | 85 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; |
62 | 86 |
63 // Due to the way delete_sessions_closure_ is registered, the Dispatcher | 87 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must |
64 // must live until epoll_server Shutdown. |supported_versions| specifies the | 88 // live until epoll_server Shutdown. |supported_versions| specifies the list |
65 // list of supported QUIC versions. | 89 // of supported QUIC versions. Takes ownership of |packet_writer_factory|, |
90 // which is used to create per-connection writers. | |
66 QuicDispatcher(const QuicConfig& config, | 91 QuicDispatcher(const QuicConfig& config, |
67 const QuicCryptoServerConfig& crypto_config, | 92 const QuicCryptoServerConfig& crypto_config, |
68 const QuicVersionVector& supported_versions, | 93 const QuicVersionVector& supported_versions, |
94 PacketWriterFactory* packet_writer_factory, | |
69 QuicConnectionHelperInterface* helper); | 95 QuicConnectionHelperInterface* helper); |
70 | 96 |
71 virtual ~QuicDispatcher(); | 97 virtual ~QuicDispatcher(); |
72 | 98 |
73 // Takes ownership of the packet writer | 99 // Takes ownership of the packet writer |
74 virtual void Initialize(QuicServerPacketWriter* writer); | 100 virtual void Initialize(QuicServerPacketWriter* writer); |
75 | 101 |
76 // Process the incoming packet by creating a new session, passing it to | 102 // Process the incoming packet by creating a new session, passing it to |
77 // an existing session, or passing it to the TimeWaitListManager. | 103 // an existing session, or passing it to the TimeWaitListManager. |
78 virtual void ProcessPacket(const IPEndPoint& server_address, | 104 virtual void ProcessPacket(const IPEndPoint& server_address, |
(...skipping 27 matching lines...) Expand all Loading... | |
106 const SessionMap& session_map() const { return session_map_; } | 132 const SessionMap& session_map() const { return session_map_; } |
107 | 133 |
108 protected: | 134 protected: |
109 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, | 135 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, |
110 const IPEndPoint& server_address, | 136 const IPEndPoint& server_address, |
111 const IPEndPoint& client_address); | 137 const IPEndPoint& client_address); |
112 | 138 |
113 virtual QuicConnection* CreateQuicConnection( | 139 virtual QuicConnection* CreateQuicConnection( |
114 QuicConnectionId connection_id, | 140 QuicConnectionId connection_id, |
115 const IPEndPoint& server_address, | 141 const IPEndPoint& server_address, |
116 const IPEndPoint& client_address, | 142 const IPEndPoint& client_address); |
117 QuicPerConnectionPacketWriter* writer); | |
118 | 143 |
119 // Called by |framer_visitor_| when the public header has been parsed. | 144 // Called by |framer_visitor_| when the public header has been parsed. |
120 virtual bool OnUnauthenticatedPublicHeader( | 145 virtual bool OnUnauthenticatedPublicHeader( |
121 const QuicPacketPublicHeader& header); | 146 const QuicPacketPublicHeader& header); |
122 | 147 |
123 // Create and return the time wait list manager for this dispatcher, which | 148 // Create and return the time wait list manager for this dispatcher, which |
124 // will be owned by the dispatcher as time_wait_list_manager_ | 149 // will be owned by the dispatcher as time_wait_list_manager_ |
125 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); | 150 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); |
126 | 151 |
127 // Replaces the packet writer with |writer|. Takes ownership of |writer|. | 152 // Replaces the packet writer with |writer|. Takes ownership of |writer|. |
(...skipping 22 matching lines...) Expand all Loading... | |
150 const QuicConfig& config() const { return config_; } | 175 const QuicConfig& config() const { return config_; } |
151 | 176 |
152 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } | 177 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
153 | 178 |
154 QuicFramer* framer() { return &framer_; } | 179 QuicFramer* framer() { return &framer_; } |
155 | 180 |
156 QuicConnectionHelperInterface* helper() { return helper_; } | 181 QuicConnectionHelperInterface* helper() { return helper_; } |
157 | 182 |
158 QuicServerPacketWriter* writer() { return writer_.get(); } | 183 QuicServerPacketWriter* writer() { return writer_.get(); } |
159 | 184 |
185 const QuicConnection::PacketWriterFactory& connection_writer_factory() { | |
186 return connection_writer_factory_; | |
187 } | |
188 | |
160 private: | 189 private: |
161 class QuicFramerVisitor; | 190 class QuicFramerVisitor; |
162 friend class net::test::QuicDispatcherPeer; | 191 friend class net::test::QuicDispatcherPeer; |
163 | 192 |
193 // An adapter that creates packet writers using the dispatcher's | |
194 // PacketWriterFactory and shared writer. Essentially, it just curries the | |
195 // writer argument away from QuicDispatcher::PacketWriterFactory. | |
196 class PacketWriterFactoryAdapter : | |
197 public QuicConnection::PacketWriterFactory { | |
198 public: | |
199 PacketWriterFactoryAdapter(QuicDispatcher* dispatcher); | |
200 virtual ~PacketWriterFactoryAdapter (); | |
201 | |
202 virtual QuicPacketWriter* Create(QuicConnection* connection) const OVERRIDE; | |
203 | |
204 private: | |
205 QuicDispatcher* dispatcher_; | |
206 }; | |
207 | |
164 // Called by |framer_visitor_| when the private header has been parsed | 208 // Called by |framer_visitor_| when the private header has been parsed |
165 // of a data packet that is destined for the time wait manager. | 209 // of a data packet that is destined for the time wait manager. |
166 void OnUnauthenticatedHeader(const QuicPacketHeader& header); | 210 void OnUnauthenticatedHeader(const QuicPacketHeader& header); |
167 | 211 |
168 // Removes the session from the session map and write blocked list, and | 212 // Removes the session from the session map and write blocked list, and |
169 // adds the ConnectionId to the time-wait list. | 213 // adds the ConnectionId to the time-wait list. |
170 void CleanUpSession(SessionMap::iterator it); | 214 void CleanUpSession(SessionMap::iterator it); |
171 | 215 |
172 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); | 216 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); |
173 | 217 |
(...skipping 14 matching lines...) Expand all Loading... | |
188 | 232 |
189 // An alarm which deletes closed sessions. | 233 // An alarm which deletes closed sessions. |
190 scoped_ptr<QuicAlarm> delete_sessions_alarm_; | 234 scoped_ptr<QuicAlarm> delete_sessions_alarm_; |
191 | 235 |
192 // The list of closed but not-yet-deleted sessions. | 236 // The list of closed but not-yet-deleted sessions. |
193 std::list<QuicSession*> closed_session_list_; | 237 std::list<QuicSession*> closed_session_list_; |
194 | 238 |
195 // The writer to write to the socket with. | 239 // The writer to write to the socket with. |
196 scoped_ptr<QuicServerPacketWriter> writer_; | 240 scoped_ptr<QuicServerPacketWriter> writer_; |
197 | 241 |
242 // Used to create per-connection packet writers, not |writer_| itself. | |
243 scoped_ptr<PacketWriterFactory> packet_writer_factory_; | |
244 | |
245 // Passed in to QuicConnection for it to create the per-connection writers | |
246 PacketWriterFactoryAdapter connection_writer_factory_; | |
247 | |
198 // This vector contains QUIC versions which we currently support. | 248 // This vector contains QUIC versions which we currently support. |
199 // This should be ordered such that the highest supported version is the first | 249 // This should be ordered such that the highest supported version is the first |
200 // element, with subsequent elements in descending order (versions can be | 250 // element, with subsequent elements in descending order (versions can be |
201 // skipped as necessary). | 251 // skipped as necessary). |
202 const QuicVersionVector supported_versions_; | 252 const QuicVersionVector supported_versions_; |
203 | 253 |
204 // Information about the packet currently being handled. | 254 // Information about the packet currently being handled. |
205 IPEndPoint current_client_address_; | 255 IPEndPoint current_client_address_; |
206 IPEndPoint current_server_address_; | 256 IPEndPoint current_server_address_; |
207 const QuicEncryptedPacket* current_packet_; | 257 const QuicEncryptedPacket* current_packet_; |
208 | 258 |
209 QuicFramer framer_; | 259 QuicFramer framer_; |
210 scoped_ptr<QuicFramerVisitor> framer_visitor_; | 260 scoped_ptr<QuicFramerVisitor> framer_visitor_; |
211 | 261 |
212 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); | 262 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); |
213 }; | 263 }; |
214 | 264 |
215 } // namespace net | 265 } // namespace net |
216 | 266 |
217 #endif // NET_QUIC_QUIC_DISPATCHER_H_ | 267 #endif // NET_QUIC_QUIC_DISPATCHER_H_ |
OLD | NEW |