-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ce9a0a
commit 5c8ab21
Showing
13 changed files
with
163 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/SuperSocket.SerialIO/SerialIOConnectionFactoryBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
src/SuperSocket.SerialIO/SerialIOConnectionListenerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters