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/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
9 #include "base/win/scoped_handle.h" | 9 #include "base/win/scoped_handle.h" |
10 | 10 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 const BOOL operation_ok = operation( | 147 const BOOL operation_ok = operation( |
148 file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol); | 148 file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol); |
149 if (!operation_ok) { | 149 if (!operation_ok) { |
150 if (::GetLastError() == ERROR_IO_PENDING) { | 150 if (::GetLastError() == ERROR_IO_PENDING) { |
151 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; | 151 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; |
152 const int wait_result = WaitForMultipleObjects( | 152 const int wait_result = WaitForMultipleObjects( |
153 ARRAYSIZE_UNSAFE(events), events, FALSE, | 153 ARRAYSIZE_UNSAFE(events), events, FALSE, |
154 timeout_in_ms == INFINITE | 154 timeout_in_ms == INFINITE |
155 ? timeout_in_ms | 155 ? timeout_in_ms |
156 : (finish_time - current_time).InMilliseconds()); | 156 : (finish_time - current_time).InMilliseconds()); |
157 if (wait_result == (WAIT_OBJECT_0 + 0)) { | 157 if (wait_result != WAIT_OBJECT_0 + 0) { |
158 GetOverlappedResult(file, &ol, &len, TRUE); | 158 // CancelIo() doesn't synchronously cancel outstanding IO, only marks |
159 } else if (wait_result == (WAIT_OBJECT_0 + 1)) { | 159 // outstanding IO for cancellation. We must call GetOverlappedResult() |
| 160 // below to ensure in flight writes complete before returning. |
| 161 CancelIo(file); |
| 162 } |
| 163 |
| 164 // We set the |bWait| parameter to TRUE for GetOverlappedResult() to |
| 165 // ensure writes are complete before returning. |
| 166 if (!GetOverlappedResult(file, &ol, &len, TRUE)) |
| 167 len = 0; |
| 168 |
| 169 if (wait_result == WAIT_OBJECT_0 + 1) { |
160 DVLOG(1) << "Shutdown was signaled. Closing socket."; | 170 DVLOG(1) << "Shutdown was signaled. Closing socket."; |
161 CancelIo(file); | |
162 socket->Close(); | 171 socket->Close(); |
163 count = 0; | 172 return count; |
164 break; | |
165 } else { | |
166 // Timeout happened. | |
167 DCHECK_EQ(WAIT_TIMEOUT, wait_result); | |
168 if (!CancelIo(file)) | |
169 DLOG(WARNING) << "CancelIo() failed"; | |
170 break; | |
171 } | 173 } |
| 174 |
| 175 // Timeouts will be handled by the while() condition below since |
| 176 // GetOverlappedResult() may complete successfully after CancelIo(). |
| 177 DCHECK(wait_result == WAIT_OBJECT_0 + 0 || wait_result == WAIT_TIMEOUT); |
172 } else { | 178 } else { |
173 break; | 179 break; |
174 } | 180 } |
175 } | 181 } |
176 | 182 |
177 count += len; | 183 count += len; |
178 | 184 |
179 // Quit the operation if we can't write/read anymore. | 185 // Quit the operation if we can't write/read anymore. |
180 if (len != chunk) | 186 if (len != chunk) |
181 break; | 187 break; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 &file_operation_, &shutdown_event_, this, timeout.InMilliseconds()); | 331 &file_operation_, &shutdown_event_, this, timeout.InMilliseconds()); |
326 } | 332 } |
327 | 333 |
328 // static | 334 // static |
329 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 335 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
330 CancelableSyncSocket* socket_b) { | 336 CancelableSyncSocket* socket_b) { |
331 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); | 337 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); |
332 } | 338 } |
333 | 339 |
334 } // namespace base | 340 } // namespace base |
OLD | NEW |