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_packet_writer.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/tools/epoll_server/epoll_server.h"
22 #include "net/tools/quic/quic_server_session.h"
23 #include "net/tools/quic/quic_time_wait_list_manager.h"
25 #if defined(COMPILER_GCC)
26 namespace BASE_HASH_NAMESPACE {
28 struct hash<net::QuicBlockedWriterInterface*> {
29 std::size_t operator()(
30 const net::QuicBlockedWriterInterface* ptr) const {
31 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
41 class QuicConnectionHelper;
42 class QuicCryptoServerConfig;
47 class QuicPacketWriterWrapper;
50 class QuicDispatcherPeer;
53 class DeleteSessionsAlarm;
54 class QuicEpollConnectionHelper;
56 class QuicDispatcher : public QuicPacketWriter,
57 public QuicServerSessionVisitor {
59 // Ideally we'd have a linked_hash_set: the boolean is unused.
60 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
62 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
63 // must live until epoll_server Shutdown. |supported_versions| specifies the
64 // list of supported QUIC versions.
65 QuicDispatcher(const QuicConfig& config,
66 const QuicCryptoServerConfig& crypto_config,
67 const QuicVersionVector& supported_versions,
68 EpollServer* epoll_server);
70 virtual ~QuicDispatcher();
72 void Initialize(int fd);
75 virtual WriteResult WritePacket(
76 const char* buffer, size_t buf_len,
77 const IPAddressNumber& self_address,
78 const IPEndPoint& peer_address,
79 QuicBlockedWriterInterface* writer) OVERRIDE;
80 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE;
81 virtual bool IsWriteBlocked() const OVERRIDE;
82 virtual void SetWritable() OVERRIDE;
84 // Process the incoming packet by creating a new session, passing it to
85 // an existing session, or passing it to the TimeWaitListManager.
86 virtual void ProcessPacket(const IPEndPoint& server_address,
87 const IPEndPoint& client_address,
88 const QuicEncryptedPacket& packet);
90 // Called when the underyling connection becomes writable to allow
91 // queued writes to happen.
93 // Returns true if more writes are possible, false otherwise.
94 virtual bool OnCanWrite();
96 // Sends ConnectionClose frames to all connected clients.
99 // QuicServerSessionVisitor interface implementation:
100 // Ensure that the closed connection is cleaned up asynchronously.
101 virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE;
103 // Queues the blocked writer for later resumption.
104 virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
106 typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
108 virtual QuicSession* CreateQuicSession(
110 const IPEndPoint& server_address,
111 const IPEndPoint& client_address);
113 // Deletes all sessions on the closed session list and clears the list.
114 void DeleteSessions();
116 const SessionMap& session_map() const { return session_map_; }
118 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
121 // Instantiates a new low-level packet writer. Caller takes ownership of the
123 QuicPacketWriter* CreateWriter(int fd);
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);
130 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
131 void set_writer(QuicPacketWriter* writer);
133 QuicTimeWaitListManager* time_wait_list_manager() {
134 return time_wait_list_manager_.get();
137 QuicEpollConnectionHelper* helper() { return helper_.get(); }
138 EpollServer* epoll_server() { return epoll_server_; }
140 const QuicVersionVector& supported_versions() const {
141 return supported_versions_;
144 // Called by |framer_visitor_| when the public header has been parsed.
145 virtual bool OnUnauthenticatedPublicHeader(
146 const QuicPacketPublicHeader& header);
148 // Information about the packet currently being dispatched.
149 const IPEndPoint& current_client_address() {
150 return current_client_address_;
152 const IPEndPoint& current_server_address() {
153 return current_server_address_;
155 const QuicEncryptedPacket& current_packet() {
156 return *current_packet_;
159 const QuicConfig& config() const { return config_; }
161 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
164 class QuicFramerVisitor;
165 friend class net::tools::test::QuicDispatcherPeer;
167 // Called by |framer_visitor_| when the private header has been parsed
168 // of a data packet that is destined for the time wait manager.
169 void OnUnauthenticatedHeader(const QuicPacketHeader& header);
171 // Removes the session from the session map and write blocked list, and
172 // adds the GUID to the time-wait list.
173 void CleanUpSession(SessionMap::iterator it);
175 bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
177 const QuicConfig& config_;
179 const QuicCryptoServerConfig& crypto_config_;
181 // The list of connections waiting to write.
182 WriteBlockedList write_blocked_list_;
184 SessionMap session_map_;
186 // Entity that manages guids in time wait state.
187 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
189 // An alarm which deletes closed sessions.
190 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
192 // The list of closed but not-yet-deleted sessions.
193 std::list<QuicSession*> closed_session_list_;
195 EpollServer* epoll_server_; // Owned by the server.
197 // The helper used for all connections.
198 scoped_ptr<QuicEpollConnectionHelper> helper_;
200 // The writer to write to the socket with. We require a writer wrapper to
201 // allow replacing writer implementation without disturbing running
203 scoped_ptr<QuicPacketWriterWrapper> writer_;
205 // This vector contains QUIC versions which we currently support.
206 // This should be ordered such that the highest supported version is the first
207 // element, with subsequent elements in descending order (versions can be
208 // skipped as necessary).
209 const QuicVersionVector supported_versions_;
211 // Information about the packet currently being handled.
212 IPEndPoint current_client_address_;
213 IPEndPoint current_server_address_;
214 const QuicEncryptedPacket* current_packet_;
217 scoped_ptr<QuicFramerVisitor> framer_visitor_;
219 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
225 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_