From 3f190bb4bb4c186ccf85128ccdf7520f3cb49509 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Thu, 4 May 2017 02:02:38 -0700 Subject: [PATCH] Some file stream changes --- DS4Windows/DS4Library/DS4Device.cs | 5 ++++ DS4Windows/HidLibrary/HidDevice.cs | 43 +++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index ccc4916..85323b6 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -391,6 +391,11 @@ namespace DS4Windows { if (ds4Input == null) { + if (!hDevice.IsFileStreamOpen()) + { + hDevice.OpenFileStream(inputReport.Length); + } + Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> start"); sendOutputReport(true); // initialize the output report ds4Output = new Thread(performDs4Output); diff --git a/DS4Windows/HidLibrary/HidDevice.cs b/DS4Windows/HidLibrary/HidDevice.cs index a7a865d..6a3ec90 100644 --- a/DS4Windows/HidLibrary/HidDevice.cs +++ b/DS4Windows/HidLibrary/HidDevice.cs @@ -83,6 +83,25 @@ namespace DS4Windows IsExclusive = isExclusive; } + public void OpenFileStream(int reportSize) + { + if (fileStream == null && !safeReadHandle.IsInvalid) + { + fileStream = new FileStream(safeReadHandle, FileAccess.ReadWrite, reportSize, true); + } + } + + public bool IsFileStreamOpen() + { + bool result = false; + if (fileStream != null) + { + result = !fileStream.SafeFileHandle.IsInvalid && !fileStream.SafeFileHandle.IsClosed; + } + + return result; + } + public void CloseDevice() { if (!IsOpen) return; @@ -140,6 +159,7 @@ namespace DS4Windows NativeMethods.HidP_GetCaps(preparsedDataPointer, ref capabilities); NativeMethods.HidD_FreePreparsedData(preparsedDataPointer); } + return new HidDeviceCapabilities(capabilities); } @@ -151,19 +171,25 @@ namespace DS4Windows { fileStream.Close(); } - catch (IOException) - { - } + catch (IOException) { } + catch (OperationCanceledException) { } } fileStream = null; Console.WriteLine("Close fs"); if (safeReadHandle != null && !safeReadHandle.IsInvalid) { - safeReadHandle.Close(); - Console.WriteLine("Close sh"); - + try + { + if (!safeReadHandle.IsClosed) + { + safeReadHandle.Close(); + Console.WriteLine("Close sh"); + } + } + catch (IOException) { } } + safeReadHandle = null; } @@ -193,6 +219,7 @@ namespace DS4Windows return ReadStatus.ReadError; } } + public ReadStatus ReadFile(byte[] inputBuffer) { if (safeReadHandle == null) @@ -223,6 +250,7 @@ namespace DS4Windows safeReadHandle = OpenHandle(_devicePath, true); if (fileStream == null && !safeReadHandle.IsInvalid) fileStream = new FileStream(safeReadHandle, FileAccess.ReadWrite, inputBuffer.Length, true); + if (!safeReadHandle.IsInvalid && fileStream.CanRead) { Task readFileTask = new Task(() => ReadWithFileStreamTask(inputBuffer)); @@ -261,9 +289,6 @@ namespace DS4Windows } } - - - return ReadStatus.ReadError; }