- add sources.
[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/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/linked_hash_map.h"
17 #include "net/quic/quic_blocked_writer_interface.h"
18 #include "net/quic/quic_packet_writer.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 namespace test {
46 class QuicDispatcherPeer;
47 }  // namespace test
48
49 class DeleteSessionsAlarm;
50 class QuicEpollConnectionHelper;
51
52 class QuicDispatcher : public QuicPacketWriter, public QuicSessionOwner {
53  public:
54   // Ideally we'd have a linked_hash_set: the  boolean is unused.
55   typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
56
57   // Due to the way delete_sessions_closure_ is registered, the Dispatcher
58   // must live until epoll_server Shutdown. |supported_versions| specifies the
59   // list of supported QUIC versions.
60   QuicDispatcher(const QuicConfig& config,
61                  const QuicCryptoServerConfig& crypto_config,
62                  const QuicVersionVector& supported_versions,
63                  int fd,
64                  EpollServer* epoll_server);
65   virtual ~QuicDispatcher();
66
67   // QuicPacketWriter
68   virtual WriteResult WritePacket(
69       const char* buffer, size_t buf_len,
70       const IPAddressNumber& self_address,
71       const IPEndPoint& peer_address,
72       QuicBlockedWriterInterface* writer) OVERRIDE;
73   virtual bool IsWriteBlockedDataBuffered() const OVERRIDE;
74
75   virtual void ProcessPacket(const IPEndPoint& server_address,
76                              const IPEndPoint& client_address,
77                              QuicGuid guid,
78                              const QuicEncryptedPacket& packet);
79
80   // Called when the underyling connection becomes writable to allow
81   // queued writes to happen.
82   //
83   // Returns true if more writes are possible, false otherwise.
84   virtual bool OnCanWrite();
85
86   // Sends ConnectionClose frames to all connected clients.
87   void Shutdown();
88
89   // Ensure that the closed connection is cleaned up asynchronously.
90   virtual void OnConnectionClosed(QuicGuid guid, QuicErrorCode error) OVERRIDE;
91
92   // Sets the fd and creates a default packet writer with that fd.
93   void set_fd(int fd);
94
95   typedef base::hash_map<QuicGuid, QuicSession*> SessionMap;
96
97   virtual QuicSession* CreateQuicSession(
98       QuicGuid guid,
99       const IPEndPoint& client_address);
100
101   // Deletes all sessions on the closed session list and clears the list.
102   void DeleteSessions();
103
104   const SessionMap& session_map() const { return session_map_; }
105
106   // Uses the specified |writer| instead of QuicSocketUtils and takes ownership
107   // of writer.
108   void UseWriter(QuicPacketWriter* writer);
109
110   WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
111
112  protected:
113   const QuicConfig& config_;
114   const QuicCryptoServerConfig& crypto_config_;
115
116   QuicTimeWaitListManager* time_wait_list_manager() {
117     return time_wait_list_manager_.get();
118   }
119
120   QuicEpollConnectionHelper* helper() { return helper_.get(); }
121   EpollServer* epoll_server() { return epoll_server_; }
122
123   const QuicVersionVector& supported_versions() const {
124     return supported_versions_;
125   }
126
127  private:
128   friend class net::tools::test::QuicDispatcherPeer;
129
130   // Removes the session from the session map and write blocked list, and
131   // adds the GUID to the time-wait list.
132   void CleanUpSession(SessionMap::iterator it);
133
134   // The list of connections waiting to write.
135   WriteBlockedList write_blocked_list_;
136
137   SessionMap session_map_;
138
139   // Entity that manages guids in time wait state.
140   scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
141
142   // An alarm which deletes closed sessions.
143   scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
144
145   // The list of closed but not-yet-deleted sessions.
146   std::list<QuicSession*> closed_session_list_;
147
148   EpollServer* epoll_server_;  // Owned by the server.
149
150   // The connection for client-server communication
151   int fd_;
152
153   // True if the session is write blocked due to the socket returning EAGAIN.
154   // False if we have gotten a call to OnCanWrite after the last failed write.
155   bool write_blocked_;
156
157   // The helper used for all connections.
158   scoped_ptr<QuicEpollConnectionHelper> helper_;
159
160   // The writer to write to the socket with.
161   scoped_ptr<QuicPacketWriter> writer_;
162
163   // This vector contains QUIC versions which we currently support.
164   // This should be ordered such that the highest supported version is the first
165   // element, with subsequent elements in descending order (versions can be
166   // skipped as necessary).
167   const QuicVersionVector supported_versions_;
168
169   DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
170 };
171
172 }  // namespace tools
173 }  // namespace net
174
175 #endif  // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_