1 // Copyright (c) 2006-2008 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 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_H_ 6 #define NET_SOCKET_CLIENT_SOCKET_POOL_H_ 7 8 #include <deque> 9 #include <map> 10 #include <string> 11 12 #include "base/ref_counted.h" 13 #include "net/base/completion_callback.h" 14 #include "net/base/host_resolver.h" 15 #include "net/base/load_states.h" 16 #include "net/base/request_priority.h" 17 18 namespace net { 19 20 class ClientSocket; 21 class ClientSocketHandle; 22 23 // A ClientSocketPool is used to restrict the number of sockets open at a time. 24 // It also maintains a list of idle persistent sockets. 25 // 26 class ClientSocketPool : public base::RefCounted<ClientSocketPool> { 27 public: 28 // Requests a connected socket for a group_name. 29 // 30 // There are four possible results from calling this function: 31 // 1) RequestSocket returns OK and initializes |handle| with a reused socket. 32 // 2) RequestSocket returns OK with a newly connected socket. 33 // 3) RequestSocket returns ERR_IO_PENDING. The handle will be added to a 34 // wait list until a socket is available to reuse or a new socket finishes 35 // connecting. |priority| will determine the placement into the wait list. 36 // 4) An error occurred early on, so RequestSocket returns an error code. 37 // 38 // If this function returns OK, then |handle| is initialized upon return. 39 // The |handle|'s is_initialized method will return true in this case. If a 40 // ClientSocket was reused, then ClientSocketPool will call 41 // |handle|->set_reused(true). In either case, the socket will have been 42 // allocated and will be connected. A client might want to know whether or 43 // not the socket is reused in order to know whether or not he needs to 44 // perform SSL connection or tunnel setup or to request a new socket if he 45 // encounters an error with the reused socket. 46 // 47 // If ERR_IO_PENDING is returned, then the callback will be used to notify the 48 // client of completion. 49 // 50 // Profiling information for the request is saved to |load_log| if non-NULL. 51 virtual int RequestSocket(const std::string& group_name, 52 const void* params, 53 RequestPriority priority, 54 ClientSocketHandle* handle, 55 CompletionCallback* callback, 56 LoadLog* load_log) = 0; 57 58 // Called to cancel a RequestSocket call that returned ERR_IO_PENDING. The 59 // same handle parameter must be passed to this method as was passed to the 60 // RequestSocket call being cancelled. The associated CompletionCallback is 61 // not run. 62 virtual void CancelRequest(const std::string& group_name, 63 const ClientSocketHandle* handle) = 0; 64 65 // Called to release a socket once the socket is no longer needed. If the 66 // socket still has an established connection, then it will be added to the 67 // set of idle sockets to be used to satisfy future RequestSocket calls. 68 // Otherwise, the ClientSocket is destroyed. 69 virtual void ReleaseSocket(const std::string& group_name, 70 ClientSocket* socket) = 0; 71 72 // Called to close any idle connections held by the connection manager. 73 virtual void CloseIdleSockets() = 0; 74 75 // The total number of idle sockets in the pool. 76 virtual int IdleSocketCount() const = 0; 77 78 // The total number of idle sockets in a connection group. 79 virtual int IdleSocketCountInGroup(const std::string& group_name) const = 0; 80 81 // Determine the LoadState of a connecting ClientSocketHandle. 82 virtual LoadState GetLoadState(const std::string& group_name, 83 const ClientSocketHandle* handle) const = 0; 84 85 protected: 86 ClientSocketPool() {} 87 virtual ~ClientSocketPool() {} 88 89 private: 90 friend class base::RefCounted<ClientSocketPool>; 91 92 DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); 93 }; 94 95 // Declaration, but no definition. ClientSocketPool subclasses should indicate 96 // valid SocketParams via the REGISTER_SOCKET_PARAMS_FOR_POOL macro below, which 97 // will provide a definition of CheckIsValidSocketParamsForPool for the 98 // ClientSocketPool subtype and SocketParams pair. Trying to use a SocketParams 99 // type that has not been registered with the corresponding ClientSocketPool 100 // subtype will result in a link time error stating that 101 // CheckIsValidSocketParamsForPool with those template parameters is undefined. 102 template <typename PoolType, typename SocketParams> 103 void CheckIsValidSocketParamsForPool(); 104 105 // Provides an empty definition for CheckIsValidSocketParamsForPool() which 106 // should be optimized out by the compiler. 107 #define REGISTER_SOCKET_PARAMS_FOR_POOL(pool_type, socket_params) \ 108 template<> \ 109 inline void CheckIsValidSocketParamsForPool<pool_type, socket_params>() {} 110 111 } // namespace net 112 113 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_H_ 114