1 // Copyright (c) 2012 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.
5 #include "net/socket/tcp_client_socket_win.h"
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/metrics/stats_counters.h"
12 #include "base/string_util.h"
13 #include "base/win/object_watcher.h"
14 #include "base/win/windows_version.h"
15 #include "net/base/connection_type_histograms.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_log.h"
20 #include "net/base/net_util.h"
21 #include "net/base/network_change_notifier.h"
22 #include "net/base/winsock_init.h"
23 #include "net/base/winsock_util.h"
24 #include "net/socket/socket_net_log_params.h"
30 const int kTCPKeepAliveSeconds
= 45;
31 bool g_disable_overlapped_reads
= false;
33 bool SetSocketReceiveBufferSize(SOCKET socket
, int32 size
) {
34 int rv
= setsockopt(socket
, SOL_SOCKET
, SO_RCVBUF
,
35 reinterpret_cast<const char*>(&size
), sizeof(size
));
36 DCHECK(!rv
) << "Could not set socket receive buffer size: " << GetLastError();
40 bool SetSocketSendBufferSize(SOCKET socket
, int32 size
) {
41 int rv
= setsockopt(socket
, SOL_SOCKET
, SO_SNDBUF
,
42 reinterpret_cast<const char*>(&size
), sizeof(size
));
43 DCHECK(!rv
) << "Could not set socket send buffer size: " << GetLastError();
48 // The Nagle implementation on windows is governed by RFC 896. The idea
49 // behind Nagle is to reduce small packets on the network. When Nagle is
50 // enabled, if a partial packet has been sent, the TCP stack will disallow
51 // further *partial* packets until an ACK has been received from the other
52 // side. Good applications should always strive to send as much data as
53 // possible and avoid partial-packet sends. However, in most real world
54 // applications, there are edge cases where this does not happen, and two
55 // partial packets may be sent back to back. For a browser, it is NEVER
56 // a benefit to delay for an RTT before the second packet is sent.
58 // As a practical example in Chromium today, consider the case of a small
59 // POST. I have verified this:
60 // Client writes 649 bytes of header (partial packet #1)
61 // Client writes 50 bytes of POST data (partial packet #2)
62 // In the above example, with Nagle, a RTT delay is inserted between these
63 // two sends due to nagle. RTTs can easily be 100ms or more. The best
64 // fix is to make sure that for POSTing data, we write as much data as
65 // possible and minimize partial packets. We will fix that. But disabling
66 // Nagle also ensure we don't run into this delay in other edge cases.
68 // http://technet.microsoft.com/en-us/library/bb726981.aspx
69 bool DisableNagle(SOCKET socket
, bool disable
) {
70 BOOL val
= disable
? TRUE
: FALSE
;
71 int rv
= setsockopt(socket
, IPPROTO_TCP
, TCP_NODELAY
,
72 reinterpret_cast<const char*>(&val
),
74 DCHECK(!rv
) << "Could not disable nagle";
78 // Enable TCP Keep-Alive to prevent NAT routers from timing out TCP
79 // connections. See http://crbug.com/27400 for details.
80 bool SetTCPKeepAlive(SOCKET socket
, BOOL enable
, int delay_secs
) {
81 int delay
= delay_secs
* 1000;
82 struct tcp_keepalive keepalive_vals
= {
83 enable
? 1 : 0, // TCP keep-alive on.
84 delay
, // Delay seconds before sending first TCP keep-alive packet.
85 delay
, // Delay seconds between sending TCP keep-alive packets.
87 DWORD bytes_returned
= 0xABAB;
88 int rv
= WSAIoctl(socket
, SIO_KEEPALIVE_VALS
, &keepalive_vals
,
89 sizeof(keepalive_vals
), NULL
, 0,
90 &bytes_returned
, NULL
, NULL
);
91 DCHECK(!rv
) << "Could not enable TCP Keep-Alive for socket: " << socket
92 << " [error: " << WSAGetLastError() << "].";
94 // Disregard any failure in disabling nagle or enabling TCP Keep-Alive.
98 // Sets socket parameters. Returns the OS error code (or 0 on
100 int SetupSocket(SOCKET socket
) {
101 // Increase the socket buffer sizes from the default sizes for WinXP. In
102 // performance testing, there is substantial benefit by increasing from 8KB
105 // http://support.microsoft.com/kb/823764/EN-US
106 // On Vista, if we manually set these sizes, Vista turns off its receive
107 // window auto-tuning feature.
108 // http://blogs.msdn.com/wndp/archive/2006/05/05/Winhec-blog-tcpip-2.aspx
109 // Since Vista's auto-tune is better than any static value we can could set,
110 // only change these on pre-vista machines.
111 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
112 const int32 kSocketBufferSize
= 64 * 1024;
113 SetSocketReceiveBufferSize(socket
, kSocketBufferSize
);
114 SetSocketSendBufferSize(socket
, kSocketBufferSize
);
117 DisableNagle(socket
, true);
118 SetTCPKeepAlive(socket
, true, kTCPKeepAliveSeconds
);
122 // Creates a new socket and sets default parameters for it. Returns
123 // the OS error code (or 0 on success).
124 int CreateSocket(int family
, SOCKET
* socket
) {
125 *socket
= WSASocket(family
, SOCK_STREAM
, IPPROTO_TCP
, NULL
, 0,
126 WSA_FLAG_OVERLAPPED
);
127 if (*socket
== INVALID_SOCKET
) {
128 int os_error
= WSAGetLastError();
129 LOG(ERROR
) << "WSASocket failed: " << os_error
;
132 int error
= SetupSocket(*socket
);
134 if (closesocket(*socket
) < 0)
135 PLOG(ERROR
) << "closesocket";
136 *socket
= INVALID_SOCKET
;
142 int MapConnectError(int os_error
) {
144 // connect fails with WSAEACCES when Windows Firewall blocks the
147 return ERR_NETWORK_ACCESS_DENIED
;
149 return ERR_CONNECTION_TIMED_OUT
;
151 int net_error
= MapSystemError(os_error
);
152 if (net_error
== ERR_FAILED
)
153 return ERR_CONNECTION_FAILED
; // More specific than ERR_FAILED.
155 // Give a more specific error when the user is offline.
156 if (net_error
== ERR_ADDRESS_UNREACHABLE
&&
157 NetworkChangeNotifier::IsOffline()) {
158 return ERR_INTERNET_DISCONNECTED
;
168 //-----------------------------------------------------------------------------
170 // This class encapsulates all the state that has to be preserved as long as
171 // there is a network IO operation in progress. If the owner TCPClientSocketWin
172 // is destroyed while an operation is in progress, the Core is detached and it
173 // lives until the operation completes and the OS doesn't reference any resource
174 // declared on this class anymore.
175 class TCPClientSocketWin::Core
: public base::RefCounted
<Core
> {
177 explicit Core(TCPClientSocketWin
* socket
);
179 // Start watching for the end of a read or write operation.
181 void WatchForWrite();
183 // The TCPClientSocketWin is going away.
184 void Detach() { socket_
= NULL
; }
186 // Throttle the read size based on our current slow start state.
187 // Returns the throttled read size.
188 int ThrottleReadSize(int size
) {
189 if (slow_start_throttle_
< kMaxSlowStartThrottle
) {
190 size
= std::min(size
, slow_start_throttle_
);
191 slow_start_throttle_
*= 2;
196 // The separate OVERLAPPED variables for asynchronous operation.
197 // |read_overlapped_| is used for both Connect() and Read().
198 // |write_overlapped_| is only used for Write();
199 OVERLAPPED read_overlapped_
;
200 OVERLAPPED write_overlapped_
;
202 // The buffers used in Read() and Write().
203 scoped_refptr
<IOBuffer
> read_iobuffer_
;
204 scoped_refptr
<IOBuffer
> write_iobuffer_
;
205 int read_buffer_length_
;
206 int write_buffer_length_
;
208 // Remember the state of g_disable_overlapped_reads for the duration of the
209 // socket based on what it was when the socket was created.
210 bool disable_overlapped_reads_
;
211 bool non_blocking_reads_initialized_
;
214 friend class base::RefCounted
<Core
>;
216 class ReadDelegate
: public base::win::ObjectWatcher::Delegate
{
218 explicit ReadDelegate(Core
* core
) : core_(core
) {}
219 virtual ~ReadDelegate() {}
221 // base::ObjectWatcher::Delegate methods:
222 virtual void OnObjectSignaled(HANDLE object
);
228 class WriteDelegate
: public base::win::ObjectWatcher::Delegate
{
230 explicit WriteDelegate(Core
* core
) : core_(core
) {}
231 virtual ~WriteDelegate() {}
233 // base::ObjectWatcher::Delegate methods:
234 virtual void OnObjectSignaled(HANDLE object
);
242 // The socket that created this object.
243 TCPClientSocketWin
* socket_
;
245 // |reader_| handles the signals from |read_watcher_|.
246 ReadDelegate reader_
;
247 // |writer_| handles the signals from |write_watcher_|.
248 WriteDelegate writer_
;
250 // |read_watcher_| watches for events from Connect() and Read().
251 base::win::ObjectWatcher read_watcher_
;
252 // |write_watcher_| watches for events from Write();
253 base::win::ObjectWatcher write_watcher_
;
255 // When doing reads from the socket, we try to mirror TCP's slow start.
256 // We do this because otherwise the async IO subsystem artifically delays
257 // returning data to the application.
258 static const int kInitialSlowStartThrottle
= 1 * 1024;
259 static const int kMaxSlowStartThrottle
= 32 * kInitialSlowStartThrottle
;
260 int slow_start_throttle_
;
262 DISALLOW_COPY_AND_ASSIGN(Core
);
265 TCPClientSocketWin::Core::Core(
266 TCPClientSocketWin
* socket
)
267 : read_buffer_length_(0),
268 write_buffer_length_(0),
269 disable_overlapped_reads_(g_disable_overlapped_reads
),
270 non_blocking_reads_initialized_(false),
272 ALLOW_THIS_IN_INITIALIZER_LIST(reader_(this)),
273 ALLOW_THIS_IN_INITIALIZER_LIST(writer_(this)),
274 slow_start_throttle_(kInitialSlowStartThrottle
) {
275 memset(&read_overlapped_
, 0, sizeof(read_overlapped_
));
276 memset(&write_overlapped_
, 0, sizeof(write_overlapped_
));
278 read_overlapped_
.hEvent
= WSACreateEvent();
279 write_overlapped_
.hEvent
= WSACreateEvent();
282 TCPClientSocketWin::Core::~Core() {
283 // Make sure the message loop is not watching this object anymore.
284 read_watcher_
.StopWatching();
285 write_watcher_
.StopWatching();
287 WSACloseEvent(read_overlapped_
.hEvent
);
288 memset(&read_overlapped_
, 0xaf, sizeof(read_overlapped_
));
289 WSACloseEvent(write_overlapped_
.hEvent
);
290 memset(&write_overlapped_
, 0xaf, sizeof(write_overlapped_
));
293 void TCPClientSocketWin::Core::WatchForRead() {
294 // We grab an extra reference because there is an IO operation in progress.
295 // Balanced in ReadDelegate::OnObjectSignaled().
297 read_watcher_
.StartWatching(read_overlapped_
.hEvent
, &reader_
);
300 void TCPClientSocketWin::Core::WatchForWrite() {
301 // We grab an extra reference because there is an IO operation in progress.
302 // Balanced in WriteDelegate::OnObjectSignaled().
304 write_watcher_
.StartWatching(write_overlapped_
.hEvent
, &writer_
);
307 void TCPClientSocketWin::Core::ReadDelegate::OnObjectSignaled(
309 DCHECK_EQ(object
, core_
->read_overlapped_
.hEvent
);
310 if (core_
->socket_
) {
311 if (core_
->socket_
->waiting_connect()) {
312 core_
->socket_
->DidCompleteConnect();
313 } else if (core_
->disable_overlapped_reads_
) {
314 core_
->socket_
->DidSignalRead();
316 core_
->socket_
->DidCompleteRead();
323 void TCPClientSocketWin::Core::WriteDelegate::OnObjectSignaled(
325 DCHECK_EQ(object
, core_
->write_overlapped_
.hEvent
);
327 core_
->socket_
->DidCompleteWrite();
332 //-----------------------------------------------------------------------------
334 TCPClientSocketWin::TCPClientSocketWin(const AddressList
& addresses
,
335 net::NetLog
* net_log
,
336 const net::NetLog::Source
& source
)
337 : socket_(INVALID_SOCKET
),
338 bound_socket_(INVALID_SOCKET
),
339 addresses_(addresses
),
340 current_address_index_(-1),
341 waiting_read_(false),
342 waiting_write_(false),
343 next_connect_state_(CONNECT_STATE_NONE
),
344 connect_os_error_(0),
345 net_log_(BoundNetLog::Make(net_log
, NetLog::SOURCE_SOCKET
)),
346 previously_disconnected_(false),
348 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
349 source
.ToEventParametersCallback());
353 TCPClientSocketWin::~TCPClientSocketWin() {
355 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
358 int TCPClientSocketWin::AdoptSocket(SOCKET socket
) {
359 DCHECK_EQ(socket_
, INVALID_SOCKET
);
361 int error
= SetupSocket(socket
);
363 return MapSystemError(error
);
366 SetNonBlocking(socket_
);
368 core_
= new Core(this);
369 current_address_index_
= 0;
370 use_history_
.set_was_ever_connected();
375 int TCPClientSocketWin::Bind(const IPEndPoint
& address
) {
376 if (current_address_index_
>= 0 || bind_address_
.get()) {
377 // Cannot bind the socket if we are already connected or connecting.
378 return ERR_UNEXPECTED
;
381 SockaddrStorage storage
;
382 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
383 return ERR_INVALID_ARGUMENT
;
385 // Create |bound_socket_| and try to bind it to |address|.
386 int error
= CreateSocket(address
.GetSockAddrFamily(), &bound_socket_
);
388 return MapSystemError(error
);
390 if (bind(bound_socket_
, storage
.addr
, storage
.addr_len
)) {
392 if (closesocket(bound_socket_
) < 0)
393 PLOG(ERROR
) << "closesocket";
394 bound_socket_
= INVALID_SOCKET
;
395 return MapSystemError(error
);
398 bind_address_
.reset(new IPEndPoint(address
));
404 int TCPClientSocketWin::Connect(const CompletionCallback
& callback
) {
405 DCHECK(CalledOnValidThread());
407 // If already connected, then just return OK.
408 if (socket_
!= INVALID_SOCKET
)
411 base::StatsCounter
connects("tcp.connect");
412 connects
.Increment();
414 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT
,
415 addresses_
.CreateNetLogCallback());
417 // We will try to connect to each address in addresses_. Start with the
418 // first one in the list.
419 next_connect_state_
= CONNECT_STATE_CONNECT
;
420 current_address_index_
= 0;
422 int rv
= DoConnectLoop(OK
);
423 if (rv
== ERR_IO_PENDING
) {
424 // Synchronous operation not supported.
425 DCHECK(!callback
.is_null());
426 // TODO(ajwong): Is setting read_callback_ the right thing to do here??
427 read_callback_
= callback
;
429 LogConnectCompletion(rv
);
435 int TCPClientSocketWin::DoConnectLoop(int result
) {
436 DCHECK_NE(next_connect_state_
, CONNECT_STATE_NONE
);
440 ConnectState state
= next_connect_state_
;
441 next_connect_state_
= CONNECT_STATE_NONE
;
443 case CONNECT_STATE_CONNECT
:
447 case CONNECT_STATE_CONNECT_COMPLETE
:
448 rv
= DoConnectComplete(rv
);
451 LOG(DFATAL
) << "bad state " << state
;
455 } while (rv
!= ERR_IO_PENDING
&& next_connect_state_
!= CONNECT_STATE_NONE
);
460 int TCPClientSocketWin::DoConnect() {
461 DCHECK_GE(current_address_index_
, 0);
462 DCHECK_LT(current_address_index_
, static_cast<int>(addresses_
.size()));
463 DCHECK_EQ(0, connect_os_error_
);
465 const IPEndPoint
& endpoint
= addresses_
[current_address_index_
];
467 if (previously_disconnected_
) {
468 use_history_
.Reset();
469 previously_disconnected_
= false;
472 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
473 CreateNetLogIPEndPointCallback(&endpoint
));
475 next_connect_state_
= CONNECT_STATE_CONNECT_COMPLETE
;
477 if (bound_socket_
!= INVALID_SOCKET
) {
478 DCHECK(bind_address_
.get());
479 socket_
= bound_socket_
;
480 bound_socket_
= INVALID_SOCKET
;
482 connect_os_error_
= CreateSocket(endpoint
.GetSockAddrFamily(), &socket_
);
483 if (connect_os_error_
!= 0)
484 return MapSystemError(connect_os_error_
);
486 if (bind_address_
.get()) {
487 SockaddrStorage storage
;
488 if (!bind_address_
->ToSockAddr(storage
.addr
, &storage
.addr_len
))
489 return ERR_INVALID_ARGUMENT
;
490 if (bind(socket_
, storage
.addr
, storage
.addr_len
))
491 return MapSystemError(errno
);
496 core_
= new Core(this);
497 // WSAEventSelect sets the socket to non-blocking mode as a side effect.
498 // Our connect() and recv() calls require that the socket be non-blocking.
499 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
, FD_CONNECT
);
501 SockaddrStorage storage
;
502 if (!endpoint
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
503 return ERR_INVALID_ARGUMENT
;
504 connect_start_time_
= base::TimeTicks::Now();
505 if (!connect(socket_
, storage
.addr
, storage
.addr_len
)) {
506 // Connected without waiting!
508 // The MSDN page for connect says:
509 // With a nonblocking socket, the connection attempt cannot be completed
510 // immediately. In this case, connect will return SOCKET_ERROR, and
511 // WSAGetLastError will return WSAEWOULDBLOCK.
512 // which implies that for a nonblocking socket, connect never returns 0.
513 // It's not documented whether the event object will be signaled or not
514 // if connect does return 0. So the code below is essentially dead code
515 // and we don't know if it's correct.
518 if (ResetEventIfSignaled(core_
->read_overlapped_
.hEvent
))
521 int os_error
= WSAGetLastError();
522 if (os_error
!= WSAEWOULDBLOCK
) {
523 LOG(ERROR
) << "connect failed: " << os_error
;
524 connect_os_error_
= os_error
;
525 return MapConnectError(os_error
);
529 core_
->WatchForRead();
530 return ERR_IO_PENDING
;
533 int TCPClientSocketWin::DoConnectComplete(int result
) {
534 // Log the end of this attempt (and any OS error it threw).
535 int os_error
= connect_os_error_
;
536 connect_os_error_
= 0;
538 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
539 NetLog::IntegerCallback("os_error", os_error
));
541 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
);
545 connect_time_micros_
= base::TimeTicks::Now() - connect_start_time_
;
546 use_history_
.set_was_ever_connected();
550 // Close whatever partially connected socket we currently have.
553 // Try to fall back to the next address in the list.
554 if (current_address_index_
+ 1 < static_cast<int>(addresses_
.size())) {
555 next_connect_state_
= CONNECT_STATE_CONNECT
;
556 ++current_address_index_
;
560 // Otherwise there is nothing to fall back to, so give up.
564 void TCPClientSocketWin::Disconnect() {
565 DCHECK(CalledOnValidThread());
568 current_address_index_
= -1;
569 bind_address_
.reset();
572 void TCPClientSocketWin::DoDisconnect() {
573 DCHECK(CalledOnValidThread());
575 if (socket_
== INVALID_SOCKET
)
578 // Note: don't use CancelIo to cancel pending IO because it doesn't work
579 // when there is a Winsock layered service provider.
581 // In most socket implementations, closing a socket results in a graceful
582 // connection shutdown, but in Winsock we have to call shutdown explicitly.
583 // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure"
584 // at http://msdn.microsoft.com/en-us/library/ms738547.aspx
585 shutdown(socket_
, SD_SEND
);
587 // This cancels any pending IO.
588 closesocket(socket_
);
589 socket_
= INVALID_SOCKET
;
591 if (waiting_connect()) {
592 // We closed the socket, so this notification will never come.
593 // From MSDN' WSAEventSelect documentation:
594 // "Closing a socket with closesocket also cancels the association and
595 // selection of network events specified in WSAEventSelect for the socket".
599 waiting_read_
= false;
600 waiting_write_
= false;
605 previously_disconnected_
= true;
608 bool TCPClientSocketWin::IsConnected() const {
609 DCHECK(CalledOnValidThread());
611 if (socket_
== INVALID_SOCKET
|| waiting_connect())
617 // Check if connection is alive.
619 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
622 if (rv
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
628 bool TCPClientSocketWin::IsConnectedAndIdle() const {
629 DCHECK(CalledOnValidThread());
631 if (socket_
== INVALID_SOCKET
|| waiting_connect())
637 // Check if connection is alive and we haven't received any data
640 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
643 if (WSAGetLastError() != WSAEWOULDBLOCK
)
649 int TCPClientSocketWin::GetPeerAddress(IPEndPoint
* address
) const {
650 DCHECK(CalledOnValidThread());
653 return ERR_SOCKET_NOT_CONNECTED
;
654 *address
= addresses_
[current_address_index_
];
658 int TCPClientSocketWin::GetLocalAddress(IPEndPoint
* address
) const {
659 DCHECK(CalledOnValidThread());
661 if (socket_
== INVALID_SOCKET
) {
662 if (bind_address_
.get()) {
663 *address
= *bind_address_
;
666 return ERR_SOCKET_NOT_CONNECTED
;
669 struct sockaddr_storage addr_storage
;
670 socklen_t addr_len
= sizeof(addr_storage
);
671 struct sockaddr
* addr
= reinterpret_cast<struct sockaddr
*>(&addr_storage
);
672 if (getsockname(socket_
, addr
, &addr_len
))
673 return MapSystemError(WSAGetLastError());
674 if (!address
->FromSockAddr(addr
, addr_len
))
679 void TCPClientSocketWin::SetSubresourceSpeculation() {
680 use_history_
.set_subresource_speculation();
683 void TCPClientSocketWin::SetOmniboxSpeculation() {
684 use_history_
.set_omnibox_speculation();
687 bool TCPClientSocketWin::WasEverUsed() const {
688 return use_history_
.was_used_to_convey_data();
691 bool TCPClientSocketWin::UsingTCPFastOpen() const {
692 // Not supported on windows.
696 int64
TCPClientSocketWin::NumBytesRead() const {
697 return num_bytes_read_
;
700 base::TimeDelta
TCPClientSocketWin::GetConnectTimeMicros() const {
701 return connect_time_micros_
;
704 bool TCPClientSocketWin::WasNpnNegotiated() const {
708 NextProto
TCPClientSocketWin::GetNegotiatedProtocol() const {
709 return kProtoUnknown
;
712 bool TCPClientSocketWin::GetSSLInfo(SSLInfo
* ssl_info
) {
716 int TCPClientSocketWin::Read(IOBuffer
* buf
,
718 const CompletionCallback
& callback
) {
719 DCHECK(CalledOnValidThread());
720 DCHECK_NE(socket_
, INVALID_SOCKET
);
721 DCHECK(!waiting_read_
);
722 DCHECK(read_callback_
.is_null());
723 DCHECK(!core_
->read_iobuffer_
);
725 return DoRead(buf
, buf_len
, callback
);
728 int TCPClientSocketWin::Write(IOBuffer
* buf
,
730 const CompletionCallback
& callback
) {
731 DCHECK(CalledOnValidThread());
732 DCHECK_NE(socket_
, INVALID_SOCKET
);
733 DCHECK(!waiting_write_
);
734 DCHECK(write_callback_
.is_null());
735 DCHECK_GT(buf_len
, 0);
736 DCHECK(!core_
->write_iobuffer_
);
738 base::StatsCounter
writes("tcp.writes");
742 write_buffer
.len
= buf_len
;
743 write_buffer
.buf
= buf
->data();
745 // TODO(wtc): Remove the assertion after enough testing.
746 AssertEventNotSignaled(core_
->write_overlapped_
.hEvent
);
748 int rv
= WSASend(socket_
, &write_buffer
, 1, &num
, 0,
749 &core_
->write_overlapped_
, NULL
);
751 if (ResetEventIfSignaled(core_
->write_overlapped_
.hEvent
)) {
752 rv
= static_cast<int>(num
);
753 if (rv
> buf_len
|| rv
< 0) {
754 // It seems that some winsock interceptors report that more was written
755 // than was available. Treat this as an error. http://crbug.com/27870
756 LOG(ERROR
) << "Detected broken LSP: Asked to write " << buf_len
757 << " bytes, but " << rv
<< " bytes reported.";
758 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
760 base::StatsCounter
write_bytes("tcp.write_bytes");
763 use_history_
.set_was_used_to_convey_data();
764 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, rv
,
769 int os_error
= WSAGetLastError();
770 if (os_error
!= WSA_IO_PENDING
) {
771 int net_error
= MapSystemError(os_error
);
772 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
773 CreateNetLogSocketErrorCallback(net_error
, os_error
));
777 waiting_write_
= true;
778 write_callback_
= callback
;
779 core_
->write_iobuffer_
= buf
;
780 core_
->write_buffer_length_
= buf_len
;
781 core_
->WatchForWrite();
782 return ERR_IO_PENDING
;
785 bool TCPClientSocketWin::SetReceiveBufferSize(int32 size
) {
786 DCHECK(CalledOnValidThread());
787 return SetSocketReceiveBufferSize(socket_
, size
);
790 bool TCPClientSocketWin::SetSendBufferSize(int32 size
) {
791 DCHECK(CalledOnValidThread());
792 return SetSocketSendBufferSize(socket_
, size
);
795 bool TCPClientSocketWin::SetKeepAlive(bool enable
, int delay
) {
796 return SetTCPKeepAlive(socket_
, enable
, delay
);
799 bool TCPClientSocketWin::SetNoDelay(bool no_delay
) {
800 return DisableNagle(socket_
, no_delay
);
803 void TCPClientSocketWin::DisableOverlappedReads() {
804 g_disable_overlapped_reads
= true;
807 void TCPClientSocketWin::LogConnectCompletion(int net_error
) {
809 UpdateConnectionTypeHistograms(CONNECTION_ANY
);
811 if (net_error
!= OK
) {
812 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, net_error
);
816 struct sockaddr_storage source_address
;
817 socklen_t addrlen
= sizeof(source_address
);
818 int rv
= getsockname(
819 socket_
, reinterpret_cast<struct sockaddr
*>(&source_address
), &addrlen
);
821 LOG(ERROR
) << "getsockname() [rv: " << rv
822 << "] error: " << WSAGetLastError();
824 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, rv
);
829 NetLog::TYPE_TCP_CONNECT
,
830 CreateNetLogSourceAddressCallback(
831 reinterpret_cast<const struct sockaddr
*>(&source_address
),
832 sizeof(source_address
)));
835 int TCPClientSocketWin::DoRead(IOBuffer
* buf
, int buf_len
,
836 const CompletionCallback
& callback
) {
837 if (core_
->disable_overlapped_reads_
) {
838 if (!core_
->non_blocking_reads_initialized_
) {
839 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
,
841 core_
->non_blocking_reads_initialized_
= true;
843 int rv
= recv(socket_
, buf
->data(), buf_len
, 0);
844 if (rv
== SOCKET_ERROR
) {
845 int os_error
= WSAGetLastError();
846 if (os_error
!= WSAEWOULDBLOCK
) {
847 int net_error
= MapSystemError(os_error
);
848 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
849 CreateNetLogSocketErrorCallback(net_error
, os_error
));
853 base::StatsCounter
read_bytes("tcp.read_bytes");
855 use_history_
.set_was_used_to_convey_data();
857 num_bytes_read_
+= rv
;
859 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, rv
,
864 buf_len
= core_
->ThrottleReadSize(buf_len
);
867 read_buffer
.len
= buf_len
;
868 read_buffer
.buf
= buf
->data();
870 // TODO(wtc): Remove the assertion after enough testing.
871 AssertEventNotSignaled(core_
->read_overlapped_
.hEvent
);
874 int rv
= WSARecv(socket_
, &read_buffer
, 1, &num
, &flags
,
875 &core_
->read_overlapped_
, NULL
);
877 if (ResetEventIfSignaled(core_
->read_overlapped_
.hEvent
)) {
878 base::StatsCounter
read_bytes("tcp.read_bytes");
880 use_history_
.set_was_used_to_convey_data();
882 num_bytes_read_
+= num
;
884 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, num
,
886 return static_cast<int>(num
);
889 int os_error
= WSAGetLastError();
890 if (os_error
!= WSA_IO_PENDING
) {
891 int net_error
= MapSystemError(os_error
);
892 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
893 CreateNetLogSocketErrorCallback(net_error
, os_error
));
899 waiting_read_
= true;
900 read_callback_
= callback
;
901 core_
->read_iobuffer_
= buf
;
902 core_
->read_buffer_length_
= buf_len
;
903 core_
->WatchForRead();
904 return ERR_IO_PENDING
;
907 void TCPClientSocketWin::DoReadCallback(int rv
) {
908 DCHECK_NE(rv
, ERR_IO_PENDING
);
909 DCHECK(!read_callback_
.is_null());
911 // Since Run may result in Read being called, clear read_callback_ up front.
912 CompletionCallback c
= read_callback_
;
913 read_callback_
.Reset();
917 void TCPClientSocketWin::DoWriteCallback(int rv
) {
918 DCHECK_NE(rv
, ERR_IO_PENDING
);
919 DCHECK(!write_callback_
.is_null());
921 // since Run may result in Write being called, clear write_callback_ up front.
922 CompletionCallback c
= write_callback_
;
923 write_callback_
.Reset();
927 void TCPClientSocketWin::DidCompleteConnect() {
928 DCHECK_EQ(next_connect_state_
, CONNECT_STATE_CONNECT_COMPLETE
);
931 WSANETWORKEVENTS events
;
932 int rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
,
935 if (rv
== SOCKET_ERROR
) {
937 os_error
= WSAGetLastError();
938 result
= MapSystemError(os_error
);
939 } else if (events
.lNetworkEvents
& FD_CONNECT
) {
940 os_error
= events
.iErrorCode
[FD_CONNECT_BIT
];
941 result
= MapConnectError(os_error
);
944 result
= ERR_UNEXPECTED
;
947 connect_os_error_
= os_error
;
948 rv
= DoConnectLoop(result
);
949 if (rv
!= ERR_IO_PENDING
) {
950 LogConnectCompletion(rv
);
955 void TCPClientSocketWin::DidCompleteRead() {
956 DCHECK(waiting_read_
);
957 DWORD num_bytes
, flags
;
958 BOOL ok
= WSAGetOverlappedResult(socket_
, &core_
->read_overlapped_
,
959 &num_bytes
, FALSE
, &flags
);
960 WSAResetEvent(core_
->read_overlapped_
.hEvent
);
961 waiting_read_
= false;
964 base::StatsCounter
read_bytes("tcp.read_bytes");
965 read_bytes
.Add(num_bytes
);
966 num_bytes_read_
+= num_bytes
;
968 use_history_
.set_was_used_to_convey_data();
969 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
,
970 num_bytes
, core_
->read_iobuffer_
->data());
971 rv
= static_cast<int>(num_bytes
);
973 int os_error
= WSAGetLastError();
974 rv
= MapSystemError(os_error
);
975 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
976 CreateNetLogSocketErrorCallback(rv
, os_error
));
978 core_
->read_iobuffer_
= NULL
;
979 core_
->read_buffer_length_
= 0;
983 void TCPClientSocketWin::DidCompleteWrite() {
984 DCHECK(waiting_write_
);
986 DWORD num_bytes
, flags
;
987 BOOL ok
= WSAGetOverlappedResult(socket_
, &core_
->write_overlapped_
,
988 &num_bytes
, FALSE
, &flags
);
989 WSAResetEvent(core_
->write_overlapped_
.hEvent
);
990 waiting_write_
= false;
993 int os_error
= WSAGetLastError();
994 rv
= MapSystemError(os_error
);
995 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
996 CreateNetLogSocketErrorCallback(rv
, os_error
));
998 rv
= static_cast<int>(num_bytes
);
999 if (rv
> core_
->write_buffer_length_
|| rv
< 0) {
1000 // It seems that some winsock interceptors report that more was written
1001 // than was available. Treat this as an error. http://crbug.com/27870
1002 LOG(ERROR
) << "Detected broken LSP: Asked to write "
1003 << core_
->write_buffer_length_
<< " bytes, but " << rv
1004 << " bytes reported.";
1005 rv
= ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
1007 base::StatsCounter
write_bytes("tcp.write_bytes");
1008 write_bytes
.Add(num_bytes
);
1010 use_history_
.set_was_used_to_convey_data();
1011 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, num_bytes
,
1012 core_
->write_iobuffer_
->data());
1015 core_
->write_iobuffer_
= NULL
;
1016 DoWriteCallback(rv
);
1019 void TCPClientSocketWin::DidSignalRead() {
1020 DCHECK(waiting_read_
);
1022 WSANETWORKEVENTS network_events
;
1023 int rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
,
1025 if (rv
== SOCKET_ERROR
) {
1026 os_error
= WSAGetLastError();
1027 rv
= MapSystemError(os_error
);
1028 } else if (network_events
.lNetworkEvents
& FD_READ
) {
1029 rv
= DoRead(core_
->read_iobuffer_
, core_
->read_buffer_length_
,
1031 if (rv
== ERR_IO_PENDING
)
1033 } else if (network_events
.lNetworkEvents
& FD_CLOSE
) {
1034 if (network_events
.iErrorCode
[FD_CLOSE_BIT
]) {
1035 rv
= MapSystemError(network_events
.iErrorCode
[FD_CLOSE_BIT
]);
1036 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
1037 CreateNetLogSocketErrorCallback(rv
, os_error
));
1042 // This should not happen but I have seen cases where we will get
1043 // signaled but the network events flags are all clear (0).
1044 core_
->WatchForRead();
1047 waiting_read_
= false;
1048 core_
->read_iobuffer_
= NULL
;
1049 core_
->read_buffer_length_
= 0;