From 273b111a746866d5b259cf9106cf63bb7ff34c2d Mon Sep 17 00:00:00 2001 From: Nguyen Duy Date: Tue, 11 Mar 2025 09:28:40 +0700 Subject: [PATCH] fix(core): fix controller logic, add lasterror debug --- app/Vdd/Controller.cs | 7 +++++-- app/Vdd/Core.cs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/Vdd/Controller.cs b/app/Vdd/Controller.cs index a2f30bb..066d5de 100644 --- a/app/Vdd/Controller.cs +++ b/app/Vdd/Controller.cs @@ -18,7 +18,7 @@ namespace ParsecVDisplay.Vdd Cancellation = new CancellationTokenSource(); UpdateThread = new Thread(() => UpdateLoop(Cancellation.Token)); - UpdateThread.IsBackground = false; + UpdateThread.IsBackground = true; UpdateThread.Priority = ThreadPriority.Highest; StatusThread = new Thread(() => StatusLoop(Cancellation.Token)); @@ -54,6 +54,7 @@ namespace ParsecVDisplay.Vdd static void StatusLoop(CancellationToken cancellation) { + bool first = true; var sw = Stopwatch.StartNew(); while (true) @@ -61,8 +62,10 @@ namespace ParsecVDisplay.Vdd if (cancellation.IsCancellationRequested) break; - if (sw.ElapsedMilliseconds >= 2000) + if (first || sw.ElapsedMilliseconds >= 2000) { + first = false; + var status = QueryStatus(out var _); unsafe { diff --git a/app/Vdd/Core.cs b/app/Vdd/Core.cs index 41d6bf0..0ce1803 100644 --- a/app/Vdd/Core.cs +++ b/app/Vdd/Core.cs @@ -151,14 +151,26 @@ namespace ParsecVDisplay.Vdd int outputLength = result != null ? sizeof(int) : 0; Overlapped.hEvent = Native.CreateEvent(null, false, false, null); - Native.DeviceIoControl(handle, (uint)code, + bool sent = Native.DeviceIoControl(handle, (uint)code, buffer, InBuffer.Length, result, outputLength, null, ref Overlapped); +#if DEBUG + if (code != IoCtlCode.IOCTL_UPDATE) + Console.WriteLine("[D] IoControl: {0}\n Sent: {1}, error: {2}", code, sent, DumpErrorCode(Marshal.GetLastWin32Error())); +#endif + if (!sent && Marshal.GetLastWin32Error() == 0x6) + return false; + bool success = Native.GetOverlappedResultEx(handle, ref Overlapped, out var NumberOfBytesTransferred, timeout, false); +#if DEBUG + if (code != IoCtlCode.IOCTL_UPDATE) + Console.WriteLine(" OverlappedResult: {0}, error: {1}", success, DumpErrorCode(Marshal.GetLastWin32Error())); +#endif + if (Overlapped.hEvent != IntPtr.Zero) Native.CloseHandle(Overlapped.hEvent); @@ -178,10 +190,24 @@ namespace ParsecVDisplay.Vdd result = output; return success; } + + private static string DumpErrorCode(int code) + { + string ret = code.ToString("X"); + + if (code == 0) + ret += " (SUCCESS)"; + else if (code == 0x6) + ret += " (ERROR_INVALID_HANDLE)"; + else if (code == 0x3E5) + ret += " (ERROR_IO_PENDING)"; + + return ret; + } private static class Native { - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeviceIoControl( IntPtr device, uint code, @@ -191,7 +217,7 @@ namespace ParsecVDisplay.Vdd ref OVERLAPPED lpOverlapped ); - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetOverlappedResultEx( IntPtr handle,