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 socket becomes writable to allow queued writes to happen.
79 virtual void OnCanWrite();
81 // Returns true if there's anything in the blocked writer list.
82 virtual bool HasPendingWrites() const;
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(QuicConnectionId connection_id
,
90 QuicErrorCode error
) OVERRIDE
;
92 // Queues the blocked writer for later resumption.
93 virtual void OnWriteBlocked(QuicBlockedWriterInterface
* writer
) OVERRIDE
;
95 typedef base::hash_map
<QuicConnectionId
, QuicSession
*> SessionMap
;
97 // Deletes all sessions on the closed session list and clears the list.
98 void DeleteSessions();
100 const SessionMap
& session_map() const { return session_map_
; }
102 WriteBlockedList
* write_blocked_list() { return &write_blocked_list_
; }
105 // Instantiates a new low-level packet writer. Caller takes ownership of the
107 QuicPacketWriter
* CreateWriter(int fd
);
109 // Instantiates a new top-level writer wrapper. Takes ownership of |writer|.
110 // Caller takes ownership of the returned object.
111 virtual QuicPacketWriterWrapper
* CreateWriterWrapper(
112 QuicPacketWriter
* writer
);
114 virtual QuicSession
* CreateQuicSession(QuicConnectionId connection_id
,
115 const IPEndPoint
& server_address
,
116 const IPEndPoint
& client_address
);
118 QuicConnection
* CreateQuicConnection(QuicConnectionId connection_id
,
119 const IPEndPoint
& server_address
,
120 const IPEndPoint
& client_address
);
122 // Replaces the packet writer with |writer|. Takes ownership of |writer|.
123 void set_writer(QuicPacketWriter
* writer
);
125 QuicTimeWaitListManager
* time_wait_list_manager() {
126 return time_wait_list_manager_
.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 const IPEndPoint
& current_server_address() {
140 return current_server_address_
;
142 const QuicEncryptedPacket
& current_packet() {
143 return *current_packet_
;
146 const QuicConfig
& config() const { return config_
; }
148 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
151 class QuicFramerVisitor
;
152 friend class net::tools::test::QuicDispatcherPeer
;
154 // Called by |framer_visitor_| when the private header has been parsed
155 // of a data packet that is destined for the time wait manager.
156 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
158 // Removes the session from the session map and write blocked list, and
159 // adds the ConnectionId to the time-wait list.
160 void CleanUpSession(SessionMap::iterator it
);
162 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
164 const QuicConfig
& config_
;
166 const QuicCryptoServerConfig
& crypto_config_
;
168 // The list of connections waiting to write.
169 WriteBlockedList write_blocked_list_
;
171 SessionMap session_map_
;
173 // Entity that manages connection_ids in time wait state.
174 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
176 // An alarm which deletes closed sessions.
177 scoped_ptr
<DeleteSessionsAlarm
> delete_sessions_alarm_
;
179 // The list of closed but not-yet-deleted sessions.
180 std::list
<QuicSession
*> closed_session_list_
;
182 EpollServer
* epoll_server_
; // Owned by the server.
184 // The helper used for all connections.
185 scoped_ptr
<QuicEpollConnectionHelper
> helper_
;
187 // The writer to write to the socket with. We require a writer wrapper to
188 // allow replacing writer implementation without disturbing running
190 scoped_ptr
<QuicPacketWriterWrapper
> writer_
;
192 // This vector contains QUIC versions which we currently support.
193 // This should be ordered such that the highest supported version is the first
194 // element, with subsequent elements in descending order (versions can be
195 // skipped as necessary).
196 const QuicVersionVector supported_versions_
;
198 // Information about the packet currently being handled.
199 IPEndPoint current_client_address_
;
200 IPEndPoint current_server_address_
;
201 const QuicEncryptedPacket
* current_packet_
;
204 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
206 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
212 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_