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