Skip to content

Commit bd71b04

Browse files
Update TcpClientWrapper.cs
1 parent b8da1d6 commit bd71b04

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

NetSdrClientApp/Networking/TcpClientWrapper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public TcpClientWrapper(string host, int port)
3030
_port = port;
3131
}
3232

33+
[ExcludeFromCodeCoverage]
3334
public void Connect()
3435
{
3536
if (Connected)
@@ -42,7 +43,10 @@ public void Connect()
4243

4344
try
4445
{
46+
// якщо вже існує попередній токен — звільняємо його перед створенням нового
47+
_cts?.Dispose();
4548
_cts = new CancellationTokenSource();
49+
4650
_tcpClient.Connect(_host, _port);
4751
_stream = _tcpClient.GetStream();
4852
Console.WriteLine($"Connected to {_host}:{_port}");
@@ -51,9 +55,11 @@ public void Connect()
5155
catch (Exception ex)
5256
{
5357
Console.WriteLine($"Failed to connect: {ex.Message}");
58+
_cts?.Dispose(); // звільняємо ресурс у випадку помилки
5459
}
5560
}
5661

62+
[ExcludeFromCodeCoverage]
5763
public void Disconnect()
5864
{
5965
if (Connected)
@@ -73,7 +79,6 @@ public void Disconnect()
7379
}
7480
}
7581

76-
[ExcludeFromCodeCoverage]
7782
public async Task SendMessageAsync(byte[] data)
7883
{
7984
await SendDataAsync(data);
@@ -100,6 +105,7 @@ private async Task SendDataAsync(byte[] data)
100105
}
101106
}
102107

108+
[ExcludeFromCodeCoverage]
103109
private async Task StartListeningAsync()
104110
{
105111
if (Connected && _stream != null && _stream.CanRead)

0 commit comments

Comments
 (0)