Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Adam Rice | d47f498 | 2024-09-03 16:18:23 | [diff] [blame] | 12 | #include "base/check.h" |
Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 14 | #include "base/containers/span.h" |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 15 | #include "base/logging.h" |
Peter Boström | 6e2fd08 | 2024-01-23 01:17:55 | [diff] [blame] | 16 | #include "base/notimplemented.h" |
wfh | cb35f4e7 | 2015-09-22 18:10:11 | [diff] [blame] | 17 | #include "base/rand_util.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 18 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 19 | #include "base/win/scoped_handle.h" |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 20 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 21 | namespace base { |
| 22 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 23 | using win::ScopedHandle; |
| 24 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 25 | namespace { |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 26 | // IMPORTANT: do not change how this name is generated because it will break |
| 27 | // in sandboxed scenarios as we might have by-name policies that allow pipe |
| 28 | // creation. Also keep the secure random number generation. |
| 29 | const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 30 | const size_t kPipePathMax = std::size(kPipeNameFormat) + (3 * 10) + 1; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 31 | |
| 32 | // To avoid users sending negative message lengths to Send/Receive |
| 33 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 34 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 35 | |
| 36 | const int kOutBufferSize = 4096; |
| 37 | const int kInBufferSize = 4096; |
| 38 | const int kDefaultTimeoutMilliSeconds = 1000; |
| 39 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 40 | bool CreatePairImpl(ScopedHandle* socket_a, |
| 41 | ScopedHandle* socket_b, |
| 42 | bool overlapped) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 43 | DCHECK_NE(socket_a, socket_b); |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 44 | DCHECK(!socket_a->is_valid()); |
| 45 | DCHECK(!socket_b->is_valid()); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 46 | |
| 47 | wchar_t name[kPipePathMax]; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 48 | ScopedHandle handle_a; |
| 49 | DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 50 | if (overlapped) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 51 | flags |= FILE_FLAG_OVERLAPPED; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 52 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 53 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 54 | do { |
wfh | cb35f4e7 | 2015-09-22 18:10:11 | [diff] [blame] | 55 | unsigned long rnd_name; |
danakj | 95305d27 | 2024-05-09 20:38:44 | [diff] [blame] | 56 | RandBytes(byte_span_from_ref(rnd_name)); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 57 | |
Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 58 | UNSAFE_TODO(swprintf(name, kPipePathMax, kPipeNameFormat, |
| 59 | GetCurrentProcessId(), GetCurrentThreadId(), |
| 60 | rnd_name)); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 61 | |
| 62 | handle_a.Set(CreateNamedPipeW( |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 63 | name, flags, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, kOutBufferSize, |
| 64 | kInBufferSize, kDefaultTimeoutMilliSeconds, NULL)); |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 65 | } while (!handle_a.is_valid() && (GetLastError() == ERROR_PIPE_BUSY)); |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 66 | |
Adam Rice | d47f498 | 2024-09-03 16:18:23 | [diff] [blame] | 67 | CHECK(handle_a.is_valid()); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 68 | |
| 69 | // The SECURITY_ANONYMOUS flag means that the server side (handle_a) cannot |
| 70 | // impersonate the client (handle_b). This allows us not to care which side |
[email protected] | 774bdcce | 2012-01-19 03:44:38 | [diff] [blame] | 71 | // ends up in which side of a privilege boundary. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 72 | flags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 73 | if (overlapped) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 74 | flags |= FILE_FLAG_OVERLAPPED; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 75 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 76 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 77 | ScopedHandle handle_b(CreateFileW(name, GENERIC_READ | GENERIC_WRITE, |
| 78 | 0, // no sharing. |
| 79 | NULL, // default security attributes. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 80 | OPEN_EXISTING, // opens existing pipe. |
| 81 | flags, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 82 | NULL)); // no template file. |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 83 | if (!handle_b.is_valid()) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 84 | DPLOG(ERROR) << "CreateFileW failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 85 | return false; |
| 86 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 87 | |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 88 | if (!ConnectNamedPipe(handle_a.get(), NULL)) { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 89 | DWORD error = GetLastError(); |
| 90 | if (error != ERROR_PIPE_CONNECTED) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 91 | DPLOG(ERROR) << "ConnectNamedPipe failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 95 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 96 | *socket_a = std::move(handle_a); |
| 97 | *socket_b = std::move(handle_b); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 98 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 102 | // Inline helper to avoid having the cast everywhere. |
| 103 | DWORD GetNextChunkSize(size_t current_pos, size_t max_size) { |
| 104 | // The following statement is for 64 bit portability. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 105 | return static_cast<DWORD>(((max_size - current_pos) <= UINT_MAX) |
| 106 | ? (max_size - current_pos) |
| 107 | : UINT_MAX); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Template function that supports calling ReadFile or WriteFile in an |
| 111 | // overlapped fashion and waits for IO completion. The function also waits |
| 112 | // on an event that can be used to cancel the operation. If the operation |
| 113 | // is cancelled, the function returns and closes the relevant socket object. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 114 | template <typename DataType, typename Function> |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 115 | size_t CancelableFileOperation(Function operation, |
| 116 | HANDLE file, |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 117 | span<DataType> buffer, |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 118 | WaitableEvent* io_event, |
| 119 | WaitableEvent* cancel_event, |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 120 | CancelableSyncSocket* socket, |
| 121 | DWORD timeout_in_ms) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 122 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 123 | // The buffer must be byte size or the length check won't make much sense. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 124 | static_assert(sizeof(DataType) == 1u, "incorrect buffer type"); |
| 125 | CHECK(!buffer.empty()); |
| 126 | CHECK_LE(buffer.size(), kMaxMessageLength); |
| 127 | CHECK_NE(file, SyncSocket::kInvalidHandle); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 128 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 129 | // Track the finish time so we can calculate the timeout as data is read. |
| 130 | TimeTicks current_time, finish_time; |
| 131 | if (timeout_in_ms != INFINITE) { |
| 132 | current_time = TimeTicks::Now(); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 133 | finish_time = current_time + base::Milliseconds(timeout_in_ms); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 136 | size_t count = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 137 | do { |
| 138 | // The OVERLAPPED structure will be modified by ReadFile or WriteFile. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 139 | OVERLAPPED ol = {0}; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 140 | ol.hEvent = io_event->handle(); |
| 141 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 142 | const DWORD chunk_size = GetNextChunkSize(count, buffer.size()); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 143 | // This is either the ReadFile or WriteFile call depending on whether |
| 144 | // we're receiving or sending data. |
[email protected] | 23bf698 | 2012-02-03 12:44:44 | [diff] [blame] | 145 | DWORD len = 0; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 146 | auto operation_buffer = buffer.subspan(count, chunk_size); |
| 147 | // SAFETY: The below static_cast is in range for DWORD because |
| 148 | // `operation_buffer` is constructed with a DWORD length above from |
| 149 | // `chunk_size`. |
| 150 | const BOOL operation_ok = |
| 151 | operation(file, operation_buffer.data(), |
| 152 | static_cast<DWORD>(operation_buffer.size()), &len, &ol); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 153 | if (!operation_ok) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 154 | if (::GetLastError() == ERROR_IO_PENDING) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 155 | HANDLE events[] = {io_event->handle(), cancel_event->handle()}; |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 156 | const DWORD wait_result = WaitForMultipleObjects( |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 157 | std::size(events), events, FALSE, |
Avi Drissman | e3b70bf | 2019-01-04 19:50:22 | [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. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 171 | if (!GetOverlappedResult(file, &ol, &len, TRUE)) { |
dalecurtis | 3bdbb016 | 2014-09-16 00:41:22 | [diff] [blame] | 172 | len = 0; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 173 | } |
dalecurtis | 3bdbb016 | 2014-09-16 00:41:22 | [diff] [blame] | 174 | |
| 175 | if (wait_result == WAIT_OBJECT_0 + 1) { |
| 176 | DVLOG(1) << "Shutdown was signaled. Closing socket."; |
| 177 | socket->Close(); |
| 178 | return count; |
| 179 | } |
| 180 | |
| 181 | // Timeouts will be handled by the while() condition below since |
| 182 | // GetOverlappedResult() may complete successfully after CancelIo(). |
| 183 | DCHECK(wait_result == WAIT_OBJECT_0 + 0 || wait_result == WAIT_TIMEOUT); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 184 | } else { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 185 | break; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 186 | } |
| 187 | } |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 188 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 189 | count += len; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 190 | |
| 191 | // Quit the operation if we can't write/read anymore. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 192 | if (len != chunk_size) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 193 | break; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 194 | } |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 195 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 196 | // Since TimeTicks::Now() is expensive, only bother updating the time if we |
| 197 | // have more work to do. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 198 | if (timeout_in_ms != INFINITE && count < buffer.size()) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 199 | current_time = base::TimeTicks::Now(); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 200 | } |
| 201 | } while (count < buffer.size() && |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 202 | (timeout_in_ms == INFINITE || current_time < finish_time)); |
| 203 | |
| 204 | return count; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace |
| 208 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 209 | // static |
| 210 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 211 | return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false); |
| 212 | } |
| 213 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 214 | void SyncSocket::Close() { |
| 215 | handle_.Close(); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 216 | } |
| 217 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 218 | size_t SyncSocket::Send(span<const uint8_t> data) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 219 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 220 | CHECK_LE(data.size(), kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 221 | DCHECK(IsValid()); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 222 | size_t count = 0; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 223 | while (count < data.size()) { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 224 | DWORD len; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 225 | const DWORD chunk_size = GetNextChunkSize(count, data.size()); |
| 226 | auto data_chunk = data.subspan(count, chunk_size); |
| 227 | // SAFETY: The below static_cast is in range for DWORD because `data_chunk` |
| 228 | // is constructed with a DWORD length above from `chunk_size`. |
| 229 | if (::WriteFile(handle(), data_chunk.data(), |
| 230 | static_cast<DWORD>(data_chunk.size()), &len, |
| 231 | NULL) == FALSE) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 232 | return count; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 233 | } |
| 234 | count += len; |
| 235 | } |
| 236 | return count; |
| 237 | } |
| 238 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 239 | size_t SyncSocket::ReceiveWithTimeout(span<uint8_t> buffer, TimeDelta timeout) { |
| 240 | NOTIMPLEMENTED(); |
| 241 | return 0; |
| 242 | } |
| 243 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 244 | size_t SyncSocket::Receive(span<uint8_t> buffer) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 245 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 246 | CHECK_LE(buffer.size(), kMaxMessageLength); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 247 | DCHECK(IsValid()); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 248 | size_t count = 0; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 249 | while (count < buffer.size()) { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 250 | DWORD len; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 251 | const DWORD chunk_size = GetNextChunkSize(count, buffer.size()); |
| 252 | auto data_chunk = buffer.subspan(count, chunk_size); |
| 253 | // SAFETY: The below static_cast is in range for DWORD because `data_chunk` |
| 254 | // is constructed with a DWORD length above from `chunk_size`. |
| 255 | if (::ReadFile(handle(), data_chunk.data(), |
| 256 | static_cast<DWORD>(data_chunk.size()), &len, |
Nico Weber | 2499aee | 2017-10-17 20:56:49 | [diff] [blame] | 257 | 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] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 265 | size_t SyncSocket::Peek() { |
| 266 | DWORD available = 0; |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 267 | PeekNamedPipe(handle(), NULL, 0, NULL, &available, NULL); |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 268 | return available; |
| 269 | } |
| 270 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 271 | bool SyncSocket::IsValid() const { |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 272 | return handle_.is_valid(); |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 273 | } |
| 274 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 275 | SyncSocket::Handle SyncSocket::handle() const { |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 276 | return handle_.get(); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 277 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 278 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 279 | SyncSocket::Handle SyncSocket::Release() { |
Lei Zhang | 04c0bcb | 2022-02-04 04:13:45 | [diff] [blame] | 280 | return handle_.release(); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 281 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 282 | |
| 283 | bool CancelableSyncSocket::Shutdown() { |
| 284 | // This doesn't shut down the pipe immediately, but subsequent Receive or Send |
| 285 | // methods will fail straight away. |
| 286 | shutdown_event_.Signal(); |
| 287 | return true; |
| 288 | } |
| 289 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 290 | void CancelableSyncSocket::Close() { |
| 291 | SyncSocket::Close(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 292 | shutdown_event_.Reset(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 293 | } |
| 294 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 295 | size_t CancelableSyncSocket::Send(span<const uint8_t> data) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 296 | static const DWORD kWaitTimeOutInMs = 500; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 297 | return CancelableFileOperation(&::WriteFile, handle(), data, &file_operation_, |
| 298 | &shutdown_event_, this, kWaitTimeOutInMs); |
| 299 | } |
| 300 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 301 | size_t CancelableSyncSocket::Receive(span<uint8_t> buffer) { |
| 302 | return CancelableFileOperation(&::ReadFile, handle(), buffer, |
| 303 | &file_operation_, &shutdown_event_, this, |
| 304 | INFINITE); |
[email protected] |
|