Skip to content

Commit

Permalink
upgraded SuperSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed May 4, 2024
1 parent 9ce9a0a commit 5c8ab21
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<TargetFrameworks>net5.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<PackageProjectUrl>https://github.com/SuperSocket/SuperSocket.SerialIO</PackageProjectUrl>
Expand Down
5 changes: 4 additions & 1 deletion src/SuperSocket.SerialIO/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -12,7 +14,8 @@ public static IHostBuilder UseSerialIO(this ISuperSocketHostBuilder hostBuilder)
return hostBuilder.ConfigureServices(
(hostCtx, services) =>
{
services.AddSingleton<IChannelCreatorFactory, SerialIOChannelCreatorFactory>();
services.AddSingleton<IConnectionFactoryBuilder, SerialIOConnectionFactoryBuilder>();
services.AddSingleton<IConnectionListenerFactory, SerialIOConnectionListenerFactory>();
}
);
}
Expand Down
95 changes: 0 additions & 95 deletions src/SuperSocket.SerialIO/SerialIOChannelCreator.cs

This file was deleted.

27 changes: 0 additions & 27 deletions src/SuperSocket.SerialIO/SerialIOChannelCreatorFactory.cs

This file was deleted.

35 changes: 35 additions & 0 deletions src/SuperSocket.SerialIO/SerialIOConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// the serialport channel creator
/// </summary>
internal class SerialIOConnectionFactory : IConnectionFactory
{
public ListenOptions Options { get; }

private ConnectionOptions _connectionOptions { get; }

public SerialIOConnectionFactory(ListenOptions options, ConnectionOptions connectionOptions)
{
Options = options;
_connectionOptions = connectionOptions;
}

public Task<IConnection> 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<IConnection>(new SerialIOPipeConnection(serialPort, _connectionOptions));
}
}
}
14 changes: 14 additions & 0 deletions src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
77 changes: 77 additions & 0 deletions src/SuperSocket.SerialIO/SerialIOConnectionListener.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
24 changes: 24 additions & 0 deletions src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -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<SerialIOConnectionListener>());
}
}
}
3 changes: 1 addition & 2 deletions src/SuperSocket.SerialIO/SerialIOListenOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using SuperSocket.Server.Abstractions;

namespace SuperSocket.SerialIO
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TPackageInfo> : PipeChannel<TPackageInfo>
public class SerialIOPipeConnection : PipeConnection
{
private SerialPort _serialPort = null;

public SerialIOPipeChannel(SerialPort serialPort, IPipelineFilter<TPackageInfo> pipelineFilter, ChannelOptions options)
: base(pipelineFilter, options)
public SerialIOPipeConnection(SerialPort serialPort, ConnectionOptions options)
: base(options)
{
_serialPort = serialPort;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SuperSocket.SerialIO/SuperSocket.SerialIO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Ports" Version="4.7.0" />
<PackageReference Include="SuperSocket.Primitives" Version="2.0.0-beta.10" />
<PackageReference Include="SuperSocket.Server.Abstractions" Version="2.0.0-beta.21" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion test/Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -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+)?$"
Expand Down

0 comments on commit 5c8ab21

Please sign in to comment.