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