Upstream version 5.34.92.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_packet_writer.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/tools/epoll_server/epoll_server.h"
22 #include "net/tools/quic/quic_server_session.h"
23 #include "net/tools/quic/quic_time_wait_list_manager.h"
24
25 #if defined(COMPILER_GCC)
26 namespace BASE_HASH_NAMESPACE {
27 template<>
28 struct hash<net::QuicBlockedWriterInterface*> {
29   std::size_t operator()(
30       const net::QuicBlockedWriterInterface* ptr) const {
31     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
32   }
33 };
34 }
35 #endif
36
37 namespace net {
38
39 class EpollServer;
40 class QuicConfig;
41 class QuicConnectionHelper;
42 class QuicCryptoServerConfig;
43 class QuicSession;
44
45 namespace tools {
46
47 class QuicPacketWriterWrapper;
48
49 namespace test {
50 class QuicDispatcherPeer;
51 }  // namespace test
52
53 class DeleteSessionsAlarm;
54 class QuicEpollConnectionHelper;
55
56 class QuicDispatcher : public QuicPacketWriter,
57                        public QuicServerSessionVisitor {
58  public:
59   // Ideally we'd have a linked_hash_set: the  boolean is unused.
60   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
61
62   // Due to the way delete_sessions_closure_ is registered, the Dispatcher
63   // must live until epoll_server Shutdown. |supported_versions| specifies the
64   // list of supported QUIC versions.
65   QuicDispatcher(const QuicConfig& config,
66                  const QuicCryptoServerConfig& crypto_config,
67                  const QuicVersionVector& supported_versions,
68                  EpollServer* epoll_server);
69
70   virtual ~QuicDispatcher();
71
72   void Initialize(int fd);
73
74   // QuicPacketWriter
75   virtual WriteResult WritePacket(
76       const char* buffer, size_t buf_len,
77       const IPAddressNumber& self_address,
78       const IPEndPoint& peer_address,
79       QuicBlockedWriterInterface* writer) OVERRIDE;
80   virtual bool IsWriteBlockedDataBuffered() const OVERRIDE;
81   virtual bool IsWriteBlocked() const OVERRIDE;
82   virtual void SetWritable() OVERRIDE;
83
84   // Process the incoming packet by creating a new session, passing it to
85   // an existing session, or passing it to the TimeWaitListManager.
86   virtual void ProcessPacket(const IPEndPoint& server_address,
87                              const IPEndPoint& client_address,
88                              const QuicEncryptedPacket& packet);
89
90   // Called when the underyling connection becomes writable to allow
91   // queued writes to happen.
92   //
93   // Returns true if more writes are possible, false otherwise.
94   virtual bool OnCanWrite();
95
96   // Sends ConnectionClose frames to all connected clients.
97   void Shutdown();
98
99   // QuicServerSessionVisitor interface implementation:
100   // Ensure that the closed connection is cleaned up asynchronously.
101   virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE;
102
103   // Queues the blocked writer for later resumption.
104   virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
105
106   typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
107
108   virtual QuicSession* CreateQuicSession(
109       QuicGuid guid,
110       const IPEndPoint& server_address,
111       const IPEndPoint& client_address);
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   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
119
120  protected:
121   // Instantiates a new low-level packet writer. Caller takes ownership of the
122   // returned object.
123   QuicPacketWriter* CreateWriter(int fd);
124
125   // Instantiates a new top-level writer wrapper. Takes ownership of |writer|.
126   // Caller takes ownership of the returned object.
127   virtual QuicPacketWriterWrapper* CreateWriterWrapper(
128       QuicPacketWriter* writer);
129
130   // Replaces the packet writer with |writer|. Takes ownership of |writer|.
131   void set_writer(QuicPacketWriter* writer);
132
133   QuicTimeWaitListManager* time_wait_list_manager() {
134     return time_wait_list_manager_.get();
135   }
136
137   QuicEpollConnectionHelper* helper() { return helper_.get(); }
138   EpollServer* epoll_server() { return epoll_server_; }
139
140   const QuicVersionVector& supported_versions() const {
141     return supported_versions_;
142   }
143
144   // Called by |framer_visitor_| when the public header has been parsed.
145   virtual bool OnUnauthenticatedPublicHeader(
146       const QuicPacketPublicHeader& header);
147
148   // Information about the packet currently being dispatched.
149   const IPEndPoint& current_client_address() {
150     return current_client_address_;
151   }
152   const IPEndPoint& current_server_address() {
153     return current_server_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  private:
164   class QuicFramerVisitor;
165   friend class net::tools::test::QuicDispatcherPeer;
166
167   // Called by |framer_visitor_| when the private header has been parsed
168   // of a data packet that is destined for the time wait manager.
169   void OnUnauthenticatedHeader(const QuicPacketHeader& header);
170
171   // Removes the session from the session map and write blocked list, and
172   // adds the GUID to the time-wait list.
173   void CleanUpSession(SessionMap::iterator it);
174
175   bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
176
177   const QuicConfig& config_;
178
179   const QuicCryptoServerConfig& crypto_config_;
180
181   // The list of connections waiting to write.
182   WriteBlockedList write_blocked_list_;
183
184   SessionMap session_map_;
185
186   // Entity that manages guids in time wait state.
187   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
188
189   // An alarm which deletes closed sessions.
190   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
191
192   // The list of closed but not-yet-deleted sessions.
193   std::list<QuicSession*> closed_session_list_;
194
195   EpollServer* epoll_server_;  // Owned by the server.
196
197   // The helper used for all connections.
198   scoped_ptr<QuicEpollConnectionHelper> helper_;
199
200   // The writer to write to the socket with. We require a writer wrapper to
201   // allow replacing writer implementation without disturbing running
202   // connections.
203   scoped_ptr<QuicPacketWriterWrapper> writer_;
204
205   // This vector contains QUIC versions which we currently support.
206   // This should be ordered such that the highest supported version is the first
207   // element, with subsequent elements in descending order (versions can be
208   // skipped as necessary).
209   const QuicVersionVector supported_versions_;
210
211   // Information about the packet currently being handled.
212   IPEndPoint current_client_address_;
213   IPEndPoint current_server_address_;
214   const QuicEncryptedPacket* current_packet_;
215
216   QuicFramer framer_;
217   scoped_ptr<QuicFramerVisitor> framer_visitor_;
218
219   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
220 };
221
222 }  // namespace tools
223 }  // namespace net
224
225 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_