-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
6 changed files
with
271 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.IO.Ports; | ||
using Firejox.App.WinSocat; | ||
|
||
namespace APPTest; | ||
|
||
public class SerialPortPiperInfoTest | ||
{ | ||
|
||
[TestCase("SP:COM1")] | ||
[TestCase("SP:COM2,baudrate=12500,parity=1,databits=16,stopbits=0")] | ||
public void ValidInputParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
Assert.NotNull(SerialPortPiperInfo.TryParse(element)); | ||
} | ||
|
||
[TestCase("sp:COM1")] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0")] | ||
public void CaseInsensitiveValidInputParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
Assert.NotNull(SerialPortPiperInfo.TryParse(element)); | ||
} | ||
|
||
[TestCase("STDIO")] | ||
[TestCase("TCP:127.0.0.1:80")] | ||
[TestCase("TCP-LISTEN:127.0.0.1:80")] | ||
[TestCase("NPIPE:fooServer:barPipe")] | ||
[TestCase("NPIPE-LISTEN:fooPipe")] | ||
[TestCase(@"EXEC:'C:\Foo.exe bar'")] | ||
[TestCase("UNIX:foo.sock")] | ||
[TestCase("UNIX-LISTEN:foo.sock")] | ||
public void InvalidInputParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
Assert.Null(SerialPortPiperInfo.TryParse(element)); | ||
} | ||
|
||
[TestCase("sp:COM1", ExpectedResult = "COM1")] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0", ExpectedResult = "COM2")] | ||
public string PortNamePatternParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
return SerialPortPiperInfo.TryParse(element).PortName; | ||
} | ||
|
||
[TestCase("sp:COM1", ExpectedResult = 9600)] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0", ExpectedResult = 12500)] | ||
public int BaudRatePatternParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
return SerialPortPiperInfo.TryParse(element).BaudRate; | ||
} | ||
|
||
[TestCase("sp:COM1", ExpectedResult = Parity.None)] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0", ExpectedResult = Parity.Odd)] | ||
public Parity PartityPatternParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
return SerialPortPiperInfo.TryParse(element).Partiy; | ||
} | ||
|
||
[TestCase("sp:COM1", ExpectedResult = 8)] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0", ExpectedResult = 16)] | ||
public int DataBitsPatternParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
return SerialPortPiperInfo.TryParse(element).DataBits; | ||
} | ||
|
||
[TestCase("sp:COM1", ExpectedResult = StopBits.One)] | ||
[TestCase("sp:COM2,baudrate=12500,parity=1,databits=16,stopbits=0", ExpectedResult = StopBits.None)] | ||
public StopBits StopBitsPatternParseTest(string input) | ||
{ | ||
var element = AddressElement.TryParse(input); | ||
return SerialPortPiperInfo.TryParse(element).StopBits; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
using System.IO.Ports; | ||
|
||
namespace Firejox.App.WinSocat; | ||
|
||
public class SerialPortPiperInfo | ||
{ | ||
private readonly string _portName; | ||
public string PortName => _portName; | ||
|
||
private readonly int _baudRate; | ||
public int BaudRate => _baudRate; | ||
|
||
private readonly Parity _parity; | ||
public Parity Partiy => _parity; | ||
|
||
private readonly int _dataBits; | ||
public int DataBits => _dataBits; | ||
|
||
private readonly StopBits _stopBits; | ||
public StopBits StopBits => _stopBits; | ||
|
||
public SerialPortPiperInfo(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) | ||
{ | ||
_portName = portName; | ||
_baudRate = baudRate; | ||
_parity = parity; | ||
_dataBits = dataBits; | ||
_stopBits = stopBits; | ||
} | ||
|
||
public static SerialPortPiperInfo TryParse(AddressElement element) | ||
{ | ||
if (!element.Tag.Equals("SP", StringComparison.OrdinalIgnoreCase)) | ||
return null!; | ||
|
||
string portName = element.Address; | ||
int baudRate; | ||
Parity parity; | ||
int dataBits; | ||
StopBits stopBits; | ||
|
||
if (!element.Options.TryGetValue("baudrate", out var tmp)) | ||
baudRate = 9600; | ||
else if (!Int32.TryParse(tmp, out baudRate)) | ||
return null!; | ||
|
||
if (!element.Options.TryGetValue("parity", out tmp!)) | ||
parity = Parity.None; | ||
else if (!Enum.TryParse(tmp, out parity)) | ||
return null!; | ||
|
||
if (!element.Options.TryGetValue("databits", out tmp!)) | ||
dataBits = 8; | ||
else if (!Int32.TryParse(tmp, out dataBits)) | ||
return null!; | ||
|
||
if (!element.Options.TryGetValue("stopbits", out tmp!)) | ||
stopBits = StopBits.One; | ||
else if (!Enum.TryParse(tmp, out stopBits)) | ||
return null!; | ||
return new SerialPortPiperInfo(portName, baudRate, parity, dataBits, stopBits); | ||
} | ||
} | ||
|
||
public class SerialPortPiper : StreamPiper | ||
{ | ||
private SerialPort _serialPort; | ||
|
||
public SerialPortPiper(SerialPort serialPort) : base(OpenAndGet(serialPort)) | ||
{ | ||
_serialPort = serialPort; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
try | ||
{ | ||
base.Dispose(disposing); | ||
if (disposing && _serialPort is not null) | ||
{ | ||
_serialPort.Dispose(); | ||
} | ||
} | ||
finally | ||
{ | ||
_serialPort = null!; | ||
} | ||
} | ||
|
||
private static Stream OpenAndGet(SerialPort serialPort) | ||
{ | ||
if (!serialPort.IsOpen) | ||
serialPort.Open(); | ||
return serialPort.BaseStream; | ||
} | ||
} | ||
|
||
public class SerialPortPiperFactory : IPiperFactory | ||
{ | ||
private readonly SerialPortPiperInfo _info; | ||
|
||
public SerialPortPiperFactory(SerialPortPiperInfo info) | ||
{ | ||
_info = info; | ||
} | ||
|
||
public IPiper NewPiper() | ||
{ | ||
return new SerialPortPiper( | ||
new SerialPort( | ||
_info.PortName, | ||
_info.BaudRate, | ||
_info.Partiy, | ||
_info.DataBits, | ||
_info.StopBits | ||
) | ||
); | ||
} | ||
|
||
public static SerialPortPiperFactory TryParse(AddressElement element) | ||
{ | ||
SerialPortPiperInfo info; | ||
if ((info = SerialPortPiperInfo.TryParse(element)) is not null) | ||
return new SerialPortPiperFactory(info); | ||
return null!; | ||
} | ||
} | ||
|
||
public class SerialPortPiperStrategy : PiperStrategy | ||
{ | ||
private readonly SerialPortPiperInfo _info; | ||
|
||
public SerialPortPiperStrategy(SerialPortPiperInfo info) | ||
{ | ||
_info = info; | ||
} | ||
|
||
protected override IPiper NewPiper() | ||
{ | ||
return new SerialPortPiper( | ||
new SerialPort( | ||
_info.PortName, | ||
_info.BaudRate, | ||
_info.Partiy, | ||
_info.DataBits, | ||
_info.StopBits | ||
) | ||
); | ||
} | ||
|
||
public static SerialPortPiperStrategy TryParse(AddressElement element) | ||
{ | ||
SerialPortPiperInfo info; | ||
|
||
if ((info = SerialPortPiperInfo.TryParse(element)) is not null) | ||
return new SerialPortPiperStrategy(info); | ||
|
||
return null!; | ||
} | ||
} |
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