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
3 // found in the LICENSE file.
5 // A server side dispatcher which dispatches a given client's data to their
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
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_protocol.h"
20 #include "net/tools/epoll_server/epoll_server.h"
21 #include "net/tools/quic/quic_server_session.h"
22 #include "net/tools/quic/quic_time_wait_list_manager.h"
24 #if defined(COMPILER_GCC)
25 namespace BASE_HASH_NAMESPACE {
27 struct hash<net::QuicBlockedWriterInterface*> {
28 std::size_t operator()(
29 const net::QuicBlockedWriterInterface* ptr) const {
30 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
40 class QuicConnectionHelper;
41 class QuicCryptoServerConfig;
46 class QuicPacketWriterWrapper;
49 class QuicDispatcherPeer;
52 class DeleteSessionsAlarm;
53 class QuicEpollConnectionHelper;
55 class QuicDispatcher : public QuicServerSessionVisitor {
57 // Ideally we'd have a linked_hash_set: the boolean is unused.
58 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
60 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
61 // must live until epoll_server Shutdown. |supported_versions| specifies the
62 // list of supported QUIC versions.
63 QuicDispatcher(const QuicConfig& config,
64 const QuicCryptoServerConfig& crypto_config,
65 const QuicVersionVector& supported_versions,
66 EpollServer* epoll_server);
68 virtual ~QuicDispatcher();
70 void Initialize(int fd);
72 // Process the incoming packet by creating a new session, passing it to
73 // an existing session, or passing it to the TimeWaitListManager.
74 virtual void ProcessPacket(const IPEndPoint& server_address,
75 const IPEndPoint& client_address,
76 const QuicEncryptedPacket& packet);
78 // Called when the underyling connection becomes writable to allow
79 // queued writes to happen.
81 // Returns true if more writes are possible, false otherwise.
82 virtual bool OnCanWrite();
84 // Sends ConnectionClose frames to all connected clients.
87 // QuicServerSessionVisitor interface implementation:
88 // Ensure that the closed connection is cleaned up asynchronously.
89 virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE;
91 // Queues the blocked writer for later resumption.
92 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
94 typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
96 // Deletes all sessions on the closed session list and clears the list.
97 void DeleteSessions();
99 const SessionMap& session_map() const { return session_map_; }
101 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
104 // Instantiates a new low-level packet writer. Caller takes ownership of the
106 QuicPacketWriter* CreateWriter(int fd);
108 // Instantiates a new top-level writer wrapper. Takes ownership of |writer|.
109 // Caller takes ownership of the returned object.
110 virtual QuicPacketWriterWrapper* CreateWriterWrapper(
111 QuicPacketWriter* writer);
113 virtual QuicSession* CreateQuicSession(QuicGuid guid,
114 const IPEndPoint& server_address,
115 const IPEndPoint& client_address);
117 QuicConnection* CreateQuicConnection(QuicGuid guid,
118 const IPEndPoint& server_address,
119 const IPEndPoint& client_address);
121 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
122 void set_writer(QuicPacketWriter* writer);
124 QuicTimeWaitListManager* time_wait_list_manager() {
125 return time_wait_list_manager_.get();
128 QuicEpollConnectionHelper* helper() { return helper_.get(); }
129 EpollServer* epoll_server() { return epoll_server_; }
131 const QuicVersionVector& supported_versions() const {
132 return supported_versions_;
135 // Called by |framer_visitor_| when the public header has been parsed.
136 virtual bool OnUnauthenticatedPublicHeader(
137 const QuicPacketPublicHeader& header);
139 // Information about the packet currently being dispatched.
140 const IPEndPoint& current_client_address() {
141 return current_client_address_;
143 const IPEndPoint& current_server_address() {
144 return current_server_address_;
146 const QuicEncryptedPacket& current_packet() {
147 return *current_packet_;
150 const QuicConfig& config() const { return config_; }
152 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
155 class QuicFramerVisitor;
156 friend class net::tools::test::QuicDispatcherPeer;
158 // Called by |framer_visitor_| when the private header has been parsed
159 // of a data packet that is destined for the time wait manager.
160 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
162 // Removes the session from the session map and write blocked list, and
163 // adds the GUID to the time-wait list.
164 void CleanUpSession(SessionMap::iterator it);
166 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
168 const QuicConfig& config_;
170 const QuicCryptoServerConfig& crypto_config_;
172 // The list of connections waiting to write.
173 WriteBlockedList write_blocked_list_;
175 SessionMap session_map_;
177 // Entity that manages guids in time wait state.
178 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
180 // An alarm which deletes closed sessions.
181 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
183 // The list of closed but not-yet-deleted sessions.
184 std::list<QuicSession*> closed_session_list_;
186 EpollServer* epoll_server_; // Owned by the server.
188 // The helper used for all connections.
189 scoped_ptr<QuicEpollConnectionHelper> helper_;
191 // The writer to write to the socket with. We require a writer wrapper to
192 // allow replacing writer implementation without disturbing running
194 scoped_ptr<QuicPacketWriterWrapper> writer_;
196 // This vector contains QUIC versions which we currently support.
197 // This should be ordered such that the highest supported version is the first
198 // element, with subsequent elements in descending order (versions can be
199 // skipped as necessary).
200 const QuicVersionVector supported_versions_;
202 // Information about the packet currently being handled.
203 IPEndPoint current_client_address_;
204 IPEndPoint current_server_address_;
205 const QuicEncryptedPacket* current_packet_;
208 scoped_ptr<QuicFramerVisitor> framer_visitor_;
210 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
216 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_