Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/Longbow.Modbus/Extensions/ITcpSocketClientExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Longbow.Modbus/Longbow.Modbus.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>9.0.10</Version>
<Version>9.1.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
21 changes: 6 additions & 15 deletions src/Longbow.Modbus/Rtu/DefaultRtuClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,12 @@ namespace Longbow.Modbus;

class DefaultRtuClient(ISerialPortClient client, IModbusRtuMessageBuilder builder) : ModbusClientBase(builder), IModbusRtuClient
{
public async ValueTask<bool> ConnectAsync(CancellationToken token = default)
{
var ret = false;
try
{
ret = await client.OpenAsync(token);
}
catch (Exception ex)
{
Exception = ex;
}

return ret;
}
public ValueTask<bool> ConnectAsync(CancellationToken token = default) => client.OpenAsync(token);
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simplified implementation removes exception handling that was previously setting the Exception property. This could break error handling expectations if other parts of the codebase rely on accessing connection errors through this property.

Suggested change
public ValueTask<bool> ConnectAsync(CancellationToken token = default) => client.OpenAsync(token);
public async ValueTask<bool> ConnectAsync(CancellationToken token = default)
{
try
{
var result = await client.OpenAsync(token);
Exception = null;
return result;
}
catch (Exception ex)
{
Exception = ex;
return false;
}
}

Copilot uses AI. Check for mistakes.

protected override async Task<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byte> request, CancellationToken token = default)
{
await client.SendAsync(request, token);

return await client.ReceiveAsync(token);
}

Expand All @@ -34,6 +22,9 @@ protected override async Task<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byt
/// </summary>
public override async ValueTask CloseAsync()
{
await client.CloseAsync();
if (client.IsOpen)
{
await client.CloseAsync();
}
}
}
3 changes: 1 addition & 2 deletions src/Longbow.Modbus/Tcp/DefaultTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class DefaultTcpClient(ITcpSocketClient client, IModbusMessageBuilder builder) :

protected override async Task<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byte> request, CancellationToken token = default)
{
client.ThrowIfNotConnected();

await client.SendAsync(request, token);

return await client.ReceiveAsync(token);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Longbow.Modbus/Udp/DefaultUdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class DefaultUdpClient(ModbusUdpClientOptions options, IModbusMessageBuilder bui
{
private UdpClient _client = default!;

public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
public ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
{
await CloseAsync();
_client = new UdpClient(options.LocalEndPoint);
_client.Connect(endPoint);
return true;

return ValueTask.FromResult(true);
Comment on lines +14 to +19
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of await CloseAsync() could cause resource leaks if the method is called multiple times. The previous implementation properly cleaned up existing connections before creating new ones.

Copilot uses AI. Check for mistakes.
}

protected override async Task<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byte> request, CancellationToken token = default)
Expand Down
Loading