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