This commit is contained in:
Пытков Роман
2025-09-17 00:44:21 +03:00
parent 1f0e7c285c
commit bbe5a75dba
15 changed files with 783 additions and 0 deletions

18
Client/Client.csproj Normal file
View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Domain/Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="3.1.4" />
</ItemGroup>
</Project>

96
Client/HttpClient.cs Normal file
View File

@@ -0,0 +1,96 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Domain;
using Domain.Dto;
using MessagePack;
namespace Client;
public class HttpClientWrapper : IClient
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
private CancellationTokenSource _cts = new CancellationTokenSource();
private Task? _runningTask;
private Func<HttpResponseMessage, Task<Data?>>? _responseConverter;
public HttpClientWrapper(Func<HttpResponseMessage, Task<Data?>> responseConverter, string baseUrl = "http://localhost:5555/")
{
_httpClient = new HttpClient();
_baseUrl = baseUrl;
_responseConverter = responseConverter;
}
public void Start()
{
_cts = new CancellationTokenSource();
_runningTask = Task.Run(() => RunAsync(_cts.Token));
}
public void Stop()
{
_cts.Cancel();
_runningTask?.Wait();
_httpClient.Dispose();
}
private async Task RunAsync(CancellationToken token)
{
long index = 0;
var sw = Stopwatch.StartNew();
var lastMs = 0L;
var lastIndex = 0L;
var ms = 1000;
while (!token.IsCancellationRequested)
{
try
{
var url = $"{_baseUrl}fetchpackage?index={index}";
var response = await _httpClient.GetAsync(url, token);
if (response.IsSuccessStatusCode)
{
if (_responseConverter != null)
{
var data = await _responseConverter(response);
if (data != null)
{
var diff = sw.ElapsedMilliseconds - lastMs;
if (diff >= ms)
{
var fetched = index - lastIndex;
System.Console.WriteLine($"Fetched {fetched} data packages in {diff} ms.");
lastIndex = index;
lastMs = sw.ElapsedMilliseconds;
}
//System.Console.WriteLine(data);
}
}
else
{
Console.WriteLine("Response converter not set.");
}
}
else
{
Console.WriteLine($"Failed to fetch data for index {index}: {response.StatusCode}");
}
index++;
//await Task.Delay(100, token); // Wait 1 second between requests
}
catch (TaskCanceledException)
{
break;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
await Task.Delay(1000, token);
}
}
}
}

7
Client/IClient.cs Normal file
View File

@@ -0,0 +1,7 @@
namespace Client;
interface IClient
{
public void Start();
public void Stop();
}

53
Client/Program.cs Normal file
View File

@@ -0,0 +1,53 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Client;
using Domain;
using Domain.Dto;
using MessagePack;
if (args.Length < 2)
{
System.Console.WriteLine("Pass two args: http/tcp and json/bin");
return -1;
}
var protocol = args[0];
var serialization = args[1];
IClient? client = protocol == "http" ? new HttpClientWrapper(serialization == "json" ? ConvertJsonResponse : ConvertMessagePackResponse) : null;
client?.Start();
System.Console.WriteLine("Client started:");
System.Console.WriteLine(client);
// Обработка выхода по Ctrl+C
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true; // Prevent immediate termination
Console.WriteLine("Shutdown signal received. Stopping client...");
client?.Stop();
Console.WriteLine("Goodbye!");
Environment.Exit(0);
};
// Бесконечный цикл ожидания
while (true)
{
Thread.Sleep(1000);
}
static async Task<Data?> ConvertJsonResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
var jsonData = JsonSerializer.Deserialize<JsonData>(json);
return jsonData?.ToData();
}
static async Task<Data?> ConvertMessagePackResponse(HttpResponseMessage response)
{
var bytes = await response.Content.ReadAsByteArrayAsync();
var msgPackData = MessagePackSerializer.Deserialize<MessagePackData>(bytes);
return msgPackData?.ToData();
}