Index: base/sync_socket_win.cc |
diff --git a/base/sync_socket_win.cc b/base/sync_socket_win.cc |
index 99a6afea3ec2fdde77f7ac829cbf102fa6998098..c3f179970fb9d892baa6aa8c4028c9f273afe180 100644 |
--- a/base/sync_socket_win.cc |
+++ b/base/sync_socket_win.cc |
@@ -120,10 +120,15 @@ size_t CancelableFileOperation(Function operation, HANDLE file, |
COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); |
DCHECK_LE(length, kMaxMessageLength); |
+ // Track the start time so we can reduce the timeout as data is read. |
+ TimeTicks start_time; |
+ if (timeout_in_ms != INFINITE) |
+ start_time = base::TimeTicks::Now(); |
+ |
OVERLAPPED ol = {0}; |
ol.hEvent = io_event->handle(); |
size_t count = 0; |
- while (count < length) { |
+ while (count < length && timeout_in_ms > 0) { |
DWORD chunk = GetNextChunkSize(count, length); |
// This is either the ReadFile or WriteFile call depending on whether |
// we're receiving or sending data. |
@@ -138,7 +143,7 @@ size_t CancelableFileOperation(Function operation, HANDLE file, |
if (wait_result == (WAIT_OBJECT_0 + 0)) { |
GetOverlappedResult(file, &ol, &len, TRUE); |
} else if (wait_result == (WAIT_OBJECT_0 + 1)) { |
- VLOG(1) << "Shutdown was signaled. Closing socket."; |
+ DVLOG(1) << "Shutdown was signaled. Closing socket."; |
CancelIo(file); |
socket->Close(); |
count = 0; |
@@ -146,9 +151,8 @@ size_t CancelableFileOperation(Function operation, HANDLE file, |
} else { |
// Timeout happened. |
DCHECK_EQ(WAIT_TIMEOUT, wait_result); |
- if (!CancelIo(file)){ |
+ if (!CancelIo(file)) |
DLOG(WARNING) << "CancelIo() failed"; |
- } |
break; |
} |
} else { |
@@ -161,6 +165,11 @@ size_t CancelableFileOperation(Function operation, HANDLE file, |
// Quit the operation if we can't write/read anymore. |
if (len != chunk) |
break; |
+ |
+ // Since TimeTicks::Now() is expensive, only bother updating the timeout if |
+ // we have more work to do. |
+ if (timeout_in_ms != INFINITE && count < length) |
+ timeout_in_ms -= (base::TimeTicks::Now() - start_time).InMilliseconds(); |
} |
return (count > 0) ? count : 0; |
@@ -207,6 +216,13 @@ size_t SyncSocket::Send(const void* buffer, size_t length) { |
return count; |
} |
+size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
+ size_t length, |
+ TimeDelta timeout) { |
+ NOTIMPLEMENTED(); |
+ return 0; |
+} |
+ |
size_t SyncSocket::Receive(void* buffer, size_t length) { |
DCHECK_LE(length, kMaxMessageLength); |
size_t count = 0; |
@@ -258,9 +274,17 @@ size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
} |
size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { |
- return CancelableFileOperation(&ReadFile, handle_, |
- reinterpret_cast<char*>(buffer), length, &file_operation_, |
- &shutdown_event_, this, INFINITE); |
+ return CancelableFileOperation( |
+ &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
+ &file_operation_, &shutdown_event_, this, INFINITE); |
+} |
+ |
+size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer, |
+ size_t length, |
+ base::TimeDelta timeout) { |
+ return CancelableFileOperation( |
+ &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
+ &file_operation_, &shutdown_event_, this, timeout.InMilliseconds()); |
} |
// static |