Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_dispatcher.h
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.
4 //
5 // A server side dispatcher which dispatches a given client's data to their
6 // stream.
7
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_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_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"
23
24 #if defined(COMPILER_GCC)
25 namespace BASE_HASH_NAMESPACE {
26 template<>
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));
31   }
32 };
33 }
34 #endif
35
36 namespace net {
37
38 class EpollServer;
39 class QuicConfig;
40 class QuicCryptoServerConfig;
41 class QuicSession;
42
43 namespace tools {
44
45 class QuicPacketWriterWrapper;
46
47 namespace test {
48 class QuicDispatcherPeer;
49 }  // namespace test
50
51 class DeleteSessionsAlarm;
52 class QuicEpollConnectionHelper;
53
54 class QuicDispatcher : public QuicServerSessionVisitor {
55  public:
56   // Ideally we'd have a linked_hash_set: the  boolean is unused.
57   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
58
59   // Due to the way delete_sessions_closure_ is registered, the Dispatcher
60   // must live until epoll_server Shutdown. |supported_versions| specifies the
61   // list of supported QUIC versions.
62   QuicDispatcher(const QuicConfig& config,
63                  const QuicCryptoServerConfig& crypto_config,
64                  const QuicVersionVector& supported_versions,
65                  EpollServer* epoll_server,
66                  uint32 initial_flow_control_window_bytes);
67
68   virtual ~QuicDispatcher();
69
70   virtual void Initialize(int fd);
71
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);
77
78   // Called when the socket becomes writable to allow queued writes to happen.
79   virtual void OnCanWrite();
80
81   // Returns true if there's anything in the blocked writer list.
82   virtual bool HasPendingWrites() const;
83
84   // Sends ConnectionClose frames to all connected clients.
85   void Shutdown();
86
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;
91
92   // Queues the blocked writer for later resumption.
93   virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
94
95   typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
96
97   // Deletes all sessions on the closed session list and clears the list.
98   void DeleteSessions();
99
100   const SessionMap& session_map() const { return session_map_; }
101
102   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
103
104  protected:
105   // Instantiates a new low-level packet writer. Caller takes ownership of the
106   // returned object.
107   virtual QuicPacketWriter* CreateWriter(int fd);
108
109   virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
110                                          const IPEndPoint& server_address,
111                                          const IPEndPoint& client_address);
112
113   virtual QuicConnection* CreateQuicConnection(
114       QuicConnectionId connection_id,
115       const IPEndPoint& server_address,
116       const IPEndPoint& client_address,
117       uint32 initial_flow_control_window);
118
119   // Called by |framer_visitor_| when the public header has been parsed.
120   virtual bool OnUnauthenticatedPublicHeader(
121       const QuicPacketPublicHeader& header);
122
123   // Create and return the time wait list manager for this dispatcher, which
124   // will be owned by the dispatcher as time_wait_list_manager_
125   virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
126
127   // Replaces the packet writer with |writer|. Takes ownership of |writer|.
128   void set_writer(QuicPacketWriter* writer) {
129     writer_.reset(writer);
130   }
131
132   QuicTimeWaitListManager* time_wait_list_manager() {
133     return time_wait_list_manager_.get();
134   }
135
136   EpollServer* epoll_server() { return epoll_server_; }
137
138   const QuicVersionVector& supported_versions() const {
139     return supported_versions_;
140   }
141
142   const QuicVersionVector& supported_versions_no_flow_control() const {
143     return supported_versions_no_flow_control_;
144   }
145
146   const QuicVersionVector& supported_versions_no_connection_flow_control()
147       const {
148     return supported_versions_no_connection_flow_control_;
149   }
150
151   const IPEndPoint& current_server_address() {
152     return current_server_address_;
153   }
154   const IPEndPoint& current_client_address() {
155     return current_client_address_;
156   }
157   const QuicEncryptedPacket& current_packet() {
158     return *current_packet_;
159   }
160
161   const QuicConfig& config() const { return config_; }
162
163   const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
164
165   QuicFramer* framer() { return &framer_; }
166
167   QuicEpollConnectionHelper* helper() { return helper_.get(); }
168
169   QuicPacketWriter* writer() { return writer_.get(); }
170
171   const uint32 initial_flow_control_window_bytes() const {
172     return initial_flow_control_window_bytes_;
173   }
174
175  private:
176   class QuicFramerVisitor;
177   friend class net::tools::test::QuicDispatcherPeer;
178
179   // Called by |framer_visitor_| when the private header has been parsed
180   // of a data packet that is destined for the time wait manager.
181   void OnUnauthenticatedHeader(const QuicPacketHeader& header);
182
183   // Removes the session from the session map and write blocked list, and
184   // adds the ConnectionId to the time-wait list.
185   void CleanUpSession(SessionMap::iterator it);
186
187   bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
188
189   const QuicConfig& config_;
190
191   const QuicCryptoServerConfig& crypto_config_;
192
193   // The list of connections waiting to write.
194   WriteBlockedList write_blocked_list_;
195
196   SessionMap session_map_;
197
198   // Entity that manages connection_ids in time wait state.
199   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
200
201   // An alarm which deletes closed sessions.
202   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
203
204   // The list of closed but not-yet-deleted sessions.
205   std::list<QuicSession*> closed_session_list_;
206
207   EpollServer* epoll_server_;  // Owned by the server.
208
209   // The helper used for all connections.
210   scoped_ptr<QuicEpollConnectionHelper> helper_;
211
212   // The writer to write to the socket with.
213   scoped_ptr<QuicPacketWriter> writer_;
214
215   // This vector contains QUIC versions which we currently support.
216   // This should be ordered such that the highest supported version is the first
217   // element, with subsequent elements in descending order (versions can be
218   // skipped as necessary).
219   const QuicVersionVector supported_versions_;
220
221   // Versions which do not support flow control (introduced in QUIC_VERSION_17).
222   // This is used to construct new QuicConnections when flow control is disabled
223   // via flag.
224   // TODO(rjshade): Remove this when
225   // FLAGS_enable_quic_stream_flow_control_2 is removed.
226   QuicVersionVector supported_versions_no_flow_control_;
227   // Versions which do not support *connection* flow control (introduced in
228   // QUIC_VERSION_19).
229   // This is used to construct new QuicConnections when connection flow control
230   // is disabled via flag.
231   // TODO(rjshade): Remove this when
232   // FLAGS_enable_quic_connection_flow_control is removed.
233   QuicVersionVector supported_versions_no_connection_flow_control_;
234
235   // Information about the packet currently being handled.
236   IPEndPoint current_client_address_;
237   IPEndPoint current_server_address_;
238   const QuicEncryptedPacket* current_packet_;
239
240   QuicFramer framer_;
241   scoped_ptr<QuicFramerVisitor> framer_visitor_;
242
243   // Initial flow control window size to advertize to peer on newly created
244   // connections.
245   const uint32 initial_flow_control_window_bytes_;
246
247   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
248 };
249
250 }  // namespace tools
251 }  // namespace net
252
253 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_