OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/win/scoped_handle.h" | 8 #include "base/win/scoped_handle.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 107 |
108 // Template function that supports calling ReadFile or WriteFile in an | 108 // Template function that supports calling ReadFile or WriteFile in an |
109 // overlapped fashion and waits for IO completion. The function also waits | 109 // overlapped fashion and waits for IO completion. The function also waits |
110 // on an event that can be used to cancel the operation. If the operation | 110 // on an event that can be used to cancel the operation. If the operation |
111 // is cancelled, the function returns and closes the relevant socket object. | 111 // is cancelled, the function returns and closes the relevant socket object. |
112 template <typename BufferType, typename Function> | 112 template <typename BufferType, typename Function> |
113 size_t CancelableFileOperation(Function operation, HANDLE file, | 113 size_t CancelableFileOperation(Function operation, HANDLE file, |
114 BufferType* buffer, size_t length, | 114 BufferType* buffer, size_t length, |
115 base::WaitableEvent* io_event, | 115 base::WaitableEvent* io_event, |
116 base::WaitableEvent* cancel_event, | 116 base::WaitableEvent* cancel_event, |
117 CancelableSyncSocket* socket) { | 117 CancelableSyncSocket* socket, |
| 118 DWORD timeout_in_ms) { |
118 // The buffer must be byte size or the length check won't make much sense. | 119 // The buffer must be byte size or the length check won't make much sense. |
119 COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); | 120 COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); |
120 DCHECK_LE(length, kMaxMessageLength); | 121 DCHECK_LE(length, kMaxMessageLength); |
121 | 122 |
122 OVERLAPPED ol = {0}; | 123 OVERLAPPED ol = {0}; |
123 ol.hEvent = io_event->handle(); | 124 ol.hEvent = io_event->handle(); |
124 size_t count = 0; | 125 size_t count = 0; |
125 while (count < length) { | 126 while (count < length) { |
126 DWORD chunk = GetNextChunkSize(count, length); | 127 DWORD chunk = GetNextChunkSize(count, length); |
127 // This is either the ReadFile or WriteFile call depending on whether | 128 // This is either the ReadFile or WriteFile call depending on whether |
128 // we're receiving or sending data. | 129 // we're receiving or sending data. |
129 DWORD len = 0; | 130 DWORD len = 0; |
130 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, | 131 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, |
131 &len, &ol); | 132 &len, &ol); |
132 if (!ok) { | 133 if (!ok) { |
133 if (::GetLastError() == ERROR_IO_PENDING) { | 134 if (::GetLastError() == ERROR_IO_PENDING) { |
134 base::WaitableEvent* events[] = { io_event, cancel_event }; | 135 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; |
135 size_t signaled = WaitableEvent::WaitMany(events, arraysize(events)); | 136 int wait_result = WaitForMultipleObjects( |
136 if (signaled == 1) { | 137 arraysize(events), events, FALSE, timeout_in_ms); |
| 138 if (wait_result == (WAIT_OBJECT_0 + 0)) { |
| 139 GetOverlappedResult(file, &ol, &len, TRUE); |
| 140 } else if (wait_result == (WAIT_OBJECT_0 + 1)) { |
137 VLOG(1) << "Shutdown was signaled. Closing socket."; | 141 VLOG(1) << "Shutdown was signaled. Closing socket."; |
138 CancelIo(file); | 142 CancelIo(file); |
139 socket->Close(); | 143 socket->Close(); |
140 count = 0; | 144 count = 0; |
141 break; | 145 break; |
142 } else { | 146 } else { |
143 GetOverlappedResult(file, &ol, &len, TRUE); | 147 // Timeout happened. |
| 148 DCHECK_EQ(WAIT_TIMEOUT, wait_result); |
| 149 if (!CancelIo(file)){ |
| 150 DLOG(WARNING) << "CancelIo() failed"; |
| 151 } |
| 152 break; |
144 } | 153 } |
145 } else { | 154 } else { |
146 return (0 < count) ? count : 0; | 155 break; |
147 } | 156 } |
148 } | 157 } |
| 158 |
149 count += len; | 159 count += len; |
| 160 |
| 161 // Quit the operation if we can't write/read anymore. |
| 162 if (len != chunk) |
| 163 break; |
150 } | 164 } |
151 return count; | 165 |
| 166 return (count > 0) ? count : 0; |
152 } | 167 } |
153 | 168 |
154 } // namespace | 169 } // namespace |
155 | 170 |
156 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; | 171 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
157 | 172 |
158 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} | 173 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
159 | 174 |
160 SyncSocket::~SyncSocket() { | 175 SyncSocket::~SyncSocket() { |
161 Close(); | 176 Close(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 return true; | 242 return true; |
228 } | 243 } |
229 | 244 |
230 bool CancelableSyncSocket::Close() { | 245 bool CancelableSyncSocket::Close() { |
231 bool ret = SyncSocket::Close(); | 246 bool ret = SyncSocket::Close(); |
232 shutdown_event_.Reset(); | 247 shutdown_event_.Reset(); |
233 return ret; | 248 return ret; |
234 } | 249 } |
235 | 250 |
236 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | 251 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
237 return CancelableFileOperation(&WriteFile, handle_, | 252 static const DWORD kWaitTimeOutInMs = 500; |
238 reinterpret_cast<const char*>(buffer), length, &file_operation_, | 253 return CancelableFileOperation( |
239 &shutdown_event_, this); | 254 &WriteFile, handle_, reinterpret_cast<const char*>(buffer), |
| 255 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); |
240 } | 256 } |
241 | 257 |
242 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { | 258 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { |
243 return CancelableFileOperation(&ReadFile, handle_, | 259 return CancelableFileOperation(&ReadFile, handle_, |
244 reinterpret_cast<char*>(buffer), length, &file_operation_, | 260 reinterpret_cast<char*>(buffer), length, &file_operation_, |
245 &shutdown_event_, this); | 261 &shutdown_event_, this, INFINITE); |
246 } | 262 } |
247 | 263 |
248 // static | 264 // static |
249 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 265 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
250 CancelableSyncSocket* socket_b) { | 266 CancelableSyncSocket* socket_b) { |
251 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); | 267 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); |
252 } | 268 } |
253 | 269 |
254 | 270 |
255 } // namespace base | 271 } // namespace base |
OLD | NEW |