Commit 6a6c6a4c authored by Andreas Müller's avatar Andreas Müller

Upgrade to C#7.1 and small fixes.

parent 127b2203
Pipeline #33 passed with stage
in 41 seconds
......@@ -24,7 +24,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Serial", "src\Modbus
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Tcp", "src\Modbus.Tcp\Modbus.Tcp.csproj", "{BF60D1D4-3767-4EC4-AC53-C1772958BE14}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{33EB4212-B2B4-4CFA-A374-1DD9D2675822}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{33EB4212-B2B4-4CFA-A374-1DD9D2675822}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -32,10 +32,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Release|Any CPU.Build.0 = Release|Any CPU
{2E6A165D-23DE-40D2-8A43-FE6EFA86F076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E6A165D-23DE-40D2-8A43-FE6EFA86F076}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E6A165D-23DE-40D2-8A43-FE6EFA86F076}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -48,6 +44,10 @@ Global
{30629B0B-B815-4951-AE56-9A0038210ECB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30629B0B-B815-4951-AE56-9A0038210ECB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30629B0B-B815-4951-AE56-9A0038210ECB}.Release|Any CPU.Build.0 = Release|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF60D1D4-3767-4EC4-AC53-C1772958BE14}.Release|Any CPU.Build.0 = Release|Any CPU
{33EB4212-B2B4-4CFA-A374-1DD9D2675822}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33EB4212-B2B4-4CFA-A374-1DD9D2675822}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33EB4212-B2B4-4CFA-A374-1DD9D2675822}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -57,10 +57,10 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BF60D1D4-3767-4EC4-AC53-C1772958BE14} = {D4E6650D-2156-4660-B531-0B2AAD475BA1}
{2E6A165D-23DE-40D2-8A43-FE6EFA86F076} = {D4E6650D-2156-4660-B531-0B2AAD475BA1}
{71D10922-FE10-4A77-B4AC-E947797E9CFE} = {D4E6650D-2156-4660-B531-0B2AAD475BA1}
{30629B0B-B815-4951-AE56-9A0038210ECB} = {D4E6650D-2156-4660-B531-0B2AAD475BA1}
{BF60D1D4-3767-4EC4-AC53-C1772958BE14} = {D4E6650D-2156-4660-B531-0B2AAD475BA1}
{33EB4212-B2B4-4CFA-A374-1DD9D2675822} = {3B6130C0-9168-4208-A677-29DD20301220}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
......
......@@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
......
......@@ -13,6 +13,7 @@
<PackageId>AMWD.Modbus.Common</PackageId>
<AssemblyName>AMWD.Modbus.Common</AssemblyName>
<RootNamespace>AMWD.Modbus.Common</RootNamespace>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup>
<NrtRevisionFormat>{semvertag}</NrtRevisionFormat>
......
......@@ -13,6 +13,7 @@
<PackageId>AMWD.Modbus.Serial</PackageId>
<AssemblyName>AMWD.Modbus.Serial</AssemblyName>
<RootNamespace>AMWD.Modbus.Serial</RootNamespace>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup>
<NrtRevisionFormat>{semvertag}</NrtRevisionFormat>
......
......@@ -36,6 +36,7 @@ namespace AMWD.Modbus.Tcp.Client
private bool wasConnected = false;
private bool isReconnecting = false;
private TaskCompletionSource<bool> reconnectTcs;
private Task receiveTask;
// Transaction handling
private ushort transactionId = 0;
......@@ -182,7 +183,7 @@ namespace AMWD.Modbus.Tcp.Client
wasConnected = false;
mainCts = new CancellationTokenSource();
Task.Run(() => ReceiveLoop(mainCts.Token));
receiveTask = Task.Run(() => ReceiveLoop(mainCts.Token));
Task.Run(() => Reconnect(mainCts.Token));
ConnectingTask = GetWaitTask(mainCts.Token);
......@@ -979,7 +980,7 @@ namespace AMWD.Modbus.Tcp.Client
#region Private Methods
private async void ReceiveLoop(CancellationToken ct)
private async Task ReceiveLoop(CancellationToken ct)
{
logger?.LogInformation("ModbusClient.ReceiveLoop started");
var reported = false;
......@@ -991,13 +992,23 @@ namespace AMWD.Modbus.Tcp.Client
if (stream == null)
{
if (!reported)
{
logger?.LogTrace("ModbusClient.ReceiveLoop got no stream, waiting...");
}
reported = true;
await Task.Delay(100);
continue;
}
reported = false;
if (reported)
{
logger?.LogTrace("ModbusClient.ReceiveLoop stream available");
reported = false;
}
SpinWait.SpinUntil(() => stream.DataAvailable || ct.IsCancellationRequested);
if (ct.IsCancellationRequested)
continue;
var bytes = new List<byte>();
......@@ -1010,13 +1021,14 @@ namespace AMWD.Modbus.Tcp.Client
expectedCount -= count;
}
while (expectedCount > 0 && !ct.IsCancellationRequested);
if (ct.IsCancellationRequested)
continue;
var lenBytes = bytes.Skip(4).Take(2).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(lenBytes);
}
expectedCount = BitConverter.ToUInt16(lenBytes, 0);
do
......@@ -1027,6 +1039,8 @@ namespace AMWD.Modbus.Tcp.Client
expectedCount -= count;
}
while (expectedCount > 0 && !ct.IsCancellationRequested);
if (ct.IsCancellationRequested)
continue;
var response = new Response(bytes.ToArray());
if (awaitedResponses.TryRemove(response.TransactionId, out var tcs))
......@@ -1034,6 +1048,10 @@ namespace AMWD.Modbus.Tcp.Client
tcs.TrySetResult(response);
}
}
catch (ObjectDisposedException) when (ct.IsCancellationRequested)
{
// stream already gone
}
catch (Exception ex)
{
logger?.LogError(ex, "ModbusClient.ReceiveLoop");
......@@ -1242,7 +1260,7 @@ namespace AMWD.Modbus.Tcp.Client
{
Task.Run(() => Disconnected?.Invoke(this, EventArgs.Empty)).Forget();
}
await Task.CompletedTask;
await Task.WhenAll(ConnectingTask, receiveTask);
logger?.LogInformation("ModbusClient.Disconnect done");
}
......@@ -1272,6 +1290,11 @@ namespace AMWD.Modbus.Tcp.Client
DisconnectInternal(true)
.GetAwaiter()
.GetResult();
if (logger is IDisposable dl)
{
dl?.Dispose();
}
}
#endregion IDisposable implementation
......
......@@ -13,6 +13,7 @@
<PackageId>AMWD.Modbus.Tcp</PackageId>
<AssemblyName>AMWD.Modbus.Tcp</AssemblyName>
<RootNamespace>AMWD.Modbus.Tcp</RootNamespace>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup>
<NrtRevisionFormat>{semvertag}</NrtRevisionFormat>
......
......@@ -65,7 +65,7 @@ namespace AMWD.Modbus.Tcp.Protocol
/// </summary>
public byte[] Bytes
{
get { return Data.Buffer; }
get { return Data?.Buffer ?? new byte[0]; }
set { Data = new DataBuffer(value); }
}
......
......@@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment