Реализована запись полученных кадров в файл
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user