Upstream version 5.34.104.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 QuicConnectionHelper;
41 class QuicCryptoServerConfig;
42 class QuicSession;
43
44 namespace tools {
45
46 class QuicPacketWriterWrapper;
47
48 namespace test {
49 class QuicDispatcherPeer;
50 }  // namespace test
51
52 class DeleteSessionsAlarm;
53 class QuicEpollConnectionHelper;
54
55 class QuicDispatcher : public QuicServerSessionVisitor {
56  public:
57   // Ideally we'd have a linked_hash_set: the  boolean is unused.
58   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
59
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);
67
68   virtual ~QuicDispatcher();
69
70   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 underyling connection becomes writable to allow
79   // queued writes to happen.
80   //
81   // Returns true if more writes are possible, false otherwise.
82   virtual bool OnCanWrite();
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(QuicGuid guid, QuicErrorCode error) OVERRIDE;
90
91   // Queues the blocked writer for later resumption.
92   virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
93
94   typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
95
96   // Deletes all sessions on the closed session list and clears the list.
97   void DeleteSessions();
98
99   const SessionMap& session_map() const { return session_map_; }
100
101   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
102
103  protected:
104   // Instantiates a new low-level packet writer. Caller takes ownership of the
105   // returned object.
106   QuicPacketWriter* CreateWriter(int fd);
107
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);
112
113   virtual QuicSession* CreateQuicSession(QuicGuid guid,
114                                          const IPEndPoint& server_address,
115                                          const IPEndPoint& client_address);
116
117   QuicConnection* CreateQuicConnection(QuicGuid guid,
118                                        const IPEndPoint& server_address,
119                                        const IPEndPoint& client_address);
120
121   // Replaces the packet writer with |writer|. Takes ownership of |writer|.
122   void set_writer(QuicPacketWriter* writer);
123
124   QuicTimeWaitListManager* time_wait_list_manager() {
125     return time_wait_list_manager_.get();
126   }
127
128   QuicEpollConnectionHelper* helper() { return helper_.get(); }
129   EpollServer* epoll_server() { return epoll_server_; }
130
131   const QuicVersionVector& supported_versions() const {
132     return supported_versions_;
133   }
134
135   // Called by |framer_visitor_| when the public header has been parsed.
136   virtual bool OnUnauthenticatedPublicHeader(
137       const QuicPacketPublicHeader& header);
138
139   // Information about the packet currently being dispatched.
140   const IPEndPoint& current_client_address() {
141     return current_client_address_;
142   }
143   const IPEndPoint& current_server_address() {
144     return current_server_address_;
145   }
146   const QuicEncryptedPacket& current_packet() {
147     return *current_packet_;
148   }
149
150   const QuicConfig& config() const { return config_; }
151
152   const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
153
154  private:
155   class QuicFramerVisitor;
156   friend class net::tools::test::QuicDispatcherPeer;
157
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);
161
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);
165
166   bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
167
168   const QuicConfig& config_;
169
170   const QuicCryptoServerConfig& crypto_config_;
171
172   // The list of connections waiting to write.
173   WriteBlockedList write_blocked_list_;
174
175   SessionMap session_map_;
176
177   // Entity that manages guids in time wait state.
178   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
179
180   // An alarm which deletes closed sessions.
181   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
182
183   // The list of closed but not-yet-deleted sessions.
184   std::list<QuicSession*> closed_session_list_;
185
186   EpollServer* epoll_server_;  // Owned by the server.
187
188   // The helper used for all connections.
189   scoped_ptr<QuicEpollConnectionHelper> helper_;
190
191   // The writer to write to the socket with. We require a writer wrapper to
192   // allow replacing writer implementation without disturbing running
193   // connections.
194   scoped_ptr<QuicPacketWriterWrapper> writer_;
195
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_;
201
202   // Information about the packet currently being handled.
203   IPEndPoint current_client_address_;
204   IPEndPoint current_server_address_;
205   const QuicEncryptedPacket* current_packet_;
206
207   QuicFramer framer_;
208   scoped_ptr<QuicFramerVisitor> framer_visitor_;
209
210   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
211 };
212
213 }  // namespace tools
214 }  // namespace net
215
216 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_