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 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_QUIC_QUIC_DISPATCHER_H_ 9 #define NET_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_connection_helper.h" 20 #include "net/quic/quic_protocol.h" 21 #include "net/quic/quic_server_session.h" 22 #include "net/quic/quic_time_wait_list_manager.h" 23 24 #if defined(COMPILER_GCC) 25 namespace BASE_HASH_NAMESPACE { 26 template <> 27 struct hash<net::QuicBlockedWriterInterface*> { 28 std::size_t operator()(const net::QuicBlockedWriterInterface* ptr) const { 29 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); 30 } 31 }; 32 } 33 #endif 34 35 namespace net { 36 37 class QuicConfig; 38 class QuicCryptoServerConfig; 39 class QuicPacketWriterWrapper; 40 class QuicSession; 41 class UDPServerSocket; 42 43 namespace test { 44 class QuicDispatcherPeer; 45 } // namespace test 46 47 class DeleteSessionsAlarm; 48 49 class QuicDispatcher : public QuicServerSessionVisitor { 50 public: 51 // Ideally we'd have a linked_hash_set: the boolean is unused. 52 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList; 53 54 // Due to the way delete_sessions_closure_ is registered, the Dispatcher 55 // must live until epoll_server Shutdown. |supported_versions| specifies the 56 // list of supported QUIC versions. 57 QuicDispatcher(const QuicConfig& config, 58 const QuicCryptoServerConfig& crypto_config, 59 const QuicVersionVector& supported_versions, 60 QuicConnectionHelperInterface* helper); 61 62 virtual ~QuicDispatcher(); 63 64 // Takes ownership of the packet writer 65 virtual void Initialize(QuicPacketWriter* writer); 66 67 // Process the incoming packet by creating a new session, passing it to 68 // an existing session, or passing it to the TimeWaitListManager. 69 virtual void ProcessPacket(const IPEndPoint& server_address, 70 const IPEndPoint& client_address, 71 const QuicEncryptedPacket& packet); 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. 77 virtual bool HasPendingWrites() const; 78 79 // Sends ConnectionClose frames to all connected clients. 80 void Shutdown(); 81 82 // QuicServerSessionVisitor interface implementation: 83 // Ensure that the closed connection is cleaned up asynchronously. 84 virtual void OnConnectionClosed(QuicConnectionId connection_id, 85 QuicErrorCode error) OVERRIDE; 86 87 // Queues the blocked writer for later resumption. 88 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE; 89 90 typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap; 91 92 // Deletes all sessions on the closed session list and clears the list. 93 void DeleteSessions(); 94 95 const SessionMap& session_map() const { return session_map_; } 96 97 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; } 98 99 protected: 100 virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id, 101 const IPEndPoint& server_address, 102 const IPEndPoint& client_address); 103 104 virtual QuicConnection* CreateQuicConnection( 105 QuicConnectionId connection_id, 106 const IPEndPoint& server_address, 107 const IPEndPoint& client_address); 108 109 // Called by |framer_visitor_| when the public header has been parsed. 110 virtual bool OnUnauthenticatedPublicHeader( 111 const QuicPacketPublicHeader& header); 112 113 // Create and return the time wait list manager for this dispatcher, which 114 // will be owned by the dispatcher as time_wait_list_manager_ 115 virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager(); 116 117 // Replaces the packet writer with |writer|. Takes ownership of |writer|. 118 void set_writer(QuicPacketWriter* writer) { 119 writer_.reset(writer); 120 } 121 122 QuicTimeWaitListManager* time_wait_list_manager() { 123 return time_wait_list_manager_.get(); 124 } 125 126 const QuicVersionVector& supported_versions() const { 127 return supported_versions_; 128 } 129 130 const QuicVersionVector& supported_versions_no_flow_control() const { 131 return supported_versions_no_flow_control_; 132 } 133 134 const QuicVersionVector& supported_versions_no_connection_flow_control() 135 const { 136 return supported_versions_no_connection_flow_control_; 137 } 138 139 const IPEndPoint& current_server_address() { 140 return current_server_address_; 141 } 142 const IPEndPoint& current_client_address() { 143 return current_client_address_; 144 } 145 const QuicEncryptedPacket& current_packet() { 146 return *current_packet_; 147 } 148 149 const QuicConfig& config() const { return config_; } 150 151 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } 152 153 QuicFramer* framer() { return &framer_; } 154 155 QuicConnectionHelperInterface* helper() { return helper_; } 156 157 QuicPacketWriter* writer() { return writer_.get(); } 158 159 private: 160 class QuicFramerVisitor; 161 friend class net::test::QuicDispatcherPeer; 162 163 // Called by |framer_visitor_| when the private header has been parsed 164 // of a data packet that is destined for the time wait manager. 165 void OnUnauthenticatedHeader(const QuicPacketHeader& header); 166 167 // Removes the session from the session map and write blocked list, and 168 // adds the ConnectionId to the time-wait list. 169 void CleanUpSession(SessionMap::iterator it); 170 171 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header); 172 173 const QuicConfig& config_; 174 175 const QuicCryptoServerConfig& crypto_config_; 176 177 // The list of connections waiting to write. 178 WriteBlockedList write_blocked_list_; 179 180 SessionMap session_map_; 181 182 // Entity that manages connection_ids in time wait state. 183 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_; 184 185 // The helper used for all connections. Owned by the server. 186 QuicConnectionHelperInterface* helper_; 187 188 // An alarm which deletes closed sessions. 189 scoped_ptr<QuicAlarm> delete_sessions_alarm_; 190 191 // The list of closed but not-yet-deleted sessions. 192 std::list<QuicSession*> closed_session_list_; 193 194 // The writer to write to the socket with. 195 scoped_ptr<QuicPacketWriter> writer_; 196 197 // This vector contains QUIC versions which we currently support. 198 // This should be ordered such that the highest supported version is the first 199 // element, with subsequent elements in descending order (versions can be 200 // skipped as necessary). 201 const QuicVersionVector supported_versions_; 202 203 // 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 205 // via flag. 206 // TODO(rjshade): Remove this when 207 // FLAGS_enable_quic_stream_flow_control_2 is removed. 208 QuicVersionVector supported_versions_no_flow_control_; 209 // Versions which do not support *connection* flow control (introduced in 210 // QUIC_VERSION_19). 211 // This is used to construct new QuicConnections when connection flow control 212 // is disabled via flag. 213 // TODO(rjshade): Remove this when 214 // FLAGS_enable_quic_connection_flow_control_2 is removed. 215 QuicVersionVector supported_versions_no_connection_flow_control_; 216 217 // Information about the packet currently being handled. 218 IPEndPoint current_client_address_; 219 IPEndPoint current_server_address_; 220 const QuicEncryptedPacket* current_packet_; 221 222 QuicFramer framer_; 223 scoped_ptr<QuicFramerVisitor> framer_visitor_; 224 225 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher); 226 }; 227 228 } // namespace net 229 230 #endif // NET_QUIC_QUIC_DISPATCHER_H_ 231