Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: base/sync_socket_win.cc

Issue 10000004: Make the CancellableSyncSocket non-blocking on Send, and blocking on Receive (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use WaitForMultipleObjects in windows Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 const unsigned int timeout_in_ms) {
tommi (sloooow) - chröme 2012/04/16 13:06:36 s/const unsigned int/DWORD No need for const on in
no longer working on chromium 2012/04/16 15:45:30 Done.
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 DWORD 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.
tommi (sloooow) - chröme 2012/04/16 13:06:36 DCHECK_EQ(WAIT_TIMEOUT, wait_result)?
no longer working on chromium 2012/04/16 15:45:30 Done.
148 if (!CancelIo(file)){
149 DLOG(WARNING) << "CancelIo() failed";
tommi (sloooow) - chröme 2012/04/16 13:06:36 Perhaps you should also add to the documentation o
no longer working on chromium 2012/04/16 15:45:30 Done.
150 }
151 break;
144 } 152 }
145 } else { 153 } else {
146 return (0 < count) ? count : 0; 154 break;
147 } 155 }
148 } 156 }
157
158 // Quit the operation if we can't write/read anymore.
159 if (len != chunk)
tommi (sloooow) - chröme 2012/04/16 13:06:36 should we move this check below |count += len;|? T
no longer working on chromium 2012/04/16 15:45:30 Done.
160 break;
161
149 count += len; 162 count += len;
150 } 163 }
151 return count; 164
165 return (count > 0) ? count : 0;
152 } 166 }
153 167
154 } // namespace 168 } // namespace
155 169
156 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; 170 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
157 171
158 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} 172 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
159 173
160 SyncSocket::~SyncSocket() { 174 SyncSocket::~SyncSocket() {
161 Close(); 175 Close();
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 return true; 241 return true;
228 } 242 }
229 243
230 bool CancelableSyncSocket::Close() { 244 bool CancelableSyncSocket::Close() {
231 bool ret = SyncSocket::Close(); 245 bool ret = SyncSocket::Close();
232 shutdown_event_.Reset(); 246 shutdown_event_.Reset();
233 return ret; 247 return ret;
234 } 248 }
235 249
236 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { 250 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
237 return CancelableFileOperation(&WriteFile, handle_, 251 static const unsigned int kWaitTimeOutInMs = 500;
tommi (sloooow) - chröme 2012/04/16 13:06:36 s/unsigned int/DWORD (ok in this file since it is
no longer working on chromium 2012/04/16 15:45:30 Done.
238 reinterpret_cast<const char*>(buffer), length, &file_operation_, 252 return CancelableFileOperation(
239 &shutdown_event_, this); 253 &WriteFile, handle_, reinterpret_cast<const char*>(buffer),
254 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs);
240 } 255 }
241 256
242 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { 257 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) {
243 return CancelableFileOperation(&ReadFile, handle_, 258 return CancelableFileOperation(&ReadFile, handle_,
244 reinterpret_cast<char*>(buffer), length, &file_operation_, 259 reinterpret_cast<char*>(buffer), length, &file_operation_,
245 &shutdown_event_, this); 260 &shutdown_event_, this, INFINITE);
246 } 261 }
247 262
248 // static 263 // static
249 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 264 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
250 CancelableSyncSocket* socket_b) { 265 CancelableSyncSocket* socket_b) {
251 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); 266 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true);
252 } 267 }
253 268
254 269
255 } // namespace base 270 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698