[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sync_socket.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <stddef.h> |
| 9 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 10 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "base/macros.h" |
wfh | cb35f4e7 | 2015-09-22 18:10:11 | [diff] [blame] | 12 | #include "base/rand_util.h" |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 14 | #include "base/win/scoped_handle.h" |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 15 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 16 | namespace base { |
| 17 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 18 | using win::ScopedHandle; |
| 19 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 20 | namespace { |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 21 | // IMPORTANT: do not change how this name is generated because it will break |
| 22 | // in sandboxed scenarios as we might have by-name policies that allow pipe |
| 23 | // creation. Also keep the secure random number generation. |
| 24 | const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; |
| 25 | const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 26 | |
| 27 | // To avoid users sending negative message lengths to Send/Receive |
| 28 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 29 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 30 | |
| 31 | const int kOutBufferSize = 4096; |
| 32 | const int kInBufferSize = 4096; |
| 33 | const int kDefaultTimeoutMilliSeconds = 1000; |
| 34 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 35 | bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 36 | DCHECK_NE(socket_a, socket_b); |
| 37 | DCHECK_EQ(*socket_a, SyncSocket::kInvalidHandle); |
| 38 | DCHECK_EQ(*socket_b, SyncSocket::kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 39 | |
| 40 | wchar_t name[kPipePathMax]; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 41 | ScopedHandle handle_a; |
| 42 | DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; |
| 43 | if (overlapped) |
| 44 | flags |= FILE_FLAG_OVERLAPPED; |
| 45 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 46 | do { |
wfh | cb35f4e7 | 2015-09-22 18:10:11 | [diff] [blame] | 47 | unsigned long rnd_name; |
| 48 | RandBytes(&rnd_name, sizeof(rnd_name)); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 49 | |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 50 | swprintf(name, kPipePathMax, |
| 51 | kPipeNameFormat, |
| 52 | GetCurrentProcessId(), |
| 53 | GetCurrentThreadId(), |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 54 | rnd_name); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 55 | |
| 56 | handle_a.Set(CreateNamedPipeW( |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 57 | name, |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 58 | flags, |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 59 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 60 | 1, |
| 61 | kOutBufferSize, |
| 62 | kInBufferSize, |
| 63 | kDefaultTimeoutMilliSeconds, |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 64 | NULL)); |
| 65 | } while (!handle_a.IsValid() && |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 66 | (GetLastError() == ERROR_PIPE_BUSY)); |
| 67 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 68 | if (!handle_a.IsValid()) { |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 69 | NOTREACHED(); |
| 70 | return false; |
| 71 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 72 | |
| 73 | // The SECURITY_ANONYMOUS flag means that the server side (handle_a) cannot |
| 74 | // impersonate the client (handle_b). This allows us not to care which side |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 75 | // ends up in which side of a privilege boundary. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 76 | flags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS; |
| 77 | if (overlapped) |
| 78 | flags |= FILE_FLAG_OVERLAPPED; |
| 79 | |
| 80 | ScopedHandle handle_b(CreateFileW(name, |
| 81 | GENERIC_READ | GENERIC_WRITE, |
| 82 | 0, // no sharing. |
| 83 | NULL, // default security attributes. |
| 84 | OPEN_EXISTING, // opens existing pipe. |
| 85 | flags, |
| 86 | NULL)); // no template file. |
| 87 | if (!handle_b.IsValid()) { |
| 88 | DPLOG(ERROR) << "CreateFileW failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 89 | return false; |
| 90 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 91 | |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 92 | if (!ConnectNamedPipe(handle_a.Get(), NULL)) { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 93 | DWORD error = GetLastError(); |
| 94 | if (error != ERROR_PIPE_CONNECTED) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 95 | DPLOG(ERROR) << "ConnectNamedPipe failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 99 | |
| 100 | *socket_a = handle_a.Take(); |
| 101 | *socket_b = handle_b.Take(); |
| 102 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 106 | // Inline helper to avoid having the cast everywhere. |
| 107 | DWORD GetNextChunkSize(size_t current_pos, size_t max_size) { |
| 108 | // The following statement is for 64 bit portability. |
| 109 | return static_cast<DWORD>(((max_size - current_pos) <= UINT_MAX) ? |
| 110 | (max_size - current_pos) : UINT_MAX); |
| 111 | } |
| 112 | |
| 113 | // Template function that supports calling ReadFile or WriteFile in an |
| 114 | // overlapped fashion and waits for IO completion. The function also waits |
| 115 | // on an event that can be used to cancel the operation. If the operation |
| 116 | // is cancelled, the function returns and closes the relevant socket object. |
| 117 | template <typename BufferType, typename Function> |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 118 | size_t CancelableFileOperation(Function operation, |
| 119 | HANDLE file, |
| 120 | BufferType* buffer, |
| 121 | size_t length, |
| 122 | WaitableEvent* io_event, |
| 123 | WaitableEvent* cancel_event, |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 124 | CancelableSyncSocket* socket, |
| 125 | DWORD timeout_in_ms) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 126 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 127 | // The buffer must be byte size or the length check won't make much sense. |
avi | 4ec0dff | 2015-11-24 14:26:24 | [diff] [blame] | 128 | static_assert(sizeof(buffer[0]) == sizeof(char), "incorrect buffer type"); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 129 | DCHECK_GT(length, 0u); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 130 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 131 | DCHECK_NE(file, SyncSocket::kInvalidHandle); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 132 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 133 | // Track the finish time so we can calculate the timeout as data is read. |
| 134 | TimeTicks current_time, finish_time; |
| 135 | if (timeout_in_ms != INFINITE) { |
| 136 | current_time = TimeTicks::Now(); |
| 137 | finish_time = |
| 138 | current_time + base::TimeDelta::FromMilliseconds(timeout_in_ms); |
| 139 | } |
| 140 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 141 | size_t count = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 142 | do { |
| 143 | // The OVERLAPPED structure will be modified by ReadFile or WriteFile. |
| 144 | OVERLAPPED ol = { 0 }; |
| 145 | ol.hEvent = io_event->handle(); |
| 146 | |
| 147 | const DWORD chunk = GetNextChunkSize(count, length); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 148 | // This is either the ReadFile or WriteFile call depending on whether |
| 149 | // we're receiving or sending data. |
[email protected] | 23bf698 | 2012-02-03 12:44:44 | [diff] [blame] | 150 | DWORD len = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 151 | const BOOL operation_ok = operation( |
| 152 | file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol); |
| 153 | if (!operation_ok) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 154 | if (::GetLastError() == ERROR_IO_PENDING) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 155 | HANDLE events[] = { io_event->handle(), cancel_event->handle() }; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 156 | const int wait_result = WaitForMultipleObjects( |
viettrungluu | 805eabb | 2014-10-16 04:02:49 | [diff] [blame] | 157 | arraysize(events), events, FALSE, |
pkasting | 9cf9b94 | 2014-10-01 22:18:43 | [diff] [blame] | 158 | timeout_in_ms == INFINITE ? |
| 159 | timeout_in_ms : |
| 160 | static_cast<DWORD>( |
| 161 | (finish_time - current_time).InMilliseconds())); |
dalecurtis | 3bdbb016 | 2014-09-16 00:41:22 | [diff] [blame] | 162 | if (wait_result != WAIT_OBJECT_0 + 0) { |
| 163 | // CancelIo() doesn't synchronously cancel outstanding IO, only marks |
| 164 | // outstanding IO for cancellation. We must call GetOverlappedResult() |
| 165 | // below to ensure in flight writes complete before returning. |
[email protected] | 23bf698 | 2012-02-03 12:44:44 | [diff] [blame] | 166 | CancelIo(file); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 167 | } |
dalecurtis | 3bdbb016 | 2014-09-16 00:41:22 | [diff] [blame] | 168 | |
| 169 | // We set the |bWait| parameter to TRUE for GetOverlappedResult() to |
| 170 | // ensure writes are complete before returning. |
| 171 | if (!GetOverlappedResult(file, &ol, &len, TRUE)) |
| 172 | len = 0; |
| 173 | |
| 174 | if (wait_result == WAIT_OBJECT_0 + 1) { |
| 175 | DVLOG(1) << "Shutdown was signaled. Closing socket."; |
| 176 | socket->Close(); |
| 177 | return count; |
| 178 | } |
| 179 | |
| 180 | // Timeouts will be handled by the while() condition below since |
| 181 | // GetOverlappedResult() may complete successfully after CancelIo(). |
| 182 | DCHECK(wait_result == WAIT_OBJECT_0 + 0 || wait_result == WAIT_TIMEOUT); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 183 | } else { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 184 | break; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 185 | } |
| 186 | } |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 187 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 188 | count += len; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 189 | |
| 190 | // Quit the operation if we can't write/read anymore. |
| 191 | if (len != chunk) |
| 192 | break; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 193 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 194 | // Since TimeTicks::Now() is expensive, only bother updating the time if we |
| 195 | // have more work to do. |
| 196 | if (timeout_in_ms != INFINITE && count < length) |
| 197 | current_time = base::TimeTicks::Now(); |
| 198 | } while (count < length && |
| 199 | (timeout_in_ms == INFINITE || current_time < finish_time)); |
| 200 | |
| 201 | return count; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace |
| 205 | |
[email protected] | 0f248ef | 2013-05-23 04:34:11 | [diff] [blame] | 206 | #if defined(COMPONENT_BUILD) |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 207 | const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
[email protected] | 0f248ef | 2013-05-23 04:34:11 | [diff] [blame] | 208 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 209 | |
| 210 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 211 | |
| 212 | SyncSocket::~SyncSocket() { |
| 213 | Close(); |
| 214 | } |
| 215 | |
| 216 | // static |
| 217 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 218 | return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false); |
| 219 | } |
| 220 | |
burnik | 3d67005 | 2014-09-08 06:58:22 | [diff] [blame] | 221 | // static |
| 222 | SyncSocket::Handle SyncSocket::UnwrapHandle( |
| 223 | const TransitDescriptor& descriptor) { |
| 224 | return descriptor; |
| 225 | } |
| 226 | |
| 227 | bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle, |
| 228 | TransitDescriptor* descriptor) { |
| 229 | DCHECK(descriptor); |
| 230 | if (!::DuplicateHandle(GetCurrentProcess(), handle(), peer_process_handle, |
| 231 | descriptor, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 232 | DPLOG(ERROR) << "Cannot duplicate socket handle for peer process."; |
| 233 | return false; |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 238 | bool SyncSocket::Close() { |
| 239 | if (handle_ == kInvalidHandle) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 240 | return true; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 241 | |
brucedawson | d350943 | 2015-09-18 18:28:13 | [diff] [blame] | 242 | const BOOL result = CloseHandle(handle_); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 243 | handle_ = kInvalidHandle; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 244 | return result == TRUE; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 248 | ThreadRestrictions::AssertIOAllowed(); |
| 249 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 250 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 251 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 252 | size_t count = 0; |
| 253 | while (count < length) { |
| 254 | DWORD len; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 255 | DWORD chunk = GetNextChunkSize(count, length); |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame^] | 256 | if (::WriteFile(handle_, static_cast<const char*>(buffer) + count, chunk, |
| 257 | &len, NULL) == FALSE) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 258 | return count; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 259 | } |
| 260 | count += len; |
| 261 | } |
| 262 | return count; |
| 263 | } |
| 264 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 265 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 266 | size_t length, |
| 267 | TimeDelta timeout) { |
| 268 | NOTIMPLEMENTED(); |
| 269 | return 0; |
| 270 | } |
| 271 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 272 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 273 | ThreadRestrictions::AssertIOAllowed(); |
| 274 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 275 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 276 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 277 | size_t count = 0; |
| 278 | while (count < length) { |
| 279 | DWORD len; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 280 | DWORD chunk = GetNextChunkSize(count, length); |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame^] | 281 | if (::ReadFile(handle_, static_cast<char*>(buffer) + count, chunk, &len, |
| 282 | NULL) == FALSE) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 283 | return count; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 284 | } |
| 285 | count += len; |
| 286 | } |
| 287 | return count; |
| 288 | } |
| 289 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 290 | size_t SyncSocket::Peek() { |
| 291 | DWORD available = 0; |
| 292 | PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL); |
| 293 | return available; |
| 294 | } |
| 295 | |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 296 | SyncSocket::Handle SyncSocket::Release() { |
| 297 | Handle r = handle_; |
| 298 | handle_ = kInvalidHandle; |
| 299 | return r; |
| 300 | } |
| 301 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 302 | CancelableSyncSocket::CancelableSyncSocket() |
gab | 7c19b196 | 2016-06-08 20:24:08 | [diff] [blame] | 303 | : shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 304 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 305 | file_operation_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 306 | base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 307 | |
| 308 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
gab | 7c19b196 | 2016-06-08 20:24:08 | [diff] [blame] | 309 | : SyncSocket(handle), |
| 310 | shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 311 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 312 | file_operation_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 313 | base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 314 | |
| 315 | bool CancelableSyncSocket::Shutdown() { |
| 316 | // This doesn't shut down the pipe immediately, but subsequent Receive or Send |
| 317 | // methods will fail straight away. |
| 318 | shutdown_event_.Signal(); |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | bool CancelableSyncSocket::Close() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 323 | const bool result = SyncSocket::Close(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 324 | shutdown_event_.Reset(); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 325 | return result; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 329 | static const DWORD kWaitTimeOutInMs = 500; |
| 330 | return CancelableFileOperation( |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame^] | 331 | &::WriteFile, handle_, reinterpret_cast<const char*>(buffer), length, |
| 332 | &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 336 | return CancelableFileOperation( |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame^] | 337 | &::ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 338 | &file_operation_, &shutdown_event_, this, INFINITE); |
| 339 | } |
| 340 | |
| 341 | size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer, |
| 342 | size_t length, |
| 343 | TimeDelta timeout) { |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame^] | 344 | return CancelableFileOperation(&::ReadFile, handle_, |
| 345 | reinterpret_cast<char*>(buffer), length, |
| 346 | &file_operation_, &shutdown_event_, this, |
| 347 | static_cast<DWORD>(timeout.InMilliseconds())); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // static |
| 351 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 352 | CancelableSyncSocket* socket_b) { |
| 353 | return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); |
| 354 | } |
| 355 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 356 | } // namespace base |