From 19377cf324356c2a8ac1a8f1b1d9794a0012c6b0 Mon Sep 17 00:00:00 2001 From: cupsos Date: Wed, 10 Nov 2021 17:22:59 +0900 Subject: [PATCH 1/2] Do not specefiy isAsync on FileStream ctor https://github.com/dotnet/runtime/blob/e5eafc9bf8be55087dcc2907e9c4d6fa1c2e5773/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs#L110 IsAsync provided by SafeFileHandle so we don't need to do --- src/Hid.Net/Windows/WindowsHidApiService.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Hid.Net/Windows/WindowsHidApiService.cs b/src/Hid.Net/Windows/WindowsHidApiService.cs index 97d454ee..6083322b 100644 --- a/src/Hid.Net/Windows/WindowsHidApiService.cs +++ b/src/Hid.Net/Windows/WindowsHidApiService.cs @@ -137,14 +137,9 @@ public Guid GetHidGuid() //TODO: These are not opening as async. If we do, we get an error. This is probably why cancellation tokens don't work. //https://github.com/MelbourneDeveloper/Device.Net/issues/188 -#if NETFRAMEWORK - private const bool _isAsync = false; -#else - private const bool _isAsync = true; -#endif - public Stream OpenRead(SafeFileHandle readSafeFileHandle, ushort readBufferSize) => new FileStream(readSafeFileHandle, FileAccess.Read, readBufferSize, _isAsync); - - public Stream OpenWrite(SafeFileHandle writeSafeFileHandle, ushort writeBufferSize) => new FileStream(writeSafeFileHandle, FileAccess.ReadWrite, writeBufferSize, _isAsync); + public Stream OpenRead(SafeFileHandle readSafeFileHandle, ushort readBufferSize) => new FileStream(readSafeFileHandle, FileAccess.Read, readBufferSize); + + public Stream OpenWrite(SafeFileHandle writeSafeFileHandle, ushort writeBufferSize) => new FileStream(writeSafeFileHandle, FileAccess.ReadWrite, writeBufferSize); #endregion #region Private Methods From 0bbebbb858bd0d1fff110c8c388fba50301a4c7f Mon Sep 17 00:00:00 2001 From: cupsos Date: Wed, 10 Nov 2021 17:50:53 +0900 Subject: [PATCH 2/2] Disable #215 for .NET 6 FileStream breaking change --- src/Device.Net/Windows/ApiService.cs | 32 ++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Device.Net/Windows/ApiService.cs b/src/Device.Net/Windows/ApiService.cs index a3ecc11a..4d2075ea 100644 --- a/src/Device.Net/Windows/ApiService.cs +++ b/src/Device.Net/Windows/ApiService.cs @@ -3,6 +3,7 @@ using Microsoft.Win32.SafeHandles; using System; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; #pragma warning disable CA1707 // Identifiers should not contain underscores #pragma warning disable CA1021 // Avoid out parameters @@ -14,17 +15,36 @@ namespace Device.Net.Windows internal class ApiService : IApiService { #region Fields -#if NETFRAMEWORK - private const uint FILE_FLAG_OVERLAPPED = 0; -#else private const uint FILE_FLAG_OVERLAPPED = 0x40000000; -#endif + + private readonly uint fileFlag; protected ILogger Logger { get; } #endregion #region Constructor - public ApiService(ILogger logger = null) => Logger = logger ?? NullLogger.Instance; + public ApiService(ILogger logger = null) + { + Logger = logger ?? NullLogger.Instance; + fileFlag = FILE_FLAG_OVERLAPPED; + var runtime = RuntimeInformation.FrameworkDescription; + if (runtime.StartsWith(".NET Framework", StringComparison.InvariantCultureIgnoreCase)) + { + fileFlag = 0; + } + else if (Regex.Match(runtime, @"^\.NET (\d+)") is { Success: true } m) + { + var majorVersion = m.Groups[1].Value; + if (majorVersion != "5") + { + var isSwitchConfigured = AppContext.TryGetSwitch("System.IO.UseNet5CompatFileStream", out var isSwitchEnabled); + if (!isSwitchConfigured || !isSwitchEnabled) + { + fileFlag = 0; + } + } + } + } #endregion #region Implementation @@ -44,7 +64,7 @@ internal class ApiService : IApiService private SafeFileHandle CreateConnection(string deviceId, FileAccessRights desiredAccess, uint shareMode, uint creationDisposition) { Logger.LogInformation("Calling {call} Area: {area} for DeviceId: {deviceId}. Desired Access: {desiredAccess}. Share mode: {shareMode}. Creation Disposition: {creationDisposition}", nameof(APICalls.CreateFile), nameof(ApiService), deviceId, desiredAccess, shareMode, creationDisposition); - return APICalls.CreateFile(deviceId, desiredAccess, shareMode, IntPtr.Zero, creationDisposition, FILE_FLAG_OVERLAPPED, IntPtr.Zero); + return APICalls.CreateFile(deviceId, desiredAccess, shareMode, IntPtr.Zero, creationDisposition, fileFlag, IntPtr.Zero); } #endregion