| OLD | NEW |
| (Empty) |
| 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_BASE_CLIENT_SOCKET_HANDLE_H_ | |
| 6 #define NET_BASE_CLIENT_SOCKET_HANDLE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/ref_counted.h" | |
| 11 #include "base/scoped_ptr.h" | |
| 12 #include "net/base/client_socket.h" | |
| 13 #include "net/base/completion_callback.h" | |
| 14 #include "net/base/host_resolver.h" | |
| 15 #include "net/base/load_states.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 class ClientSocketPool; | |
| 20 | |
| 21 // A container for a ClientSocket. | |
| 22 // | |
| 23 // The handle's |group_name| uniquely identifies the origin and type of the | |
| 24 // connection. It is used by the ClientSocketPool to group similar connected | |
| 25 // client socket objects. | |
| 26 // | |
| 27 class ClientSocketHandle { | |
| 28 public: | |
| 29 explicit ClientSocketHandle(ClientSocketPool* pool); | |
| 30 ~ClientSocketHandle(); | |
| 31 | |
| 32 // Initializes a ClientSocketHandle object, which involves talking to the | |
| 33 // ClientSocketPool to obtain a connected socket, possibly reusing one. This | |
| 34 // method returns either OK or ERR_IO_PENDING. On ERR_IO_PENDING, |priority| | |
| 35 // is used to determine the placement in ClientSocketPool's wait list. | |
| 36 // | |
| 37 // If this method succeeds, then the socket member will be set to an existing | |
| 38 // connected socket if an existing connected socket was available to reuse, | |
| 39 // otherwise it will be set to a new connected socket. Consumers can then | |
| 40 // call is_reused() to see if the socket was reused. If not reusing an | |
| 41 // existing socket, ClientSocketPool may need to establish a new | |
| 42 // connection to the |resolve_info.host| |resolve_info.port| pair. | |
| 43 // | |
| 44 // This method returns ERR_IO_PENDING if it cannot complete synchronously, in | |
| 45 // which case the consumer will be notified of completion via |callback|. | |
| 46 // | |
| 47 // Init may be called multiple times. | |
| 48 // | |
| 49 int Init(const std::string& group_name, | |
| 50 const HostResolver::RequestInfo& resolve_info, | |
| 51 int priority, | |
| 52 CompletionCallback* callback); | |
| 53 | |
| 54 // An initialized handle can be reset, which causes it to return to the | |
| 55 // un-initialized state. This releases the underlying socket, which in the | |
| 56 // case of a socket that still has an established connection, indicates that | |
| 57 // the socket may be kept alive for use by a subsequent ClientSocketHandle. | |
| 58 // | |
| 59 // NOTE: To prevent the socket from being kept alive, be sure to call its | |
| 60 // Disconnect method. This will result in the ClientSocketPool deleting the | |
| 61 // ClientSocket. | |
| 62 void Reset(); | |
| 63 | |
| 64 // Used after Init() is called, but before the ClientSocketPool has | |
| 65 // initialized the ClientSocketHandle. | |
| 66 LoadState GetLoadState() const; | |
| 67 | |
| 68 // Returns true when Init() has completed successfully. | |
| 69 bool is_initialized() const { return socket_ != NULL; } | |
| 70 | |
| 71 // Used by ClientSocketPool to initialize the ClientSocketHandle. | |
| 72 void set_is_reused(bool is_reused) { is_reused_ = is_reused; } | |
| 73 void set_socket(ClientSocket* s) { socket_.reset(s); } | |
| 74 | |
| 75 // These may only be used if is_initialized() is true. | |
| 76 const std::string& group_name() const { return group_name_; } | |
| 77 ClientSocket* socket() { return socket_.get(); } | |
| 78 ClientSocket* release_socket() { return socket_.release(); } | |
| 79 bool is_reused() const { return is_reused_; } | |
| 80 | |
| 81 private: | |
| 82 void OnIOComplete(int result); | |
| 83 | |
| 84 // Resets the state of the ClientSocketHandle. |cancel| indicates whether or | |
| 85 // not to try to cancel the request with the ClientSocketPool. | |
| 86 void ResetInternal(bool cancel); | |
| 87 | |
| 88 scoped_refptr<ClientSocketPool> pool_; | |
| 89 scoped_ptr<ClientSocket> socket_; | |
| 90 std::string group_name_; | |
| 91 bool is_reused_; | |
| 92 CompletionCallbackImpl<ClientSocketHandle> callback_; | |
| 93 CompletionCallback* user_callback_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(ClientSocketHandle); | |
| 96 }; | |
| 97 | |
| 98 } // namespace net | |
| 99 | |
| 100 #endif // NET_BASE_CLIENT_SOCKET_HANDLE_H_ | |
| OLD | NEW |