| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 HANDLE object) { | 265 HANDLE object) { |
| 266 DCHECK_EQ(object, core_->write_overlapped_.hEvent); | 266 DCHECK_EQ(object, core_->write_overlapped_.hEvent); |
| 267 if (core_->socket_) | 267 if (core_->socket_) |
| 268 core_->socket_->DidCompleteWrite(); | 268 core_->socket_->DidCompleteWrite(); |
| 269 | 269 |
| 270 core_->Release(); | 270 core_->Release(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 //----------------------------------------------------------------------------- | 273 //----------------------------------------------------------------------------- |
| 274 | 274 |
| 275 TCPClientSocketWin::TCPClientSocketWin(const AddressList& addresses) | 275 TCPClientSocketWin::TCPClientSocketWin(const AddressList& addresses, |
| 276 net::NetLog* net_log) |
| 276 : socket_(INVALID_SOCKET), | 277 : socket_(INVALID_SOCKET), |
| 277 addresses_(addresses), | 278 addresses_(addresses), |
| 278 current_ai_(addresses_.head()), | 279 current_ai_(addresses_.head()), |
| 279 waiting_connect_(false), | 280 waiting_connect_(false), |
| 280 waiting_read_(false), | 281 waiting_read_(false), |
| 281 waiting_write_(false), | 282 waiting_write_(false), |
| 282 read_callback_(NULL), | 283 read_callback_(NULL), |
| 283 write_callback_(NULL) { | 284 write_callback_(NULL), |
| 285 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 284 EnsureWinsockInit(); | 286 EnsureWinsockInit(); |
| 285 } | 287 } |
| 286 | 288 |
| 287 TCPClientSocketWin::~TCPClientSocketWin() { | 289 TCPClientSocketWin::~TCPClientSocketWin() { |
| 288 Disconnect(); | 290 Disconnect(); |
| 291 net_log_.AddEvent(NetLog::TYPE_TCP_SOCKET_DONE); |
| 289 } | 292 } |
| 290 | 293 |
| 291 int TCPClientSocketWin::Connect(CompletionCallback* callback, | 294 int TCPClientSocketWin::Connect(CompletionCallback* callback) { |
| 292 const BoundNetLog& net_log) { | |
| 293 // If already connected, then just return OK. | 295 // If already connected, then just return OK. |
| 294 if (socket_ != INVALID_SOCKET) | 296 if (socket_ != INVALID_SOCKET) |
| 295 return OK; | 297 return OK; |
| 296 | 298 |
| 297 DCHECK(!net_log_.net_log()); | |
| 298 | |
| 299 static StatsCounter connects("tcp.connect"); | 299 static StatsCounter connects("tcp.connect"); |
| 300 connects.Increment(); | 300 connects.Increment(); |
| 301 | 301 |
| 302 TRACE_EVENT_BEGIN("socket.connect", this, ""); | 302 TRACE_EVENT_BEGIN("socket.connect", this, ""); |
| 303 | 303 |
| 304 net_log.BeginEvent(NetLog::TYPE_TCP_CONNECT); | 304 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT); |
| 305 | 305 |
| 306 int rv = DoConnect(); | 306 int rv = DoConnect(); |
| 307 | 307 |
| 308 if (rv == ERR_IO_PENDING) { | 308 if (rv == ERR_IO_PENDING) { |
| 309 // Synchronous operation not supported. | 309 // Synchronous operation not supported. |
| 310 DCHECK(callback); | 310 DCHECK(callback); |
| 311 | 311 |
| 312 net_log_ = net_log; | |
| 313 waiting_connect_ = true; | 312 waiting_connect_ = true; |
| 314 read_callback_ = callback; | 313 read_callback_ = callback; |
| 315 } else { | 314 } else { |
| 316 TRACE_EVENT_END("socket.connect", this, ""); | 315 TRACE_EVENT_END("socket.connect", this, ""); |
| 317 net_log.EndEvent(NetLog::TYPE_TCP_CONNECT); | 316 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 318 if (rv == OK) | 317 if (rv == OK) |
| 319 UpdateConnectionTypeHistograms(CONNECTION_ANY); | 318 UpdateConnectionTypeHistograms(CONNECTION_ANY); |
| 320 } | 319 } |
| 321 | 320 |
| 322 return rv; | 321 return rv; |
| 323 } | 322 } |
| 324 | 323 |
| 325 int TCPClientSocketWin::DoConnect() { | 324 int TCPClientSocketWin::DoConnect() { |
| 326 const struct addrinfo* ai = current_ai_; | 325 const struct addrinfo* ai = current_ai_; |
| 327 DCHECK(ai); | 326 DCHECK(ai); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 | 470 |
| 472 // Because of how WSARecv fills memory when used asynchronously, Purify | 471 // Because of how WSARecv fills memory when used asynchronously, Purify |
| 473 // isn't able to detect that it's been initialized, so it scans for 0xcd | 472 // isn't able to detect that it's been initialized, so it scans for 0xcd |
| 474 // in the buffer and reports UMRs (uninitialized memory reads) for those | 473 // in the buffer and reports UMRs (uninitialized memory reads) for those |
| 475 // individual bytes. We override that in PURIFY builds to avoid the | 474 // individual bytes. We override that in PURIFY builds to avoid the |
| 476 // false error reports. | 475 // false error reports. |
| 477 // See bug 5297. | 476 // See bug 5297. |
| 478 base::MemoryDebug::MarkAsInitialized(core_->read_buffer_.buf, num); | 477 base::MemoryDebug::MarkAsInitialized(core_->read_buffer_.buf, num); |
| 479 static StatsCounter read_bytes("tcp.read_bytes"); | 478 static StatsCounter read_bytes("tcp.read_bytes"); |
| 480 read_bytes.Add(num); | 479 read_bytes.Add(num); |
| 480 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, num); |
| 481 return static_cast<int>(num); | 481 return static_cast<int>(num); |
| 482 } | 482 } |
| 483 } else { | 483 } else { |
| 484 int os_error = WSAGetLastError(); | 484 int os_error = WSAGetLastError(); |
| 485 if (os_error != WSA_IO_PENDING) | 485 if (os_error != WSA_IO_PENDING) |
| 486 return MapWinsockError(os_error); | 486 return MapWinsockError(os_error); |
| 487 } | 487 } |
| 488 core_->WatchForRead(); | 488 core_->WatchForRead(); |
| 489 waiting_read_ = true; | 489 waiting_read_ = true; |
| 490 read_callback_ = callback; | 490 read_callback_ = callback; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 521 if (rv > buf_len || rv < 0) { | 521 if (rv > buf_len || rv < 0) { |
| 522 // It seems that some winsock interceptors report that more was written | 522 // It seems that some winsock interceptors report that more was written |
| 523 // than was available. Treat this as an error. http://crbug.com/27870 | 523 // than was available. Treat this as an error. http://crbug.com/27870 |
| 524 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len | 524 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len |
| 525 << " bytes, but " << rv << " bytes reported."; | 525 << " bytes, but " << rv << " bytes reported."; |
| 526 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 526 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 527 } | 527 } |
| 528 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", rv)); | 528 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", rv)); |
| 529 static StatsCounter write_bytes("tcp.write_bytes"); | 529 static StatsCounter write_bytes("tcp.write_bytes"); |
| 530 write_bytes.Add(rv); | 530 write_bytes.Add(rv); |
| 531 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, rv); |
| 531 return rv; | 532 return rv; |
| 532 } | 533 } |
| 533 } else { | 534 } else { |
| 534 int os_error = WSAGetLastError(); | 535 int os_error = WSAGetLastError(); |
| 535 if (os_error != WSA_IO_PENDING) | 536 if (os_error != WSA_IO_PENDING) |
| 536 return MapWinsockError(os_error); | 537 return MapWinsockError(os_error); |
| 537 } | 538 } |
| 538 core_->WatchForWrite(); | 539 core_->WatchForWrite(); |
| 539 waiting_write_ = true; | 540 waiting_write_ = true; |
| 540 write_callback_ = callback; | 541 write_callback_ = callback; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 if (rv == SOCKET_ERROR) { | 653 if (rv == SOCKET_ERROR) { |
| 653 NOTREACHED(); | 654 NOTREACHED(); |
| 654 result = MapWinsockError(WSAGetLastError()); | 655 result = MapWinsockError(WSAGetLastError()); |
| 655 } else if (events.lNetworkEvents & FD_CONNECT) { | 656 } else if (events.lNetworkEvents & FD_CONNECT) { |
| 656 int os_error = events.iErrorCode[FD_CONNECT_BIT]; | 657 int os_error = events.iErrorCode[FD_CONNECT_BIT]; |
| 657 if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) { | 658 if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) { |
| 658 // Try using the next address. | 659 // Try using the next address. |
| 659 const struct addrinfo* next = current_ai_->ai_next; | 660 const struct addrinfo* next = current_ai_->ai_next; |
| 660 Disconnect(); | 661 Disconnect(); |
| 661 current_ai_ = next; | 662 current_ai_ = next; |
| 662 BoundNetLog net_log(net_log_); | |
| 663 net_log_ = BoundNetLog(); | |
| 664 TRACE_EVENT_END("socket.connect", this, ""); | 663 TRACE_EVENT_END("socket.connect", this, ""); |
| 665 net_log.EndEvent(NetLog::TYPE_TCP_CONNECT); | 664 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 666 result = Connect(read_callback_, net_log); | 665 result = Connect(read_callback_); |
| 667 } else { | 666 } else { |
| 668 result = MapConnectError(os_error); | 667 result = MapConnectError(os_error); |
| 669 TRACE_EVENT_END("socket.connect", this, ""); | 668 TRACE_EVENT_END("socket.connect", this, ""); |
| 670 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); | 669 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 671 net_log_ = BoundNetLog(); | |
| 672 } | 670 } |
| 673 } else { | 671 } else { |
| 674 NOTREACHED(); | 672 NOTREACHED(); |
| 675 result = ERR_UNEXPECTED; | 673 result = ERR_UNEXPECTED; |
| 676 TRACE_EVENT_END("socket.connect", this, ""); | 674 TRACE_EVENT_END("socket.connect", this, ""); |
| 677 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); | 675 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 678 net_log_ = BoundNetLog(); | |
| 679 } | 676 } |
| 680 | 677 |
| 681 if (result != ERR_IO_PENDING) { | 678 if (result != ERR_IO_PENDING) { |
| 682 if (result == OK) | 679 if (result == OK) |
| 683 UpdateConnectionTypeHistograms(CONNECTION_ANY); | 680 UpdateConnectionTypeHistograms(CONNECTION_ANY); |
| 684 DoReadCallback(result); | 681 DoReadCallback(result); |
| 685 } | 682 } |
| 686 } | 683 } |
| 687 | 684 |
| 688 void TCPClientSocketWin::DidCompleteRead() { | 685 void TCPClientSocketWin::DidCompleteRead() { |
| 689 DCHECK(waiting_read_); | 686 DCHECK(waiting_read_); |
| 690 DWORD num_bytes, flags; | 687 DWORD num_bytes, flags; |
| 691 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, | 688 BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_, |
| 692 &num_bytes, FALSE, &flags); | 689 &num_bytes, FALSE, &flags); |
| 693 WSAResetEvent(core_->read_overlapped_.hEvent); | 690 WSAResetEvent(core_->read_overlapped_.hEvent); |
| 694 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", num_bytes)); | 691 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", num_bytes)); |
| 695 waiting_read_ = false; | 692 waiting_read_ = false; |
| 696 core_->read_iobuffer_ = NULL; | 693 core_->read_iobuffer_ = NULL; |
| 694 if (ok) |
| 695 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_RECEIVED, num_bytes); |
| 697 DoReadCallback(ok ? num_bytes : MapWinsockError(WSAGetLastError())); | 696 DoReadCallback(ok ? num_bytes : MapWinsockError(WSAGetLastError())); |
| 698 } | 697 } |
| 699 | 698 |
| 700 void TCPClientSocketWin::DidCompleteWrite() { | 699 void TCPClientSocketWin::DidCompleteWrite() { |
| 701 DCHECK(waiting_write_); | 700 DCHECK(waiting_write_); |
| 702 | 701 |
| 703 DWORD num_bytes, flags; | 702 DWORD num_bytes, flags; |
| 704 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | 703 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, |
| 705 &num_bytes, FALSE, &flags); | 704 &num_bytes, FALSE, &flags); |
| 706 WSAResetEvent(core_->write_overlapped_.hEvent); | 705 WSAResetEvent(core_->write_overlapped_.hEvent); |
| 707 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num_bytes)); | 706 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num_bytes)); |
| 708 waiting_write_ = false; | 707 waiting_write_ = false; |
| 709 int rv; | 708 int rv; |
| 710 if (!ok) { | 709 if (!ok) { |
| 711 rv = MapWinsockError(WSAGetLastError()); | 710 rv = MapWinsockError(WSAGetLastError()); |
| 712 } else { | 711 } else { |
| 713 rv = static_cast<int>(num_bytes); | 712 rv = static_cast<int>(num_bytes); |
| 714 if (rv > core_->write_buffer_length_ || rv < 0) { | 713 if (rv > core_->write_buffer_length_ || rv < 0) { |
| 715 // It seems that some winsock interceptors report that more was written | 714 // It seems that some winsock interceptors report that more was written |
| 716 // than was available. Treat this as an error. http://crbug.com/27870 | 715 // than was available. Treat this as an error. http://crbug.com/27870 |
| 717 LOG(ERROR) << "Detected broken LSP: Asked to write " | 716 LOG(ERROR) << "Detected broken LSP: Asked to write " |
| 718 << core_->write_buffer_length_ << " bytes, but " << rv | 717 << core_->write_buffer_length_ << " bytes, but " << rv |
| 719 << " bytes reported."; | 718 << " bytes reported."; |
| 720 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; | 719 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES; |
| 720 } else { |
| 721 net_log_.AddEventWithInteger(NetLog::TYPE_SOCKET_BYTES_SENT, rv); |
| 721 } | 722 } |
| 722 } | 723 } |
| 723 core_->write_iobuffer_ = NULL; | 724 core_->write_iobuffer_ = NULL; |
| 724 DoWriteCallback(rv); | 725 DoWriteCallback(rv); |
| 725 } | 726 } |
| 726 | 727 |
| 727 } // namespace net | 728 } // namespace net |
| OLD | NEW |