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.
5 // A server side dispatcher which dispatches a given client's data to their
8 #ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
9 #define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
13 #include "base/containers/hash_tables.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/quic/blocked_list.h"
16 #include "net/quic/quic_blocked_writer_interface.h"
17 #include "net/quic/quic_protocol.h"
18 #include "net/tools/flip_server/epoll_server.h"
19 #include "net/tools/quic/quic_packet_writer.h"
20 #include "net/tools/quic/quic_server_session.h"
21 #include "net/tools/quic/quic_time_wait_list_manager.h"
23 #if defined(COMPILER_GCC)
24 namespace BASE_HASH_NAMESPACE
{
26 struct hash
<net::QuicBlockedWriterInterface
*> {
27 std::size_t operator()(
28 const net::QuicBlockedWriterInterface
* ptr
) const {
29 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
39 class QuicCryptoServerConfig
;
45 class QuicDispatcherPeer
;
48 class DeleteSessionsAlarm
;
49 class QuicDispatcher
: public QuicPacketWriter
, public QuicSessionOwner
{
51 typedef BlockedList
<QuicBlockedWriterInterface
*> WriteBlockedList
;
53 // Due to the way delete_sessions_closure_ is registered, the Dispatcher
54 // must live until epoll_server Shutdown.
55 QuicDispatcher(const QuicConfig
& config
,
56 const QuicCryptoServerConfig
& crypto_config
,
58 EpollServer
* epoll_server
);
59 virtual ~QuicDispatcher();
62 virtual int WritePacket(const char* buffer
, size_t buf_len
,
63 const IPAddressNumber
& self_address
,
64 const IPEndPoint
& peer_address
,
65 QuicBlockedWriterInterface
* writer
,
68 virtual void ProcessPacket(const IPEndPoint
& server_address
,
69 const IPEndPoint
& client_address
,
71 const QuicEncryptedPacket
& packet
);
73 // Called when the underyling connection becomes writable to allow
74 // queued writes to happen.
76 // Returns true if more writes are possible, false otherwise.
77 virtual bool OnCanWrite();
79 // Sends ConnectionClose frames to all connected clients.
82 // Ensure that the closed connection is cleaned up asynchronously.
83 virtual void OnConnectionClose(QuicGuid guid
, QuicErrorCode error
) OVERRIDE
;
85 int fd() { return fd_
; }
86 void set_fd(int fd
) { fd_
= fd
; }
88 typedef base::hash_map
<QuicGuid
, QuicSession
*> SessionMap
;
90 virtual QuicSession
* CreateQuicSession(
92 const IPEndPoint
& client_address
,
94 EpollServer
* epoll_server
);
96 // Deletes all sessions on the closed session list and clears the list.
97 void DeleteSessions();
99 const SessionMap
& session_map() const { return session_map_
; }
101 WriteBlockedList
* write_blocked_list() { return &write_blocked_list_
; }
104 const QuicConfig
& config_
;
105 const QuicCryptoServerConfig
& crypto_config_
;
107 QuicTimeWaitListManager
* time_wait_list_manager() {
108 return time_wait_list_manager_
.get();
112 friend class net::tools::test::QuicDispatcherPeer
;
114 // Removes the session from the session map and write blocked list, and
115 // adds the GUID to the time-wait list.
116 void CleanUpSession(SessionMap::iterator it
);
118 // The list of connections waiting to write.
119 WriteBlockedList write_blocked_list_
;
121 SessionMap session_map_
;
123 // Entity that manages guids in time wait state.
124 scoped_ptr
<QuicTimeWaitListManager
> time_wait_list_manager_
;
126 // An alarm which deletes closed sessions.
127 scoped_ptr
<DeleteSessionsAlarm
> delete_sessions_alarm_
;
129 // The list of closed but not-yet-deleted sessions.
130 std::list
<QuicSession
*> closed_session_list_
;
132 EpollServer
* epoll_server_
; // Owned by the server.
134 // The connection for client-server communication
137 // True if the session is write blocked due to the socket returning EAGAIN.
138 // False if we have gotten a call to OnCanWrite after the last failed write.
141 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
147 #endif // NET_TOOLS_QUIC_QUIC_DISPATCHER_H_