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) {
347 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
348 source
.ToEventParametersCallback());
352 TCPClientSocketWin::~TCPClientSocketWin() {
354 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
357 int TCPClientSocketWin::AdoptSocket(SOCKET socket
) {
358 DCHECK_EQ(socket_
, INVALID_SOCKET
);
360 int error
= SetupSocket(socket
);
362 return MapSystemError(error
);
365 SetNonBlocking(socket_
);
367 core_
= new Core(this);
368 current_address_index_
= 0;
369 use_history_
.set_was_ever_connected();
374 int TCPClientSocketWin::Bind(const IPEndPoint
& address
) {
375 if (current_address_index_
>= 0 || bind_address_
.get()) {
376 // Cannot bind the socket if we are already connected or connecting.
377 return ERR_UNEXPECTED
;
380 SockaddrStorage storage
;
381 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
382 return ERR_INVALID_ARGUMENT
;
384 // Create |bound_socket_| and try to bind it to |address|.
385 int error
= CreateSocket(address
.GetSockAddrFamily(), &bound_socket_
);
387 return MapSystemError(error
);
389 if (bind(bound_socket_
, storage
.addr
, storage
.addr_len
)) {
391 if (closesocket(bound_socket_
) < 0)
392 PLOG(ERROR
) << "closesocket";
393 bound_socket_
= INVALID_SOCKET
;
394 return MapSystemError(error
);
397 bind_address_
.reset(new IPEndPoint(address
));
403 int TCPClientSocketWin::Connect(const CompletionCallback
& callback
) {
404 DCHECK(CalledOnValidThread());
406 // If already connected, then just return OK.
407 if (socket_
!= INVALID_SOCKET
)
410 base::StatsCounter
connects("tcp.connect");
411 connects
.Increment();
413 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT
,
414 addresses_
.CreateNetLogCallback());
416 // We will try to connect to each address in addresses_. Start with the
417 // first one in the list.
418 next_connect_state_
= CONNECT_STATE_CONNECT
;
419 current_address_index_
= 0;
421 int rv
= DoConnectLoop(OK
);
422 if (rv
== ERR_IO_PENDING
) {
423 // Synchronous operation not supported.
424 DCHECK(!callback
.is_null());
425 // TODO(ajwong): Is setting read_callback_ the right thing to do here??
426 read_callback_
= callback
;
428 LogConnectCompletion(rv
);
434 int TCPClientSocketWin::DoConnectLoop(int result
) {
435 DCHECK_NE(next_connect_state_
, CONNECT_STATE_NONE
);
439 ConnectState state
= next_connect_state_
;
440 next_connect_state_
= CONNECT_STATE_NONE
;
442 case CONNECT_STATE_CONNECT
:
446 case CONNECT_STATE_CONNECT_COMPLETE
:
447 rv
= DoConnectComplete(rv
);
450 LOG(DFATAL
) << "bad state " << state
;
454 } while (rv
!= ERR_IO_PENDING
&& next_connect_state_
!= CONNECT_STATE_NONE
);
459 int TCPClientSocketWin::DoConnect() {
460 DCHECK_GE(current_address_index_
, 0);
461 DCHECK_LT(current_address_index_
, static_cast<int>(addresses_
.size()));
462 DCHECK_EQ(0, connect_os_error_
);
464 const IPEndPoint
& endpoint
= addresses_
[current_address_index_
];
466 if (previously_disconnected_
) {
467 use_history_
.Reset();
468 previously_disconnected_
= false;
471 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
472 CreateNetLogIPEndPointCallback(&endpoint
));
474 next_connect_state_
= CONNECT_STATE_CONNECT_COMPLETE
;
476 if (bound_socket_
!= INVALID_SOCKET
) {
477 DCHECK(bind_address_
.get());
478 socket_
= bound_socket_
;
479 bound_socket_
= INVALID_SOCKET
;
481 connect_os_error_
= CreateSocket(endpoint
.GetSockAddrFamily(), &socket_
);
482 if (connect_os_error_
!= 0)
483 return MapSystemError(connect_os_error_
);
485 if (bind_address_
.get()) {
486 SockaddrStorage storage
;
487 if (!bind_address_
->ToSockAddr(storage
.addr
, &storage
.addr_len
))
488 return ERR_INVALID_ARGUMENT
;
489 if (bind(socket_
, storage
.addr
, storage
.addr_len
))
490 return MapSystemError(errno
);
495 core_
= new Core(this);
496 // WSAEventSelect sets the socket to non-blocking mode as a side effect.
497 // Our connect() and recv() calls require that the socket be non-blocking.
498 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
, FD_CONNECT
);
500 SockaddrStorage storage
;
501 if (!endpoint
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
502 return ERR_INVALID_ARGUMENT
;
503 if (!connect(socket_
, storage
.addr
, storage
.addr_len
)) {
504 // Connected without waiting!
506 // The MSDN page for connect says:
507 // With a nonblocking socket, the connection attempt cannot be completed
508 // immediately. In this case, connect will return SOCKET_ERROR, and
509 // WSAGetLastError will return WSAEWOULDBLOCK.
510 // which implies that for a nonblocking socket, connect never returns 0.
511 // It's not documented whether the event object will be signaled or not
512 // if connect does return 0. So the code below is essentially dead code
513 // and we don't know if it's correct.
516 if (ResetEventIfSignaled(core_
->read_overlapped_
.hEvent
))
519 int os_error
= WSAGetLastError();
520 if (os_error
!= WSAEWOULDBLOCK
) {
521 LOG(ERROR
) << "connect failed: " << os_error
;
522 connect_os_error_
= os_error
;
523 return MapConnectError(os_error
);
527 core_
->WatchForRead();
528 return ERR_IO_PENDING
;
531 int TCPClientSocketWin::DoConnectComplete(int result
) {
532 // Log the end of this attempt (and any OS error it threw).
533 int os_error
= connect_os_error_
;
534 connect_os_error_
= 0;
536 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
537 NetLog::IntegerCallback("os_error", os_error
));
539 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
);
543 use_history_
.set_was_ever_connected();
547 // Close whatever partially connected socket we currently have.
550 // Try to fall back to the next address in the list.
551 if (current_address_index_
+ 1 < static_cast<int>(addresses_
.size())) {
552 next_connect_state_
= CONNECT_STATE_CONNECT
;
553 ++current_address_index_
;
557 // Otherwise there is nothing to fall back to, so give up.
561 void TCPClientSocketWin::Disconnect() {
562 DCHECK(CalledOnValidThread());
565 current_address_index_
= -1;
566 bind_address_
.reset();
569 void TCPClientSocketWin::DoDisconnect() {
570 DCHECK(CalledOnValidThread());
572 if (socket_
== INVALID_SOCKET
)
575 // Note: don't use CancelIo to cancel pending IO because it doesn't work
576 // when there is a Winsock layered service provider.
578 // In most socket implementations, closing a socket results in a graceful
579 // connection shutdown, but in Winsock we have to call shutdown explicitly.
580 // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure"
581 // at http://msdn.microsoft.com/en-us/library/ms738547.aspx
582 shutdown(socket_
, SD_SEND
);
584 // This cancels any pending IO.
585 closesocket(socket_
);
586 socket_
= INVALID_SOCKET
;
588 if (waiting_connect()) {
589 // We closed the socket, so this notification will never come.
590 // From MSDN' WSAEventSelect documentation:
591 // "Closing a socket with closesocket also cancels the association and
592 // selection of network events specified in WSAEventSelect for the socket".
596 waiting_read_
= false;
597 waiting_write_
= false;
602 previously_disconnected_
= true;
605 bool TCPClientSocketWin::IsConnected() const {
606 DCHECK(CalledOnValidThread());
608 if (socket_
== INVALID_SOCKET
|| waiting_connect())
614 // Check if connection is alive.
616 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
619 if (rv
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
625 bool TCPClientSocketWin::IsConnectedAndIdle() const {
626 DCHECK(CalledOnValidThread());
628 if (socket_
== INVALID_SOCKET
|| waiting_connect())
634 // Check if connection is alive and we haven't received any data
637 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
640 if (WSAGetLastError() != WSAEWOULDBLOCK
)
646 int TCPClientSocketWin::GetPeerAddress(IPEndPoint
* address
) const {
647 DCHECK(CalledOnValidThread());
650 return ERR_SOCKET_NOT_CONNECTED
;
651 *address
= addresses_
[current_address_index_
];
655 int TCPClientSocketWin::GetLocalAddress(IPEndPoint
* address
) const {
656 DCHECK(CalledOnValidThread());
658 if (socket_
== INVALID_SOCKET
) {
659 if (bind_address_
.get()) {
660 *address
= *bind_address_
;
663 return ERR_SOCKET_NOT_CONNECTED
;
666 struct sockaddr_storage addr_storage
;
667 socklen_t addr_len
= sizeof(addr_storage
);
668 struct sockaddr
* addr
= reinterpret_cast<struct sockaddr
*>(&addr_storage
);
669 if (getsockname(socket_
, addr
, &addr_len
))
670 return MapSystemError(WSAGetLastError());
671 if (!address
->FromSockAddr(addr
, addr_len
))
676 void TCPClientSocketWin::SetSubresourceSpeculation() {
677 use_history_
.set_subresource_speculation();
680 void TCPClientSocketWin::SetOmniboxSpeculation() {
681 use_history_
.set_omnibox_speculation();
684 bool TCPClientSocketWin::WasEverUsed() const {
685 return use_history_
.was_used_to_convey_data();
688 bool TCPClientSocketWin::UsingTCPFastOpen() const {
689 // Not supported on windows.
693 bool TCPClientSocketWin::WasNpnNegotiated() const {
697 NextProto
TCPClientSocketWin::GetNegotiatedProtocol() const {
698 return kProtoUnknown
;
701 bool TCPClientSocketWin::GetSSLInfo(SSLInfo
* ssl_info
) {
705 int TCPClientSocketWin::Read(IOBuffer
* buf
,
707 const CompletionCallback
& callback
) {
708 DCHECK(CalledOnValidThread());
709 DCHECK_NE(socket_
, INVALID_SOCKET
);
710 DCHECK(!waiting_read_
);
711 DCHECK(read_callback_
.is_null());
712 DCHECK(!core_
->read_iobuffer_
);
714 return DoRead(buf
, buf_len
, callback
);
717 int TCPClientSocketWin::Write(IOBuffer
* buf
,
719 const CompletionCallback
& callback
) {
720 DCHECK(CalledOnValidThread());
721 DCHECK_NE(socket_
, INVALID_SOCKET
);
722 DCHECK(!waiting_write_
);
723 DCHECK(write_callback_
.is_null());
724 DCHECK_GT(buf_len
, 0);
725 DCHECK(!core_
->write_iobuffer_
);
727 base::StatsCounter
writes("tcp.writes");
731 write_buffer
.len
= buf_len
;
732 write_buffer
.buf
= buf
->data();
734 // TODO(wtc): Remove the assertion after enough testing.
735 AssertEventNotSignaled(core_
->write_overlapped_
.hEvent
);
737 int rv
= WSASend(socket_
, &write_buffer
, 1, &num
, 0,
738 &core_
->write_overlapped_
, NULL
);
740 if (ResetEventIfSignaled(core_
->write_overlapped_
.hEvent
)) {
741 rv
= static_cast<int>(num
);
742 if (rv
> buf_len
|| rv
< 0) {
743 // It seems that some winsock interceptors report that more was written
744 // than was available. Treat this as an error. http://crbug.com/27870
745 LOG(ERROR
) << "Detected broken LSP: Asked to write " << buf_len
746 << " bytes, but " << rv
<< " bytes reported.";
747 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
749 base::StatsCounter
write_bytes("tcp.write_bytes");
752 use_history_
.set_was_used_to_convey_data();
753 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, rv
,
758 int os_error
= WSAGetLastError();
759 if (os_error
!= WSA_IO_PENDING
) {
760 int net_error
= MapSystemError(os_error
);
761 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
762 CreateNetLogSocketErrorCallback(net_error
, os_error
));
766 waiting_write_
= true;
767 write_callback_
= callback
;
768 core_
->write_iobuffer_
= buf
;
769 core_
->write_buffer_length_
= buf_len
;
770 core_
->WatchForWrite();
771 return ERR_IO_PENDING
;
774 bool TCPClientSocketWin::SetReceiveBufferSize(int32 size
) {
775 DCHECK(CalledOnValidThread());
776 return SetSocketReceiveBufferSize(socket_
, size
);
779 bool TCPClientSocketWin::SetSendBufferSize(int32 size
) {
780 DCHECK(CalledOnValidThread());
781 return SetSocketSendBufferSize(socket_
, size
);
784 bool TCPClientSocketWin::SetKeepAlive(bool enable
, int delay
) {
785 return SetTCPKeepAlive(socket_
, enable
, delay
);
788 bool TCPClientSocketWin::SetNoDelay(bool no_delay
) {
789 return DisableNagle(socket_
, no_delay
);
792 void TCPClientSocketWin::DisableOverlappedReads() {
793 g_disable_overlapped_reads
= true;
796 void TCPClientSocketWin::LogConnectCompletion(int net_error
) {
798 UpdateConnectionTypeHistograms(CONNECTION_ANY
);
800 if (net_error
!= OK
) {
801 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, net_error
);
805 struct sockaddr_storage source_address
;
806 socklen_t addrlen
= sizeof(source_address
);
807 int rv
= getsockname(
808 socket_
, reinterpret_cast<struct sockaddr
*>(&source_address
), &addrlen
);
810 LOG(ERROR
) << "getsockname() [rv: " << rv
811 << "] error: " << WSAGetLastError();
813 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, rv
);
818 NetLog::TYPE_TCP_CONNECT
,
819 CreateNetLogSourceAddressCallback(
820 reinterpret_cast<const struct sockaddr
*>(&source_address
),
821 sizeof(source_address
)));
824 int TCPClientSocketWin::DoRead(IOBuffer
* buf
, int buf_len
,
825 const CompletionCallback
& callback
) {
826 if (core_
->disable_overlapped_reads_
) {
827 if (!core_
->non_blocking_reads_initialized_
) {
828 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
,
830 core_
->non_blocking_reads_initialized_
= true;
832 int rv
= recv(socket_
, buf
->data(), buf_len
, 0);
833 if (rv
== SOCKET_ERROR
) {
834 int os_error
= WSAGetLastError();
835 if (os_error
!= WSAEWOULDBLOCK
) {
836 int net_error
= MapSystemError(os_error
);
837 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
838 CreateNetLogSocketErrorCallback(net_error
, os_error
));
842 base::StatsCounter
read_bytes("tcp.read_bytes");
844 use_history_
.set_was_used_to_convey_data();
847 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, rv
,
852 buf_len
= core_
->ThrottleReadSize(buf_len
);
855 read_buffer
.len
= buf_len
;
856 read_buffer
.buf
= buf
->data();
858 // TODO(wtc): Remove the assertion after enough testing.
859 AssertEventNotSignaled(core_
->read_overlapped_
.hEvent
);
862 int rv
= WSARecv(socket_
, &read_buffer
, 1, &num
, &flags
,
863 &core_
->read_overlapped_
, NULL
);
865 if (ResetEventIfSignaled(core_
->read_overlapped_
.hEvent
)) {
866 base::StatsCounter
read_bytes("tcp.read_bytes");
868 use_history_
.set_was_used_to_convey_data();
871 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, num
,
873 return static_cast<int>(num
);
876 int os_error
= WSAGetLastError();
877 if (os_error
!= WSA_IO_PENDING
) {
878 int net_error
= MapSystemError(os_error
);
879 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
880 CreateNetLogSocketErrorCallback(net_error
, os_error
));
886 waiting_read_
= true;
887 read_callback_
= callback
;
888 core_
->read_iobuffer_
= buf
;
889 core_
->read_buffer_length_
= buf_len
;
890 core_
->WatchForRead();
891 return ERR_IO_PENDING
;
894 void TCPClientSocketWin::DoReadCallback(int rv
) {
895 DCHECK_NE(rv
, ERR_IO_PENDING
);
896 DCHECK(!read_callback_
.is_null());
898 // Since Run may result in Read being called, clear read_callback_ up front.
899 CompletionCallback c
= read_callback_
;
900 read_callback_
.Reset();
904 void TCPClientSocketWin::DoWriteCallback(int rv
) {
905 DCHECK_NE(rv
, ERR_IO_PENDING
);
906 DCHECK(!write_callback_
.is_null());
908 // since Run may result in Write being called, clear write_callback_ up front.
909 CompletionCallback c
= write_callback_
;
910 write_callback_
.Reset();
914 void TCPClientSocketWin::DidCompleteConnect() {
915 DCHECK_EQ(next_connect_state_
, CONNECT_STATE_CONNECT_COMPLETE
);
918 WSANETWORKEVENTS events
;
919 int rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
,
922 if (rv
== SOCKET_ERROR
) {
924 os_error
= WSAGetLastError();
925 result
= MapSystemError(os_error
);
926 } else if (events
.lNetworkEvents
& FD_CONNECT
) {
927 os_error
= events
.iErrorCode
[FD_CONNECT_BIT
];
928 result
= MapConnectError(os_error
);
931 result
= ERR_UNEXPECTED
;
934 connect_os_error_
= os_error
;
935 rv
= DoConnectLoop(result
);
936 if (rv
!= ERR_IO_PENDING
) {
937 LogConnectCompletion(rv
);
942 void TCPClientSocketWin::DidCompleteRead() {
943 DCHECK(waiting_read_
);
944 DWORD num_bytes
, flags
;
945 BOOL ok
= WSAGetOverlappedResult(socket_
, &core_
->read_overlapped_
,
946 &num_bytes
, FALSE
, &flags
);
947 waiting_read_
= false;
950 base::StatsCounter
read_bytes("tcp.read_bytes");
951 read_bytes
.Add(num_bytes
);
953 use_history_
.set_was_used_to_convey_data();
954 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
,
955 num_bytes
, core_
->read_iobuffer_
->data());
956 rv
= static_cast<int>(num_bytes
);
958 int os_error
= WSAGetLastError();
959 rv
= MapSystemError(os_error
);
960 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
961 CreateNetLogSocketErrorCallback(rv
, os_error
));
963 WSAResetEvent(core_
->read_overlapped_
.hEvent
);
964 core_
->read_iobuffer_
= NULL
;
965 core_
->read_buffer_length_
= 0;
969 void TCPClientSocketWin::DidCompleteWrite() {
970 DCHECK(waiting_write_
);
972 DWORD num_bytes
, flags
;
973 BOOL ok
= WSAGetOverlappedResult(socket_
, &core_
->write_overlapped_
,
974 &num_bytes
, FALSE
, &flags
);
975 WSAResetEvent(core_
->write_overlapped_
.hEvent
);
976 waiting_write_
= false;
979 int os_error
= WSAGetLastError();
980 rv
= MapSystemError(os_error
);
981 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
982 CreateNetLogSocketErrorCallback(rv
, os_error
));
984 rv
= static_cast<int>(num_bytes
);
985 if (rv
> core_
->write_buffer_length_
|| rv
< 0) {
986 // It seems that some winsock interceptors report that more was written
987 // than was available. Treat this as an error. http://crbug.com/27870
988 LOG(ERROR
) << "Detected broken LSP: Asked to write "
989 << core_
->write_buffer_length_
<< " bytes, but " << rv
990 << " bytes reported.";
991 rv
= ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
993 base::StatsCounter
write_bytes("tcp.write_bytes");
994 write_bytes
.Add(num_bytes
);
996 use_history_
.set_was_used_to_convey_data();
997 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, num_bytes
,
998 core_
->write_iobuffer_
->data());
1001 core_
->write_iobuffer_
= NULL
;
1002 DoWriteCallback(rv
);
1005 void TCPClientSocketWin::DidSignalRead() {
1006 DCHECK(waiting_read_
);
1008 WSANETWORKEVENTS network_events
;
1009 int rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
,
1011 if (rv
== SOCKET_ERROR
) {
1012 os_error
= WSAGetLastError();
1013 rv
= MapSystemError(os_error
);
1014 } else if (network_events
.lNetworkEvents
) {
1015 DCHECK_EQ(network_events
.lNetworkEvents
& ~(FD_READ
| FD_CLOSE
), 0);
1016 // If network_events.lNetworkEvents is FD_CLOSE and
1017 // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful
1018 // connection closure. It is tempting to directly set rv to 0 in
1019 // this case, but the MSDN pages for WSAEventSelect and
1020 // WSAAsyncSelect recommend we still call DoRead():
1021 // FD_CLOSE should only be posted after all data is read from a
1022 // socket, but an application should check for remaining data upon
1023 // receipt of FD_CLOSE to avoid any possibility of losing data.
1025 // If network_events.iErrorCode[FD_READ_BIT] or
1026 // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call
1027 // DoRead() because recv() reports a more accurate error code
1028 // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was
1030 rv
= DoRead(core_
->read_iobuffer_
, core_
->read_buffer_length_
,
1032 if (rv
== ERR_IO_PENDING
)
1035 // This may happen because Read() may succeed synchronously and
1036 // consume all the received data without resetting the event object.
1037 core_
->WatchForRead();
1040 waiting_read_
= false;
1041 core_
->read_iobuffer_
= NULL
;
1042 core_
->read_buffer_length_
= 0;