diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 03:00:02 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 03:00:02 +0000 |
commit | 653a73f611d9c3c69be17fdafcce6e41328df18a (patch) | |
tree | 71076bc6ff3b7e94c52be3e823f76ea40e61b547 /net | |
parent | 57113eacd955181e1267f3f20b889bc6fbfd07f2 (diff) | |
download | chromium_src-653a73f611d9c3c69be17fdafcce6e41328df18a.zip chromium_src-653a73f611d9c3c69be17fdafcce6e41328df18a.tar.gz chromium_src-653a73f611d9c3c69be17fdafcce6e41328df18a.tar.bz2 |
Use factories for ConnectingSockets.
This breaks the dependency between ClientSocketPoolBase and the specific ConnectingSocket.
The derived class of ClientSocketPool passes in a ConnectingSocketFactory to ClientSocketPoolBase.
BUG=http://crbug.com/13289
Review URL: http://codereview.chromium.org/125282
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18681 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/tcp_client_socket_pool.cc | 40 | ||||
-rw-r--r-- | net/base/tcp_client_socket_pool.h | 86 |
2 files changed, 93 insertions, 33 deletions
diff --git a/net/base/tcp_client_socket_pool.cc b/net/base/tcp_client_socket_pool.cc index da2d04b9..b8bb5c6 100644 --- a/net/base/tcp_client_socket_pool.cc +++ b/net/base/tcp_client_socket_pool.cc @@ -33,7 +33,7 @@ const int kIdleTimeout = 300; // 5 minutes. namespace net { -ConnectingSocket::ConnectingSocket( +TCPConnectingSocket::TCPConnectingSocket( const std::string& group_name, const HostResolver::RequestInfo& resolve_info, const ClientSocketHandle* handle, @@ -45,27 +45,27 @@ ConnectingSocket::ConnectingSocket( client_socket_factory_(client_socket_factory), ALLOW_THIS_IN_INITIALIZER_LIST( callback_(this, - &ConnectingSocket::OnIOComplete)), + &TCPConnectingSocket::OnIOComplete)), pool_(pool), resolver_(pool->GetHostResolver()) {} -ConnectingSocket::~ConnectingSocket() { +TCPConnectingSocket::~TCPConnectingSocket() { // We don't worry about cancelling the host resolution and TCP connect, since // ~SingleRequestHostResolver and ~ClientSocket will take care of it. } -int ConnectingSocket::Connect() { +int TCPConnectingSocket::Connect() { int rv = resolver_.Resolve(resolve_info_, &addresses_, &callback_); if (rv != ERR_IO_PENDING) rv = OnIOCompleteInternal(rv, true /* synchronous */); return rv; } -void ConnectingSocket::OnIOComplete(int result) { +void TCPConnectingSocket::OnIOComplete(int result) { OnIOCompleteInternal(result, false /* asynchronous */); } -int ConnectingSocket::OnIOCompleteInternal( +int TCPConnectingSocket::OnIOCompleteInternal( int result, bool synchronous) { CHECK(result != ERR_IO_PENDING); @@ -126,12 +126,11 @@ int ConnectingSocket::OnIOCompleteInternal( ClientSocketPoolBase::ClientSocketPoolBase( int max_sockets_per_group, HostResolver* host_resolver, - ClientSocketFactory* client_socket_factory) - : client_socket_factory_(client_socket_factory), - idle_socket_count_(0), + ConnectingSocketFactory* connecting_socket_factory) + : idle_socket_count_(0), max_sockets_per_group_(max_sockets_per_group), - host_resolver_(host_resolver) { -} + host_resolver_(host_resolver), + connecting_socket_factory_(connecting_socket_factory) {} ClientSocketPoolBase::~ClientSocketPoolBase() { // Clean up any idle sockets. Assert that we have no remaining active @@ -203,8 +202,7 @@ int ClientSocketPoolBase::RequestSocket( CHECK(!ContainsKey(connecting_socket_map_, handle)); ConnectingSocket* connecting_socket = - new ConnectingSocket(group_name, resolve_info, handle, - client_socket_factory_, this); + connecting_socket_factory_->NewConnectingSocket(group_name, r, this); connecting_socket_map_[handle] = connecting_socket; int rv = connecting_socket->Connect(); @@ -308,7 +306,8 @@ LoadState ClientSocketPoolBase::GetLoadState( return LOAD_STATE_IDLE; } -bool ClientSocketPoolBase::IdleSocket::ShouldCleanup(base::TimeTicks now) const { +bool ClientSocketPoolBase::IdleSocket::ShouldCleanup( + base::TimeTicks now) const { bool timed_out = (now - start_time) >= base::TimeDelta::FromSeconds(kIdleTimeout); return timed_out || !socket->IsConnectedAndIdle(); @@ -487,12 +486,23 @@ void ClientSocketPoolBase::RemoveConnectingSocket( connecting_socket_map_.erase(it); } +ConnectingSocket* +TCPClientSocketPool::TCPConnectingSocketFactory::NewConnectingSocket( + const std::string& group_name, + const ClientSocketPoolBase::Request& request, + ClientSocketPoolBase* pool) const { + return new TCPConnectingSocket( + group_name, request.resolve_info, request.handle, + client_socket_factory_, pool); +} + TCPClientSocketPool::TCPClientSocketPool( int max_sockets_per_group, HostResolver* host_resolver, ClientSocketFactory* client_socket_factory) : base_(new ClientSocketPoolBase( - max_sockets_per_group, host_resolver, client_socket_factory)) {} + max_sockets_per_group, host_resolver, + new TCPConnectingSocketFactory(client_socket_factory))) {} TCPClientSocketPool::~TCPClientSocketPool() {} diff --git a/net/base/tcp_client_socket_pool.h b/net/base/tcp_client_socket_pool.h index 377457b..40d8505 100644 --- a/net/base/tcp_client_socket_pool.h +++ b/net/base/tcp_client_socket_pool.h @@ -20,25 +20,40 @@ namespace net { class ClientSocketFactory; class ClientSocketPoolBase; -// ConnectingSocket handles the host resolution necessary for socket creation -// and the socket Connect(). +// ConnectingSocket provides an abstract interface for "connecting" a socket. +// The connection may involve host resolution, tcp connection, ssl connection, +// etc. class ConnectingSocket { public: - ConnectingSocket(const std::string& group_name, - const HostResolver::RequestInfo& resolve_info, - const ClientSocketHandle* handle, - ClientSocketFactory* client_socket_factory, - ClientSocketPoolBase* pool); - ~ConnectingSocket(); + ConnectingSocket() {} + virtual ~ConnectingSocket() {} + + // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it + // cannot complete synchronously without blocking, or another net error code + // on error. + virtual int Connect() = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(ConnectingSocket); +}; + +// TCPConnectingSocket handles the host resolution necessary for socket creation +// and the tcp connect. +class TCPConnectingSocket : public ConnectingSocket { + public: + TCPConnectingSocket(const std::string& group_name, + const HostResolver::RequestInfo& resolve_info, + const ClientSocketHandle* handle, + ClientSocketFactory* client_socket_factory, + ClientSocketPoolBase* pool); + ~TCPConnectingSocket(); + + // ConnectingSocket methods. // Begins the host resolution and the TCP connect. Returns OK on success // and ERR_IO_PENDING if it cannot immediately service the request. // Otherwise, it returns a net error code. - int Connect(); - - // Called by the TCPClientSocketPool to cancel this ConnectingSocket. Only - // necessary if a ClientSocketHandle is reused. - void Cancel(); + virtual int Connect(); private: // Handles asynchronous completion of IO. |result| represents the result of @@ -56,7 +71,7 @@ class ConnectingSocket { const HostResolver::RequestInfo resolve_info_; const ClientSocketHandle* const handle_; ClientSocketFactory* const client_socket_factory_; - CompletionCallbackImpl<ConnectingSocket> callback_; + CompletionCallbackImpl<TCPConnectingSocket> callback_; scoped_ptr<ClientSocket> socket_; ClientSocketPoolBase* const pool_; SingleRequestHostResolver resolver_; @@ -65,7 +80,7 @@ class ConnectingSocket { // The time the Connect() method was called (if it got called). base::TimeTicks connect_start_time_; - DISALLOW_COPY_AND_ASSIGN(ConnectingSocket); + DISALLOW_COPY_AND_ASSIGN(TCPConnectingSocket); }; // A ClientSocketPoolBase is used to restrict the number of sockets open at @@ -95,9 +110,23 @@ class ClientSocketPoolBase : public base::RefCounted<ClientSocketPoolBase> { LoadState load_state; }; + class ConnectingSocketFactory { + public: + ConnectingSocketFactory() {} + virtual ~ConnectingSocketFactory() {} + + virtual ConnectingSocket* NewConnectingSocket( + const std::string& group_name, + const Request& request, + ClientSocketPoolBase* pool) const = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(ConnectingSocketFactory); + }; + ClientSocketPoolBase(int max_sockets_per_group, HostResolver* host_resolver, - ClientSocketFactory* client_socket_factory); + ConnectingSocketFactory* connecting_socket_factory); ~ClientSocketPoolBase(); @@ -211,8 +240,6 @@ class ClientSocketPoolBase : public base::RefCounted<ClientSocketPoolBase> { static void CheckSocketCounts(const Group& group); - ClientSocketFactory* const client_socket_factory_; - GroupMap group_map_; ConnectingSocketMap connecting_socket_map_; @@ -231,6 +258,8 @@ class ClientSocketPoolBase : public base::RefCounted<ClientSocketPoolBase> { // sockets. HostResolver* const host_resolver_; + scoped_ptr<ConnectingSocketFactory> connecting_socket_factory_; + DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); }; @@ -272,6 +301,27 @@ class TCPClientSocketPool : public ClientSocketPool { private: virtual ~TCPClientSocketPool(); + class TCPConnectingSocketFactory + : public ClientSocketPoolBase::ConnectingSocketFactory { + public: + TCPConnectingSocketFactory(ClientSocketFactory* client_socket_factory) + : client_socket_factory_(client_socket_factory) {} + + virtual ~TCPConnectingSocketFactory() {} + + // ClientSocketPoolBase::ConnectingSocketFactory methods. + + virtual ConnectingSocket* NewConnectingSocket( + const std::string& group_name, + const ClientSocketPoolBase::Request& request, + ClientSocketPoolBase* pool) const; + + private: + ClientSocketFactory* const client_socket_factory_; + + DISALLOW_COPY_AND_ASSIGN(TCPConnectingSocketFactory); + }; + // One might ask why ClientSocketPoolBase is also refcounted if its // containing ClientSocketPool is already refcounted. The reason is because // DoReleaseSocket() posts a task. If ClientSocketPool gets deleted between |