From fa4b3e187a812ebd607712ccc40ed9385c6b4ecf Mon Sep 17 00:00:00 2001 From: s2quake Date: Sat, 15 Jun 2024 09:45:03 +0900 Subject: [PATCH] fix: Apply compatibility. --- .../JSSoft.Communication.Client.csproj | 1 + .../JSSoft.Communication.Server.csproj | 3 +++ .../CancellationTokenSourceExtensions.cs | 23 ++++++++++++++++ .../CompilerFeatureRequiredAttribute.cs | 27 +++++++++++++++++++ .../Compatibility/IsExternalInit.cs | 15 +++++++++++ .../Compatibility/ObjectDisposedException.cs | 22 +++++++++++++++ .../Compatibility/RequiredMemberAttribute.cs | 17 ++++++++++++ .../SetsRequiredMembersAttribute.cs | 13 +++++++++ .../UnreachableException.cs | 5 ++-- src/JSSoft.Communication/EndPointUtility.cs | 1 + .../Grpc/AdaptorServer.cs | 2 ++ .../ServiceInstanceBuilder.cs | 4 +++ .../JSSoft.Communication.Tests.csproj | 1 + 13 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/JSSoft.Communication/Compatibility/CancellationTokenSourceExtensions.cs create mode 100644 src/JSSoft.Communication/Compatibility/CompilerFeatureRequiredAttribute.cs create mode 100644 src/JSSoft.Communication/Compatibility/IsExternalInit.cs create mode 100644 src/JSSoft.Communication/Compatibility/ObjectDisposedException.cs create mode 100644 src/JSSoft.Communication/Compatibility/RequiredMemberAttribute.cs create mode 100644 src/JSSoft.Communication/Compatibility/SetsRequiredMembersAttribute.cs rename src/JSSoft.Communication/{ => Compatibility}/UnreachableException.cs (79%) diff --git a/src/JSSoft.Communication.Client/JSSoft.Communication.Client.csproj b/src/JSSoft.Communication.Client/JSSoft.Communication.Client.csproj index 299e87d..571b750 100644 --- a/src/JSSoft.Communication.Client/JSSoft.Communication.Client.csproj +++ b/src/JSSoft.Communication.Client/JSSoft.Communication.Client.csproj @@ -26,6 +26,7 @@ SOFTWARE. --> CLIENT client communication\client + false diff --git a/src/JSSoft.Communication.Server/JSSoft.Communication.Server.csproj b/src/JSSoft.Communication.Server/JSSoft.Communication.Server.csproj index a520cc7..e612c23 100644 --- a/src/JSSoft.Communication.Server/JSSoft.Communication.Server.csproj +++ b/src/JSSoft.Communication.Server/JSSoft.Communication.Server.csproj @@ -21,11 +21,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + Exe SERVER server communication\server + false @@ -41,4 +43,5 @@ SOFTWARE. --> + \ No newline at end of file diff --git a/src/JSSoft.Communication/Compatibility/CancellationTokenSourceExtensions.cs b/src/JSSoft.Communication/Compatibility/CancellationTokenSourceExtensions.cs new file mode 100644 index 0000000..19a8f60 --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/CancellationTokenSourceExtensions.cs @@ -0,0 +1,23 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET8_0_OR_GREATER +#pragma warning disable + +using System.Threading; +using System.Threading.Tasks; + +namespace JSSoft.Communication; + +internal static class CancellationTokenSourceExtensions +{ + public static Task CancelAsync(this CancellationTokenSource @this) + { + return Task.Run(() => @this.Cancel()); + } + +} + +#endif // !NET8_0_OR_GREATER diff --git a/src/JSSoft.Communication/Compatibility/CompilerFeatureRequiredAttribute.cs b/src/JSSoft.Communication/Compatibility/CompilerFeatureRequiredAttribute.cs new file mode 100644 index 0000000..26eac4e --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/CompilerFeatureRequiredAttribute.cs @@ -0,0 +1,27 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET7_0_OR_GREATER +#pragma warning disable +// https://stackoverflow.com/questions/74447497/is-it-possible-to-use-the-c11-required-modifier-with-net-framework-4-8-and + +namespace System.Runtime.CompilerServices; + +[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] +internal sealed class CompilerFeatureRequiredAttribute : Attribute +{ + public CompilerFeatureRequiredAttribute(string featureName) + { + FeatureName = featureName; + } + + public string FeatureName { get; } + public bool IsOptional { get; init; } + + public const string RefStructs = nameof(RefStructs); + public const string RequiredMembers = nameof(RequiredMembers); +} + +#endif // !NET7_0_OR_GREATER diff --git a/src/JSSoft.Communication/Compatibility/IsExternalInit.cs b/src/JSSoft.Communication/Compatibility/IsExternalInit.cs new file mode 100644 index 0000000..7b44b3a --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/IsExternalInit.cs @@ -0,0 +1,15 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET5_0_OR_GREATER +#pragma warning disable +using System.ComponentModel; + +namespace System.Runtime.CompilerServices; + +[EditorBrowsable(EditorBrowsableState.Never)] +internal static class IsExternalInit { } + +#endif // !NET5_0_OR_GREATER diff --git a/src/JSSoft.Communication/Compatibility/ObjectDisposedException.cs b/src/JSSoft.Communication/Compatibility/ObjectDisposedException.cs new file mode 100644 index 0000000..85f9f5c --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/ObjectDisposedException.cs @@ -0,0 +1,22 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET7_0_OR_GREATER +#pragma warning disable +namespace JSSoft.Communication; + +internal class ObjectDisposedException(string objectName) + : System.ObjectDisposedException(objectName) +{ + public static void ThrowIf(bool condition, object instance) + { + if (condition == true) + { + throw new System.ObjectDisposedException(instance.GetType().Name); + } + } +} + +#endif // !NET7_0_OR_GREATER diff --git a/src/JSSoft.Communication/Compatibility/RequiredMemberAttribute.cs b/src/JSSoft.Communication/Compatibility/RequiredMemberAttribute.cs new file mode 100644 index 0000000..8818b1c --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/RequiredMemberAttribute.cs @@ -0,0 +1,17 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET7_0_OR_GREATER +#pragma warning disable +#pragma warning disable MEN002 // Line is too long +namespace System.Runtime.CompilerServices; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field + | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] +internal sealed class RequiredMemberAttribute : Attribute { } + + +#pragma warning restore MEN002 // Line is too long +#endif // !NET7_0_OR_GREATER diff --git a/src/JSSoft.Communication/Compatibility/SetsRequiredMembersAttribute.cs b/src/JSSoft.Communication/Compatibility/SetsRequiredMembersAttribute.cs new file mode 100644 index 0000000..8f320cf --- /dev/null +++ b/src/JSSoft.Communication/Compatibility/SetsRequiredMembersAttribute.cs @@ -0,0 +1,13 @@ +// +// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. +// Licensed under the MIT License. See LICENSE.md in the project root for license information. +// + +#if !NET7_0_OR_GREATER +#pragma warning disable +namespace System.Diagnostics.CodeAnalysis; + +[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)] +internal sealed class SetsRequiredMembersAttribute : Attribute { } + +#endif // !NET7_0_OR_GREATER diff --git a/src/JSSoft.Communication/UnreachableException.cs b/src/JSSoft.Communication/Compatibility/UnreachableException.cs similarity index 79% rename from src/JSSoft.Communication/UnreachableException.cs rename to src/JSSoft.Communication/Compatibility/UnreachableException.cs index 5cb2e30..5910d06 100644 --- a/src/JSSoft.Communication/UnreachableException.cs +++ b/src/JSSoft.Communication/Compatibility/UnreachableException.cs @@ -4,11 +4,12 @@ // #if !NET7_0_OR_GREATER +#pragma warning disable using System; namespace JSSoft.Communication; -class UnreachableException : SystemException +internal sealed class UnreachableException : SystemException { public UnreachableException() { @@ -19,4 +20,4 @@ public UnreachableException(string message) { } } -#endif // !NET6_0_OR_GREATER +#endif // !NET7_0_OR_GREATER diff --git a/src/JSSoft.Communication/EndPointUtility.cs b/src/JSSoft.Communication/EndPointUtility.cs index 6d5afe7..1ada7b3 100644 --- a/src/JSSoft.Communication/EndPointUtility.cs +++ b/src/JSSoft.Communication/EndPointUtility.cs @@ -82,6 +82,7 @@ public static bool TryParse(string text, [MaybeNullWhen(false)] out EndPoint end { return new($"{iPEndPoint.Address}", iPEndPoint.Port, credentials); } + throw new NotSupportedException($"'{endPoint}' is not supported."); } #endif diff --git a/src/JSSoft.Communication/Grpc/AdaptorServer.cs b/src/JSSoft.Communication/Grpc/AdaptorServer.cs index ca0c0ed..2895b3e 100644 --- a/src/JSSoft.Communication/Grpc/AdaptorServer.cs +++ b/src/JSSoft.Communication/Grpc/AdaptorServer.cs @@ -34,7 +34,9 @@ internal sealed class AdaptorServer : IAdaptor private readonly Timer _timer; #if NETSTANDARD private Server? _server; +#pragma warning disable S1450 private AdaptorServerImpl? _adaptor; +#pragma warning restore S1450 #elif NET private IHost? _host; #endif diff --git a/src/JSSoft.Communication/ServiceInstanceBuilder.cs b/src/JSSoft.Communication/ServiceInstanceBuilder.cs index 571330e..8d4ee75 100644 --- a/src/JSSoft.Communication/ServiceInstanceBuilder.cs +++ b/src/JSSoft.Communication/ServiceInstanceBuilder.cs @@ -93,7 +93,11 @@ private static Type CreateType( } } +#if NET7_0_OR_GREATER return typeBuilder.CreateType(); +#else + return typeBuilder.CreateType() ?? throw new InvalidOperationException("type is null."); +#endif } private static void CreateInvoke( diff --git a/test/JSSoft.Communication.Tests/JSSoft.Communication.Tests.csproj b/test/JSSoft.Communication.Tests/JSSoft.Communication.Tests.csproj index a0eb54a..c657ac7 100644 --- a/test/JSSoft.Communication.Tests/JSSoft.Communication.Tests.csproj +++ b/test/JSSoft.Communication.Tests/JSSoft.Communication.Tests.csproj @@ -25,6 +25,7 @@ SOFTWARE. --> enable false true + $(NoWarn);NU1701