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