Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: net/socket/socks_client_socket.cc

Issue 8801004: base::Bind: Convert StreamSocket::Connect. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/socket/socks_client_socket.h ('k') | net/socket/ssl_client_socket_mac.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/socket/socks_client_socket.h" 5 #include "net/socket/socks_client_socket.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 COMPILE_ASSERT(sizeof(SOCKS4ServerResponse) == kReadHeaderSize, 55 COMPILE_ASSERT(sizeof(SOCKS4ServerResponse) == kReadHeaderSize,
56 socks4_server_response_struct_wrong_size); 56 socks4_server_response_struct_wrong_size);
57 57
58 SOCKSClientSocket::SOCKSClientSocket(ClientSocketHandle* transport_socket, 58 SOCKSClientSocket::SOCKSClientSocket(ClientSocketHandle* transport_socket,
59 const HostResolver::RequestInfo& req_info, 59 const HostResolver::RequestInfo& req_info,
60 HostResolver* host_resolver) 60 HostResolver* host_resolver)
61 : ALLOW_THIS_IN_INITIALIZER_LIST( 61 : ALLOW_THIS_IN_INITIALIZER_LIST(
62 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), 62 io_callback_(this, &SOCKSClientSocket::OnIOComplete)),
63 transport_(transport_socket), 63 transport_(transport_socket),
64 next_state_(STATE_NONE), 64 next_state_(STATE_NONE),
65 user_callback_(NULL), 65 old_user_callback_(NULL),
66 completed_handshake_(false), 66 completed_handshake_(false),
67 bytes_sent_(0), 67 bytes_sent_(0),
68 bytes_received_(0), 68 bytes_received_(0),
69 host_resolver_(host_resolver), 69 host_resolver_(host_resolver),
70 host_request_info_(req_info), 70 host_request_info_(req_info),
71 net_log_(transport_socket->socket()->NetLog()) { 71 net_log_(transport_socket->socket()->NetLog()) {
72 } 72 }
73 73
74 SOCKSClientSocket::SOCKSClientSocket(StreamSocket* transport_socket, 74 SOCKSClientSocket::SOCKSClientSocket(StreamSocket* transport_socket,
75 const HostResolver::RequestInfo& req_info, 75 const HostResolver::RequestInfo& req_info,
76 HostResolver* host_resolver) 76 HostResolver* host_resolver)
77 : ALLOW_THIS_IN_INITIALIZER_LIST( 77 : ALLOW_THIS_IN_INITIALIZER_LIST(
78 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), 78 io_callback_(this, &SOCKSClientSocket::OnIOComplete)),
79 transport_(new ClientSocketHandle()), 79 transport_(new ClientSocketHandle()),
80 next_state_(STATE_NONE), 80 next_state_(STATE_NONE),
81 user_callback_(NULL), 81 old_user_callback_(NULL),
82 completed_handshake_(false), 82 completed_handshake_(false),
83 bytes_sent_(0), 83 bytes_sent_(0),
84 bytes_received_(0), 84 bytes_received_(0),
85 host_resolver_(host_resolver), 85 host_resolver_(host_resolver),
86 host_request_info_(req_info), 86 host_request_info_(req_info),
87 net_log_(transport_socket->NetLog()) { 87 net_log_(transport_socket->NetLog()) {
88 transport_->set_socket(transport_socket); 88 transport_->set_socket(transport_socket);
89 } 89 }
90 90
91 SOCKSClientSocket::~SOCKSClientSocket() { 91 SOCKSClientSocket::~SOCKSClientSocket() {
92 Disconnect(); 92 Disconnect();
93 } 93 }
94 94
95 int SOCKSClientSocket::Connect(OldCompletionCallback* callback) { 95 int SOCKSClientSocket::Connect(OldCompletionCallback* callback) {
96 DCHECK(transport_.get()); 96 DCHECK(transport_.get());
97 DCHECK(transport_->socket()); 97 DCHECK(transport_->socket());
98 DCHECK_EQ(STATE_NONE, next_state_); 98 DCHECK_EQ(STATE_NONE, next_state_);
99 DCHECK(!user_callback_); 99 DCHECK(!old_user_callback_ && user_callback_.is_null());
100 100
101 // If already connected, then just return OK. 101 // If already connected, then just return OK.
102 if (completed_handshake_) 102 if (completed_handshake_)
103 return OK; 103 return OK;
104 104
105 next_state_ = STATE_RESOLVE_HOST; 105 next_state_ = STATE_RESOLVE_HOST;
106 106
107 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT, NULL); 107 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT, NULL);
108 108
109 int rv = DoLoop(OK); 109 int rv = DoLoop(OK);
110 if (rv == ERR_IO_PENDING) { 110 if (rv == ERR_IO_PENDING) {
111 user_callback_ = callback; 111 old_user_callback_ = callback;
112 } else { 112 } else {
113 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); 113 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
114 } 114 }
115 return rv; 115 return rv;
116 } 116 }
117 int SOCKSClientSocket::Connect(const net::CompletionCallback& callback) {
118 DCHECK(transport_.get());
119 DCHECK(transport_->socket());
120 DCHECK_EQ(STATE_NONE, next_state_);
121 DCHECK(!old_user_callback_ && user_callback_.is_null());
122
123 // If already connected, then just return OK.
124 if (completed_handshake_)
125 return OK;
126
127 next_state_ = STATE_RESOLVE_HOST;
128
129 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT, NULL);
130
131 int rv = DoLoop(OK);
132 if (rv == ERR_IO_PENDING)
133 user_callback_ = callback;
134 else
135 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
136
137 return rv;
138 }
117 139
118 void SOCKSClientSocket::Disconnect() { 140 void SOCKSClientSocket::Disconnect() {
119 completed_handshake_ = false; 141 completed_handshake_ = false;
120 host_resolver_.Cancel(); 142 host_resolver_.Cancel();
121 transport_->socket()->Disconnect(); 143 transport_->socket()->Disconnect();
122 144
123 // Reset other states to make sure they aren't mistakenly used later. 145 // Reset other states to make sure they aren't mistakenly used later.
124 // These are the states initialized by Connect(). 146 // These are the states initialized by Connect().
125 next_state_ = STATE_NONE; 147 next_state_ = STATE_NONE;
126 user_callback_ = NULL; 148 old_user_callback_ = NULL;
149 user_callback_.Reset();
127 } 150 }
128 151
129 bool SOCKSClientSocket::IsConnected() const { 152 bool SOCKSClientSocket::IsConnected() const {
130 return completed_handshake_ && transport_->socket()->IsConnected(); 153 return completed_handshake_ && transport_->socket()->IsConnected();
131 } 154 }
132 155
133 bool SOCKSClientSocket::IsConnectedAndIdle() const { 156 bool SOCKSClientSocket::IsConnectedAndIdle() const {
134 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); 157 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle();
135 } 158 }
136 159
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 return base::TimeDelta::FromMicroseconds(-1); 209 return base::TimeDelta::FromMicroseconds(-1);
187 } 210 }
188 211
189 212
190 // Read is called by the transport layer above to read. This can only be done 213 // Read is called by the transport layer above to read. This can only be done
191 // if the SOCKS handshake is complete. 214 // if the SOCKS handshake is complete.
192 int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, 215 int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
193 OldCompletionCallback* callback) { 216 OldCompletionCallback* callback) {
194 DCHECK(completed_handshake_); 217 DCHECK(completed_handshake_);
195 DCHECK_EQ(STATE_NONE, next_state_); 218 DCHECK_EQ(STATE_NONE, next_state_);
196 DCHECK(!user_callback_); 219 DCHECK(!old_user_callback_);
197 220
198 return transport_->socket()->Read(buf, buf_len, callback); 221 return transport_->socket()->Read(buf, buf_len, callback);
199 } 222 }
200 223
201 // Write is called by the transport layer. This can only be done if the 224 // Write is called by the transport layer. This can only be done if the
202 // SOCKS handshake is complete. 225 // SOCKS handshake is complete.
203 int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, 226 int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
204 OldCompletionCallback* callback) { 227 OldCompletionCallback* callback) {
205 DCHECK(completed_handshake_); 228 DCHECK(completed_handshake_);
206 DCHECK_EQ(STATE_NONE, next_state_); 229 DCHECK_EQ(STATE_NONE, next_state_);
207 DCHECK(!user_callback_); 230 DCHECK(!old_user_callback_);
208 231
209 return transport_->socket()->Write(buf, buf_len, callback); 232 return transport_->socket()->Write(buf, buf_len, callback);
210 } 233 }
211 234
212 bool SOCKSClientSocket::SetReceiveBufferSize(int32 size) { 235 bool SOCKSClientSocket::SetReceiveBufferSize(int32 size) {
213 return transport_->socket()->SetReceiveBufferSize(size); 236 return transport_->socket()->SetReceiveBufferSize(size);
214 } 237 }
215 238
216 bool SOCKSClientSocket::SetSendBufferSize(int32 size) { 239 bool SOCKSClientSocket::SetSendBufferSize(int32 size) {
217 return transport_->socket()->SetSendBufferSize(size); 240 return transport_->socket()->SetSendBufferSize(size);
218 } 241 }
219 242
220 void SOCKSClientSocket::DoCallback(int result) { 243 void SOCKSClientSocket::DoCallback(int result) {
221 DCHECK_NE(ERR_IO_PENDING, result); 244 DCHECK_NE(ERR_IO_PENDING, result);
222 DCHECK(user_callback_); 245 DCHECK(old_user_callback_ || !user_callback_.is_null());
223 246
224 // Since Run() may result in Read being called, 247 // Since Run() may result in Read being called,
225 // clear user_callback_ up front. 248 // clear user_callback_ up front.
226 OldCompletionCallback* c = user_callback_; 249 if (old_user_callback_) {
227 user_callback_ = NULL; 250 OldCompletionCallback* c = old_user_callback_;
228 DVLOG(1) << "Finished setting up SOCKS handshake"; 251 old_user_callback_ = NULL;
229 c->Run(result); 252 DVLOG(1) << "Finished setting up SOCKS handshake";
253 c->Run(result);
254 } else {
255 CompletionCallback c = user_callback_;
256 user_callback_.Reset();
257 DVLOG(1) << "Finished setting up SOCKS handshake";
258 c.Run(result);
259 }
230 } 260 }
231 261
232 void SOCKSClientSocket::OnIOComplete(int result) { 262 void SOCKSClientSocket::OnIOComplete(int result) {
233 DCHECK_NE(STATE_NONE, next_state_); 263 DCHECK_NE(STATE_NONE, next_state_);
234 int rv = DoLoop(result); 264 int rv = DoLoop(result);
235 if (rv != ERR_IO_PENDING) { 265 if (rv != ERR_IO_PENDING) {
236 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); 266 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
237 DoCallback(rv); 267 DoCallback(rv);
238 } 268 }
239 } 269 }
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 461
432 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { 462 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const {
433 return transport_->socket()->GetPeerAddress(address); 463 return transport_->socket()->GetPeerAddress(address);
434 } 464 }
435 465
436 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { 466 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
437 return transport_->socket()->GetLocalAddress(address); 467 return transport_->socket()->GetLocalAddress(address);
438 } 468 }
439 469
440 } // namespace net 470 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/socks_client_socket.h ('k') | net/socket/ssl_client_socket_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698