| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_client_socket_win.h" | 5 #include "net/socket/tcp_client_socket_win.h" |
| 6 | 6 |
| 7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 // The TCPClientSocketWin is going away. | 182 // The TCPClientSocketWin is going away. |
| 183 void Detach() { socket_ = NULL; } | 183 void Detach() { socket_ = NULL; } |
| 184 | 184 |
| 185 // The separate OVERLAPPED variables for asynchronous operation. | 185 // The separate OVERLAPPED variables for asynchronous operation. |
| 186 // |read_overlapped_| is used for both Connect() and Read(). | 186 // |read_overlapped_| is used for both Connect() and Read(). |
| 187 // |write_overlapped_| is only used for Write(); | 187 // |write_overlapped_| is only used for Write(); |
| 188 OVERLAPPED read_overlapped_; | 188 OVERLAPPED read_overlapped_; |
| 189 OVERLAPPED write_overlapped_; | 189 OVERLAPPED write_overlapped_; |
| 190 | 190 |
| 191 // The buffers used in Read() and Write(). | 191 // The buffers used in Read() and Write(). |
| 192 WSABUF read_buffer_; | |
| 193 WSABUF write_buffer_; | |
| 194 scoped_refptr<IOBuffer> read_iobuffer_; | 192 scoped_refptr<IOBuffer> read_iobuffer_; |
| 195 scoped_refptr<IOBuffer> write_iobuffer_; | 193 scoped_refptr<IOBuffer> write_iobuffer_; |
| 196 int write_buffer_length_; | 194 int write_buffer_length_; |
| 197 | 195 |
| 198 // Throttle the read size based on our current slow start state. | 196 // Throttle the read size based on our current slow start state. |
| 199 // Returns the throttled read size. | 197 // Returns the throttled read size. |
| 200 int ThrottleReadSize(int size) { | 198 int ThrottleReadSize(int size) { |
| 201 if (slow_start_throttle_ < kMaxSlowStartThrottle) { | 199 if (slow_start_throttle_ < kMaxSlowStartThrottle) { |
| 202 size = std::min(size, slow_start_throttle_); | 200 size = std::min(size, slow_start_throttle_); |
| 203 slow_start_throttle_ *= 2; | 201 slow_start_throttle_ *= 2; |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 int buf_len, | 706 int buf_len, |
| 709 const CompletionCallback& callback) { | 707 const CompletionCallback& callback) { |
| 710 DCHECK(CalledOnValidThread()); | 708 DCHECK(CalledOnValidThread()); |
| 711 DCHECK_NE(socket_, INVALID_SOCKET); | 709 DCHECK_NE(socket_, INVALID_SOCKET); |
| 712 DCHECK(!waiting_read_); | 710 DCHECK(!waiting_read_); |
| 713 DCHECK(read_callback_.is_null()); | 711 DCHECK(read_callback_.is_null()); |
| 714 DCHECK(!core_->read_iobuffer_); | 712 DCHECK(!core_->read_iobuffer_); |
| 715 | 713 |
| 716 buf_len = core_->ThrottleReadSize(buf_len); | 714 buf_len = core_->ThrottleReadSize(buf_len); |
| 717 | 715 |
| 718 core_->read_buffer_.len = buf_len; | 716 WSABUF read_buffer; |
| 719 core_->read_buffer_.buf = buf->data(); | 717 read_buffer.len = buf_len; |
| 718 read_buffer.buf = buf->data(); |
| 720 | 719 |
| 721 // TODO(wtc): Remove the assertion after enough testing. | 720 // TODO(wtc): Remove the assertion after enough testing. |
| 722 AssertEventNotSignaled(core_->read_overlapped_.hEvent); | 721 AssertEventNotSignaled(core_->read_overlapped_.hEvent); |
| 723 DWORD num, flags = 0; | 722 DWORD num, flags = 0; |
| 724 int rv = WSARecv(socket_, &core_->read_buffer_, 1, &num, &flags, | 723 int rv = WSARecv(socket_, &read_buffer, 1, &num, &flags, |
| 725 &core_->read_overlapped_, NULL); | 724 &core_->read_overlapped_, NULL); |
| 726 if (rv == 0) { | 725 if (rv == 0) { |
| 727 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) { | 726 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) { |
| 728 base::StatsCounter read_bytes("tcp.read_bytes"); | 727 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 729 read_bytes.Add(num); | 728 read_bytes.Add(num); |
| 730 num_bytes_read_ += num; | 729 num_bytes_read_ += num; |
| 731 if (num > 0) | 730 if (num > 0) |
| 732 use_history_.set_was_used_to_convey_data(); | 731 use_history_.set_was_used_to_convey_data(); |
| 733 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, num, | 732 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, num, |
| 734 core_->read_buffer_.buf); | 733 buf->data()); |
| 735 return static_cast<int>(num); | 734 return static_cast<int>(num); |
| 736 } | 735 } |
| 737 } else { | 736 } else { |
| 738 int os_error = WSAGetLastError(); | 737 int os_error = WSAGetLastError(); |
| 739 if (os_error != WSA_IO_PENDING) { | 738 if (os_error != WSA_IO_PENDING) { |
| 740 int net_error = MapSystemError(os_error); | 739 int net_error = MapSystemError(os_error); |
| 741 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 740 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 742 CreateNetLogSocketErrorCallback(net_error, os_error)); | 741 CreateNetLogSocketErrorCallback(net_error, os_error)); |
| 743 return net_error; | 742 return net_error; |
| 744 } | 743 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 756 DCHECK(CalledOnValidThread()); | 755 DCHECK(CalledOnValidThread()); |
| 757 DCHECK_NE(socket_, INVALID_SOCKET); | 756 DCHECK_NE(socket_, INVALID_SOCKET); |
| 758 DCHECK(!waiting_write_); | 757 DCHECK(!waiting_write_); |
| 759 DCHECK(write_callback_.is_null()); | 758 DCHECK(write_callback_.is_null()); |
| 760 DCHECK_GT(buf_len, 0); | 759 DCHECK_GT(buf_len, 0); |
| 761 DCHECK(!core_->write_iobuffer_); | 760 DCHECK(!core_->write_iobuffer_); |
| 762 | 761 |
| 763 base::StatsCounter writes("tcp.writes"); | 762 base::StatsCounter writes("tcp.writes"); |
| 764 writes.Increment(); | 763 writes.Increment(); |
| 765 | 764 |
| 766 core_->write_buffer_.len = buf_len; | 765 WSABUF write_buffer; |
| 767 core_->write_buffer_.buf = buf->data(); | 766 write_buffer.len = buf_len; |
| 767 write_buffer.buf = buf->data(); |
| 768 core_->write_buffer_length_ = buf_len; | 768 core_->write_buffer_length_ = buf_len; |
| 769 | 769 |
| 770 // TODO(wtc): Remove the assertion after enough testing. | 770 // TODO(wtc): Remove the assertion after enough testing. |
| 771 AssertEventNotSignaled(core_->write_overlapped_.hEvent); | 771 AssertEventNotSignaled(core_->write_overlapped_.hEvent); |
| 772 DWORD num; | 772 DWORD num; |
| 773 int rv = WSASend(socket_, &core_->write_buffer_, 1, &num, 0, | 773 int rv = WSASend(socket_, &write_buffer, 1, &num, 0, |
| 774 &core_->write_overlapped_, NULL); | 774 &core_->write_overlapped_, NULL); |
| 775 if (rv == 0) { | 775 if (rv == 0) { |
| 776 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { | 776 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { |
| 777 rv = static_cast<int>(num); | 777 rv = static_cast<int>(num); |
| 778 if (rv > buf_len || rv < 0) { | 778 if (rv > buf_len || rv < 0) { |
| 779 // It seems that some winsock interceptors report that more was written | 779 // It seems that some winsock interceptors report that more was written |
| 780 // than was available. Treat this as an error. http://crbug.com/27870 | 780 // than was available. Treat this as an error. http://crbug.com/27870 |
| 781 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len | 781 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len |
| 782 << " bytes, but " << rv << " bytes reported."; | 782 << " bytes, but " << rv << " bytes reported."; |
| 783 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 783 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 784 } | 784 } |
| 785 base::StatsCounter write_bytes("tcp.write_bytes"); | 785 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 786 write_bytes.Add(rv); | 786 write_bytes.Add(rv); |
| 787 if (rv > 0) | 787 if (rv > 0) |
| 788 use_history_.set_was_used_to_convey_data(); | 788 use_history_.set_was_used_to_convey_data(); |
| 789 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 789 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 790 core_->write_buffer_.buf); | 790 buf->data()); |
| 791 return rv; | 791 return rv; |
| 792 } | 792 } |
| 793 } else { | 793 } else { |
| 794 int os_error = WSAGetLastError(); | 794 int os_error = WSAGetLastError(); |
| 795 if (os_error != WSA_IO_PENDING) { | 795 if (os_error != WSA_IO_PENDING) { |
| 796 int net_error = MapSystemError(os_error); | 796 int net_error = MapSystemError(os_error); |
| 797 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 797 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 798 CreateNetLogSocketErrorCallback(net_error, os_error)); | 798 CreateNetLogSocketErrorCallback(net_error, os_error)); |
| 799 return net_error; | 799 return net_error; |
| 800 } | 800 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 } | 900 } |
| 901 } | 901 } |
| 902 | 902 |
| 903 void TCPClientSocketWin::DidCompleteRead() { | 903 void TCPClientSocketWin::DidCompleteRead() { |
| 904 DCHECK(waiting_read_); | 904 DCHECK(waiting_read_); |
| 905 DWORD num_bytes, flags; | 905 DWORD num_bytes, flags; |
| 906 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, | 906 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, |
| 907 &num_bytes, FALSE, &flags); | 907 &num_bytes, FALSE, &flags); |
| 908 WSAResetEvent(core_->read_overlapped_.hEvent); | 908 WSAResetEvent(core_->read_overlapped_.hEvent); |
| 909 waiting_read_ = false; | 909 waiting_read_ = false; |
| 910 core_->read_iobuffer_ = NULL; | |
| 911 int rv; | 910 int rv; |
| 912 if (ok) { | 911 if (ok) { |
| 913 base::StatsCounter read_bytes("tcp.read_bytes"); | 912 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 914 read_bytes.Add(num_bytes); | 913 read_bytes.Add(num_bytes); |
| 915 num_bytes_read_ += num_bytes; | 914 num_bytes_read_ += num_bytes; |
| 916 if (num_bytes > 0) | 915 if (num_bytes > 0) |
| 917 use_history_.set_was_used_to_convey_data(); | 916 use_history_.set_was_used_to_convey_data(); |
| 918 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, | 917 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, |
| 919 num_bytes, core_->read_buffer_.buf); | 918 num_bytes, core_->read_iobuffer_->data()); |
| 920 rv = static_cast<int>(num_bytes); | 919 rv = static_cast<int>(num_bytes); |
| 921 } else { | 920 } else { |
| 922 int os_error = WSAGetLastError(); | 921 int os_error = WSAGetLastError(); |
| 923 rv = MapSystemError(os_error); | 922 rv = MapSystemError(os_error); |
| 924 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 923 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 925 CreateNetLogSocketErrorCallback(rv, os_error)); | 924 CreateNetLogSocketErrorCallback(rv, os_error)); |
| 926 } | 925 } |
| 926 core_->read_iobuffer_ = NULL; |
| 927 DoReadCallback(rv); | 927 DoReadCallback(rv); |
| 928 } | 928 } |
| 929 | 929 |
| 930 void TCPClientSocketWin::DidCompleteWrite() { | 930 void TCPClientSocketWin::DidCompleteWrite() { |
| 931 DCHECK(waiting_write_); | 931 DCHECK(waiting_write_); |
| 932 | 932 |
| 933 DWORD num_bytes, flags; | 933 DWORD num_bytes, flags; |
| 934 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | 934 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, |
| 935 &num_bytes, FALSE, &flags); | 935 &num_bytes, FALSE, &flags); |
| 936 WSAResetEvent(core_->write_overlapped_.hEvent); | 936 WSAResetEvent(core_->write_overlapped_.hEvent); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 949 LOG(ERROR) << "Detected broken LSP: Asked to write " | 949 LOG(ERROR) << "Detected broken LSP: Asked to write " |
| 950 << core_->write_buffer_length_ << " bytes, but " << rv | 950 << core_->write_buffer_length_ << " bytes, but " << rv |
| 951 << " bytes reported."; | 951 << " bytes reported."; |
| 952 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 952 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 953 } else { | 953 } else { |
| 954 base::StatsCounter write_bytes("tcp.write_bytes"); | 954 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 955 write_bytes.Add(num_bytes); | 955 write_bytes.Add(num_bytes); |
| 956 if (num_bytes > 0) | 956 if (num_bytes > 0) |
| 957 use_history_.set_was_used_to_convey_data(); | 957 use_history_.set_was_used_to_convey_data(); |
| 958 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 958 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
| 959 core_->write_buffer_.buf); | 959 core_->write_iobuffer_->data()); |
| 960 } | 960 } |
| 961 } | 961 } |
| 962 core_->write_iobuffer_ = NULL; | 962 core_->write_iobuffer_ = NULL; |
| 963 DoWriteCallback(rv); | 963 DoWriteCallback(rv); |
| 964 } | 964 } |
| 965 | 965 |
| 966 } // namespace net | 966 } // namespace net |
| OLD | NEW |