OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory_debug.h" | 9 #include "base/memory_debug.h" |
10 #include "base/stats_counters.h" | 10 #include "base/stats_counters.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // |read_overlapped_| is used for both Connect() and Read(). | 140 // |read_overlapped_| is used for both Connect() and Read(). |
141 // |write_overlapped_| is only used for Write(); | 141 // |write_overlapped_| is only used for Write(); |
142 OVERLAPPED read_overlapped_; | 142 OVERLAPPED read_overlapped_; |
143 OVERLAPPED write_overlapped_; | 143 OVERLAPPED write_overlapped_; |
144 | 144 |
145 // The buffers used in Read() and Write(). | 145 // The buffers used in Read() and Write(). |
146 WSABUF read_buffer_; | 146 WSABUF read_buffer_; |
147 WSABUF write_buffer_; | 147 WSABUF write_buffer_; |
148 scoped_refptr<IOBuffer> read_iobuffer_; | 148 scoped_refptr<IOBuffer> read_iobuffer_; |
149 scoped_refptr<IOBuffer> write_iobuffer_; | 149 scoped_refptr<IOBuffer> write_iobuffer_; |
150 // TODO(vandebo) remove when bug 27870 is resolved. | 150 int write_buffer_length_; |
151 size_t write_buffer_length_; | |
152 | 151 |
153 // Throttle the read size based on our current slow start state. | 152 // Throttle the read size based on our current slow start state. |
154 // Returns the throttled read size. | 153 // Returns the throttled read size. |
155 int ThrottleReadSize(int size) { | 154 int ThrottleReadSize(int size) { |
156 if (slow_start_throttle_ < kMaxSlowStartThrottle) { | 155 if (slow_start_throttle_ < kMaxSlowStartThrottle) { |
157 size = std::min(size, slow_start_throttle_); | 156 size = std::min(size, slow_start_throttle_); |
158 slow_start_throttle_ *= 2; | 157 slow_start_throttle_ *= 2; |
159 } | 158 } |
160 return size; | 159 return size; |
161 } | 160 } |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 DCHECK(!write_callback_); | 497 DCHECK(!write_callback_); |
499 //TODO(vandebo) change back to a DCHECK when 27870 is resolved | 498 //TODO(vandebo) change back to a DCHECK when 27870 is resolved |
500 CHECK(buf_len > 0); | 499 CHECK(buf_len > 0); |
501 DCHECK(!core_->write_iobuffer_); | 500 DCHECK(!core_->write_iobuffer_); |
502 | 501 |
503 static StatsCounter reads("tcp.writes"); | 502 static StatsCounter reads("tcp.writes"); |
504 reads.Increment(); | 503 reads.Increment(); |
505 | 504 |
506 core_->write_buffer_.len = buf_len; | 505 core_->write_buffer_.len = buf_len; |
507 core_->write_buffer_.buf = buf->data(); | 506 core_->write_buffer_.buf = buf->data(); |
508 core_->write_buffer_length_ = static_cast<size_t>(buf_len); | 507 core_->write_buffer_length_ = buf_len; |
509 | 508 |
510 TRACE_EVENT_BEGIN("socket.write", this, ""); | 509 TRACE_EVENT_BEGIN("socket.write", this, ""); |
511 // TODO(wtc): Remove the CHECK after enough testing. | 510 // TODO(wtc): Remove the CHECK after enough testing. |
512 CHECK( | 511 CHECK( |
513 WaitForSingleObject(core_->write_overlapped_.hEvent, 0) == WAIT_TIMEOUT); | 512 WaitForSingleObject(core_->write_overlapped_.hEvent, 0) == WAIT_TIMEOUT); |
514 DWORD num; | 513 DWORD num; |
515 int rv = WSASend(socket_, &core_->write_buffer_, 1, &num, 0, | 514 int rv = WSASend(socket_, &core_->write_buffer_, 1, &num, 0, |
516 &core_->write_overlapped_, NULL); | 515 &core_->write_overlapped_, NULL); |
517 if (rv == 0) { | 516 if (rv == 0) { |
518 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { | 517 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) { |
519 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num)); | 518 rv = static_cast<int>(num); |
| 519 if (rv > buf_len || rv < 0) { |
| 520 // It seems that some winsock interceptors report that more was written |
| 521 // than was available. Treat this as an error. http://crbug.com/27870 |
| 522 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len |
| 523 << " bytes, but " << rv << " bytes reported."; |
| 524 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 525 } |
| 526 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", rv)); |
520 static StatsCounter write_bytes("tcp.write_bytes"); | 527 static StatsCounter write_bytes("tcp.write_bytes"); |
521 write_bytes.Add(num); | 528 write_bytes.Add(rv); |
522 return static_cast<int>(num); | 529 return rv; |
523 } | 530 } |
524 } else { | 531 } else { |
525 int os_error = WSAGetLastError(); | 532 int os_error = WSAGetLastError(); |
526 if (os_error != WSA_IO_PENDING) | 533 if (os_error != WSA_IO_PENDING) |
527 return MapWinsockError(os_error); | 534 return MapWinsockError(os_error); |
528 } | 535 } |
529 core_->WatchForWrite(); | 536 core_->WatchForWrite(); |
530 waiting_write_ = true; | 537 waiting_write_ = true; |
531 write_callback_ = callback; | 538 write_callback_ = callback; |
532 core_->write_iobuffer_ = buf; | 539 core_->write_iobuffer_ = buf; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 | 696 |
690 void TCPClientSocketWin::DidCompleteWrite() { | 697 void TCPClientSocketWin::DidCompleteWrite() { |
691 DCHECK(waiting_write_); | 698 DCHECK(waiting_write_); |
692 | 699 |
693 DWORD num_bytes, flags; | 700 DWORD num_bytes, flags; |
694 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | 701 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, |
695 &num_bytes, FALSE, &flags); | 702 &num_bytes, FALSE, &flags); |
696 WSAResetEvent(core_->write_overlapped_.hEvent); | 703 WSAResetEvent(core_->write_overlapped_.hEvent); |
697 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num_bytes)); | 704 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num_bytes)); |
698 waiting_write_ = false; | 705 waiting_write_ = false; |
699 if (ok) { | 706 int rv; |
700 CHECK(num_bytes <= core_->write_buffer_length_) << | 707 if (!ok) { |
701 core_->write_buffer_length_; | 708 rv = MapWinsockError(WSAGetLastError()); |
| 709 } else { |
| 710 rv = static_cast<int>(num_bytes); |
| 711 if (rv > core_->write_buffer_length_ || rv < 0) { |
| 712 // It seems that some winsock interceptors report that more was written |
| 713 // than was available. Treat this as an error. http://crbug.com/27870 |
| 714 LOG(ERROR) << "Detected broken LSP: Asked to write " |
| 715 << core_->write_buffer_length_ << " bytes, but " << rv |
| 716 << " bytes reported."; |
| 717 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 718 } |
702 } | 719 } |
703 core_->write_iobuffer_ = NULL; | 720 core_->write_iobuffer_ = NULL; |
704 DoWriteCallback(ok ? num_bytes : MapWinsockError(WSAGetLastError())); | 721 DoWriteCallback(rv); |
705 } | 722 } |
706 | 723 |
707 } // namespace net | 724 } // namespace net |
OLD | NEW |