| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, | 131 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, |
| 132 &len, &ol); | 132 &len, &ol); |
| 133 if (!ok) { | 133 if (!ok) { |
| 134 if (::GetLastError() == ERROR_IO_PENDING) { | 134 if (::GetLastError() == ERROR_IO_PENDING) { |
| 135 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; | 135 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; |
| 136 int wait_result = WaitForMultipleObjects( | 136 int wait_result = WaitForMultipleObjects( |
| 137 arraysize(events), events, FALSE, timeout_in_ms); | 137 arraysize(events), events, FALSE, timeout_in_ms); |
| 138 if (wait_result == (WAIT_OBJECT_0 + 0)) { | 138 if (wait_result == (WAIT_OBJECT_0 + 0)) { |
| 139 GetOverlappedResult(file, &ol, &len, TRUE); | 139 GetOverlappedResult(file, &ol, &len, TRUE); |
| 140 } else if (wait_result == (WAIT_OBJECT_0 + 1)) { | 140 } else if (wait_result == (WAIT_OBJECT_0 + 1)) { |
| 141 VLOG(1) << "Shutdown was signaled. Closing socket."; | 141 DVLOG(1) << "Shutdown was signaled. Closing socket."; |
| 142 CancelIo(file); | 142 CancelIo(file); |
| 143 socket->Close(); | 143 socket->Close(); |
| 144 count = 0; | 144 count = 0; |
| 145 break; | 145 break; |
| 146 } else { | 146 } else { |
| 147 // Timeout happened. | 147 // Timeout happened. |
| 148 DCHECK_EQ(WAIT_TIMEOUT, wait_result); | 148 DCHECK_EQ(WAIT_TIMEOUT, wait_result); |
| 149 if (!CancelIo(file)){ | 149 if (!CancelIo(file)) |
| 150 DLOG(WARNING) << "CancelIo() failed"; | 150 DLOG(WARNING) << "CancelIo() failed"; |
| 151 } | |
| 152 break; | 151 break; |
| 153 } | 152 } |
| 154 } else { | 153 } else { |
| 155 break; | 154 break; |
| 156 } | 155 } |
| 157 } | 156 } |
| 158 | 157 |
| 159 count += len; | 158 count += len; |
| 160 | 159 |
| 161 // Quit the operation if we can't write/read anymore. | 160 // Quit the operation if we can't write/read anymore. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 DWORD chunk = GetNextChunkSize(count, length); | 199 DWORD chunk = GetNextChunkSize(count, length); |
| 201 if (WriteFile(handle_, static_cast<const char*>(buffer) + count, | 200 if (WriteFile(handle_, static_cast<const char*>(buffer) + count, |
| 202 chunk, &len, NULL) == FALSE) { | 201 chunk, &len, NULL) == FALSE) { |
| 203 return (0 < count) ? count : 0; | 202 return (0 < count) ? count : 0; |
| 204 } | 203 } |
| 205 count += len; | 204 count += len; |
| 206 } | 205 } |
| 207 return count; | 206 return count; |
| 208 } | 207 } |
| 209 | 208 |
| 209 size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 210 size_t length, |
| 211 TimeDelta timeout) { |
| 212 NOTIMPLEMENTED(); |
| 213 return 0; |
| 214 } |
| 215 |
| 210 size_t SyncSocket::Receive(void* buffer, size_t length) { | 216 size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 211 DCHECK_LE(length, kMaxMessageLength); | 217 DCHECK_LE(length, kMaxMessageLength); |
| 212 size_t count = 0; | 218 size_t count = 0; |
| 213 while (count < length) { | 219 while (count < length) { |
| 214 DWORD len; | 220 DWORD len; |
| 215 DWORD chunk = GetNextChunkSize(count, length); | 221 DWORD chunk = GetNextChunkSize(count, length); |
| 216 if (ReadFile(handle_, static_cast<char*>(buffer) + count, | 222 if (ReadFile(handle_, static_cast<char*>(buffer) + count, |
| 217 chunk, &len, NULL) == FALSE) { | 223 chunk, &len, NULL) == FALSE) { |
| 218 return (0 < count) ? count : 0; | 224 return (0 < count) ? count : 0; |
| 219 } | 225 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 257 } |
| 252 | 258 |
| 253 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | 259 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
| 254 static const DWORD kWaitTimeOutInMs = 500; | 260 static const DWORD kWaitTimeOutInMs = 500; |
| 255 return CancelableFileOperation( | 261 return CancelableFileOperation( |
| 256 &WriteFile, handle_, reinterpret_cast<const char*>(buffer), | 262 &WriteFile, handle_, reinterpret_cast<const char*>(buffer), |
| 257 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); | 263 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); |
| 258 } | 264 } |
| 259 | 265 |
| 260 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { | 266 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { |
| 261 return CancelableFileOperation(&ReadFile, handle_, | 267 return CancelableFileOperation( |
| 262 reinterpret_cast<char*>(buffer), length, &file_operation_, | 268 &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
| 263 &shutdown_event_, this, INFINITE); | 269 &file_operation_, &shutdown_event_, this, INFINITE); |
| 270 } |
| 271 |
| 272 size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer, |
| 273 size_t length, |
| 274 base::TimeDelta timeout) { |
| 275 return CancelableFileOperation( |
| 276 &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
| 277 &file_operation_, &shutdown_event_, this, timeout.InMilliseconds()); |
| 264 } | 278 } |
| 265 | 279 |
| 266 // static | 280 // static |
| 267 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | 281 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 268 CancelableSyncSocket* socket_b) { | 282 CancelableSyncSocket* socket_b) { |
| 269 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); | 283 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); |
| 270 } | 284 } |
| 271 | 285 |
| 272 | 286 |
| 273 } // namespace base | 287 } // namespace base |
| OLD | NEW |