Skip to content

Commit

Permalink
Remove nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
HakamFostok committed May 10, 2022
1 parent 73bf2ce commit 09a8632
Show file tree
Hide file tree
Showing 37 changed files with 244 additions and 244 deletions.
4 changes: 2 additions & 2 deletions Modbus/Data/DataStoreEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static DataStoreEventArgs CreateDataStoreEventArgs<T>(ushort startAddre

if (typeof(T) == typeof(bool))
{
ReadOnlyCollection<bool>? a = new(data.Cast<bool>().ToArray());
ReadOnlyCollection<bool> a = new(data.Cast<bool>().ToArray());

eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
{
Expand All @@ -48,7 +48,7 @@ internal static DataStoreEventArgs CreateDataStoreEventArgs<T>(ushort startAddre
}
else if (typeof(T) == typeof(ushort))
{
ReadOnlyCollection<ushort>? b = new(data.Cast<ushort>().ToArray());
ReadOnlyCollection<ushort> b = new(data.Cast<ushort>().ToArray());

eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
{
Expand Down
8 changes: 4 additions & 4 deletions Modbus/Data/DataStoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public static DataStore CreateDefaultDataStore() =>
/// <returns>New instance of Data store with defined inputs/outputs.</returns>
public static DataStore CreateDefaultDataStore(ushort coilsCount, ushort inputsCount, ushort holdingRegistersCount, ushort inputRegistersCount)
{
bool[]? coils = new bool[coilsCount];
bool[]? inputs = new bool[inputsCount];
ushort[]? holdingRegs = new ushort[holdingRegistersCount];
ushort[]? inputRegs = new ushort[inputRegistersCount];
bool[] coils = new bool[coilsCount];
bool[] inputs = new bool[inputsCount];
ushort[] holdingRegs = new ushort[holdingRegistersCount];
ushort[] inputRegs = new ushort[inputRegistersCount];

return new DataStore(coils, inputs, holdingRegs, inputRegs);
}
Expand Down
4 changes: 2 additions & 2 deletions Modbus/Device/ModbusTcpSlave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override async Task ListenAsync()
while (true)
{
TcpClient client = await Server.AcceptTcpClientAsync().ConfigureAwait(false);
ModbusMasterTcpConnection? masterConnection = new(client, this);
ModbusMasterTcpConnection masterConnection = new(client, this);
masterConnection.ModbusMasterTcpConnectionClosed += OnMasterConnectionClosedHandler;
_masters.TryAdd(client.Client.RemoteEndPoint.ToString(), masterConnection);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ protected override void Dispose(bool disposing)
}
#endif

foreach (string? key in _masters.Keys)
foreach (string key in _masters.Keys)
{
if (_masters.TryRemove(key, out ModbusMasterTcpConnection connection))
{
Expand Down
4 changes: 2 additions & 2 deletions Modbus/Extensions/Enron/EnronModbus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static uint[] ReadHoldingRegisters32(
ValidateNumberOfPoints(numberOfPoints, 62);

// read 16 bit chunks and perform conversion
ushort[]? rawRegisters = master.ReadHoldingRegisters(
ushort[] rawRegisters = master.ReadHoldingRegisters(
slaveAddress,
startAddress,
(ushort)(numberOfPoints * 2));
Expand All @@ -53,7 +53,7 @@ public static uint[] ReadInputRegisters32(

ValidateNumberOfPoints(numberOfPoints, 62);

ushort[]? rawRegisters = master.ReadInputRegisters(
ushort[] rawRegisters = master.ReadInputRegisters(
slaveAddress,
startAddress,
(ushort)(numberOfPoints * 2));
Expand Down
8 changes: 4 additions & 4 deletions Modbus/IO/ModbusAsciiTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ internal override byte[] BuildMessageFrame(IModbusMessage message)
{
byte[]? msgFrame = message.MessageFrame;

byte[]? msgFrameAscii = ModbusUtility.GetAsciiBytes(msgFrame);
byte[]? lrcAscii = ModbusUtility.GetAsciiBytes(ModbusUtility.CalculateLrc(msgFrame));
byte[]? nlAscii = Encoding.UTF8.GetBytes(Modbus.NewLine.ToCharArray());
byte[] msgFrameAscii = ModbusUtility.GetAsciiBytes(msgFrame);
byte[] lrcAscii = ModbusUtility.GetAsciiBytes(ModbusUtility.CalculateLrc(msgFrame));
byte[] nlAscii = Encoding.UTF8.GetBytes(Modbus.NewLine.ToCharArray());

MemoryStream? frame = new(1 + msgFrameAscii.Length + lrcAscii.Length + nlAscii.Length);
MemoryStream frame = new(1 + msgFrameAscii.Length + lrcAscii.Length + nlAscii.Length);
frame.WriteByte((byte)':');
frame.Write(msgFrameAscii, 0, msgFrameAscii.Length);
frame.Write(lrcAscii, 0, lrcAscii.Length);
Expand Down
10 changes: 5 additions & 5 deletions Modbus/IO/ModbusIpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal ModbusIpTransport(IStreamResource streamResource)
internal static byte[] ReadRequestResponse(IStreamResource streamResource)
{
// read header
byte[]? mbapHeader = new byte[6];
byte[] mbapHeader = new byte[6];
int numBytesRead = 0;

while (numBytesRead != 6)
Expand All @@ -43,7 +43,7 @@ internal static byte[] ReadRequestResponse(IStreamResource streamResource)
Debug.WriteLine($"{frameLength} bytes in PDU.");

// read message
byte[]? messageFrame = new byte[frameLength];
byte[] messageFrame = new byte[frameLength];
numBytesRead = 0;

while (numBytesRead != frameLength)
Expand All @@ -59,7 +59,7 @@ internal static byte[] ReadRequestResponse(IStreamResource streamResource)
}

Debug.WriteLine($"PDU: {frameLength}");
byte[]? frame = mbapHeader.Concat(messageFrame).ToArray();
byte[] frame = mbapHeader.Concat(messageFrame).ToArray();
Debug.WriteLine($"RX: {string.Join(", ", frame)}");

return frame;
Expand All @@ -70,7 +70,7 @@ internal static byte[] GetMbapHeader(IModbusMessage message)
byte[] transactionId = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)message.TransactionId));
byte[] length = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(message.ProtocolDataUnit.Length + 1)));

MemoryStream? stream = new(7);
MemoryStream stream = new(7);
stream.Write(transactionId, 0, transactionId.Length);
stream.WriteByte(0);
stream.WriteByte(0);
Expand Down Expand Up @@ -109,7 +109,7 @@ internal override byte[] BuildMessageFrame(IModbusMessage message)
{
byte[] header = GetMbapHeader(message);
byte[] pdu = message.ProtocolDataUnit;
MemoryStream? messageBody = new(header.Length + pdu.Length);
MemoryStream messageBody = new(header.Length + pdu.Length);

messageBody.Write(header, 0, header.Length);
messageBody.Write(pdu, 0, pdu.Length);
Expand Down
2 changes: 1 addition & 1 deletion Modbus/IO/ModbusRtuTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal override byte[] BuildMessageFrame(IModbusMessage message)
{
byte[]? messageFrame = message.MessageFrame;
byte[]? crc = ModbusUtility.CalculateCrc(messageFrame);
MemoryStream? messageBody = new(messageFrame.Length + crc.Length);
MemoryStream messageBody = new(messageFrame.Length + crc.Length);

messageBody.Write(messageFrame, 0, messageFrame.Length);
messageBody.Write(crc, 0, crc.Length);
Expand Down
4 changes: 2 additions & 2 deletions Modbus/IO/StreamResourceUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ internal static class StreamResourceUtility
{
internal static string ReadLine(IStreamResource stream)
{
StringBuilder? result = new();
byte[]? singleByteBuffer = new byte[1];
StringBuilder result = new();
byte[] singleByteBuffer = new byte[1];

do
{
Expand Down
2 changes: 1 addition & 1 deletion Modbus/Message/ModbusMessageImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public byte[] MessageFrame
get
{
byte[]? pdu = ProtocolDataUnit;
MemoryStream? frame = new(1 + pdu.Length);
MemoryStream frame = new(1 + pdu.Length);

frame.WriteByte(SlaveAddress);
frame.Write(pdu, 0, pdu.Length);
Expand Down
2 changes: 1 addition & 1 deletion Modbus/Message/ReadWriteMultipleRegistersRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override byte[] ProtocolDataUnit
{
byte[] readPdu = ReadRequest.ProtocolDataUnit;
byte[] writePdu = WriteRequest.ProtocolDataUnit;
MemoryStream? stream = new(readPdu.Length + writePdu.Length);
MemoryStream stream = new(readPdu.Length + writePdu.Length);

stream.WriteByte(FunctionCode);

Expand Down
20 changes: 10 additions & 10 deletions NModbus4.IntegrationTests/ModbusIpMasterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ public class ModbusIpMasterFixture
[Fact]
public void OverrideTimeoutOnTcpClient()
{
TcpListener? listener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, listener);
Thread? slaveThread = new(async () => await slave.ListenAsync());
TcpListener listener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, listener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.Start();

TcpClient? client = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
TcpClient client = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
client.ReceiveTimeout = 1500;
client.SendTimeout = 3000;
using ModbusIpMaster? master = ModbusIpMaster.CreateIp(client);
using ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
Assert.Equal(1500, client.GetStream().ReadTimeout);
Assert.Equal(3000, client.GetStream().WriteTimeout);
}

[Fact]
public void OverrideTimeoutOnNetworkStream()
{
TcpListener? listener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, listener);
Thread? slaveThread = new(async () => await slave.ListenAsync());
TcpListener listener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, listener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.Start();

TcpClient? client = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
TcpClient client = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
client.GetStream().ReadTimeout = 1500;
client.GetStream().WriteTimeout = 3000;
using ModbusIpMaster? master = ModbusIpMaster.CreateIp(client);
using ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
Assert.Equal(1500, client.GetStream().ReadTimeout);
Assert.Equal(3000, client.GetStream().WriteTimeout);
}
Expand Down
2 changes: 1 addition & 1 deletion NModbus4.IntegrationTests/NModbusSerialRtuMasterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NModbusSerialRtuMasterFixture
public void NModbusRtuMaster_ReadTimeout()
{
SerialPort port = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultMasterSerialPortName);
using ModbusSerialMaster? master = ModbusSerialMaster.CreateRtu(port);
using ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
master.Transport.ReadTimeout = master.Transport.WriteTimeout = 1000;
master.ReadCoils(100, 1, 1);
}
Expand Down
4 changes: 2 additions & 2 deletions NModbus4.IntegrationTests/NModbusSerialRtuSlaveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public void NModbusSerialRtuSlave_BonusCharacter_VerifyTimeout()
SerialPort masterPort = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultMasterSerialPortName);
SerialPort slavePort = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultSlaveSerialPortName);

using ModbusSerialMaster? master = ModbusSerialMaster.CreateRtu(masterPort);
using ModbusSerialSlave? slave = ModbusSerialSlave.CreateRtu(1, slavePort);
using ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(masterPort);
using ModbusSerialSlave slave = ModbusSerialSlave.CreateRtu(1, slavePort);
master.Transport.ReadTimeout = master.Transport.WriteTimeout = 1000;
slave.DataStore = DataStoreFactory.CreateTestDataStore();

Expand Down
28 changes: 14 additions & 14 deletions NModbus4.IntegrationTests/NModbusTcpSlaveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class NModbusTcpSlaveFixture
public void ModbusTcpSlave_ConnectionResetByPeer()
{
TcpListener slaveListener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.IsBackground = true;
slaveThread.Start();
Expand All @@ -46,13 +46,13 @@ public void ModbusTcpSlave_ConnectionResetByPeer()
public void ModbusTcpSlave_ConnectionClosesGracefully()
{
TcpListener slaveListener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.IsBackground = true;
slaveThread.Start();

TcpClient? masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using (ModbusIpMaster? master = ModbusIpMaster.CreateIp(masterClient))
TcpClient masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using (ModbusIpMaster master = ModbusIpMaster.CreateIp(masterClient))
{
master.Transport.Retries = 0;

Expand All @@ -75,13 +75,13 @@ public void ModbusTcpSlave_ConnectionClosesGracefully()
public void ModbusTcpSlave_ConnectionSlowlyClosesGracefully()
{
TcpListener slaveListener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.IsBackground = true;
slaveThread.Start();

TcpClient? masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using (ModbusIpMaster? master = ModbusIpMaster.CreateIp(masterClient))
TcpClient masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using (ModbusIpMaster master = ModbusIpMaster.CreateIp(masterClient))
{
master.Transport.Retries = 0;

Expand All @@ -102,14 +102,14 @@ public void ModbusTcpSlave_ConnectionSlowlyClosesGracefully()
[Fact]
public void ModbusTcpSlave_MultiThreaded()
{
TcpListener? slaveListener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave? slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
TcpListener slaveListener = new(ModbusMasterFixture.TcpHost, ModbusMasterFixture.Port);
using ModbusTcpSlave slave = ModbusTcpSlave.CreateTcp(ModbusMasterFixture.SlaveAddress, slaveListener);
Thread slaveThread = new(async () => await slave.ListenAsync());
slaveThread.IsBackground = true;
slaveThread.Start();

Thread? workerThread1 = new(Read);
Thread? workerThread2 = new(Read);
Thread workerThread1 = new(Read);
Thread workerThread2 = new(Read);
workerThread1.Start();
workerThread2.Start();

Expand All @@ -119,11 +119,11 @@ public void ModbusTcpSlave_MultiThreaded()

private static void Read(object state)
{
TcpClient? masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using ModbusIpMaster? master = ModbusIpMaster.CreateIp(masterClient);
TcpClient masterClient = new(ModbusMasterFixture.TcpHost.ToString(), ModbusMasterFixture.Port);
using ModbusIpMaster master = ModbusIpMaster.CreateIp(masterClient);
master.Transport.Retries = 0;

Random? random = new();
Random random = new();
for (int i = 0; i < 5; i++)
{
bool[] coils = master.ReadCoils(1, 1);
Expand Down
20 changes: 10 additions & 10 deletions NModbus4.IntegrationTests/NModbusUdpSlaveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class NModbusUdpSlaveFixture
public void ModbusUdpSlave_EnsureTheSlaveShutsDownCleanly()
{
UdpClient client = new(ModbusMasterFixture.Port);
using ModbusUdpSlave? slave = ModbusUdpSlave.CreateUdp(1, client);
AutoResetEvent? handle = new(false);
using ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(1, client);
AutoResetEvent handle = new(false);

Thread? backgroundThread = new(async (state) =>
Thread backgroundThread = new(async (state) =>
{
handle.Set();
await slave.ListenAsync();
Expand Down Expand Up @@ -92,12 +92,12 @@ public void ModbusUdpSlave_MultipleMasters()
[Fact]
public void ModbusUdpSlave_MultiThreaded()
{
DataStore? dataStore = DataStoreFactory.CreateDefaultDataStore();
DataStore dataStore = DataStoreFactory.CreateDefaultDataStore();
dataStore.CoilDiscretes.Add(false);

using UdpClient? slave = CreateAndStartUdpSlave(502, dataStore);
Thread? workerThread1 = new(ReadThread);
Thread? workerThread2 = new(ReadThread);
using UdpClient slave = CreateAndStartUdpSlave(502, dataStore);
Thread workerThread1 = new(ReadThread);
Thread workerThread2 = new(ReadThread);
workerThread1.Start();
workerThread2.Start();

Expand Down Expand Up @@ -130,12 +130,12 @@ public void ModbusUdpSlave_SingleMasterPollingMultipleSlaves()

private static void ReadThread(object state)
{
UdpClient? masterClient = new();
UdpClient masterClient = new();
masterClient.Connect(ModbusMasterFixture.DefaultModbusIPEndPoint);
using ModbusIpMaster? master = ModbusIpMaster.CreateIp(masterClient);
using ModbusIpMaster master = ModbusIpMaster.CreateIp(masterClient);
master.Transport.Retries = 0;

Random? random = new();
Random random = new();
for (int i = 0; i < 5; i++)
{
bool[] coils = master.ReadCoils(1, 1);
Expand Down
Loading

0 comments on commit 09a8632

Please sign in to comment.