Skip to content
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
21 changes: 10 additions & 11 deletions NetSdrClientApp/Networking/TcpClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public event EventHandler<byte[]>? MessageReceived;

public TcpClientWrapper(string host, int port)

Check warning on line 25 in NetSdrClientApp/Networking/TcpClientWrapper.cs

View workflow job for this annotation

GitHub Actions / Sonar Check

Non-nullable field '_cts' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
_host = host;
_port = port;
Expand Down Expand Up @@ -60,7 +60,7 @@
_stream?.Close();
_tcpClient?.Close();

_cts = null;

Check warning on line 63 in NetSdrClientApp/Networking/TcpClientWrapper.cs

View workflow job for this annotation

GitHub Actions / Sonar Check

Cannot convert null literal to non-nullable reference type.
_tcpClient = null;
_stream = null;
Console.WriteLine("Disconnected.");
Expand All @@ -73,23 +73,22 @@

public async Task SendMessageAsync(byte[] data)
{
if (Connected && _stream != null && _stream.CanWrite)
{
Console.WriteLine($"Message sent: " + data.Select(b => Convert.ToString(b, toBase: 16)).Aggregate((l, r) => $"{l} {r}"));
await _stream.WriteAsync(data, 0, data.Length);
}
else
{
throw new InvalidOperationException("Not connected to a server.");
}

await SendDataAsync(data);
}

public async Task SendMessageAsync(string str)
public async Task SendMessageAsync(string str)
{
var data = Encoding.UTF8.GetBytes(str);
await SendDataAsync(data);
}
private async Task SendDataAsync(byte[] data)
{
if (Connected && _stream != null && _stream.CanWrite)
{
Console.WriteLine($"Message sent: " + data.Select(b => Convert.ToString(b, toBase: 16)).Aggregate((l, r) => $"{l} {r}"));

string hexString = string.Join(" ", data.Select(b => b.ToString("X2")));
Console.WriteLine($"Message sent: {hexString}");
await _stream.WriteAsync(data, 0, data.Length);
}
else
Expand Down Expand Up @@ -117,7 +116,7 @@
}
}
}
catch (OperationCanceledException ex)

Check warning on line 119 in NetSdrClientApp/Networking/TcpClientWrapper.cs

View workflow job for this annotation

GitHub Actions / Sonar Check

The variable 'ex' is declared but never used
{
//empty
}
Expand Down
20 changes: 7 additions & 13 deletions NetSdrClientApp/Networking/UdpClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Console.WriteLine($"Received from {result.RemoteEndPoint}");
}
}
catch (OperationCanceledException ex)

Check warning on line 40 in NetSdrClientApp/Networking/UdpClientWrapper.cs

View workflow job for this annotation

GitHub Actions / Sonar Check

The variable 'ex' is declared but never used
{
//empty
}
Expand All @@ -46,12 +46,12 @@
Console.WriteLine($"Error receiving message: {ex.Message}");
}
}

public void StopListening()
private void StopInternal()
{
try
{
_cts?.Cancel();
_cts?.Dispose();
_udpClient?.Close();
Console.WriteLine("Stopped listening for UDP messages.");
}
Expand All @@ -60,19 +60,13 @@
Console.WriteLine($"Error while stopping: {ex.Message}");
}
}

public void StopListening()
{
StopInternal();
}
public void Exit()
{
try
{
_cts?.Cancel();
_udpClient?.Close();
Console.WriteLine("Stopped listening for UDP messages.");
}
catch (Exception ex)
{
Console.WriteLine($"Error while stopping: {ex.Message}");
}
StopInternal();
}

public override int GetHashCode()
Expand Down
Loading