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/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/linked_hash_map.h"
17 #include "net/quic/quic_blocked_writer_interface.h"
18 #include "net/quic/quic_packet_writer.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 QuicCryptoServerConfig;
46 class QuicDispatcherPeer;
49 class DeleteSessionsAlarm;
50 class QuicEpollConnectionHelper;
52 class QuicDispatcher : public QuicPacketWriter, public QuicSessionOwner {
54 // Ideally we'd have a linked_hash_set: the boolean is unused.
55 typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
57 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
58 // must live until epoll_server Shutdown. |supported_versions| specifies the
59 // list of supported QUIC versions.
60 QuicDispatcher(const QuicConfig& config,
61 const QuicCryptoServerConfig& crypto_config,
62 const QuicVersionVector& supported_versions,
64 EpollServer* epoll_server);
65 virtual ~QuicDispatcher();
68 virtual WriteResult WritePacket(
69 const char* buffer, size_t buf_len,
70 const IPAddressNumber& self_address,
71 const IPEndPoint& peer_address,
72 QuicBlockedWriterInterface* writer) OVERRIDE;
73 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE;
75 virtual void ProcessPacket(const IPEndPoint& server_address,
76 const IPEndPoint& client_address,
78 const QuicEncryptedPacket& packet);
80 // Called when the underyling connection becomes writable to allow
81 // queued writes to happen.
83 // Returns true if more writes are possible, false otherwise.
84 virtual bool OnCanWrite();
86 // Sends ConnectionClose frames to all connected clients.
89 // Ensure that the closed connection is cleaned up asynchronously.
90 virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE;
92 // Sets the fd and creates a default packet writer with that fd.
95 typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
97 virtual QuicSession* CreateQuicSession(
99 const IPEndPoint& client_address);
101 // Deletes all sessions on the closed session list and clears the list.
102 void DeleteSessions();
104 const SessionMap& session_map() const { return session_map_; }
106 // Uses the specified |writer| instead of QuicSocketUtils and takes ownership
108 void UseWriter(QuicPacketWriter* writer);
110 WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
113 const QuicConfig& config_;
114 const QuicCryptoServerConfig& crypto_config_;
116 QuicTimeWaitListManager* time_wait_list_manager() {
117 return time_wait_list_manager_.get();
120 QuicEpollConnectionHelper* helper() { return helper_.get(); }
121 EpollServer* epoll_server() { return epoll_server_; }
123 const QuicVersionVector& supported_versions() const {
124 return supported_versions_;
128 friend class net::tools::test::QuicDispatcherPeer;
130 // Removes the session from the session map and write blocked list, and
131 // adds the GUID to the time-wait list.
132 void CleanUpSession(SessionMap::iterator it);
134 // The list of connections waiting to write.
135 WriteBlockedList write_blocked_list_;
137 SessionMap session_map_;
139 // Entity that manages guids in time wait state.
140 scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
142 // An alarm which deletes closed sessions.
143 scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
145 // The list of closed but not-yet-deleted sessions.
146 std::list<QuicSession*> closed_session_list_;
148 EpollServer* epoll_server_; // Owned by the server.
150 // The connection for client-server communication
153 // True if the session is write blocked due to the socket returning EAGAIN.
154 // False if we have gotten a call to OnCanWrite after the last failed write.
157 // The helper used for all connections.
158 scoped_ptr<QuicEpollConnectionHelper> helper_;
160 // The writer to write to the socket with.
161 scoped_ptr<QuicPacketWriter> writer_;
163 // This vector contains QUIC versions which we currently support.
164 // This should be ordered such that the highest supported version is the first
165 // element, with subsequent elements in descending order (versions can be
166 // skipped as necessary).
167 const QuicVersionVector supported_versions_;
169 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
175 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_