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