From a9589346f99f97020a3c3595732943602f5b746b Mon Sep 17 00:00:00 2001 From: olekca160406 Date: Mon, 8 Dec 2025 12:03:48 +0200 Subject: [PATCH 1/4] Update IUdpClient.cs --- NetSdrClientApp/Networking/IUdpClient.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/NetSdrClientApp/Networking/IUdpClient.cs b/NetSdrClientApp/Networking/IUdpClient.cs index 1b9f9311..8bc89c69 100644 --- a/NetSdrClientApp/Networking/IUdpClient.cs +++ b/NetSdrClientApp/Networking/IUdpClient.cs @@ -1,10 +1,13 @@ - -public interface IUdpClient +namespace NetSdrClientApp.Networking { - event EventHandler? MessageReceived; + public interface IUdpClient + { + event EventHandler? MessageReceived; - Task StartListeningAsync(); + Task StartListeningAsync(); - void StopListening(); - void Exit(); -} \ No newline at end of file + void StopListening(); + + void Exit(); + } +} From 0b0bb17033bc68b0ed7a03cb34df7dfa03e29214 Mon Sep 17 00:00:00 2001 From: olekca160406 Date: Mon, 8 Dec 2025 12:09:53 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8a8d22c9..9dd87dfe 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ -Usage: -C - connect -D - disconnet -F - set frequency -S - Start/Stop IQ listener -Q - quit +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=olekca160406_NetSdrClient&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=olekca160406_NetSdrClient) +[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=olekca160406_NetSdrClient&metric=bugs)](https://sonarcloud.io/summary/new_code?id=olekca160406_NetSdrClient) +[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=olekca160406_NetSdrClient&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=olekca160406_NetSdrClient) +[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=olekca160406_NetSdrClient&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=olekca160406_NetSdrClient) +[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=olekca160406_NetSdrClient&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=olekca160406_NetSdrClient) From cc9c37ab6aa6a6139c9d7d5c56b045f0dbb1d8df Mon Sep 17 00:00:00 2001 From: olekca160406 Date: Mon, 8 Dec 2025 12:13:38 +0200 Subject: [PATCH 3/4] Update UdpClientWrapper.cs --- .../Networking/UdpClientWrapper.cs | 118 ++++++++++-------- 1 file changed, 66 insertions(+), 52 deletions(-) diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 31e0b798..1402b3be 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -1,85 +1,99 @@ -using System; +using System; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; +using NetSdrClientApp.Networking; -public class UdpClientWrapper : IUdpClient +namespace NetSdrClientApp.Networking { - private readonly IPEndPoint _localEndPoint; - private CancellationTokenSource? _cts; - private UdpClient? _udpClient; - - public event EventHandler? MessageReceived; - - public UdpClientWrapper(int port) + public class UdpClientWrapper : IUdpClient, IDisposable { - _localEndPoint = new IPEndPoint(IPAddress.Any, port); - } + private readonly IPEndPoint _localEndPoint; + private CancellationTokenSource? _cts; + private UdpClient? _udpClient; + private bool _disposed; - public async Task StartListeningAsync() - { - _cts = new CancellationTokenSource(); - Console.WriteLine("Start listening for UDP messages..."); + public event EventHandler? MessageReceived; + + public UdpClientWrapper(int port) + { + _localEndPoint = new IPEndPoint(IPAddress.Any, port); + } - try + public async Task StartListeningAsync() { - _udpClient = new UdpClient(_localEndPoint); - while (!_cts.Token.IsCancellationRequested) + _cts = new CancellationTokenSource(); + Console.WriteLine("Start listening for UDP messages..."); + + try { - UdpReceiveResult result = await _udpClient.ReceiveAsync(_cts.Token); - MessageReceived?.Invoke(this, result.Buffer); + _udpClient = new UdpClient(_localEndPoint); - Console.WriteLine($"Received from {result.RemoteEndPoint}"); + while (!_cts.Token.IsCancellationRequested) + { + UdpReceiveResult result = await _udpClient.ReceiveAsync(_cts.Token); + MessageReceived?.Invoke(this, result.Buffer); + + Console.WriteLine($"Received from {result.RemoteEndPoint}"); + } + } + catch (OperationCanceledException) + { + Console.WriteLine("Listening cancelled."); + } + catch (Exception ex) + { + Console.WriteLine($"Error receiving message: {ex.Message}"); } } - catch (OperationCanceledException ex) - { - //empty - } - catch (Exception ex) - { - Console.WriteLine($"Error receiving message: {ex.Message}"); - } - } - public void StopListening() - { - try + public void StopListening() { _cts?.Cancel(); _udpClient?.Close(); Console.WriteLine("Stopped listening for UDP messages."); } - catch (Exception ex) + + public void Exit() { - Console.WriteLine($"Error while stopping: {ex.Message}"); + StopListening(); } - } - public void Exit() - { - try + + public override bool Equals(object? obj) { - _cts?.Cancel(); - _udpClient?.Close(); - Console.WriteLine("Stopped listening for UDP messages."); + if (obj is not UdpClientWrapper other) return false; + + return _localEndPoint.Port == other._localEndPoint.Port && + _localEndPoint.Address.Equals(other._localEndPoint.Address); + } + + public override int GetHashCode() + { + return HashCode.Combine(_localEndPoint.Address, _localEndPoint.Port); } - catch (Exception ex) + + public void Dispose() { - Console.WriteLine($"Error while stopping: {ex.Message}"); + Dispose(true); + GC.SuppressFinalize(this); } - } - public override int GetHashCode() - { - var payload = $"{nameof(UdpClientWrapper)}|{_localEndPoint.Address}|{_localEndPoint.Port}"; + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; - using var md5 = MD5.Create(); - var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(payload)); + if (disposing) + { + _cts?.Cancel(); + _cts?.Dispose(); + _udpClient?.Dispose(); + } - return BitConverter.ToInt32(hash, 0); + _disposed = true; + } } -} \ No newline at end of file +} From b222926bc4182595e1d6c47894554786fe05f720 Mon Sep 17 00:00:00 2001 From: olekca160406 Date: Mon, 8 Dec 2025 12:15:58 +0200 Subject: [PATCH 4/4] Update UdpClientWrapper.cs --- NetSdrClientApp/Networking/UdpClientWrapper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 1402b3be..20bf1db3 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -62,7 +62,6 @@ public void Exit() StopListening(); } - public override bool Equals(object? obj) { if (obj is not UdpClientWrapper other) return false;