Upstream version 11.40.277.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/quic/quic_server_session.h"
21 #include "net/tools/quic/quic_time_wait_list_manager.h"
22
23 namespace net {
24
25 class EpollServer;
26 class QuicConfig;
27 class QuicCryptoServerConfig;
28 class QuicSession;
29
30 namespace tools {
31
32 class DeleteSessionsAlarm;
33 class QuicEpollConnectionHelper;
34 class QuicPacketWriterWrapper;
35
36 namespace test {
37 class QuicDispatcherPeer;
38 }  // namespace test
39
40 class ProcessPacketInterface {
41  public:
42   virtual ~ProcessPacketInterface() {}
43   virtual void ProcessPacket(const IPEndPoint& server_address,
44                              const IPEndPoint& client_address,
45                              const QuicEncryptedPacket& packet) = 0;
46 };
47
48 class QuicDispatcher : public QuicServerSessionVisitor,
49                        public ProcessPacketInterface {
50  public:
51   // Creates per-connection packet writers out of the QuicDispatcher's shared
52   // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
53   // always be the same as the shared writer's IsWriteBlocked(), or else the
54   // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
55   // cleaned up for bug 16950226.)
56   class PacketWriterFactory {
57    public:
58     virtual ~PacketWriterFactory() {}
59
60     virtual QuicPacketWriter* Create(QuicPacketWriter* writer,
61                                      QuicConnection* connection) = 0;
62   };
63
64   // Creates ordinary QuicPerConnectionPacketWriter instances.
65   class DefaultPacketWriterFactory : public PacketWriterFactory {
66    public:
67     ~DefaultPacketWriterFactory() override {}
68
69     QuicPacketWriter* Create(QuicPacketWriter* writer,
70                              QuicConnection* connection) override;
71   };
72
73   // Ideally we'd have a linked_hash_set: the  boolean is unused.
74   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
75
76   // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
77   // live until epoll_server Shutdown. |supported_versions| specifies the list
78   // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
79   // which is used to create per-connection writers.
80   QuicDispatcher(const QuicConfig& config,
81                  const QuicCryptoServerConfig& crypto_config,
82                  const QuicVersionVector& supported_versions,
83                  PacketWriterFactory* packet_writer_factory,
84                  EpollServer* epoll_server);
85
86   ~QuicDispatcher() override;
87
88   virtual void Initialize(int fd);
89
90   // Process the incoming packet by creating a new session, passing it to
91   // an existing session, or passing it to the TimeWaitListManager.
92   void ProcessPacket(const IPEndPoint& server_address,
93                      const IPEndPoint& client_address,
94                      const QuicEncryptedPacket& packet) override;
95
96   // Called when the socket becomes writable to allow queued writes to happen.
97   virtual void OnCanWrite();
98
99   // Returns true if there's anything in the blocked writer list.
100   virtual bool HasPendingWrites() const;
101
102   // Sends ConnectionClose frames to all connected clients.
103   void Shutdown();
104
105   // QuicServerSessionVisitor interface implementation:
106   // Ensure that the closed connection is cleaned up asynchronously.
107   void OnConnectionClosed(QuicConnectionId connection_id,
108                           QuicErrorCode error) override;
109
110   // Queues the blocked writer for later resumption.
111   void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) override;
112
113   typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
114
115   // Deletes all sessions on the closed session list and clears the list.
116   void DeleteSessions();
117
118   const SessionMap& session_map() const { return session_map_; }
119
120  protected:
121   // Instantiates a new low-level packet writer. Caller takes ownership of the
122   // returned object.
123   virtual QuicPacketWriter* CreateWriter(int fd);
124
125   virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
126                                          const IPEndPoint& server_address,
127                                          const IPEndPoint& client_address);
128
129   virtual QuicConnection* CreateQuicConnection(
130       QuicConnectionId connection_id,
131       const IPEndPoint& server_address,
132       const IPEndPoint& client_address);
133
134   // Called by |framer_visitor_| when the public header has been parsed.
135   virtual bool OnUnauthenticatedPublicHeader(
136       const QuicPacketPublicHeader& header);
137
138   // Create and return the time wait list manager for this dispatcher, which
139   // will be owned by the dispatcher as time_wait_list_manager_
140   virtual QuicTimeWaitListManager* CreateQuicTimeWaitListManager();
141
142   // Replaces the packet writer with |writer|. Takes ownership of |writer|.
143   void set_writer(QuicPacketWriter* writer) {
144     writer_.reset(writer);
145   }
146
147   QuicTimeWaitListManager* time_wait_list_manager() {
148     return time_wait_list_manager_.get();
149   }
150
151   EpollServer* epoll_server() { return epoll_server_; }
152
153   const QuicVersionVector& supported_versions() const {
154     return supported_versions_;
155   }
156
157   const IPEndPoint& current_server_address() {
158     return current_server_address_;
159   }
160   const IPEndPoint& current_client_address() {
161     return current_client_address_;
162   }
163   const QuicEncryptedPacket& current_packet() {
164     return *current_packet_;
165   }
166
167   const QuicConfig& config() const { return config_; }
168
169   const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
170
171   QuicFramer* framer() { return &framer_; }
172
173   QuicEpollConnectionHelper* helper() { return helper_.get(); }
174
175   QuicPacketWriter* writer() { return writer_.get(); }
176
177   const QuicConnection::PacketWriterFactory& connection_writer_factory() {
178     return connection_writer_factory_;
179   }
180
181  private:
182   class QuicFramerVisitor;
183   friend class net::tools::test::QuicDispatcherPeer;
184
185   // An adapter that creates packet writers using the dispatcher's
186   // PacketWriterFactory and shared writer. Essentially, it just curries the
187   // writer argument away from QuicDispatcher::PacketWriterFactory.
188   class PacketWriterFactoryAdapter :
189     public QuicConnection::PacketWriterFactory {
190    public:
191     PacketWriterFactoryAdapter(QuicDispatcher* dispatcher);
192     ~PacketWriterFactoryAdapter() override;
193
194     QuicPacketWriter* Create(QuicConnection* connection) const override;
195
196    private:
197     QuicDispatcher* dispatcher_;
198   };
199
200   // Called by |framer_visitor_| when the private header has been parsed
201   // of a data packet that is destined for the time wait manager.
202   void OnUnauthenticatedHeader(const QuicPacketHeader& header);
203
204   // Removes the session from the session map and write blocked list, and
205   // adds the ConnectionId to the time-wait list.
206   void CleanUpSession(SessionMap::iterator it);
207
208   bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
209
210   const QuicConfig& config_;
211
212   const QuicCryptoServerConfig& crypto_config_;
213
214   // The list of connections waiting to write.
215   WriteBlockedList write_blocked_list_;
216
217   SessionMap session_map_;
218
219   // Entity that manages connection_ids in time wait state.
220   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
221
222   // An alarm which deletes closed sessions.
223   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
224
225   // The list of closed but not-yet-deleted sessions.
226   std::list<QuicSession*> closed_session_list_;
227
228   EpollServer* epoll_server_;  // Owned by the server.
229
230   // The helper used for all connections.
231   scoped_ptr<QuicEpollConnectionHelper> helper_;
232
233   // The writer to write to the socket with.
234   scoped_ptr<QuicPacketWriter> writer_;
235
236   // Used to create per-connection packet writers, not |writer_| itself.
237   scoped_ptr<PacketWriterFactory> packet_writer_factory_;
238
239   // Passed in to QuicConnection for it to create the per-connection writers
240   PacketWriterFactoryAdapter connection_writer_factory_;
241
242   // This vector contains QUIC versions which we currently support.
243   // This should be ordered such that the highest supported version is the first
244   // element, with subsequent elements in descending order (versions can be
245   // skipped as necessary).
246   const QuicVersionVector supported_versions_;
247
248   // Information about the packet currently being handled.
249   IPEndPoint current_client_address_;
250   IPEndPoint current_server_address_;
251   const QuicEncryptedPacket* current_packet_;
252
253   QuicFramer framer_;
254   scoped_ptr<QuicFramerVisitor> framer_visitor_;
255
256   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
257 };
258
259 }  // namespace tools
260 }  // namespace net
261
262 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_