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_session.h" | 22 #include "net/quic/quic_server_session.h" |
22 #include "net/quic/quic_time_wait_list_manager.h" | 23 #include "net/quic/quic_time_wait_list_manager.h" |
23 | 24 |
24 #if defined(COMPILER_GCC) | 25 #if defined(COMPILER_GCC) |
25 namespace BASE_HASH_NAMESPACE { | 26 namespace BASE_HASH_NAMESPACE { |
26 template <> | 27 template <> |
27 struct hash<net::QuicBlockedWriterInterface*> { | 28 struct hash<net::QuicBlockedWriterInterface*> { |
28 std::size_t operator()(const net::QuicBlockedWriterInterface* ptr) const { | 29 std::size_t operator()(const net::QuicBlockedWriterInterface* ptr) const { |
29 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 30 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
30 } | 31 } |
31 }; | 32 }; |
32 } | 33 } |
33 #endif | 34 #endif |
34 | 35 |
35 namespace net { | 36 namespace net { |
36 | 37 |
37 class QuicConfig; | 38 class QuicConfig; |
38 class QuicCryptoServerConfig; | 39 class QuicCryptoServerConfig; |
39 class QuicPacketWriterWrapper; | |
40 class QuicSession; | 40 class QuicSession; |
41 class UDPServerSocket; | |
42 | 41 |
43 namespace test { | 42 namespace test { |
44 class QuicDispatcherPeer; | 43 class QuicDispatcherPeer; |
45 } // namespace test | 44 } // namespace test |
46 | 45 |
47 class DeleteSessionsAlarm; | 46 class DeleteSessionsAlarm; |
48 | 47 |
49 class QuicDispatcher : public QuicServerSessionVisitor { | 48 class QuicDispatcher : public QuicBlockedWriterInterface, |
| 49 public QuicServerSessionVisitor { |
50 public: | 50 public: |
51 // Ideally we'd have a linked_hash_set: the boolean is unused. | 51 // Ideally we'd have a linked_hash_set: the boolean is unused. |
52 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; | 52 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; |
53 | 53 |
54 // Due to the way delete_sessions_closure_ is registered, the Dispatcher | 54 // Due to the way delete_sessions_closure_ is registered, the Dispatcher |
55 // must live until epoll_server Shutdown. |supported_versions| specifies the | 55 // must live until epoll_server Shutdown. |supported_versions| specifies the |
56 // list of supported QUIC versions. | 56 // list of supported QUIC versions. |
57 QuicDispatcher(const QuicConfig& config, | 57 QuicDispatcher(const QuicConfig& config, |
58 const QuicCryptoServerConfig& crypto_config, | 58 const QuicCryptoServerConfig& crypto_config, |
59 const QuicVersionVector& supported_versions, | 59 const QuicVersionVector& supported_versions, |
60 QuicConnectionHelperInterface* helper); | 60 QuicConnectionHelperInterface* helper); |
61 | 61 |
62 virtual ~QuicDispatcher(); | 62 virtual ~QuicDispatcher(); |
63 | 63 |
64 // Takes ownership of the packet writer | 64 // Takes ownership of the packet writer |
65 virtual void Initialize(QuicPacketWriter* writer); | 65 virtual void Initialize(QuicServerPacketWriter* writer); |
66 | 66 |
67 // Process the incoming packet by creating a new session, passing it to | 67 // Process the incoming packet by creating a new session, passing it to |
68 // an existing session, or passing it to the TimeWaitListManager. | 68 // an existing session, or passing it to the TimeWaitListManager. |
69 virtual void ProcessPacket(const IPEndPoint& server_address, | 69 virtual void ProcessPacket(const IPEndPoint& server_address, |
70 const IPEndPoint& client_address, | 70 const IPEndPoint& client_address, |
71 const QuicEncryptedPacket& packet); | 71 const QuicEncryptedPacket& packet); |
72 | 72 |
73 // Called when the socket becomes writable to allow queued writes to happen. | |
74 virtual void OnCanWrite(); | |
75 | |
76 // Returns true if there's anything in the blocked writer list. | 73 // Returns true if there's anything in the blocked writer list. |
77 virtual bool HasPendingWrites() const; | 74 virtual bool HasPendingWrites() const; |
78 | 75 |
79 // Sends ConnectionClose frames to all connected clients. | 76 // Sends ConnectionClose frames to all connected clients. |
80 void Shutdown(); | 77 void Shutdown(); |
81 | 78 |
| 79 // QuicBlockedWriterInterface implementation: |
| 80 // Called when the socket becomes writable to allow queued writes to happen. |
| 81 virtual void OnCanWrite() OVERRIDE; |
| 82 |
82 // QuicServerSessionVisitor interface implementation: | 83 // QuicServerSessionVisitor interface implementation: |
83 // Ensure that the closed connection is cleaned up asynchronously. | 84 // Ensure that the closed connection is cleaned up asynchronously. |
84 virtual void OnConnectionClosed(QuicConnectionId connection_id, | 85 virtual void OnConnectionClosed(QuicConnectionId connection_id, |
85 QuicErrorCode error) OVERRIDE; | 86 QuicErrorCode error) OVERRIDE; |
86 | 87 |
87 // Queues the blocked writer for later resumption. | 88 // Queues the blocked writer for later resumption. |
88 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE; | 89 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE; |
89 | 90 |
90 typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap; | 91 typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap; |
91 | 92 |
92 // Deletes all sessions on the closed session list and clears the list. | 93 // Deletes all sessions on the closed session list and clears the list. |
93 void DeleteSessions(); | 94 void DeleteSessions(); |
94 | 95 |
95 const SessionMap& session_map() const { return session_map_; } | 96 const SessionMap& session_map() const { return session_map_; } |
96 | 97 |
97 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; } | 98 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; } |
98 | 99 |
99 protected: | 100 protected: |
100 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, | 101 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, |
101 const IPEndPoint& server_address, | 102 const IPEndPoint& server_address, |
102 const IPEndPoint& client_address); | 103 const IPEndPoint& client_address); |
103 | 104 |
104 virtual QuicConnection* CreateQuicConnection( | 105 virtual QuicConnection* CreateQuicConnection( |
105 QuicConnectionId connection_id, | 106 QuicConnectionId connection_id, |
106 const IPEndPoint& server_address, | 107 const IPEndPoint& server_address, |
107 const IPEndPoint& client_address); | 108 const IPEndPoint& client_address, |
| 109 QuicPerConnectionPacketWriter* writer); |
108 | 110 |
109 // Called by |framer_visitor_| when the public header has been parsed. | 111 // Called by |framer_visitor_| when the public header has been parsed. |
110 virtual bool OnUnauthenticatedPublicHeader( | 112 virtual bool OnUnauthenticatedPublicHeader( |
111 const QuicPacketPublicHeader& header); | 113 const QuicPacketPublicHeader& header); |
112 | 114 |
113 // Create and return the time wait list manager for this dispatcher, which | 115 // Create and return the time wait list manager for this dispatcher, which |
114 // will be owned by the dispatcher as time_wait_list_manager_ | 116 // will be owned by the dispatcher as time_wait_list_manager_ |
115 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); | 117 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); |
116 | 118 |
117 // Replaces the packet writer with |writer|. Takes ownership of |writer|. | 119 // Replaces the packet writer with |writer|. Takes ownership of |writer|. |
118 void set_writer(QuicPacketWriter* writer) { | 120 void set_writer(QuicServerPacketWriter* writer) { |
119 writer_.reset(writer); | 121 writer_.reset(writer); |
120 } | 122 } |
121 | 123 |
122 QuicTimeWaitListManager* time_wait_list_manager() { | 124 QuicTimeWaitListManager* time_wait_list_manager() { |
123 return time_wait_list_manager_.get(); | 125 return time_wait_list_manager_.get(); |
124 } | 126 } |
125 | 127 |
126 const QuicVersionVector& supported_versions() const { | 128 const QuicVersionVector& supported_versions() const { |
127 return supported_versions_; | 129 return supported_versions_; |
128 } | 130 } |
(...skipping 18 matching lines...) Expand all Loading... |
147 } | 149 } |
148 | 150 |
149 const QuicConfig& config() const { return config_; } | 151 const QuicConfig& config() const { return config_; } |
150 | 152 |
151 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } | 153 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
152 | 154 |
153 QuicFramer* framer() { return &framer_; } | 155 QuicFramer* framer() { return &framer_; } |
154 | 156 |
155 QuicConnectionHelperInterface* helper() { return helper_; } | 157 QuicConnectionHelperInterface* helper() { return helper_; } |
156 | 158 |
157 QuicPacketWriter* writer() { return writer_.get(); } | 159 QuicServerPacketWriter* writer() { return writer_.get(); } |
158 | 160 |
159 private: | 161 private: |
160 class QuicFramerVisitor; | 162 class QuicFramerVisitor; |
161 friend class net::test::QuicDispatcherPeer; | 163 friend class net::test::QuicDispatcherPeer; |
162 | 164 |
163 // Called by |framer_visitor_| when the private header has been parsed | 165 // Called by |framer_visitor_| when the private header has been parsed |
164 // of a data packet that is destined for the time wait manager. | 166 // of a data packet that is destined for the time wait manager. |
165 void OnUnauthenticatedHeader(const QuicPacketHeader& header); | 167 void OnUnauthenticatedHeader(const QuicPacketHeader& header); |
166 | 168 |
167 // Removes the session from the session map and write blocked list, and | 169 // Removes the session from the session map and write blocked list, and |
(...skipping 17 matching lines...) Expand all Loading... |
185 // The helper used for all connections. Owned by the server. | 187 // The helper used for all connections. Owned by the server. |
186 QuicConnectionHelperInterface* helper_; | 188 QuicConnectionHelperInterface* helper_; |
187 | 189 |
188 // An alarm which deletes closed sessions. | 190 // An alarm which deletes closed sessions. |
189 scoped_ptr<QuicAlarm> delete_sessions_alarm_; | 191 scoped_ptr<QuicAlarm> delete_sessions_alarm_; |
190 | 192 |
191 // The list of closed but not-yet-deleted sessions. | 193 // The list of closed but not-yet-deleted sessions. |
192 std::list<QuicSession*> closed_session_list_; | 194 std::list<QuicSession*> closed_session_list_; |
193 | 195 |
194 // The writer to write to the socket with. | 196 // The writer to write to the socket with. |
195 scoped_ptr<QuicPacketWriter> writer_; | 197 scoped_ptr<QuicServerPacketWriter> writer_; |
196 | 198 |
197 // This vector contains QUIC versions which we currently support. | 199 // This vector contains QUIC versions which we currently support. |
198 // This should be ordered such that the highest supported version is the first | 200 // This should be ordered such that the highest supported version is the first |
199 // element, with subsequent elements in descending order (versions can be | 201 // element, with subsequent elements in descending order (versions can be |
200 // skipped as necessary). | 202 // skipped as necessary). |
201 const QuicVersionVector supported_versions_; | 203 const QuicVersionVector supported_versions_; |
202 | 204 |
203 // Versions which do not support flow control (introduced in QUIC_VERSION_17). | 205 // Versions which do not support flow control (introduced in QUIC_VERSION_17). |
204 // This is used to construct new QuicConnections when flow control is disabled | 206 // This is used to construct new QuicConnections when flow control is disabled |
205 // via flag. | 207 // via flag. |
(...skipping 15 matching lines...) Expand all Loading... |
221 | 223 |
222 QuicFramer framer_; | 224 QuicFramer framer_; |
223 scoped_ptr<QuicFramerVisitor> framer_visitor_; | 225 scoped_ptr<QuicFramerVisitor> framer_visitor_; |
224 | 226 |
225 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); | 227 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); |
226 }; | 228 }; |
227 | 229 |
228 } // namespace net | 230 } // namespace net |
229 | 231 |
230 #endif // NET_QUIC_QUIC_DISPATCHER_H_ | 232 #endif // NET_QUIC_QUIC_DISPATCHER_H_ |
OLD | NEW |