From 7ca46f321cd308182148a7e053928e309ecfee66 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 11:37:31 +0300 Subject: [PATCH 1/9] lab1 --- .github/workflows/sonarcloud.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index e784069..73fa9dc 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -37,6 +37,8 @@ on: permissions: pull-requests: read # allows SonarCloud to decorate PRs with analysis results + contents: read + jobs: sonar-check: @@ -56,8 +58,8 @@ jobs: dotnet tool install --global dotnet-sonarscanner echo "$env:USERPROFILE\.dotnet\tools" >> $env:GITHUB_PATH dotnet sonarscanner begin ` - /k:"ppanchen_NetSdrClient" ` - /o:"ppanchen" ` + /k:"slavik22_ReengineeringCourse" ` + /o:"slavik22" ` /d:sonar.token="${{ secrets.SONAR_TOKEN }}" ` /d:sonar.cs.opencover.reportsPaths="**/coverage.xml" ` /d:sonar.cpd.cs.minimumTokens=40 ` From 4fb8cef3e61254a5c037a63fda488a5813e58210 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 11:58:31 +0300 Subject: [PATCH 2/9] lab2 --- NetSdrClientApp/Messages/NetSdrMessageHelper.cs | 8 +------- NetSdrClientApp/NetSdrClient.cs | 8 -------- NetSdrClientApp/Networking/ITcpClient.cs | 9 +-------- NetSdrClientApp/Networking/TcpClientWrapper.cs | 9 +-------- NetSdrClientApp/Networking/UdpClientWrapper.cs | 5 +---- 5 files changed, 4 insertions(+), 35 deletions(-) diff --git a/NetSdrClientApp/Messages/NetSdrMessageHelper.cs b/NetSdrClientApp/Messages/NetSdrMessageHelper.cs index 0d69b4d..43ee883 100644 --- a/NetSdrClientApp/Messages/NetSdrMessageHelper.cs +++ b/NetSdrClientApp/Messages/NetSdrMessageHelper.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.PortableExecutable; -using System.Text; -using System.Threading.Tasks; - + namespace NetSdrClientApp.Messages { //TODO: analyze possible use of [StructLayout] for better performance and readability diff --git a/NetSdrClientApp/NetSdrClient.cs b/NetSdrClientApp/NetSdrClient.cs index b0a7c05..022acc1 100644 --- a/NetSdrClientApp/NetSdrClient.cs +++ b/NetSdrClientApp/NetSdrClient.cs @@ -1,14 +1,6 @@ using NetSdrClientApp.Messages; using NetSdrClientApp.Networking; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Channels; -using System.Threading.Tasks; using static NetSdrClientApp.Messages.NetSdrMessageHelper; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace NetSdrClientApp { diff --git a/NetSdrClientApp/Networking/ITcpClient.cs b/NetSdrClientApp/Networking/ITcpClient.cs index 3470b5d..e779e95 100644 --- a/NetSdrClientApp/Networking/ITcpClient.cs +++ b/NetSdrClientApp/Networking/ITcpClient.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static System.Runtime.InteropServices.JavaScript.JSType; - -namespace NetSdrClientApp.Networking +namespace NetSdrClientApp.Networking { public interface ITcpClient { diff --git a/NetSdrClientApp/Networking/TcpClientWrapper.cs b/NetSdrClientApp/Networking/TcpClientWrapper.cs index 1f37e2e..2d4e555 100644 --- a/NetSdrClientApp/Networking/TcpClientWrapper.cs +++ b/NetSdrClientApp/Networking/TcpClientWrapper.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Net.Sockets; +using System.Net.Sockets; using System.Text; -using System.Threading; -using System.Threading.Tasks; namespace NetSdrClientApp.Networking { diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 31e0b79..04091a0 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -1,10 +1,7 @@ -using System; -using System.Net; +using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; -using System.Threading; -using System.Threading.Tasks; public class UdpClientWrapper : IUdpClient { From 78b60e6f6f03d15eed9e80c2b93a9ab6d474fcfa Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:13:20 +0300 Subject: [PATCH 3/9] make private fields readonly --- NetSdrClientApp/NetSdrClient.cs | 4 ++-- NetSdrClientApp/Networking/TcpClientWrapper.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NetSdrClientApp/NetSdrClient.cs b/NetSdrClientApp/NetSdrClient.cs index 022acc1..97f52c3 100644 --- a/NetSdrClientApp/NetSdrClient.cs +++ b/NetSdrClientApp/NetSdrClient.cs @@ -6,8 +6,8 @@ namespace NetSdrClientApp { public class NetSdrClient { - private ITcpClient _tcpClient; - private IUdpClient _udpClient; + private readonly ITcpClient _tcpClient; + private readonly IUdpClient _udpClient; public bool IQStarted { get; set; } diff --git a/NetSdrClientApp/Networking/TcpClientWrapper.cs b/NetSdrClientApp/Networking/TcpClientWrapper.cs index 2d4e555..afbe43d 100644 --- a/NetSdrClientApp/Networking/TcpClientWrapper.cs +++ b/NetSdrClientApp/Networking/TcpClientWrapper.cs @@ -5,8 +5,8 @@ namespace NetSdrClientApp.Networking { public class TcpClientWrapper : ITcpClient { - private string _host; - private int _port; + private readonly string _host; + private readonly int _port; private TcpClient? _tcpClient; private NetworkStream? _stream; private CancellationTokenSource _cts; From ef1262c6604320e578f756a6f0291c537f1f96d5 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:19:36 +0300 Subject: [PATCH 4/9] fix: make private fields readonly (S2933) --- NetSdrClientApp/NetSdrClient.cs | 8 ++++---- NetSdrClientApp/Networking/TcpClientWrapper.cs | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/NetSdrClientApp/NetSdrClient.cs b/NetSdrClientApp/NetSdrClient.cs index 97f52c3..1b9a04f 100644 --- a/NetSdrClientApp/NetSdrClient.cs +++ b/NetSdrClientApp/NetSdrClient.cs @@ -58,7 +58,7 @@ public async Task StartIQAsync() return; } -; var iqDataMode = (byte)0x80; + var iqDataMode = (byte)0x80; var start = (byte)0x02; var fifo16bitCaptureMode = (byte)0x01; var n = (byte)1; @@ -108,7 +108,7 @@ public async Task ChangeFrequencyAsync(long hz, int channel) private void _udpClient_MessageReceived(object? sender, byte[] e) { - NetSdrMessageHelper.TranslateMessage(e, out MsgTypes type, out ControlItemCodes code, out ushort sequenceNum, out byte[] body); + NetSdrMessageHelper.TranslateMessage(e, out _, out _, out _, out byte[] body); var samples = NetSdrMessageHelper.GetSamples(16, body); Console.WriteLine($"Samples recieved: " + body.Select(b => Convert.ToString(b, toBase: 16)).Aggregate((l, r) => $"{l} {r}")); @@ -123,9 +123,9 @@ private void _udpClient_MessageReceived(object? sender, byte[] e) } } - private TaskCompletionSource responseTaskSource; + private TaskCompletionSource? responseTaskSource; - private async Task SendTcpRequest(byte[] msg) + private async Task SendTcpRequest(byte[] msg) { if (!_tcpClient.Connected) { diff --git a/NetSdrClientApp/Networking/TcpClientWrapper.cs b/NetSdrClientApp/Networking/TcpClientWrapper.cs index afbe43d..7500897 100644 --- a/NetSdrClientApp/Networking/TcpClientWrapper.cs +++ b/NetSdrClientApp/Networking/TcpClientWrapper.cs @@ -9,7 +9,7 @@ public class TcpClientWrapper : ITcpClient private readonly int _port; private TcpClient? _tcpClient; private NetworkStream? _stream; - private CancellationTokenSource _cts; + private CancellationTokenSource? _cts; public bool Connected => _tcpClient != null && _tcpClient.Connected && _stream != null; @@ -50,6 +50,7 @@ public void Disconnect() if (Connected) { _cts?.Cancel(); + _cts?.Dispose(); _stream?.Close(); _tcpClient?.Close(); @@ -110,7 +111,7 @@ private async Task StartListeningAsync() } } } - catch (OperationCanceledException ex) + catch (OperationCanceledException) { //empty } From a07a5418b7da28ea1e019ed970a83e7a31d1dd05 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:23:11 +0300 Subject: [PATCH 5/9] fix: dispose CancellationTokenSource to prevent resource leak (S2930) --- NetSdrClientApp/Networking/UdpClientWrapper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 04091a0..7646972 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -47,6 +47,7 @@ public void StopListening() try { _cts?.Cancel(); + _cts?.Dispose(); _udpClient?.Close(); Console.WriteLine("Stopped listening for UDP messages."); } From e968a0f43bac820fd9553f9a612e3ce724fd5546 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:23:20 +0300 Subject: [PATCH 6/9] fix: remove unused exception variable in empty catch (S2486) --- NetSdrClientApp/Networking/UdpClientWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 7646972..c2c9e18 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -32,7 +32,7 @@ public async Task StartListeningAsync() Console.WriteLine($"Received from {result.RemoteEndPoint}"); } } - catch (OperationCanceledException ex) + catch (OperationCanceledException) { //empty } From 3ca003c66f605fab7b90d0f931859849671ee281 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:23:52 +0300 Subject: [PATCH 7/9] fix: move IUdpClient and UdpClientWrapper into named namespace (S3903) --- NetSdrClientApp/Networking/IUdpClient.cs | 17 ++- .../Networking/UdpClientWrapper.cs | 123 +++++++++--------- 2 files changed, 73 insertions(+), 67 deletions(-) diff --git a/NetSdrClientApp/Networking/IUdpClient.cs b/NetSdrClientApp/Networking/IUdpClient.cs index 1b9f931..053262b 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(); + } +} diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index c2c9e18..941a33a 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -1,83 +1,86 @@ -using System.Net; +using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; -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 { - _localEndPoint = new IPEndPoint(IPAddress.Any, port); - } + private readonly IPEndPoint _localEndPoint; + private CancellationTokenSource? _cts; + private UdpClient? _udpClient; - public async Task StartListeningAsync() - { - _cts = new CancellationTokenSource(); - Console.WriteLine("Start listening for UDP messages..."); + public event EventHandler? MessageReceived; - try + public UdpClientWrapper(int port) { - _udpClient = new UdpClient(_localEndPoint); - while (!_cts.Token.IsCancellationRequested) + _localEndPoint = new IPEndPoint(IPAddress.Any, port); + } + + public async Task StartListeningAsync() + { + _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); + while (!_cts.Token.IsCancellationRequested) + { + UdpReceiveResult result = await _udpClient.ReceiveAsync(_cts.Token); + MessageReceived?.Invoke(this, result.Buffer); - Console.WriteLine($"Received from {result.RemoteEndPoint}"); + Console.WriteLine($"Received from {result.RemoteEndPoint}"); + } + } + catch (OperationCanceledException) + { + //empty + } + catch (Exception ex) + { + Console.WriteLine($"Error receiving message: {ex.Message}"); } } - catch (OperationCanceledException) - { - //empty - } - catch (Exception ex) - { - Console.WriteLine($"Error receiving message: {ex.Message}"); - } - } - public void StopListening() - { - try + public void StopListening() { - _cts?.Cancel(); - _cts?.Dispose(); - _udpClient?.Close(); - Console.WriteLine("Stopped listening for UDP messages."); - } - catch (Exception ex) - { - Console.WriteLine($"Error while stopping: {ex.Message}"); + try + { + _cts?.Cancel(); + _cts?.Dispose(); + _udpClient?.Close(); + Console.WriteLine("Stopped listening for UDP messages."); + } + catch (Exception ex) + { + Console.WriteLine($"Error while stopping: {ex.Message}"); + } } - } - public void Exit() - { - try - { - _cts?.Cancel(); - _udpClient?.Close(); - Console.WriteLine("Stopped listening for UDP messages."); - } - catch (Exception ex) + public void Exit() { - Console.WriteLine($"Error while stopping: {ex.Message}"); + try + { + _cts?.Cancel(); + _udpClient?.Close(); + Console.WriteLine("Stopped listening for UDP messages."); + } + catch (Exception ex) + { + Console.WriteLine($"Error while stopping: {ex.Message}"); + } } - } - public override int GetHashCode() - { - var payload = $"{nameof(UdpClientWrapper)}|{_localEndPoint.Address}|{_localEndPoint.Port}"; + public override int GetHashCode() + { + var payload = $"{nameof(UdpClientWrapper)}|{_localEndPoint.Address}|{_localEndPoint.Port}"; - using var md5 = MD5.Create(); - var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(payload)); + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(payload)); - return BitConverter.ToInt32(hash, 0); + return BitConverter.ToInt32(hash, 0); + } } -} \ No newline at end of file +} From 2aafcda8790fa049bc0bdec4d66293593b316402 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:24:12 +0300 Subject: [PATCH 8/9] fix: provide message in ArgumentOutOfRangeException (S3928) --- NetSdrClientApp/Messages/NetSdrMessageHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetSdrClientApp/Messages/NetSdrMessageHelper.cs b/NetSdrClientApp/Messages/NetSdrMessageHelper.cs index 43ee883..f13960b 100644 --- a/NetSdrClientApp/Messages/NetSdrMessageHelper.cs +++ b/NetSdrClientApp/Messages/NetSdrMessageHelper.cs @@ -105,7 +105,7 @@ public static IEnumerable GetSamples(ushort sampleSize, byte[] body) sampleSize /= 8; //to bytes if (sampleSize > 4) { - throw new ArgumentOutOfRangeException(); + throw new ArgumentOutOfRangeException(nameof(sampleSize), sampleSize, "Sample size must be 8, 16, 24, or 32 bits."); } var bodyEnumerable = body as IEnumerable; From b972a36258c4ae0f7a82393422a6d571dd318944 Mon Sep 17 00:00:00 2001 From: Yaroslav Fedyna Date: Sun, 24 May 2026 12:24:28 +0300 Subject: [PATCH 9/9] fix: use static MD5.HashData instead of ComputeHash (CA1850) --- NetSdrClientApp/Networking/UdpClientWrapper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/NetSdrClientApp/Networking/UdpClientWrapper.cs b/NetSdrClientApp/Networking/UdpClientWrapper.cs index 941a33a..be691a0 100644 --- a/NetSdrClientApp/Networking/UdpClientWrapper.cs +++ b/NetSdrClientApp/Networking/UdpClientWrapper.cs @@ -77,8 +77,7 @@ public override int GetHashCode() { var payload = $"{nameof(UdpClientWrapper)}|{_localEndPoint.Address}|{_localEndPoint.Port}"; - using var md5 = MD5.Create(); - var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(payload)); + var hash = MD5.HashData(Encoding.UTF8.GetBytes(payload)); return BitConverter.ToInt32(hash, 0); }