MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / FillBufferWithTimeout

Function FillBufferWithTimeout

Sources/nCine/Application.cpp:186–227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184
185template<typename TCallback>
186static 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
229static void CheckConsoleCapabilities()
230{

Callers 1

CheckConsoleCapabilitiesFunction · 0.85

Calls 1

callbackClass · 0.85

Tested by

no test coverage detected