Реализована запись полученных кадров в файл

This commit is contained in:
Пытков Роман
2025-09-19 23:10:54 +03:00
parent c27e78cffe
commit af62ec6375
6 changed files with 183 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ public class TcpClientWrapper : IClient
private Task? _runningTask;
private Func<byte[], Task<Data?>>? _responseConverter;
private TcpClient? _tcpClient;
private Action<Domain.Data>? _callback;
public TcpClientWrapper(Func<byte[], Task<Data?>> responseConverter, string host = "localhost", int port = 5555)
{
@@ -37,11 +38,37 @@ public class TcpClientWrapper : IClient
_tcpClient?.Close();
}
public void RegisterCallback(Action<Domain.Data> callback)
{
_callback = callback;
}
private async Task RunAsync(CancellationToken token)
{
_tcpClient = new TcpClient();
await _tcpClient.ConnectAsync(_host, _port, token);
using (var stream = _tcpClient.GetStream())
const int maxRetries = 5;
const int retryDelayMs = 1000;
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
try
{
_tcpClient = new TcpClient();
await _tcpClient.ConnectAsync(_host, _port, token);
break; // Connection successful
}
catch (Exception ex) when (attempt < maxRetries)
{
Console.WriteLine($"Connection attempt {attempt} failed: {ex.Message}. Retrying in {retryDelayMs}ms...");
await Task.Delay(retryDelayMs, token);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to connect after {maxRetries} attempts: {ex.Message}");
throw;
}
}
using (var stream = _tcpClient!.GetStream())
{
long index = 0;
var sw = Stopwatch.StartNew();
@@ -106,6 +133,7 @@ public class TcpClientWrapper : IClient
lastMs = sw.ElapsedMilliseconds;
}
//Console.WriteLine(data);
_callback?.Invoke(data);
}
}
else