| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 #include "net/socket/tcp_socket_win.h" | 6 #include "net/socket/tcp_socket_win.h" |
| 7 | 7 |
| 8 #include <mstcpip.h> | 8 #include <mstcpip.h> |
| 9 | 9 |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/stats_counters.h" | |
| 13 #include "base/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 15 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
| 16 #include "net/base/connection_type_histograms.h" | 15 #include "net/base/connection_type_histograms.h" |
| 17 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 19 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 21 #include "net/base/network_activity_monitor.h" | 20 #include "net/base/network_activity_monitor.h" |
| 22 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 int TCPSocketWin::Write(IOBuffer* buf, | 516 int TCPSocketWin::Write(IOBuffer* buf, |
| 518 int buf_len, | 517 int buf_len, |
| 519 const CompletionCallback& callback) { | 518 const CompletionCallback& callback) { |
| 520 DCHECK(CalledOnValidThread()); | 519 DCHECK(CalledOnValidThread()); |
| 521 DCHECK_NE(socket_, INVALID_SOCKET); | 520 DCHECK_NE(socket_, INVALID_SOCKET); |
| 522 DCHECK(!waiting_write_); | 521 DCHECK(!waiting_write_); |
| 523 CHECK(write_callback_.is_null()); | 522 CHECK(write_callback_.is_null()); |
| 524 DCHECK_GT(buf_len, 0); | 523 DCHECK_GT(buf_len, 0); |
| 525 DCHECK(!core_->write_iobuffer_.get()); | 524 DCHECK(!core_->write_iobuffer_.get()); |
| 526 | 525 |
| 527 base::StatsCounter writes("tcp.writes"); | |
| 528 writes.Increment(); | |
| 529 | |
| 530 WSABUF write_buffer; | 526 WSABUF write_buffer; |
| 531 write_buffer.len = buf_len; | 527 write_buffer.len = buf_len; |
| 532 write_buffer.buf = buf->data(); | 528 write_buffer.buf = buf->data(); |
| 533 | 529 |
| 534 // TODO(wtc): Remove the assertion after enough testing. | 530 // TODO(wtc): Remove the assertion after enough testing. |
| 535 AssertEventNotSignaled(core_->write_overlapped_.hEvent); | 531 AssertEventNotSignaled(core_->write_overlapped_.hEvent); |
| 536 DWORD num; | 532 DWORD num; |
| 537 int rv = WSASend(socket_, &write_buffer, 1, &num, 0, | 533 int rv = WSASend(socket_, &write_buffer, 1, &num, 0, |
| 538 &core_->write_overlapped_, NULL); | 534 &core_->write_overlapped_, NULL); |
| 539 if (rv == 0) { | 535 if (rv == 0) { |
| 540 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { | 536 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { |
| 541 rv = static_cast<int>(num); | 537 rv = static_cast<int>(num); |
| 542 if (rv > buf_len || rv < 0) { | 538 if (rv > buf_len || rv < 0) { |
| 543 // It seems that some winsock interceptors report that more was written | 539 // It seems that some winsock interceptors report that more was written |
| 544 // than was available. Treat this as an error. http://crbug.com/27870 | 540 // than was available. Treat this as an error. http://crbug.com/27870 |
| 545 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len | 541 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len |
| 546 << " bytes, but " << rv << " bytes reported."; | 542 << " bytes, but " << rv << " bytes reported."; |
| 547 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 543 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 548 } | 544 } |
| 549 base::StatsCounter write_bytes("tcp.write_bytes"); | |
| 550 write_bytes.Add(rv); | |
| 551 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 545 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 552 buf->data()); | 546 buf->data()); |
| 553 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv); | 547 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv); |
| 554 return rv; | 548 return rv; |
| 555 } | 549 } |
| 556 } else { | 550 } else { |
| 557 int os_error = WSAGetLastError(); | 551 int os_error = WSAGetLastError(); |
| 558 if (os_error != WSA_IO_PENDING) { | 552 if (os_error != WSA_IO_PENDING) { |
| 559 int net_error = MapSystemError(os_error); | 553 int net_error = MapSystemError(os_error); |
| 560 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 554 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 NetLog::IntegerCallback("os_error", os_error)); | 876 NetLog::IntegerCallback("os_error", os_error)); |
| 883 } else { | 877 } else { |
| 884 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT); | 878 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT); |
| 885 } | 879 } |
| 886 | 880 |
| 887 if (!logging_multiple_connect_attempts_) | 881 if (!logging_multiple_connect_attempts_) |
| 888 LogConnectEnd(result); | 882 LogConnectEnd(result); |
| 889 } | 883 } |
| 890 | 884 |
| 891 void TCPSocketWin::LogConnectBegin(const AddressList& addresses) { | 885 void TCPSocketWin::LogConnectBegin(const AddressList& addresses) { |
| 892 base::StatsCounter connects("tcp.connect"); | |
| 893 connects.Increment(); | |
| 894 | |
| 895 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, | 886 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, |
| 896 addresses.CreateNetLogCallback()); | 887 addresses.CreateNetLogCallback()); |
| 897 } | 888 } |
| 898 | 889 |
| 899 void TCPSocketWin::LogConnectEnd(int net_error) { | 890 void TCPSocketWin::LogConnectEnd(int net_error) { |
| 900 if (net_error == OK) | 891 if (net_error == OK) |
| 901 UpdateConnectionTypeHistograms(CONNECTION_ANY); | 892 UpdateConnectionTypeHistograms(CONNECTION_ANY); |
| 902 | 893 |
| 903 if (net_error != OK) { | 894 if (net_error != OK) { |
| 904 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, net_error); | 895 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, net_error); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 935 if (rv == SOCKET_ERROR) { | 926 if (rv == SOCKET_ERROR) { |
| 936 int os_error = WSAGetLastError(); | 927 int os_error = WSAGetLastError(); |
| 937 if (os_error != WSAEWOULDBLOCK) { | 928 if (os_error != WSAEWOULDBLOCK) { |
| 938 int net_error = MapSystemError(os_error); | 929 int net_error = MapSystemError(os_error); |
| 939 net_log_.AddEvent( | 930 net_log_.AddEvent( |
| 940 NetLog::TYPE_SOCKET_READ_ERROR, | 931 NetLog::TYPE_SOCKET_READ_ERROR, |
| 941 CreateNetLogSocketErrorCallback(net_error, os_error)); | 932 CreateNetLogSocketErrorCallback(net_error, os_error)); |
| 942 return net_error; | 933 return net_error; |
| 943 } | 934 } |
| 944 } else { | 935 } else { |
| 945 base::StatsCounter read_bytes("tcp.read_bytes"); | |
| 946 if (rv > 0) | |
| 947 read_bytes.Add(rv); | |
| 948 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 936 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
| 949 buf->data()); | 937 buf->data()); |
| 950 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv); | 938 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv); |
| 951 return rv; | 939 return rv; |
| 952 } | 940 } |
| 953 | 941 |
| 954 waiting_read_ = true; | 942 waiting_read_ = true; |
| 955 read_callback_ = callback; | 943 read_callback_ = callback; |
| 956 core_->read_iobuffer_ = buf; | 944 core_->read_iobuffer_ = buf; |
| 957 core_->read_buffer_length_ = buf_len; | 945 core_->read_buffer_length_ = buf_len; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 } else { | 1011 } else { |
| 1024 rv = static_cast<int>(num_bytes); | 1012 rv = static_cast<int>(num_bytes); |
| 1025 if (rv > core_->write_buffer_length_ || rv < 0) { | 1013 if (rv > core_->write_buffer_length_ || rv < 0) { |
| 1026 // It seems that some winsock interceptors report that more was written | 1014 // It seems that some winsock interceptors report that more was written |
| 1027 // than was available. Treat this as an error. http://crbug.com/27870 | 1015 // than was available. Treat this as an error. http://crbug.com/27870 |
| 1028 LOG(ERROR) << "Detected broken LSP: Asked to write " | 1016 LOG(ERROR) << "Detected broken LSP: Asked to write " |
| 1029 << core_->write_buffer_length_ << " bytes, but " << rv | 1017 << core_->write_buffer_length_ << " bytes, but " << rv |
| 1030 << " bytes reported."; | 1018 << " bytes reported."; |
| 1031 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 1019 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 1032 } else { | 1020 } else { |
| 1033 base::StatsCounter write_bytes("tcp.write_bytes"); | |
| 1034 write_bytes.Add(num_bytes); | |
| 1035 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 1021 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
| 1036 core_->write_iobuffer_->data()); | 1022 core_->write_iobuffer_->data()); |
| 1037 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(num_bytes); | 1023 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(num_bytes); |
| 1038 } | 1024 } |
| 1039 } | 1025 } |
| 1040 | 1026 |
| 1041 core_->write_iobuffer_ = NULL; | 1027 core_->write_iobuffer_ = NULL; |
| 1042 | 1028 |
| 1043 DCHECK_NE(rv, ERR_IO_PENDING); | 1029 DCHECK_NE(rv, ERR_IO_PENDING); |
| 1044 base::ResetAndReturn(&write_callback_).Run(rv); | 1030 base::ResetAndReturn(&write_callback_).Run(rv); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 core_->read_buffer_length_ = 0; | 1086 core_->read_buffer_length_ = 0; |
| 1101 | 1087 |
| 1102 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 1088 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 1103 tracked_objects::ScopedTracker tracking_profile4( | 1089 tracked_objects::ScopedTracker tracking_profile4( |
| 1104 FROM_HERE_WITH_EXPLICIT_FUNCTION("418183 TCPSocketWin::DidSignalRead4")); | 1090 FROM_HERE_WITH_EXPLICIT_FUNCTION("418183 TCPSocketWin::DidSignalRead4")); |
| 1105 DCHECK_NE(rv, ERR_IO_PENDING); | 1091 DCHECK_NE(rv, ERR_IO_PENDING); |
| 1106 base::ResetAndReturn(&read_callback_).Run(rv); | 1092 base::ResetAndReturn(&read_callback_).Run(rv); |
| 1107 } | 1093 } |
| 1108 | 1094 |
| 1109 } // namespace net | 1095 } // namespace net |
| OLD | NEW |