#ifndef NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
#define NET_TOOLS_QUIC_QUIC_DISPATCHER_H_
#include <list>
#include "base/basictypes.h"
#include "base/containers/hash_tables.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/ip_endpoint.h"
#include "net/base/linked_hash_map.h"
#include "net/quic/quic_blocked_writer_interface.h"
#include "net/quic/quic_protocol.h"
#include "net/tools/epoll_server/epoll_server.h"
#include "net/tools/quic/quic_server_session.h"
#include "net/tools/quic/quic_time_wait_list_manager.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<net::QuicBlockedWriterInterface*> {
std::size_t operator()(
const net::QuicBlockedWriterInterface* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
}
#endif
namespace net {
class EpollServer;
class QuicConfig;
class QuicConnectionHelper;
class QuicCryptoServerConfig;
class QuicSession;
namespace tools {
class QuicPacketWriterWrapper;
namespace test {
class QuicDispatcherPeer;
}
class DeleteSessionsAlarm;
class QuicEpollConnectionHelper;
class QuicDispatcher : public QuicServerSessionVisitor {
public:
typedef linked_hash_map<QuicBlockedWriterInterface*, bool> WriteBlockedList;
QuicDispatcher(const QuicConfig& config,
const QuicCryptoServerConfig& crypto_config,
const QuicVersionVector& supported_versions,
EpollServer* epoll_server,
uint32 initial_flow_control_window_bytes);
virtual ~QuicDispatcher();
virtual void Initialize(int fd);
virtual void ProcessPacket(const IPEndPoint& server_address,
const IPEndPoint& client_address,
const QuicEncryptedPacket& packet);
virtual void OnCanWrite();
virtual bool HasPendingWrites() const;
void Shutdown();
virtual void OnConnectionClosed(QuicConnectionId connection_id,
QuicErrorCode error) OVERRIDE;
virtual void OnWriteBlocked(QuicBlockedWriterInterface* writer) OVERRIDE;
typedef base::hash_map<QuicConnectionId, QuicSession*> SessionMap;
void DeleteSessions();
const SessionMap& session_map() const { return session_map_; }
WriteBlockedList* write_blocked_list() { return &write_blocked_list_; }
protected:
QuicPacketWriter* CreateWriter(int fd);
virtual QuicPacketWriterWrapper* CreateWriterWrapper(
QuicPacketWriter* writer);
virtual QuicSession* CreateQuicSession(QuicConnectionId connection_id,
const IPEndPoint& server_address,
const IPEndPoint& client_address);
QuicConnection* CreateQuicConnection(QuicConnectionId connection_id,
const IPEndPoint& server_address,
const IPEndPoint& client_address,
uint32 initial_flow_control_window);
void set_writer(QuicPacketWriter* writer);
QuicTimeWaitListManager* time_wait_list_manager() {
return time_wait_list_manager_.get();
}
EpollServer* epoll_server() { return epoll_server_; }
const QuicVersionVector& supported_versions() const {
return supported_versions_;
}
virtual bool OnUnauthenticatedPublicHeader(
const QuicPacketPublicHeader& header);
const IPEndPoint& current_server_address() {
return current_server_address_;
}
const IPEndPoint& current_client_address() {
return current_client_address_;
}
const QuicEncryptedPacket& current_packet() {
return *current_packet_;
}
const QuicConfig& config() const { return config_; }
const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
QuicFramer* framer() { return &framer_; }
private:
class QuicFramerVisitor;
friend class net::tools::test::QuicDispatcherPeer;
void OnUnauthenticatedHeader(const QuicPacketHeader& header);
void CleanUpSession(SessionMap::iterator it);
bool HandlePacketForTimeWait(const QuicPacketPublicHeader& header);
const QuicConfig& config_;
const QuicCryptoServerConfig& crypto_config_;
WriteBlockedList write_blocked_list_;
SessionMap session_map_;
scoped_ptr<QuicTimeWaitListManager> time_wait_list_manager_;
scoped_ptr<DeleteSessionsAlarm> delete_sessions_alarm_;
std::list<QuicSession*> closed_session_list_;
EpollServer* epoll_server_;
scoped_ptr<QuicEpollConnectionHelper> helper_;
scoped_ptr<QuicPacketWriterWrapper> writer_;
const QuicVersionVector supported_versions_;
QuicVersionVector supported_versions_no_flow_control_;
IPEndPoint current_client_address_;
IPEndPoint current_server_address_;
const QuicEncryptedPacket* current_packet_;
QuicFramer framer_;
scoped_ptr<QuicFramerVisitor> framer_visitor_;
const uint32 initial_flow_control_window_bytes_;
DISALLOW_COPY_AND_ASSIGN(QuicDispatcher);
};
}
}
#endif