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

Side by Side Diff: base/sync_socket_win.cc

Issue 23875019: Add SyncSocket::ReceiveWithTimeout() and SyncSocket unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Handle EINTR. Reverse expectations. Created 7 years, 2 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
« base/sync_socket_unittest.cc ('K') | « base/sync_socket_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/threading/thread_restrictions.h"
8 #include "base/win/scoped_handle.h" 9 #include "base/win/scoped_handle.h"
9 10
10 namespace base { 11 namespace base {
11 12
12 using win::ScopedHandle; 13 using win::ScopedHandle;
13 14
14 namespace { 15 namespace {
15 // IMPORTANT: do not change how this name is generated because it will break 16 // IMPORTANT: do not change how this name is generated because it will break
16 // in sandboxed scenarios as we might have by-name policies that allow pipe 17 // in sandboxed scenarios as we might have by-name policies that allow pipe
17 // creation. Also keep the secure random number generation. 18 // creation. Also keep the secure random number generation.
18 const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; 19 const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu";
19 const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1; 20 const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1;
20 21
21 // To avoid users sending negative message lengths to Send/Receive 22 // To avoid users sending negative message lengths to Send/Receive
22 // we clamp message lengths, which are size_t, to no more than INT_MAX. 23 // we clamp message lengths, which are size_t, to no more than INT_MAX.
23 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); 24 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
24 25
25 const int kOutBufferSize = 4096; 26 const int kOutBufferSize = 4096;
26 const int kInBufferSize = 4096; 27 const int kInBufferSize = 4096;
27 const int kDefaultTimeoutMilliSeconds = 1000; 28 const int kDefaultTimeoutMilliSeconds = 1000;
28 29
29 bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) { 30 bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) {
30 DCHECK(socket_a != socket_b); 31 DCHECK_NE(socket_a, socket_b);
31 DCHECK(*socket_a == SyncSocket::kInvalidHandle); 32 DCHECK_EQ(*socket_a, SyncSocket::kInvalidHandle);
32 DCHECK(*socket_b == SyncSocket::kInvalidHandle); 33 DCHECK_EQ(*socket_b, SyncSocket::kInvalidHandle);
33 34
34 wchar_t name[kPipePathMax]; 35 wchar_t name[kPipePathMax];
35 ScopedHandle handle_a; 36 ScopedHandle handle_a;
36 DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; 37 DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE;
37 if (overlapped) 38 if (overlapped)
38 flags |= FILE_FLAG_OVERLAPPED; 39 flags |= FILE_FLAG_OVERLAPPED;
39 40
40 do { 41 do {
41 unsigned int rnd_name; 42 unsigned int rnd_name;
42 if (rand_s(&rnd_name) != 0) 43 if (rand_s(&rnd_name) != 0)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 (max_size - current_pos) : UINT_MAX); 106 (max_size - current_pos) : UINT_MAX);
106 } 107 }
107 108
108 // Template function that supports calling ReadFile or WriteFile in an 109 // Template function that supports calling ReadFile or WriteFile in an
109 // overlapped fashion and waits for IO completion. The function also waits 110 // 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 111 // 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. 112 // is cancelled, the function returns and closes the relevant socket object.
112 template <typename BufferType, typename Function> 113 template <typename BufferType, typename Function>
113 size_t CancelableFileOperation(Function operation, HANDLE file, 114 size_t CancelableFileOperation(Function operation, HANDLE file,
114 BufferType* buffer, size_t length, 115 BufferType* buffer, size_t length,
115 base::WaitableEvent* io_event, 116 WaitableEvent* io_event,
116 base::WaitableEvent* cancel_event, 117 WaitableEvent* cancel_event,
117 CancelableSyncSocket* socket, 118 CancelableSyncSocket* socket,
118 DWORD timeout_in_ms) { 119 DWORD timeout_in_ms) {
120 ThreadRestrictions::AssertIOAllowed();
119 // The buffer must be byte size or the length check won't make much sense. 121 // The buffer must be byte size or the length check won't make much sense.
120 COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); 122 COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type);
123 DCHECK_GT(length, 0u);
121 DCHECK_LE(length, kMaxMessageLength); 124 DCHECK_LE(length, kMaxMessageLength);
125 DCHECK_NE(file, SyncSocket::kInvalidHandle);
122 126
123 OVERLAPPED ol = {0}; 127 // Track the start time so we can reduce the timeout as data is read.
124 ol.hEvent = io_event->handle(); 128 TimeTicks start_time;
129 if (timeout_in_ms != INFINITE)
130 start_time = TimeTicks::Now();
131
125 size_t count = 0; 132 size_t count = 0;
126 while (count < length) { 133 do {
127 DWORD chunk = GetNextChunkSize(count, length); 134 // The OVERLAPPED structure will be modified by ReadFile or WriteFile.
135 OVERLAPPED ol = { 0 };
136 ol.hEvent = io_event->handle();
137
138 const DWORD chunk = GetNextChunkSize(count, length);
128 // This is either the ReadFile or WriteFile call depending on whether 139 // This is either the ReadFile or WriteFile call depending on whether
129 // we're receiving or sending data. 140 // we're receiving or sending data.
130 DWORD len = 0; 141 DWORD len = 0;
131 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, 142 const BOOL operation_ok = operation(
132 &len, &ol); 143 file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol);
133 if (!ok) { 144 if (!operation_ok) {
134 if (::GetLastError() == ERROR_IO_PENDING) { 145 if (::GetLastError() == ERROR_IO_PENDING) {
135 HANDLE events[] = { io_event->handle(), cancel_event->handle() }; 146 HANDLE events[] = { io_event->handle(), cancel_event->handle() };
136 int wait_result = WaitForMultipleObjects( 147 const int wait_result = WaitForMultipleObjects(
137 arraysize(events), events, FALSE, timeout_in_ms); 148 ARRAYSIZE_UNSAFE(events), events, FALSE, timeout_in_ms);
138 if (wait_result == (WAIT_OBJECT_0 + 0)) { 149 if (wait_result == (WAIT_OBJECT_0 + 0)) {
139 GetOverlappedResult(file, &ol, &len, TRUE); 150 GetOverlappedResult(file, &ol, &len, TRUE);
140 } else if (wait_result == (WAIT_OBJECT_0 + 1)) { 151 } else if (wait_result == (WAIT_OBJECT_0 + 1)) {
141 VLOG(1) << "Shutdown was signaled. Closing socket."; 152 DVLOG(1) << "Shutdown was signaled. Closing socket.";
142 CancelIo(file); 153 CancelIo(file);
143 socket->Close(); 154 socket->Close();
144 count = 0; 155 count = 0;
145 break; 156 break;
146 } else { 157 } else {
147 // Timeout happened. 158 // Timeout happened.
148 DCHECK_EQ(WAIT_TIMEOUT, wait_result); 159 DCHECK_EQ(WAIT_TIMEOUT, wait_result);
149 if (!CancelIo(file)){ 160 if (!CancelIo(file))
150 DLOG(WARNING) << "CancelIo() failed"; 161 DLOG(WARNING) << "CancelIo() failed";
151 }
152 break; 162 break;
153 } 163 }
154 } else { 164 } else {
155 break; 165 break;
156 } 166 }
157 } 167 }
158 168
159 count += len; 169 count += len;
160 170
161 // Quit the operation if we can't write/read anymore. 171 // Quit the operation if we can't write/read anymore.
162 if (len != chunk) 172 if (len != chunk)
163 break; 173 break;
164 }
165 174
166 return (count > 0) ? count : 0; 175 // Since TimeTicks::Now() is expensive, only bother updating the timeout if
176 // we have more work to do.
177 if (timeout_in_ms != INFINITE && count < length)
178 timeout_in_ms -= (TimeTicks::Now() - start_time).InMilliseconds();
jar (doing other things) 2013/10/10 02:01:28 This also looks wrong. Here again, timeout_in_ms
DaleCurtis 2013/10/10 17:37:34 Same question as the POSIX variant.
DaleCurtis 2013/10/11 22:48:24 I'm now tracking a current_time value to avoid upd
179 } while (count < length && timeout_in_ms > 0);
180
181 return count;
167 } 182 }
168 183
169 } // namespace 184 } // namespace
170 185
171 #if defined(COMPONENT_BUILD) 186 #if defined(COMPONENT_BUILD)
172 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; 187 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
173 #endif 188 #endif
174 189
175 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} 190 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
176 191
177 SyncSocket::~SyncSocket() { 192 SyncSocket::~SyncSocket() {
178 Close(); 193 Close();
179 } 194 }
180 195
181 // static 196 // static
182 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { 197 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
183 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false); 198 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false);
184 } 199 }
185 200
186 bool SyncSocket::Close() { 201 bool SyncSocket::Close() {
187 if (handle_ == kInvalidHandle) 202 if (handle_ == kInvalidHandle)
188 return false; 203 return true;
189 204
190 BOOL retval = CloseHandle(handle_); 205 const BOOL result = CloseHandle(handle_);
191 handle_ = kInvalidHandle; 206 handle_ = kInvalidHandle;
192 return retval ? true : false; 207 return result == TRUE;
193 } 208 }
194 209
195 size_t SyncSocket::Send(const void* buffer, size_t length) { 210 size_t SyncSocket::Send(const void* buffer, size_t length) {
211 ThreadRestrictions::AssertIOAllowed();
212 DCHECK_GT(length, 0u);
196 DCHECK_LE(length, kMaxMessageLength); 213 DCHECK_LE(length, kMaxMessageLength);
214 DCHECK_NE(handle_, kInvalidHandle);
197 size_t count = 0; 215 size_t count = 0;
198 while (count < length) { 216 while (count < length) {
199 DWORD len; 217 DWORD len;
200 DWORD chunk = GetNextChunkSize(count, length); 218 DWORD chunk = GetNextChunkSize(count, length);
201 if (WriteFile(handle_, static_cast<const char*>(buffer) + count, 219 if (WriteFile(handle_, static_cast<const char*>(buffer) + count,
202 chunk, &len, NULL) == FALSE) { 220 chunk, &len, NULL) == FALSE) {
203 return (0 < count) ? count : 0; 221 return count;
204 } 222 }
205 count += len; 223 count += len;
206 } 224 }
207 return count; 225 return count;
208 } 226 }
209 227
228 size_t SyncSocket::ReceiveWithTimeout(void* buffer,
229 size_t length,
230 TimeDelta timeout) {
231 NOTIMPLEMENTED();
232 return 0;
233 }
234
210 size_t SyncSocket::Receive(void* buffer, size_t length) { 235 size_t SyncSocket::Receive(void* buffer, size_t length) {
236 ThreadRestrictions::AssertIOAllowed();
237 DCHECK_GT(length, 0u);
211 DCHECK_LE(length, kMaxMessageLength); 238 DCHECK_LE(length, kMaxMessageLength);
239 DCHECK_NE(handle_, kInvalidHandle);
212 size_t count = 0; 240 size_t count = 0;
213 while (count < length) { 241 while (count < length) {
214 DWORD len; 242 DWORD len;
215 DWORD chunk = GetNextChunkSize(count, length); 243 DWORD chunk = GetNextChunkSize(count, length);
216 if (ReadFile(handle_, static_cast<char*>(buffer) + count, 244 if (ReadFile(handle_, static_cast<char*>(buffer) + count,
217 chunk, &len, NULL) == FALSE) { 245 chunk, &len, NULL) == FALSE) {
218 return (0 < count) ? count : 0; 246 return count;
219 } 247 }
220 count += len; 248 count += len;
221 } 249 }
222 return count; 250 return count;
223 } 251 }
224 252
225 size_t SyncSocket::Peek() { 253 size_t SyncSocket::Peek() {
226 DWORD available = 0; 254 DWORD available = 0;
227 PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL); 255 PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL);
228 return available; 256 return available;
229 } 257 }
230 258
231 CancelableSyncSocket::CancelableSyncSocket() 259 CancelableSyncSocket::CancelableSyncSocket()
232 : shutdown_event_(true, false), file_operation_(true, false) { 260 : shutdown_event_(true, false), file_operation_(true, false) {
233 } 261 }
234 262
235 CancelableSyncSocket::CancelableSyncSocket(Handle handle) 263 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
236 : SyncSocket(handle), shutdown_event_(true, false), 264 : SyncSocket(handle), shutdown_event_(true, false),
237 file_operation_(true, false) { 265 file_operation_(true, false) {
238 } 266 }
239 267
240 bool CancelableSyncSocket::Shutdown() { 268 bool CancelableSyncSocket::Shutdown() {
241 // This doesn't shut down the pipe immediately, but subsequent Receive or Send 269 // This doesn't shut down the pipe immediately, but subsequent Receive or Send
242 // methods will fail straight away. 270 // methods will fail straight away.
243 shutdown_event_.Signal(); 271 shutdown_event_.Signal();
244 return true; 272 return true;
245 } 273 }
246 274
247 bool CancelableSyncSocket::Close() { 275 bool CancelableSyncSocket::Close() {
248 bool ret = SyncSocket::Close(); 276 const bool result = SyncSocket::Close();
249 shutdown_event_.Reset(); 277 shutdown_event_.Reset();
250 return ret; 278 return result;
251 } 279 }
252 280
253 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { 281 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
254 static const DWORD kWaitTimeOutInMs = 500; 282 static const DWORD kWaitTimeOutInMs = 500;
255 return CancelableFileOperation( 283 return CancelableFileOperation(
256 &WriteFile, handle_, reinterpret_cast<const char*>(buffer), 284 &WriteFile, handle_, reinterpret_cast<const char*>(buffer),
257 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); 285 length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs);
258 } 286 }
259 287
260 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { 288 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) {
261 return CancelableFileOperation(&ReadFile, handle_, 289 return CancelableFileOperation(
262 reinterpret_cast<char*>(buffer), length, &file_operation_, 290 &ReadFile, handle_, reinterpret_cast<char*>(buffer), length,
263 &shutdown_event_, this, INFINITE); 291 &file_operation_, &shutdown_event_, this, INFINITE);
292 }
293
294 size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer,
295 size_t length,
296 TimeDelta timeout) {
297 return CancelableFileOperation(
298 &ReadFile, handle_, reinterpret_cast<char*>(buffer), length,
299 &file_operation_, &shutdown_event_, this, timeout.InMilliseconds());
264 } 300 }
265 301
266 // static 302 // static
267 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 303 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
268 CancelableSyncSocket* socket_b) { 304 CancelableSyncSocket* socket_b) {
269 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); 305 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true);
270 } 306 }
271 307
272
273 } // namespace base 308 } // namespace base
OLDNEW
« base/sync_socket_unittest.cc ('K') | « base/sync_socket_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698