Files
NetworkTest/Client/Program.cs
Пытков Роман bbe5a75dba Init
2025-09-17 00:44:21 +03:00

54 lines
1.5 KiB
C#

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();
}