From 5c8ab21519f70213dc071d9c30d7db73a2414e00 Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Sat, 4 May 2024 16:39:03 -0700 Subject: [PATCH] upgraded SuperSocket --- Directory.Build.props | 2 +- .../HostBuilderExtensions.cs | 5 +- .../SerialIOChannelCreator.cs | 95 ------------------- .../SerialIOChannelCreatorFactory.cs | 27 ------ .../SerialIOConnectionFactory.cs | 35 +++++++ .../SerialIOConnectionFactoryBuilder.cs | 14 +++ .../SerialIOConnectionListener.cs | 77 +++++++++++++++ .../SerialIOConnectionListenerFactory.cs | 24 +++++ .../SerialIOListenOptions.cs | 3 +- ...peChannel.cs => SerialIOPipeConnection.cs} | 8 +- .../SuperSocket.SerialIO.csproj | 2 +- test/Test/Test.csproj | 2 +- version.json | 2 +- 13 files changed, 163 insertions(+), 133 deletions(-) delete mode 100644 src/SuperSocket.SerialIO/SerialIOChannelCreator.cs delete mode 100644 src/SuperSocket.SerialIO/SerialIOChannelCreatorFactory.cs create mode 100644 src/SuperSocket.SerialIO/SerialIOConnectionFactory.cs create mode 100644 src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs create mode 100644 src/SuperSocket.SerialIO/SerialIOConnectionListener.cs create mode 100644 src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs rename src/SuperSocket.SerialIO/{SerialIOPipeChannel.cs => SerialIOPipeConnection.cs} (86%) diff --git a/Directory.Build.props b/Directory.Build.props index 081af4f..974833d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ 8.0 - net5.0;netstandard2.1 + net6.0;net7.0;net8.0;netstandard2.1 https://github.com/SuperSocket/SuperSocket.SerialIO diff --git a/src/SuperSocket.SerialIO/HostBuilderExtensions.cs b/src/SuperSocket.SerialIO/HostBuilderExtensions.cs index 2686045..5f70686 100644 --- a/src/SuperSocket.SerialIO/HostBuilderExtensions.cs +++ b/src/SuperSocket.SerialIO/HostBuilderExtensions.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SuperSocket.SerialIO; +using SuperSocket.Server.Abstractions.Connections; +using SuperSocket.Server.Abstractions.Host; namespace SuperSocket { @@ -12,7 +14,8 @@ public static IHostBuilder UseSerialIO(this ISuperSocketHostBuilder hostBuilder) return hostBuilder.ConfigureServices( (hostCtx, services) => { - services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); } ); } diff --git a/src/SuperSocket.SerialIO/SerialIOChannelCreator.cs b/src/SuperSocket.SerialIO/SerialIOChannelCreator.cs deleted file mode 100644 index e8b5001..0000000 --- a/src/SuperSocket.SerialIO/SerialIOChannelCreator.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Linq; -using System.IO.Ports; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using SuperSocket.Channel; - -namespace SuperSocket.SerialIO -{ - /// - /// the serialport channel creator - /// - public class SerialIOChannelCreator : IChannelCreator - { - private ILogger _logger; - private SerialPort _port = null; - - private Func> _channelFactory; - - public ListenOptions Options { get; } - - /// - /// if the serial port is open,then return true - /// - public bool IsRunning - { - get { return _port?.IsOpen ?? false; } - } - - public event NewClientAcceptHandler NewClientAccepted; - - private SerialPort CreateSerialPort(ListenOptions options) - { - var sioEndpoint = options as ISerialIOEndPoint; - - if (sioEndpoint == null) - { - if(string.IsNullOrEmpty(options.Path)) - throw new Exception("Invalid Path value in the ListenOptions."); - - sioEndpoint = new SerialIOEndPoint(options.Path); - } - - return new SerialPort(sioEndpoint.PortName, sioEndpoint.BaudRate, sioEndpoint.Parity, sioEndpoint.Databits) - { - StopBits = sioEndpoint.StopBits - }; - } - - public SerialIOChannelCreator(ListenOptions options, Func> channelFactory, ILogger logger) - { - _port = CreateSerialPort(options); - - Options = options; - - _channelFactory = channelFactory; - _logger = logger; - } - - public async Task CreateChannel(object connection) - { - return await _channelFactory((SerialPort) connection); - } - - public bool Start() - { - - try - { - _port.Open(); - - //the serial port is diffent with the tcp, Client connections cannot be distinguished,and serial port never trigger the accept event - //so the serial port only one channel and one client - //so it need manual trigger the accept ,let the supersocket framework to force create the connection session - NewClientAccepted?.Invoke( this,_channelFactory(_port).Result); - - return true; - } - catch (Exception e) - { - _logger.LogError(e, $"serial port: {_port.PortName} open fail!"); - - return false; - } - } - - public Task StopAsync() - { - //close the port when the server stop - _port.Close(); - - return Task.CompletedTask; - } - } -} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOChannelCreatorFactory.cs b/src/SuperSocket.SerialIO/SerialIOChannelCreatorFactory.cs deleted file mode 100644 index 025b5c1..0000000 --- a/src/SuperSocket.SerialIO/SerialIOChannelCreatorFactory.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Buffers; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using SuperSocket.Channel; -using SuperSocket.ProtoBase; - -namespace SuperSocket.SerialIO -{ - public class SerialIOChannelCreatorFactory : IChannelCreatorFactory - { - public SerialIOChannelCreatorFactory(IServiceProvider serviceProvider) - { - - } - - public IChannelCreator CreateChannelCreator(ListenOptions options, ChannelOptions channelOptions, ILoggerFactory loggerFactory, object pipelineFilterFactory) - { - var filterFactory = pipelineFilterFactory as IPipelineFilterFactory; - channelOptions.Logger = loggerFactory.CreateLogger(nameof(IChannel)); - var channelFactoryLogger = loggerFactory.CreateLogger(nameof(SerialIOChannelCreator)); - - return new SerialIOChannelCreator(options, (s) => - new ValueTask(new SerialIOPipeChannel(s, filterFactory.Create(s), channelOptions)), channelFactoryLogger); - } - } -} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOConnectionFactory.cs b/src/SuperSocket.SerialIO/SerialIOConnectionFactory.cs new file mode 100644 index 0000000..4f55c0a --- /dev/null +++ b/src/SuperSocket.SerialIO/SerialIOConnectionFactory.cs @@ -0,0 +1,35 @@ +using System; +using System.IO.Ports; +using System.Threading.Tasks; +using SuperSocket.Connection; +using System.Threading; +using SuperSocket.Server.Abstractions; + +namespace SuperSocket.SerialIO +{ + /// + /// the serialport channel creator + /// + internal class SerialIOConnectionFactory : IConnectionFactory + { + public ListenOptions Options { get; } + + private ConnectionOptions _connectionOptions { get; } + + public SerialIOConnectionFactory(ListenOptions options, ConnectionOptions connectionOptions) + { + Options = options; + _connectionOptions = connectionOptions; + } + + public Task CreateConnection(object connection, CancellationToken cancellationToken) + { + var serialPort = connection as SerialPort; + + if (serialPort == null) + throw new ArgumentException("connection must be SerialPort", nameof(connection)); + + return Task.FromResult(new SerialIOPipeConnection(serialPort, _connectionOptions)); + } + } +} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs b/src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs new file mode 100644 index 0000000..3ff0028 --- /dev/null +++ b/src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs @@ -0,0 +1,14 @@ +using SuperSocket.Connection; +using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; + +namespace SuperSocket.SerialIO +{ + internal class SerialIOConnectionFactoryBuilder : IConnectionFactoryBuilder + { + public IConnectionFactory Build(ListenOptions listenOptions, ConnectionOptions connectionOptions) + { + return new SerialIOConnectionFactory(listenOptions, connectionOptions); + } + } +} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOConnectionListener.cs b/src/SuperSocket.SerialIO/SerialIOConnectionListener.cs new file mode 100644 index 0000000..780f861 --- /dev/null +++ b/src/SuperSocket.SerialIO/SerialIOConnectionListener.cs @@ -0,0 +1,77 @@ +using System; +using System.IO.Ports; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using SuperSocket.Connection; +using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; + +namespace SuperSocket.SerialIO +{ + internal class SerialIOConnectionListener : IConnectionListener + { + public ListenOptions Options { get; } + + public bool IsRunning { get; private set; } + + public IConnectionFactory ConnectionFactory { get; } + + private readonly ILogger _logger; + + private SerialPort _port = null; + + public event NewConnectionAcceptHandler NewConnectionAccept; + + public SerialIOConnectionListener(ListenOptions options, IConnectionFactory connectionFactory, ILogger logger) + { + Options = options; + ConnectionFactory = connectionFactory; + _logger = logger; + _port = CreateSerialPort(Options); + } + + private SerialPort CreateSerialPort(ListenOptions options) + { + var sioEndpoint = options as ISerialIOEndPoint; + + if (sioEndpoint == null) + { + if (string.IsNullOrEmpty(options.Path)) + throw new Exception("Invalid Path value in the ListenOptions."); + + sioEndpoint = new SerialIOEndPoint(options.Path); + } + + return new SerialPort(sioEndpoint.PortName, sioEndpoint.BaudRate, sioEndpoint.Parity, sioEndpoint.Databits) + { + StopBits = sioEndpoint.StopBits + }; + } + + public bool Start() + { + try + { + _port.Open(); + + // Serial port is different with the tcp. It doesn't allow concurrent multiple connections. + // We should create the single SerialIO connection at the beginning. + var connection = ConnectionFactory.CreateConnection(_port, CancellationToken.None).Result; + NewConnectionAccept?.Invoke(this.Options, connection); + return true; + } + catch (Exception e) + { + _logger.LogError(e, $"serial port: {_port.PortName} open fail!"); + return false; + } + } + + public Task StopAsync() + { + _port?.Close(); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs b/src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs new file mode 100644 index 0000000..8b9ad17 --- /dev/null +++ b/src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs @@ -0,0 +1,24 @@ +using Microsoft.Extensions.Logging; +using SuperSocket.Connection; +using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; + +namespace SuperSocket.SerialIO +{ + public class SerialIOConnectionListenerFactory : IConnectionListenerFactory + { + protected IConnectionFactoryBuilder ConnectionFactoryBuilder { get; } + + public SerialIOConnectionListenerFactory(IConnectionFactoryBuilder connectionFactoryBuilder) + { + ConnectionFactoryBuilder = connectionFactoryBuilder; + } + + + public IConnectionListener CreateConnectionListener(ListenOptions options, ConnectionOptions connectionOptions, ILoggerFactory loggerFactory) + { + var connectionFactory = ConnectionFactoryBuilder.Build(options, connectionOptions); + return new SerialIOConnectionListener(options, connectionFactory, loggerFactory.CreateLogger()); + } + } +} \ No newline at end of file diff --git a/src/SuperSocket.SerialIO/SerialIOListenOptions.cs b/src/SuperSocket.SerialIO/SerialIOListenOptions.cs index a41bf28..c0231c6 100644 --- a/src/SuperSocket.SerialIO/SerialIOListenOptions.cs +++ b/src/SuperSocket.SerialIO/SerialIOListenOptions.cs @@ -1,7 +1,6 @@ using System; -using System.Collections.Generic; using System.IO.Ports; -using System.Text; +using SuperSocket.Server.Abstractions; namespace SuperSocket.SerialIO { diff --git a/src/SuperSocket.SerialIO/SerialIOPipeChannel.cs b/src/SuperSocket.SerialIO/SerialIOPipeConnection.cs similarity index 86% rename from src/SuperSocket.SerialIO/SerialIOPipeChannel.cs rename to src/SuperSocket.SerialIO/SerialIOPipeConnection.cs index 1eb8bb6..215c7f6 100644 --- a/src/SuperSocket.SerialIO/SerialIOPipeChannel.cs +++ b/src/SuperSocket.SerialIO/SerialIOPipeConnection.cs @@ -4,17 +4,17 @@ using System.IO.Ports; using System.Threading; using System.Threading.Tasks; -using SuperSocket.Channel; +using SuperSocket.Connection; using SuperSocket.ProtoBase; namespace SuperSocket.SerialIO { - public class SerialIOPipeChannel : PipeChannel + public class SerialIOPipeConnection : PipeConnection { private SerialPort _serialPort = null; - public SerialIOPipeChannel(SerialPort serialPort, IPipelineFilter pipelineFilter, ChannelOptions options) - : base(pipelineFilter, options) + public SerialIOPipeConnection(SerialPort serialPort, ConnectionOptions options) + : base(options) { _serialPort = serialPort; } diff --git a/src/SuperSocket.SerialIO/SuperSocket.SerialIO.csproj b/src/SuperSocket.SerialIO/SuperSocket.SerialIO.csproj index 8f73888..2b944fa 100644 --- a/src/SuperSocket.SerialIO/SuperSocket.SerialIO.csproj +++ b/src/SuperSocket.SerialIO/SuperSocket.SerialIO.csproj @@ -4,6 +4,6 @@ - + \ No newline at end of file diff --git a/test/Test/Test.csproj b/test/Test/Test.csproj index 0753fe4..db50ae6 100644 --- a/test/Test/Test.csproj +++ b/test/Test/Test.csproj @@ -1,7 +1,7 @@ - net5.0 + net8.0 false diff --git a/version.json b/version.json index 86bec1c..b5632fa 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.0.0-beta.10", + "version": "2.0.0-beta.21", "publicReleaseRefSpec": [ "^refs/heads/master$", "^refs/heads/v\\d+(?:\\.\\d+)?$"