OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/socket/tcp_client_socket.h" | |
6 | |
7 #include "base/callback_helpers.h" | |
8 #include "base/logging.h" | |
9 #include "base/profiler/scoped_tracker.h" | |
10 #include "net/base/io_buffer.h" | |
11 #include "net/base/ip_endpoint.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "net/base/net_util.h" | |
14 | |
15 namespace net { | |
16 | |
17 TCPClientSocket::TCPClientSocket(const AddressList& addresses, | |
18 net::NetLog* net_log, | |
19 const net::NetLog::Source& source) | |
20 : socket_(new TCPSocket(net_log, source)), | |
21 addresses_(addresses), | |
22 current_address_index_(-1), | |
23 next_connect_state_(CONNECT_STATE_NONE), | |
24 previously_disconnected_(false) { | |
25 } | |
26 | |
27 TCPClientSocket::TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, | |
28 const IPEndPoint& peer_address) | |
29 : socket_(connected_socket.Pass()), | |
30 addresses_(AddressList(peer_address)), | |
31 current_address_index_(0), | |
32 next_connect_state_(CONNECT_STATE_NONE), | |
33 previously_disconnected_(false) { | |
34 DCHECK(socket_); | |
35 | |
36 socket_->SetDefaultOptionsForClient(); | |
37 use_history_.set_was_ever_connected(); | |
38 } | |
39 | |
40 TCPClientSocket::~TCPClientSocket() { | |
41 } | |
42 | |
43 int TCPClientSocket::Bind(const IPEndPoint& address) { | |
44 if (current_address_index_ >= 0 || bind_address_) { | |
45 // Cannot bind the socket if we are already connected or connecting. | |
46 NOTREACHED(); | |
47 return ERR_UNEXPECTED; | |
48 } | |
49 | |
50 int result = OK; | |
51 if (!socket_->IsValid()) { | |
52 result = OpenSocket(address.GetFamily()); | |
53 if (result != OK) | |
54 return result; | |
55 } | |
56 | |
57 result = socket_->Bind(address); | |
58 if (result != OK) | |
59 return result; | |
60 | |
61 bind_address_.reset(new IPEndPoint(address)); | |
62 return OK; | |
63 } | |
64 | |
65 int TCPClientSocket::Connect(const CompletionCallback& callback) { | |
66 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
67 tracked_objects::ScopedTracker tracking_profile( | |
68 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPClientSocket::Connect")); | |
69 | |
70 DCHECK(!callback.is_null()); | |
71 | |
72 // If connecting or already connected, then just return OK. | |
73 if (socket_->IsValid() && current_address_index_ >= 0) | |
74 return OK; | |
75 | |
76 socket_->StartLoggingMultipleConnectAttempts(addresses_); | |
77 | |
78 // We will try to connect to each address in addresses_. Start with the | |
79 // first one in the list. | |
80 next_connect_state_ = CONNECT_STATE_CONNECT; | |
81 current_address_index_ = 0; | |
82 | |
83 int rv = DoConnectLoop(OK); | |
84 if (rv == ERR_IO_PENDING) { | |
85 connect_callback_ = callback; | |
86 } else { | |
87 socket_->EndLoggingMultipleConnectAttempts(rv); | |
88 } | |
89 | |
90 return rv; | |
91 } | |
92 | |
93 int TCPClientSocket::DoConnectLoop(int result) { | |
94 DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE); | |
95 | |
96 int rv = result; | |
97 do { | |
98 ConnectState state = next_connect_state_; | |
99 next_connect_state_ = CONNECT_STATE_NONE; | |
100 switch (state) { | |
101 case CONNECT_STATE_CONNECT: | |
102 DCHECK_EQ(OK, rv); | |
103 rv = DoConnect(); | |
104 break; | |
105 case CONNECT_STATE_CONNECT_COMPLETE: | |
106 rv = DoConnectComplete(rv); | |
107 break; | |
108 default: | |
109 NOTREACHED() << "bad state " << state; | |
110 rv = ERR_UNEXPECTED; | |
111 break; | |
112 } | |
113 } while (rv != ERR_IO_PENDING && next_connect_state_ != CONNECT_STATE_NONE); | |
114 | |
115 return rv; | |
116 } | |
117 | |
118 int TCPClientSocket::DoConnect() { | |
119 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
120 tracked_objects::ScopedTracker tracking_profile1( | |
121 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPClientSocket::DoConnect1")); | |
122 | |
123 DCHECK_GE(current_address_index_, 0); | |
124 DCHECK_LT(current_address_index_, static_cast<int>(addresses_.size())); | |
125 | |
126 const IPEndPoint& endpoint = addresses_[current_address_index_]; | |
127 | |
128 if (previously_disconnected_) { | |
129 use_history_.Reset(); | |
130 previously_disconnected_ = false; | |
131 } | |
132 | |
133 next_connect_state_ = CONNECT_STATE_CONNECT_COMPLETE; | |
134 | |
135 if (socket_->IsValid()) { | |
136 DCHECK(bind_address_); | |
137 } else { | |
138 int result = OpenSocket(endpoint.GetFamily()); | |
139 if (result != OK) | |
140 return result; | |
141 | |
142 if (bind_address_) { | |
143 result = socket_->Bind(*bind_address_); | |
144 if (result != OK) { | |
145 socket_->Close(); | |
146 return result; | |
147 } | |
148 } | |
149 } | |
150 | |
151 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
152 tracked_objects::ScopedTracker tracking_profile2( | |
153 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPClientSocket::DoConnect2")); | |
154 | |
155 // |socket_| is owned by this class and the callback won't be run once | |
156 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. | |
157 return socket_->Connect(endpoint, | |
158 base::Bind(&TCPClientSocket::DidCompleteConnect, | |
159 base::Unretained(this))); | |
160 } | |
161 | |
162 int TCPClientSocket::DoConnectComplete(int result) { | |
163 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
164 tracked_objects::ScopedTracker tracking_profile( | |
165 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
166 "436634 TCPClientSocket::DoConnectComplete")); | |
167 | |
168 if (result == OK) { | |
169 use_history_.set_was_ever_connected(); | |
170 return OK; // Done! | |
171 } | |
172 | |
173 // Close whatever partially connected socket we currently have. | |
174 DoDisconnect(); | |
175 | |
176 // Try to fall back to the next address in the list. | |
177 if (current_address_index_ + 1 < static_cast<int>(addresses_.size())) { | |
178 next_connect_state_ = CONNECT_STATE_CONNECT; | |
179 ++current_address_index_; | |
180 return OK; | |
181 } | |
182 | |
183 // Otherwise there is nothing to fall back to, so give up. | |
184 return result; | |
185 } | |
186 | |
187 void TCPClientSocket::Disconnect() { | |
188 DoDisconnect(); | |
189 current_address_index_ = -1; | |
190 bind_address_.reset(); | |
191 } | |
192 | |
193 void TCPClientSocket::DoDisconnect() { | |
194 // If connecting or already connected, record that the socket has been | |
195 // disconnected. | |
196 previously_disconnected_ = socket_->IsValid() && current_address_index_ >= 0; | |
197 socket_->Close(); | |
198 } | |
199 | |
200 bool TCPClientSocket::IsConnected() const { | |
201 return socket_->IsConnected(); | |
202 } | |
203 | |
204 bool TCPClientSocket::IsConnectedAndIdle() const { | |
205 return socket_->IsConnectedAndIdle(); | |
206 } | |
207 | |
208 int TCPClientSocket::GetPeerAddress(IPEndPoint* address) const { | |
209 return socket_->GetPeerAddress(address); | |
210 } | |
211 | |
212 int TCPClientSocket::GetLocalAddress(IPEndPoint* address) const { | |
213 DCHECK(address); | |
214 | |
215 if (!socket_->IsValid()) { | |
216 if (bind_address_) { | |
217 *address = *bind_address_; | |
218 return OK; | |
219 } | |
220 return ERR_SOCKET_NOT_CONNECTED; | |
221 } | |
222 | |
223 return socket_->GetLocalAddress(address); | |
224 } | |
225 | |
226 const BoundNetLog& TCPClientSocket::NetLog() const { | |
227 return socket_->net_log(); | |
228 } | |
229 | |
230 void TCPClientSocket::SetSubresourceSpeculation() { | |
231 use_history_.set_subresource_speculation(); | |
232 } | |
233 | |
234 void TCPClientSocket::SetOmniboxSpeculation() { | |
235 use_history_.set_omnibox_speculation(); | |
236 } | |
237 | |
238 bool TCPClientSocket::WasEverUsed() const { | |
239 return use_history_.was_used_to_convey_data(); | |
240 } | |
241 | |
242 bool TCPClientSocket::UsingTCPFastOpen() const { | |
243 return socket_->UsingTCPFastOpen(); | |
244 } | |
245 | |
246 void TCPClientSocket::EnableTCPFastOpenIfSupported() { | |
247 socket_->EnableTCPFastOpenIfSupported(); | |
248 } | |
249 | |
250 bool TCPClientSocket::WasNpnNegotiated() const { | |
251 return false; | |
252 } | |
253 | |
254 NextProto TCPClientSocket::GetNegotiatedProtocol() const { | |
255 return kProtoUnknown; | |
256 } | |
257 | |
258 bool TCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | |
259 return false; | |
260 } | |
261 | |
262 int TCPClientSocket::Read(IOBuffer* buf, | |
263 int buf_len, | |
264 const CompletionCallback& callback) { | |
265 DCHECK(!callback.is_null()); | |
266 | |
267 // |socket_| is owned by this class and the callback won't be run once | |
268 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. | |
269 CompletionCallback read_callback = base::Bind( | |
270 &TCPClientSocket::DidCompleteReadWrite, base::Unretained(this), callback); | |
271 int result = socket_->Read(buf, buf_len, read_callback); | |
272 if (result > 0) | |
273 use_history_.set_was_used_to_convey_data(); | |
274 | |
275 return result; | |
276 } | |
277 | |
278 int TCPClientSocket::Write(IOBuffer* buf, | |
279 int buf_len, | |
280 const CompletionCallback& callback) { | |
281 DCHECK(!callback.is_null()); | |
282 | |
283 // |socket_| is owned by this class and the callback won't be run once | |
284 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. | |
285 CompletionCallback write_callback = base::Bind( | |
286 &TCPClientSocket::DidCompleteReadWrite, base::Unretained(this), callback); | |
287 int result = socket_->Write(buf, buf_len, write_callback); | |
288 if (result > 0) | |
289 use_history_.set_was_used_to_convey_data(); | |
290 | |
291 return result; | |
292 } | |
293 | |
294 int TCPClientSocket::SetReceiveBufferSize(int32 size) { | |
295 return socket_->SetReceiveBufferSize(size); | |
296 } | |
297 | |
298 int TCPClientSocket::SetSendBufferSize(int32 size) { | |
299 return socket_->SetSendBufferSize(size); | |
300 } | |
301 | |
302 bool TCPClientSocket::SetKeepAlive(bool enable, int delay) { | |
303 return socket_->SetKeepAlive(enable, delay); | |
304 } | |
305 | |
306 bool TCPClientSocket::SetNoDelay(bool no_delay) { | |
307 return socket_->SetNoDelay(no_delay); | |
308 } | |
309 | |
310 void TCPClientSocket::DidCompleteConnect(int result) { | |
311 DCHECK_EQ(next_connect_state_, CONNECT_STATE_CONNECT_COMPLETE); | |
312 DCHECK_NE(result, ERR_IO_PENDING); | |
313 DCHECK(!connect_callback_.is_null()); | |
314 | |
315 result = DoConnectLoop(result); | |
316 if (result != ERR_IO_PENDING) { | |
317 socket_->EndLoggingMultipleConnectAttempts(result); | |
318 base::ResetAndReturn(&connect_callback_).Run(result); | |
319 } | |
320 } | |
321 | |
322 void TCPClientSocket::DidCompleteReadWrite(const CompletionCallback& callback, | |
323 int result) { | |
324 if (result > 0) | |
325 use_history_.set_was_used_to_convey_data(); | |
326 | |
327 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | |
328 tracked_objects::ScopedTracker tracking_profile( | |
329 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
330 "418183 TCPClientSocket::DidCompleteReadWrite")); | |
331 callback.Run(result); | |
332 } | |
333 | |
334 int TCPClientSocket::OpenSocket(AddressFamily family) { | |
335 DCHECK(!socket_->IsValid()); | |
336 | |
337 int result = socket_->Open(family); | |
338 if (result != OK) | |
339 return result; | |
340 | |
341 socket_->SetDefaultOptionsForClient(); | |
342 | |
343 return OK; | |
344 } | |
345 | |
346 } // namespace net | |
OLD | NEW |