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