| 184 | |
| 185 | template<typename TCallback> |
| 186 | static bool FillBufferWithTimeout(HANDLE handle, OVERLAPPED& ov, char* buffer, std::uint32_t bufferSize, TCallback callback) |
| 187 | { |
| 188 | constexpr std::uint64_t TimeoutMs = 100; |
| 189 | |
| 190 | std::uint32_t bytesRead = 0; |
| 191 | std::uint64_t startTime = ::GetTickCount64(); |
| 192 | std::uint64_t now = startTime; |
| 193 | |
| 194 | while (true) { |
| 195 | if (!::ReadFile(handle, buffer + bytesRead, bufferSize - bytesRead, NULL, &ov)) { |
| 196 | DWORD err = GetLastError(); |
| 197 | if (err == ERROR_IO_PENDING) { |
| 198 | DWORD waitResult = ::WaitForSingleObject(ov.hEvent, (DWORD)(TimeoutMs - (now - startTime))); |
| 199 | if (waitResult == WAIT_TIMEOUT) { |
| 200 | ::CancelIo(handle); |
| 201 | break; |
| 202 | } else if (waitResult != WAIT_OBJECT_0) { |
| 203 | break; |
| 204 | } |
| 205 | } else { |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | DWORD partialBytesRead = 0; |
| 211 | if (!::GetOverlappedResult(handle, &ov, &partialBytesRead, FALSE)) { |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | bytesRead += partialBytesRead; |
| 216 | if (callback(StringView(buffer, bytesRead))) { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | now = ::GetTickCount64(); |
| 221 | if (now - startTime >= TimeoutMs || bytesRead >= bufferSize) { |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | static void CheckConsoleCapabilities() |
| 230 | { |
no test coverage detected