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" |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 accept_socket_ = socket; | 407 accept_socket_ = socket; |
408 accept_address_ = address; | 408 accept_address_ = address; |
409 accept_callback_ = callback; | 409 accept_callback_ = callback; |
410 } | 410 } |
411 | 411 |
412 return result; | 412 return result; |
413 } | 413 } |
414 | 414 |
415 int TCPSocketWin::Connect(const IPEndPoint& address, | 415 int TCPSocketWin::Connect(const IPEndPoint& address, |
416 const CompletionCallback& callback) { | 416 const CompletionCallback& callback) { |
417 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
418 tracked_objects::ScopedTracker tracking_profile( | |
419 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::Connect")); | |
420 | |
421 DCHECK(CalledOnValidThread()); | 417 DCHECK(CalledOnValidThread()); |
422 DCHECK_NE(socket_, INVALID_SOCKET); | 418 DCHECK_NE(socket_, INVALID_SOCKET); |
423 DCHECK(!waiting_connect_); | 419 DCHECK(!waiting_connect_); |
424 | 420 |
425 // |peer_address_| and |core_| will be non-NULL if Connect() has been called. | 421 // |peer_address_| and |core_| will be non-NULL if Connect() has been called. |
426 // Unless Close() is called to reset the internal state, a second call to | 422 // Unless Close() is called to reset the internal state, a second call to |
427 // Connect() is not allowed. | 423 // Connect() is not allowed. |
428 // Please note that we enforce this even if the previous Connect() has | 424 // Please note that we enforce this even if the previous Connect() has |
429 // completed and failed. Although it is allowed to connect the same |socket_| | 425 // completed and failed. Although it is allowed to connect the same |socket_| |
430 // again after a connection attempt failed on Windows, it results in | 426 // again after a connection attempt failed on Windows, it results in |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 // have a chance to accept it. | 770 // have a chance to accept it. |
775 DCHECK(ev.lNetworkEvents == 0); | 771 DCHECK(ev.lNetworkEvents == 0); |
776 | 772 |
777 // Start watching the next FD_ACCEPT event. | 773 // Start watching the next FD_ACCEPT event. |
778 WSAEventSelect(socket_, accept_event_, FD_ACCEPT); | 774 WSAEventSelect(socket_, accept_event_, FD_ACCEPT); |
779 accept_watcher_.StartWatching(accept_event_, this); | 775 accept_watcher_.StartWatching(accept_event_, this); |
780 } | 776 } |
781 } | 777 } |
782 | 778 |
783 int TCPSocketWin::DoConnect() { | 779 int TCPSocketWin::DoConnect() { |
784 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
785 tracked_objects::ScopedTracker tracking_profile( | |
786 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect")); | |
787 | |
788 DCHECK_EQ(connect_os_error_, 0); | 780 DCHECK_EQ(connect_os_error_, 0); |
789 DCHECK(!core_.get()); | 781 DCHECK(!core_.get()); |
790 | 782 |
791 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, | 783 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, |
792 CreateNetLogIPEndPointCallback(peer_address_.get())); | 784 CreateNetLogIPEndPointCallback(peer_address_.get())); |
793 | 785 |
794 core_ = new Core(this); | 786 core_ = new Core(this); |
795 | 787 |
796 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
797 tracked_objects::ScopedTracker tracking_profile1( | |
798 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect1")); | |
799 | |
800 // WSAEventSelect sets the socket to non-blocking mode as a side effect. | 788 // WSAEventSelect sets the socket to non-blocking mode as a side effect. |
801 // Our connect() and recv() calls require that the socket be non-blocking. | 789 // Our connect() and recv() calls require that the socket be non-blocking. |
802 WSAEventSelect(socket_, core_->read_overlapped_.hEvent, FD_CONNECT); | 790 WSAEventSelect(socket_, core_->read_overlapped_.hEvent, FD_CONNECT); |
803 | 791 |
804 SockaddrStorage storage; | 792 SockaddrStorage storage; |
805 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len)) | 793 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len)) |
806 return ERR_ADDRESS_INVALID; | 794 return ERR_ADDRESS_INVALID; |
807 | 795 |
808 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | 796 int result; |
809 tracked_objects::ScopedTracker tracking_profile2( | 797 { |
810 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect2")); | 798 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed. |
| 799 tracked_objects::ScopedTracker tracking_profile( |
| 800 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 connect()")); |
| 801 result = connect(socket_, storage.addr, storage.addr_len); |
| 802 } |
811 | 803 |
812 if (!connect(socket_, storage.addr, storage.addr_len)) { | 804 if (!result) { |
813 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
814 tracked_objects::ScopedTracker tracking_profile3( | |
815 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect3")); | |
816 | |
817 // Connected without waiting! | 805 // Connected without waiting! |
818 // | 806 // |
819 // The MSDN page for connect says: | 807 // The MSDN page for connect says: |
820 // With a nonblocking socket, the connection attempt cannot be completed | 808 // With a nonblocking socket, the connection attempt cannot be completed |
821 // immediately. In this case, connect will return SOCKET_ERROR, and | 809 // immediately. In this case, connect will return SOCKET_ERROR, and |
822 // WSAGetLastError will return WSAEWOULDBLOCK. | 810 // WSAGetLastError will return WSAEWOULDBLOCK. |
823 // which implies that for a nonblocking socket, connect never returns 0. | 811 // which implies that for a nonblocking socket, connect never returns 0. |
824 // It's not documented whether the event object will be signaled or not | 812 // It's not documented whether the event object will be signaled or not |
825 // if connect does return 0. So the code below is essentially dead code | 813 // if connect does return 0. So the code below is essentially dead code |
826 // and we don't know if it's correct. | 814 // and we don't know if it's correct. |
827 NOTREACHED(); | 815 NOTREACHED(); |
828 | 816 |
829 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) | 817 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) |
830 return OK; | 818 return OK; |
831 } else { | 819 } else { |
832 int os_error = WSAGetLastError(); | 820 int os_error = WSAGetLastError(); |
833 | |
834 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | |
835 tracked_objects::ScopedTracker tracking_profile4( | |
836 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect4")); | |
837 | |
838 if (os_error != WSAEWOULDBLOCK) { | 821 if (os_error != WSAEWOULDBLOCK) { |
839 LOG(ERROR) << "connect failed: " << os_error; | 822 LOG(ERROR) << "connect failed: " << os_error; |
840 connect_os_error_ = os_error; | 823 connect_os_error_ = os_error; |
841 int rv = MapConnectError(os_error); | 824 int rv = MapConnectError(os_error); |
842 CHECK_NE(ERR_IO_PENDING, rv); | 825 CHECK_NE(ERR_IO_PENDING, rv); |
843 return rv; | 826 return rv; |
844 } | 827 } |
845 } | 828 } |
846 | 829 |
847 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed. | 830 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed. |
848 tracked_objects::ScopedTracker tracking_profile5( | 831 tracked_objects::ScopedTracker tracking_profile( |
849 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 TCPSocketWin::DoConnect5")); | 832 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 WatchForRead()")); |
850 | 833 |
851 core_->WatchForRead(); | 834 core_->WatchForRead(); |
852 return ERR_IO_PENDING; | 835 return ERR_IO_PENDING; |
853 } | 836 } |
854 | 837 |
855 void TCPSocketWin::DoConnectComplete(int result) { | 838 void TCPSocketWin::DoConnectComplete(int result) { |
856 // Log the end of this attempt (and any OS error it threw). | 839 // Log the end of this attempt (and any OS error it threw). |
857 int os_error = connect_os_error_; | 840 int os_error = connect_os_error_; |
858 connect_os_error_ = 0; | 841 connect_os_error_ = 0; |
859 if (result != OK) { | 842 if (result != OK) { |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 | 1037 |
1055 waiting_read_ = false; | 1038 waiting_read_ = false; |
1056 core_->read_iobuffer_ = NULL; | 1039 core_->read_iobuffer_ = NULL; |
1057 core_->read_buffer_length_ = 0; | 1040 core_->read_buffer_length_ = 0; |
1058 | 1041 |
1059 DCHECK_NE(rv, ERR_IO_PENDING); | 1042 DCHECK_NE(rv, ERR_IO_PENDING); |
1060 base::ResetAndReturn(&read_callback_).Run(rv); | 1043 base::ResetAndReturn(&read_callback_).Run(rv); |
1061 } | 1044 } |
1062 | 1045 |
1063 } // namespace net | 1046 } // namespace net |
OLD | NEW |