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/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
16 #include "net/base/linked_hash_map.h" | 17 #include "net/base/linked_hash_map.h" |
17 #include "net/quic/quic_blocked_writer_interface.h" | 18 #include "net/quic/quic_blocked_writer_interface.h" |
18 #include "net/quic/quic_packet_writer.h" | 19 #include "net/quic/quic_packet_writer.h" |
19 #include "net/quic/quic_protocol.h" | 20 #include "net/quic/quic_protocol.h" |
20 #include "net/tools/epoll_server/epoll_server.h" | 21 #include "net/tools/epoll_server/epoll_server.h" |
21 #include "net/tools/quic/quic_server_session.h" | 22 #include "net/tools/quic/quic_server_session.h" |
22 #include "net/tools/quic/quic_time_wait_list_manager.h" | 23 #include "net/tools/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()( | 29 std::size_t operator()( |
29 const net::QuicBlockedWriterInterface* ptr) const { | 30 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)); |
31 } | 32 } |
32 }; | 33 }; |
33 } | 34 } |
34 #endif | 35 #endif |
35 | 36 |
36 namespace net { | 37 namespace net { |
37 | 38 |
38 class EpollServer; | 39 class EpollServer; |
39 class QuicConfig; | 40 class QuicConfig; |
| 41 class QuicConnectionHelper; |
40 class QuicCryptoServerConfig; | 42 class QuicCryptoServerConfig; |
41 class QuicSession; | 43 class QuicSession; |
42 | 44 |
43 namespace tools { | 45 namespace tools { |
44 | 46 |
| 47 class QuicPacketWriterWrapper; |
| 48 |
45 namespace test { | 49 namespace test { |
46 class QuicDispatcherPeer; | 50 class QuicDispatcherPeer; |
47 } // namespace test | 51 } // namespace test |
48 | 52 |
49 class DeleteSessionsAlarm; | 53 class DeleteSessionsAlarm; |
50 class QuicEpollConnectionHelper; | 54 class QuicEpollConnectionHelper; |
51 | 55 |
52 class QuicDispatcher : public QuicPacketWriter, public QuicSessionOwner { | 56 class QuicDispatcher : public QuicPacketWriter, |
| 57 public QuicServerSessionVisitor { |
53 public: | 58 public: |
54 // Ideally we'd have a linked_hash_set: the boolean is unused. | 59 // Ideally we'd have a linked_hash_set: the boolean is unused. |
55 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; | 60 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; |
56 | 61 |
57 // Due to the way delete_sessions_closure_ is registered, the Dispatcher | 62 // Due to the way delete_sessions_closure_ is registered, the Dispatcher |
58 // must live until epoll_server Shutdown. |supported_versions| specifies the | 63 // must live until epoll_server Shutdown. |supported_versions| specifies the |
59 // list of supported QUIC versions. | 64 // list of supported QUIC versions. |
60 QuicDispatcher(const QuicConfig& config, | 65 QuicDispatcher(const QuicConfig& config, |
61 const QuicCryptoServerConfig& crypto_config, | 66 const QuicCryptoServerConfig& crypto_config, |
62 const QuicVersionVector& supported_versions, | 67 const QuicVersionVector& supported_versions, |
63 int fd, | |
64 EpollServer* epoll_server); | 68 EpollServer* epoll_server); |
| 69 |
65 virtual ~QuicDispatcher(); | 70 virtual ~QuicDispatcher(); |
66 | 71 |
| 72 void Initialize(int fd); |
| 73 |
67 // QuicPacketWriter | 74 // QuicPacketWriter |
68 virtual WriteResult WritePacket( | 75 virtual WriteResult WritePacket( |
69 const char* buffer, size_t buf_len, | 76 const char* buffer, size_t buf_len, |
70 const IPAddressNumber& self_address, | 77 const IPAddressNumber& self_address, |
71 const IPEndPoint& peer_address, | 78 const IPEndPoint& peer_address, |
72 QuicBlockedWriterInterface* writer) OVERRIDE; | 79 QuicBlockedWriterInterface* writer) OVERRIDE; |
73 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE; | 80 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE; |
74 virtual bool IsWriteBlocked() const OVERRIDE; | 81 virtual bool IsWriteBlocked() const OVERRIDE; |
75 virtual void SetWritable() OVERRIDE; | 82 virtual void SetWritable() OVERRIDE; |
76 | 83 |
77 // Process the incoming packet by creating a new session, passing it to | 84 // Process the incoming packet by creating a new session, passing it to |
78 // an existing session, or passing it to the TimeWaitListManager. | 85 // an existing session, or passing it to the TimeWaitListManager. |
79 virtual void ProcessPacket(const IPEndPoint& server_address, | 86 virtual void ProcessPacket(const IPEndPoint& server_address, |
80 const IPEndPoint& client_address, | 87 const IPEndPoint& client_address, |
81 const QuicEncryptedPacket& packet); | 88 const QuicEncryptedPacket& packet); |
82 | 89 |
83 // Called when the underyling connection becomes writable to allow | 90 // Called when the underyling connection becomes writable to allow |
84 // queued writes to happen. | 91 // queued writes to happen. |
85 // | 92 // |
86 // Returns true if more writes are possible, false otherwise. | 93 // Returns true if more writes are possible, false otherwise. |
87 virtual bool OnCanWrite(); | 94 virtual bool OnCanWrite(); |
88 | 95 |
89 // Sends ConnectionClose frames to all connected clients. | 96 // Sends ConnectionClose frames to all connected clients. |
90 void Shutdown(); | 97 void Shutdown(); |
91 | 98 |
| 99 // QuicServerSessionVisitor interface implementation: |
92 // Ensure that the closed connection is cleaned up asynchronously. | 100 // Ensure that the closed connection is cleaned up asynchronously. |
93 virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE; | 101 virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE; |
94 | 102 |
95 // Sets the fd and creates a default packet writer with that fd. | 103 // Queues the blocked writer for later resumption. |
96 void set_fd(int fd); | 104 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE; |
97 | 105 |
98 typedef base::hash_map<QuicGuid, QuicSession*> SessionMap; | 106 typedef base::hash_map<QuicGuid, QuicSession*> SessionMap; |
99 | 107 |
100 virtual QuicSession* CreateQuicSession( | 108 virtual QuicSession* CreateQuicSession( |
101 QuicGuid guid, | 109 QuicGuid guid, |
102 const IPEndPoint& server_address, | 110 const IPEndPoint& server_address, |
103 const IPEndPoint& client_address); | 111 const IPEndPoint& client_address); |
104 | 112 |
105 // Deletes all sessions on the closed session list and clears the list. | 113 // Deletes all sessions on the closed session list and clears the list. |
106 void DeleteSessions(); | 114 void DeleteSessions(); |
107 | 115 |
108 const SessionMap& session_map() const { return session_map_; } | 116 const SessionMap& session_map() const { return session_map_; } |
109 | 117 |
110 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; } | 118 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; } |
111 | 119 |
112 protected: | 120 protected: |
113 const QuicConfig& config_; | 121 // Instantiates a new low-level packet writer. Caller takes ownership of the |
114 const QuicCryptoServerConfig& crypto_config_; | 122 // returned object. |
| 123 QuicPacketWriter* CreateWriter(int fd); |
| 124 |
| 125 // Instantiates a new top-level writer wrapper. Takes ownership of |writer|. |
| 126 // Caller takes ownership of the returned object. |
| 127 virtual QuicPacketWriterWrapper* CreateWriterWrapper( |
| 128 QuicPacketWriter* writer); |
| 129 |
| 130 // Replaces the packet writer with |writer|. Takes ownership of |writer|. |
| 131 void set_writer(QuicPacketWriter* writer); |
115 | 132 |
116 QuicTimeWaitListManager* time_wait_list_manager() { | 133 QuicTimeWaitListManager* time_wait_list_manager() { |
117 return time_wait_list_manager_.get(); | 134 return time_wait_list_manager_.get(); |
118 } | 135 } |
119 | 136 |
120 QuicEpollConnectionHelper* helper() { return helper_.get(); } | 137 QuicEpollConnectionHelper* helper() { return helper_.get(); } |
121 EpollServer* epoll_server() { return epoll_server_; } | 138 EpollServer* epoll_server() { return epoll_server_; } |
122 | 139 |
123 const QuicVersionVector& supported_versions() const { | 140 const QuicVersionVector& supported_versions() const { |
124 return supported_versions_; | 141 return supported_versions_; |
125 } | 142 } |
126 | 143 |
127 protected: | |
128 // Called by |framer_visitor_| when the public header has been parsed. | 144 // Called by |framer_visitor_| when the public header has been parsed. |
129 virtual bool OnUnauthenticatedPublicHeader( | 145 virtual bool OnUnauthenticatedPublicHeader( |
130 const QuicPacketPublicHeader& header); | 146 const QuicPacketPublicHeader& header); |
131 | 147 |
132 // Information about the packet currently being dispatched. | 148 // Information about the packet currently being dispatched. |
133 const IPEndPoint& current_client_address() { | 149 const IPEndPoint& current_client_address() { |
134 return current_client_address_; | 150 return current_client_address_; |
135 } | 151 } |
136 const IPEndPoint& current_server_address() { | 152 const IPEndPoint& current_server_address() { |
137 return current_server_address_; | 153 return current_server_address_; |
138 } | 154 } |
139 const QuicEncryptedPacket& current_packet() { | 155 const QuicEncryptedPacket& current_packet() { |
140 return *current_packet_; | 156 return *current_packet_; |
141 } | 157 } |
142 | 158 |
| 159 const QuicConfig& config() const { return config_; } |
| 160 |
| 161 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
| 162 |
143 private: | 163 private: |
144 class QuicFramerVisitor; | 164 class QuicFramerVisitor; |
145 friend class net::tools::test::QuicDispatcherPeer; | 165 friend class net::tools::test::QuicDispatcherPeer; |
146 | 166 |
147 // Called by |framer_visitor_| when the private header has been parsed | 167 // Called by |framer_visitor_| when the private header has been parsed |
148 // of a data packet that is destined for the time wait manager. | 168 // of a data packet that is destined for the time wait manager. |
149 void OnUnauthenticatedHeader(const QuicPacketHeader& header); | 169 void OnUnauthenticatedHeader(const QuicPacketHeader& header); |
150 | 170 |
151 // Removes the session from the session map and write blocked list, and | 171 // Removes the session from the session map and write blocked list, and |
152 // adds the GUID to the time-wait list. | 172 // adds the GUID to the time-wait list. |
153 void CleanUpSession(SessionMap::iterator it); | 173 void CleanUpSession(SessionMap::iterator it); |
154 | 174 |
155 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); | 175 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); |
156 | 176 |
| 177 const QuicConfig& config_; |
| 178 |
| 179 const QuicCryptoServerConfig& crypto_config_; |
| 180 |
157 // The list of connections waiting to write. | 181 // The list of connections waiting to write. |
158 WriteBlockedList write_blocked_list_; | 182 WriteBlockedList write_blocked_list_; |
159 | 183 |
160 SessionMap session_map_; | 184 SessionMap session_map_; |
161 | 185 |
162 // Entity that manages guids in time wait state. | 186 // Entity that manages guids in time wait state. |
163 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_; | 187 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_; |
164 | 188 |
165 // An alarm which deletes closed sessions. | 189 // An alarm which deletes closed sessions. |
166 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_; | 190 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_; |
167 | 191 |
168 // The list of closed but not-yet-deleted sessions. | 192 // The list of closed but not-yet-deleted sessions. |
169 std::list<QuicSession*> closed_session_list_; | 193 std::list<QuicSession*> closed_session_list_; |
170 | 194 |
171 EpollServer* epoll_server_; // Owned by the server. | 195 EpollServer* epoll_server_; // Owned by the server. |
172 | 196 |
173 // The connection for client-server communication | |
174 int fd_; | |
175 | |
176 // The helper used for all connections. | 197 // The helper used for all connections. |
177 scoped_ptr<QuicEpollConnectionHelper> helper_; | 198 scoped_ptr<QuicEpollConnectionHelper> helper_; |
178 | 199 |
179 // The writer to write to the socket with. | 200 // The writer to write to the socket with. We require a writer wrapper to |
180 scoped_ptr<QuicPacketWriter> writer_; | 201 // allow replacing writer implementation without disturbing running |
| 202 // connections. |
| 203 scoped_ptr<QuicPacketWriterWrapper> writer_; |
181 | 204 |
182 // This vector contains QUIC versions which we currently support. | 205 // This vector contains QUIC versions which we currently support. |
183 // This should be ordered such that the highest supported version is the first | 206 // This should be ordered such that the highest supported version is the first |
184 // element, with subsequent elements in descending order (versions can be | 207 // element, with subsequent elements in descending order (versions can be |
185 // skipped as necessary). | 208 // skipped as necessary). |
186 const QuicVersionVector supported_versions_; | 209 const QuicVersionVector supported_versions_; |
187 | 210 |
188 // Information about the packet currently being handled. | 211 // Information about the packet currently being handled. |
189 IPEndPoint current_client_address_; | 212 IPEndPoint current_client_address_; |
190 IPEndPoint current_server_address_; | 213 IPEndPoint current_server_address_; |
191 const QuicEncryptedPacket* current_packet_; | 214 const QuicEncryptedPacket* current_packet_; |
192 | 215 |
193 QuicFramer framer_; | 216 QuicFramer framer_; |
194 scoped_ptr<QuicFramerVisitor> framer_visitor_; | 217 scoped_ptr<QuicFramerVisitor> framer_visitor_; |
195 | 218 |
196 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); | 219 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); |
197 }; | 220 }; |
198 | 221 |
199 } // namespace tools | 222 } // namespace tools |
200 } // namespace net | 223 } // namespace net |
201 | 224 |
202 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ | 225 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_ |
OLD | NEW |