Skip to content

Commit

Permalink
Merge pull request #5 from s2quake/refactor/compatible-netstandard
Browse files Browse the repository at this point in the history
Add netstandard2.1 compatibility
  • Loading branch information
s2quake authored Jun 4, 2024
2 parents 9a6eb8b + 8f03482 commit ee3adf4
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/JSSoft.Communication/EndPointUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ public static bool TryParse(string text, [MaybeNullWhen(false)] out EndPoint end
}
}

#if NETSTANDARD
internal static global::Grpc.Core.ServerPort GetServerPort(EndPoint endPoint, global::Grpc.Core.ServerCredentials credentials)
{
if (endPoint is DnsEndPoint dnsEndPoint)
{
return new(dnsEndPoint.Host, dnsEndPoint.Port, credentials);
}
else if (endPoint is IPEndPoint iPEndPoint)
{
return new($"{iPEndPoint.Address}", iPEndPoint.Port, credentials);
}
throw new NotSupportedException($"'{endPoint}' is not supported.");
}
#endif

internal static IPEndPoint ConvertToIPEndPoint(EndPoint endPoint)
{
var (host, port) = GetElements(endPoint);
Expand Down
13 changes: 11 additions & 2 deletions src/JSSoft.Communication/Grpc/AdaptorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
// SOFTWARE.

using Grpc.Core;
using Grpc.Net.Client;
using JSSoft.Communication.Extensions;
using JSSoft.Communication.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
#if NETSTANDARD
using GrpcChannel = Grpc.Core.Channel;
#elif NET
using System.Diagnostics;
using GrpcChannel = Grpc.Net.Client.GrpcChannel;
#endif

namespace JSSoft.Communication.Grpc;

Expand Down Expand Up @@ -64,8 +68,13 @@ public async Task OpenAsync(EndPoint endPoint, CancellationToken cancellationTok
throw new InvalidOperationException("Already opened.");
try
{
#if NETSTANDARD
_channel = new Channel(EndPointUtility.ToString(endPoint), ChannelCredentials.Insecure);
await _channel.ConnectAsync(deadline: DateTime.UtcNow.AddSeconds(15));
#elif NET
_channel = GrpcChannel.ForAddress($"http://{EndPointUtility.ConvertToIPEndPoint(endPoint)}");
await _channel.ConnectAsync(cancellationToken);
#endif
_adaptorImpl = new AdaptorClientImpl(_channel, _serviceContext.Id, _serviceByName.Values.ToArray());
await _adaptorImpl.OpenAsync(cancellationToken);
_descriptor = _instanceContext.CreateInstance(_adaptorImpl);
Expand Down
6 changes: 5 additions & 1 deletion src/JSSoft.Communication/Grpc/AdaptorClientImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
// SOFTWARE.

using Grpc.Core;
using Grpc.Net.Client;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#if NETSTANDARD
using GrpcChannel = Grpc.Core.Channel;
#elif NET
using GrpcChannel = Grpc.Net.Client.GrpcChannel;
#endif

namespace JSSoft.Communication.Grpc;

Expand Down
33 changes: 32 additions & 1 deletion src/JSSoft.Communication/Grpc/AdaptorServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
using JSSoft.Communication.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
#if NET
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
#endif

namespace JSSoft.Communication.Grpc;

Expand All @@ -48,7 +50,12 @@ sealed class AdaptorServer : IAdaptor
private readonly IServiceContext _serviceContext;
private readonly IReadOnlyDictionary<string, IService> _serviceByName;
private readonly Dictionary<IService, MethodDescriptorCollection> _methodsByService;
#if NETSTANDARD
private Server? _server;
private AdaptorServerImpl? _adaptor;
#elif NET
private IHost? _host;
#endif
private ISerializer? _serializer;
private readonly Timer _timer;
private EventHandler? _disconnectedEventHandler;
Expand Down Expand Up @@ -226,6 +233,29 @@ where dateTime - peer.PingTime > Timeout

#region IAdaptor

#if NETSTANDARD
async Task IAdaptor.OpenAsync(EndPoint endPoint, CancellationToken cancellationToken)
{
_adaptor = new AdaptorServerImpl(this);
_server = new Server()
{
Services = { Adaptor.BindService(_adaptor) },
Ports = { EndPointUtility.GetServerPort(endPoint, ServerCredentials.Insecure) },
};
_serializer = _serviceContext.GetService(typeof(ISerializer)) as ISerializer;
await Task.Run(_server.Start, cancellationToken);
}

async Task IAdaptor.CloseAsync(CancellationToken cancellationToken)
{
await Peers.DisconnectAsync(_serviceContext, cancellationToken);
await _server!.ShutdownAsync();
_adaptor = null;
_serializer = null;
_server = null;
}

#elif NET
async Task IAdaptor.OpenAsync(EndPoint endPoint, CancellationToken cancellationToken)
{
var builder = Host.CreateDefaultBuilder();
Expand Down Expand Up @@ -280,6 +310,7 @@ async Task IAdaptor.CloseAsync(CancellationToken cancellationToken)
_host.Dispose();
_host = null;
}
#endif

void IAdaptor.Invoke(InstanceBase instance, string name, Type[] types, object?[] args)
{
Expand Down
8 changes: 5 additions & 3 deletions src/JSSoft.Communication/JSSoft.Communication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ SOFTWARE. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PublishName>communication\lib</PublishName>
<IsNet Condition="$(TargetFramework.StartsWith('net')) and !$(TargetFramework.StartsWith('netstandard'))">true</IsNet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.63.0" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.63.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.63.0" />
<PackageReference Condition="'$(IsNet)'=='true'" Include="Grpc.AspNetCore" Version="2.63.0" />
<PackageReference Condition="'$(IsNet)'=='true'" Include="Grpc.AspNetCore.Server" Version="2.63.0" />
<PackageReference Condition="'$(IsNet)'=='true'" Include="Grpc.Net.Client" Version="2.63.0" />
<PackageReference Condition="'$(IsNet)'!='true'" Include="Grpc.Core" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.64.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
5 changes: 5 additions & 0 deletions src/JSSoft.Communication/ServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public class ServiceCollection(IEnumerable<IService> services) : IReadOnlyDictio

public bool ContainsKey(string key) => _serviceByName.ContainsKey(key);

#if NETSTANDARD
public bool TryGetValue(string key, out IService value)
=> _serviceByName.TryGetValue(key, out value);
#elif NET
public bool TryGetValue(string key, [MaybeNullWhen(false)] out IService value)
=> _serviceByName.TryGetValue(key, out value);
#endif

#region IEnumerable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SOFTWARE. -->
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
5 changes: 4 additions & 1 deletion test/JSSoft.Communication.Tests/RandomEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public override string ToString()

public void Dispose()
{
ObjectDisposedException.ThrowIf(_isDisposed, this);
if (_isDisposed == true)
{
throw new ObjectDisposedException($"{this}");
}

lock (LockObject)
{
Expand Down

0 comments on commit ee3adf4

Please sign in to comment.