| 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 #include "net/base/client_socket_handle.h" | 5 #include "net/base/client_socket_handle.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" |
| 7 #include "net/base/client_socket.h" | 8 #include "net/base/client_socket.h" |
| 8 #include "net/base/client_socket_pool.h" | 9 #include "net/base/client_socket_pool.h" |
| 10 #include "net/base/net_errors.h" |
| 9 | 11 |
| 10 namespace net { | 12 namespace net { |
| 11 | 13 |
| 12 ClientSocketHandle::ClientSocketHandle(ClientSocketPool* pool) | 14 ClientSocketHandle::ClientSocketHandle(ClientSocketPool* pool) |
| 13 : pool_(pool), socket_(NULL) { | 15 : pool_(pool), |
| 14 } | 16 socket_(NULL), |
| 17 is_reused_(false), |
| 18 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 19 callback_(this, &ClientSocketHandle::OnIOComplete)) {} |
| 15 | 20 |
| 16 ClientSocketHandle::~ClientSocketHandle() { | 21 ClientSocketHandle::~ClientSocketHandle() { |
| 17 Reset(); | 22 Reset(); |
| 18 } | 23 } |
| 19 | 24 |
| 20 int ClientSocketHandle::Init(const std::string& group_name, | 25 int ClientSocketHandle::Init(const std::string& group_name, |
| 26 const std::string& host, |
| 27 int port, |
| 21 int priority, | 28 int priority, |
| 22 CompletionCallback* callback) { | 29 CompletionCallback* callback) { |
| 23 Reset(); | 30 ResetInternal(true); |
| 24 group_name_ = group_name; | 31 group_name_ = group_name; |
| 25 return pool_->RequestSocket(this, priority, callback); | 32 user_callback_ = callback; |
| 33 return pool_->RequestSocket( |
| 34 group_name, host, port, priority, this, &callback_); |
| 26 } | 35 } |
| 27 | 36 |
| 28 void ClientSocketHandle::Reset() { | 37 void ClientSocketHandle::Reset() { |
| 38 ResetInternal(true); |
| 39 } |
| 40 |
| 41 void ClientSocketHandle::ResetInternal(bool cancel) { |
| 29 if (group_name_.empty()) // Was Init called? | 42 if (group_name_.empty()) // Was Init called? |
| 30 return; | 43 return; |
| 31 if (socket_) { | 44 if (socket_.get()) { |
| 32 pool_->ReleaseSocket(this); | 45 // If we've still got a socket, release it back to the ClientSocketPool so |
| 33 socket_ = NULL; | 46 // it can be deleted or reused. |
| 34 } else { | 47 pool_->ReleaseSocket(group_name_, release_socket()); |
| 35 pool_->CancelRequest(this); | 48 } else if (cancel) { |
| 49 // If we did not get initialized yet, so we've got a socket request pending. |
| 50 // Cancel it. |
| 51 pool_->CancelRequest(group_name_, this); |
| 36 } | 52 } |
| 37 group_name_.clear(); | 53 group_name_.clear(); |
| 54 is_reused_ = false; |
| 55 user_callback_ = NULL; |
| 56 } |
| 57 |
| 58 LoadState ClientSocketHandle::GetLoadState() const { |
| 59 DCHECK(!is_initialized()); |
| 60 DCHECK(!group_name_.empty()); |
| 61 return pool_->GetLoadState(group_name_, this); |
| 62 } |
| 63 |
| 64 void ClientSocketHandle::OnIOComplete(int result) { |
| 65 DCHECK_NE(ERR_IO_PENDING, result); |
| 66 CompletionCallback* callback = user_callback_; |
| 67 user_callback_ = NULL; |
| 68 if (result != OK) |
| 69 ResetInternal(false); // The request failed, so there's nothing to cancel. |
| 70 callback->Run(result); |
| 38 } | 71 } |
| 39 | 72 |
| 40 } // namespace net | 73 } // namespace net |
| OLD | NEW |