using System; using System.Net.Http; using System.Text; 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 switch { "http" => new HttpClientWrapper(serialization == "json" ? ConvertJsonResponse : ConvertMessagePackResponse), "tcp" => new TcpClientWrapper(serialization == "json" ? ConvertBytesJson : ConvertBytesMessagePack), _ => 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 ConvertJsonResponse(HttpResponseMessage response) { var json = await response.Content.ReadAsStringAsync(); var jsonData = JsonSerializer.Deserialize(json); return jsonData?.ToData(); } static async Task ConvertMessagePackResponse(HttpResponseMessage response) { var bytes = await response.Content.ReadAsByteArrayAsync(); var msgPackData = MessagePackSerializer.Deserialize(bytes); return msgPackData?.ToData(); } static Task ConvertBytesJson(byte[] bytes) { var json = Encoding.UTF8.GetString(bytes); var jsonData = JsonSerializer.Deserialize(json); return Task.FromResult(jsonData?.ToData()); } static Task ConvertBytesMessagePack(byte[] bytes) { var msgPackData = MessagePackSerializer.Deserialize(bytes); return Task.FromResult(msgPackData?.ToData()); }