| 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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // We grab an extra reference because there is an IO operation in progress. | 243 // We grab an extra reference because there is an IO operation in progress. |
| 244 // Balanced in WriteDelegate::OnObjectSignaled(). | 244 // Balanced in WriteDelegate::OnObjectSignaled(). |
| 245 AddRef(); | 245 AddRef(); |
| 246 write_watcher_.StartWatching(write_overlapped_.hEvent, &writer_); | 246 write_watcher_.StartWatching(write_overlapped_.hEvent, &writer_); |
| 247 } | 247 } |
| 248 | 248 |
| 249 void TCPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object) { | 249 void TCPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object) { |
| 250 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 250 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 251 tracked_objects::ScopedTracker tracking_profile( | 251 tracked_objects::ScopedTracker tracking_profile( |
| 252 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 252 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 253 "TCPSocketWin_Core_ReadDelegate_OnObjectSignaled")); | 253 "418183 TCPSocketWin::Core::ReadDelegate::OnObjectSignaled")); |
| 254 | 254 |
| 255 DCHECK_EQ(object, core_->read_overlapped_.hEvent); | 255 DCHECK_EQ(object, core_->read_overlapped_.hEvent); |
| 256 if (core_->socket_) { | 256 if (core_->socket_) { |
| 257 if (core_->socket_->waiting_connect_) | 257 if (core_->socket_->waiting_connect_) |
| 258 core_->socket_->DidCompleteConnect(); | 258 core_->socket_->DidCompleteConnect(); |
| 259 else | 259 else |
| 260 core_->socket_->DidSignalRead(); | 260 core_->socket_->DidSignalRead(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 core_->Release(); | 263 core_->Release(); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void TCPSocketWin::Core::WriteDelegate::OnObjectSignaled( | 266 void TCPSocketWin::Core::WriteDelegate::OnObjectSignaled( |
| 267 HANDLE object) { | 267 HANDLE object) { |
| 268 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 268 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 269 tracked_objects::ScopedTracker tracking_profile( | 269 tracked_objects::ScopedTracker tracking_profile( |
| 270 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 270 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 271 "TCPSocketWin_Core_WriteDelegate_OnObjectSignaled")); | 271 "418183 TCPSocketWin::Core::WriteDelegate::OnObjectSignaled")); |
| 272 | 272 |
| 273 DCHECK_EQ(object, core_->write_overlapped_.hEvent); | 273 DCHECK_EQ(object, core_->write_overlapped_.hEvent); |
| 274 if (core_->socket_) | 274 if (core_->socket_) |
| 275 core_->socket_->DidCompleteWrite(); | 275 core_->socket_->DidCompleteWrite(); |
| 276 | 276 |
| 277 core_->Release(); | 277 core_->Release(); |
| 278 } | 278 } |
| 279 | 279 |
| 280 //----------------------------------------------------------------------------- | 280 //----------------------------------------------------------------------------- |
| 281 | 281 |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 *socket = tcp_socket.Pass(); | 767 *socket = tcp_socket.Pass(); |
| 768 *address = ip_end_point; | 768 *address = ip_end_point; |
| 769 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 769 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| 770 CreateNetLogIPEndPointCallback(&ip_end_point)); | 770 CreateNetLogIPEndPointCallback(&ip_end_point)); |
| 771 return OK; | 771 return OK; |
| 772 } | 772 } |
| 773 | 773 |
| 774 void TCPSocketWin::OnObjectSignaled(HANDLE object) { | 774 void TCPSocketWin::OnObjectSignaled(HANDLE object) { |
| 775 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 775 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 776 tracked_objects::ScopedTracker tracking_profile( | 776 tracked_objects::ScopedTracker tracking_profile( |
| 777 FROM_HERE_WITH_EXPLICIT_FUNCTION("TCPSocketWin_OnObjectSignaled")); | 777 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 778 "418383 TCPSocketWin::OnObjectSignaled")); |
| 778 | 779 |
| 779 WSANETWORKEVENTS ev; | 780 WSANETWORKEVENTS ev; |
| 780 if (WSAEnumNetworkEvents(socket_, accept_event_, &ev) == SOCKET_ERROR) { | 781 if (WSAEnumNetworkEvents(socket_, accept_event_, &ev) == SOCKET_ERROR) { |
| 781 PLOG(ERROR) << "WSAEnumNetworkEvents()"; | 782 PLOG(ERROR) << "WSAEnumNetworkEvents()"; |
| 782 return; | 783 return; |
| 783 } | 784 } |
| 784 | 785 |
| 785 if (ev.lNetworkEvents & FD_ACCEPT) { | 786 if (ev.lNetworkEvents & FD_ACCEPT) { |
| 786 int result = AcceptInternal(accept_socket_, accept_address_); | 787 int result = AcceptInternal(accept_socket_, accept_address_); |
| 787 if (result != ERR_IO_PENDING) { | 788 if (result != ERR_IO_PENDING) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 core_->read_buffer_length_ = buf_len; | 957 core_->read_buffer_length_ = buf_len; |
| 957 core_->WatchForRead(); | 958 core_->WatchForRead(); |
| 958 return ERR_IO_PENDING; | 959 return ERR_IO_PENDING; |
| 959 } | 960 } |
| 960 | 961 |
| 961 void TCPSocketWin::DidCompleteConnect() { | 962 void TCPSocketWin::DidCompleteConnect() { |
| 962 DCHECK(waiting_connect_); | 963 DCHECK(waiting_connect_); |
| 963 DCHECK(!read_callback_.is_null()); | 964 DCHECK(!read_callback_.is_null()); |
| 964 int result; | 965 int result; |
| 965 | 966 |
| 967 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 968 tracked_objects::ScopedTracker tracking_profile1( |
| 969 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 970 "418183 TCPSocketWin::DidCompleteConnect1")); |
| 966 WSANETWORKEVENTS events; | 971 WSANETWORKEVENTS events; |
| 967 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent, | 972 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent, |
| 968 &events); | 973 &events); |
| 969 int os_error = 0; | 974 int os_error = 0; |
| 970 if (rv == SOCKET_ERROR) { | 975 if (rv == SOCKET_ERROR) { |
| 971 NOTREACHED(); | 976 NOTREACHED(); |
| 972 os_error = WSAGetLastError(); | 977 os_error = WSAGetLastError(); |
| 973 result = MapSystemError(os_error); | 978 result = MapSystemError(os_error); |
| 974 } else if (events.lNetworkEvents & FD_CONNECT) { | 979 } else if (events.lNetworkEvents & FD_CONNECT) { |
| 980 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is |
| 981 // fixed. |
| 982 tracked_objects::ScopedTracker tracking_profile2( |
| 983 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 984 "418183 TCPSocketWin::DidCompleteConnect2")); |
| 975 os_error = events.iErrorCode[FD_CONNECT_BIT]; | 985 os_error = events.iErrorCode[FD_CONNECT_BIT]; |
| 976 result = MapConnectError(os_error); | 986 result = MapConnectError(os_error); |
| 977 } else { | 987 } else { |
| 978 NOTREACHED(); | 988 NOTREACHED(); |
| 979 result = ERR_UNEXPECTED; | 989 result = ERR_UNEXPECTED; |
| 980 } | 990 } |
| 981 | 991 |
| 992 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 993 tracked_objects::ScopedTracker tracking_profile3( |
| 994 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 995 "418183 TCPSocketWin::DidCompleteConnect3")); |
| 982 connect_os_error_ = os_error; | 996 connect_os_error_ = os_error; |
| 983 DoConnectComplete(result); | 997 DoConnectComplete(result); |
| 984 waiting_connect_ = false; | 998 waiting_connect_ = false; |
| 985 | 999 |
| 1000 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 1001 tracked_objects::ScopedTracker tracking_profile4( |
| 1002 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1003 "418183 TCPSocketWin::DidCompleteConnect4")); |
| 986 DCHECK_NE(result, ERR_IO_PENDING); | 1004 DCHECK_NE(result, ERR_IO_PENDING); |
| 987 base::ResetAndReturn(&read_callback_).Run(result); | 1005 base::ResetAndReturn(&read_callback_).Run(result); |
| 988 } | 1006 } |
| 989 | 1007 |
| 990 void TCPSocketWin::DidCompleteWrite() { | 1008 void TCPSocketWin::DidCompleteWrite() { |
| 991 DCHECK(waiting_write_); | 1009 DCHECK(waiting_write_); |
| 992 DCHECK(!write_callback_.is_null()); | 1010 DCHECK(!write_callback_.is_null()); |
| 993 | 1011 |
| 994 DWORD num_bytes, flags; | 1012 DWORD num_bytes, flags; |
| 995 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, | 1013 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1023 core_->write_iobuffer_ = NULL; | 1041 core_->write_iobuffer_ = NULL; |
| 1024 | 1042 |
| 1025 DCHECK_NE(rv, ERR_IO_PENDING); | 1043 DCHECK_NE(rv, ERR_IO_PENDING); |
| 1026 base::ResetAndReturn(&write_callback_).Run(rv); | 1044 base::ResetAndReturn(&write_callback_).Run(rv); |
| 1027 } | 1045 } |
| 1028 | 1046 |
| 1029 void TCPSocketWin::DidSignalRead() { | 1047 void TCPSocketWin::DidSignalRead() { |
| 1030 DCHECK(waiting_read_); | 1048 DCHECK(waiting_read_); |
| 1031 DCHECK(!read_callback_.is_null()); | 1049 DCHECK(!read_callback_.is_null()); |
| 1032 | 1050 |
| 1051 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 1052 tracked_objects::ScopedTracker tracking_profile1( |
| 1053 FROM_HERE_WITH_EXPLICIT_FUNCTION("418183 TCPSocketWin::DidSignalRead1")); |
| 1033 int os_error = 0; | 1054 int os_error = 0; |
| 1034 WSANETWORKEVENTS network_events; | 1055 WSANETWORKEVENTS network_events; |
| 1035 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent, | 1056 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent, |
| 1036 &network_events); | 1057 &network_events); |
| 1037 if (rv == SOCKET_ERROR) { | 1058 if (rv == SOCKET_ERROR) { |
| 1038 os_error = WSAGetLastError(); | 1059 os_error = WSAGetLastError(); |
| 1039 rv = MapSystemError(os_error); | 1060 rv = MapSystemError(os_error); |
| 1040 } else if (network_events.lNetworkEvents) { | 1061 } else if (network_events.lNetworkEvents) { |
| 1062 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is |
| 1063 // fixed. |
| 1064 tracked_objects::ScopedTracker tracking_profile2( |
| 1065 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1066 "418183 TCPSocketWin::DidSignalRead2")); |
| 1041 DCHECK_EQ(network_events.lNetworkEvents & ~(FD_READ | FD_CLOSE), 0); | 1067 DCHECK_EQ(network_events.lNetworkEvents & ~(FD_READ | FD_CLOSE), 0); |
| 1042 // If network_events.lNetworkEvents is FD_CLOSE and | 1068 // If network_events.lNetworkEvents is FD_CLOSE and |
| 1043 // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful | 1069 // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful |
| 1044 // connection closure. It is tempting to directly set rv to 0 in | 1070 // connection closure. It is tempting to directly set rv to 0 in |
| 1045 // this case, but the MSDN pages for WSAEventSelect and | 1071 // this case, but the MSDN pages for WSAEventSelect and |
| 1046 // WSAAsyncSelect recommend we still call DoRead(): | 1072 // WSAAsyncSelect recommend we still call DoRead(): |
| 1047 // FD_CLOSE should only be posted after all data is read from a | 1073 // FD_CLOSE should only be posted after all data is read from a |
| 1048 // socket, but an application should check for remaining data upon | 1074 // socket, but an application should check for remaining data upon |
| 1049 // receipt of FD_CLOSE to avoid any possibility of losing data. | 1075 // receipt of FD_CLOSE to avoid any possibility of losing data. |
| 1050 // | 1076 // |
| 1051 // If network_events.iErrorCode[FD_READ_BIT] or | 1077 // If network_events.iErrorCode[FD_READ_BIT] or |
| 1052 // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call | 1078 // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call |
| 1053 // DoRead() because recv() reports a more accurate error code | 1079 // DoRead() because recv() reports a more accurate error code |
| 1054 // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was | 1080 // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was |
| 1055 // reset. | 1081 // reset. |
| 1056 rv = DoRead(core_->read_iobuffer_.get(), core_->read_buffer_length_, | 1082 rv = DoRead(core_->read_iobuffer_.get(), core_->read_buffer_length_, |
| 1057 read_callback_); | 1083 read_callback_); |
| 1058 if (rv == ERR_IO_PENDING) | 1084 if (rv == ERR_IO_PENDING) |
| 1059 return; | 1085 return; |
| 1060 } else { | 1086 } else { |
| 1087 // TODO(pkasting): Remove ScopedTracker below once crbug.com/418183 is |
| 1088 // fixed. |
| 1089 tracked_objects::ScopedTracker tracking_profile3( |
| 1090 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1091 "418183 TCPSocketWin::DidSignalRead3")); |
| 1061 // This may happen because Read() may succeed synchronously and | 1092 // This may happen because Read() may succeed synchronously and |
| 1062 // consume all the received data without resetting the event object. | 1093 // consume all the received data without resetting the event object. |
| 1063 core_->WatchForRead(); | 1094 core_->WatchForRead(); |
| 1064 return; | 1095 return; |
| 1065 } | 1096 } |
| 1066 | 1097 |
| 1067 waiting_read_ = false; | 1098 waiting_read_ = false; |
| 1068 core_->read_iobuffer_ = NULL; | 1099 core_->read_iobuffer_ = NULL; |
| 1069 core_->read_buffer_length_ = 0; | 1100 core_->read_buffer_length_ = 0; |
| 1070 | 1101 |
| 1102 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 1103 tracked_objects::ScopedTracker tracking_profile4( |
| 1104 FROM_HERE_WITH_EXPLICIT_FUNCTION("418183 TCPSocketWin::DidSignalRead4")); |
| 1071 DCHECK_NE(rv, ERR_IO_PENDING); | 1105 DCHECK_NE(rv, ERR_IO_PENDING); |
| 1072 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | |
| 1073 tracked_objects::ScopedTracker tracking_profile( | |
| 1074 FROM_HERE_WITH_EXPLICIT_FUNCTION("TCPSocketWin::DidSignalRead")); | |
| 1075 base::ResetAndReturn(&read_callback_).Run(rv); | 1106 base::ResetAndReturn(&read_callback_).Run(rv); |
| 1076 } | 1107 } |
| 1077 | 1108 |
| 1078 } // namespace net | 1109 } // namespace net |
| OLD | NEW |