112 lines
3.0 KiB
C#
112 lines
3.0 KiB
C#
using System.Diagnostics;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using Domain;
|
|
using Domain.Dto;
|
|
using MessagePack;
|
|
using NetworkTest;
|
|
using Server;
|
|
|
|
if (args.Length < 2)
|
|
{
|
|
System.Console.WriteLine("Pass twp arg: test/http/tcp and json/bin");
|
|
return -1;
|
|
}
|
|
|
|
var dataGenerator = new DataGenerator(generationInterval: TimeSpan.FromMilliseconds(1));
|
|
|
|
var protocol = args[0];
|
|
var serialization = args[1];
|
|
|
|
if (protocol == "test")
|
|
{
|
|
int nullCount = 0;
|
|
long index = 0;
|
|
var sw = Stopwatch.StartNew();
|
|
var lastMs = 0L;
|
|
var lastIndex = 0L;
|
|
var ms = 1000;
|
|
|
|
var json = serialization == "json";
|
|
while (true)
|
|
{
|
|
var data = dataGenerator.GetPackage(index);
|
|
if (data == null)
|
|
{
|
|
nullCount++;
|
|
continue;
|
|
}
|
|
|
|
if (json)
|
|
{
|
|
JsonData jsonData = new JsonData(data);
|
|
var responseText = JsonSerializer.Serialize(jsonData);
|
|
byte[] buffer = Encoding.UTF8.GetBytes(responseText);
|
|
}
|
|
else
|
|
{
|
|
MessagePackData msgPackData = new MessagePackData(data);
|
|
byte[] buffer = MessagePackSerializer.Serialize(msgPackData);
|
|
}
|
|
|
|
index++;
|
|
|
|
var diff = sw.ElapsedMilliseconds - lastMs;
|
|
if (diff >= ms)
|
|
{
|
|
var serializrd = index - lastIndex;
|
|
System.Console.WriteLine($"Serialized {serializrd} data packages in {diff} ms.");
|
|
lastIndex = index;
|
|
lastMs = sw.ElapsedMilliseconds;
|
|
}
|
|
}
|
|
}
|
|
|
|
IServer? server = protocol == "http" ?
|
|
new HttpServer(index => dataGenerator.GetPackage(index),
|
|
serialization == "json" ? PrepareResponseJson : PrepareResponseMessagePack) :
|
|
null;
|
|
|
|
server?.Start();
|
|
System.Console.WriteLine("Server started:");
|
|
System.Console.WriteLine(server);
|
|
|
|
// Обработка выхода по Ctrl+C
|
|
Console.CancelKeyPress += (sender, e) =>
|
|
{
|
|
e.Cancel = true; // Prevent immediate termination
|
|
Console.WriteLine("Shutdown signal received. Stopping server...");
|
|
server?.Stop();
|
|
Console.WriteLine("Goodbye!");
|
|
Environment.Exit(0);
|
|
};
|
|
|
|
|
|
// Бесконечный цикл ожидания
|
|
while (true)
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
|
|
void PrepareResponseJson(Data data, HttpListenerResponse response)
|
|
{
|
|
JsonData jsonData = new JsonData(data);
|
|
var responseText = JsonSerializer.Serialize(jsonData);
|
|
byte[] buffer = Encoding.UTF8.GetBytes(responseText);
|
|
response.ContentType = "application/json";
|
|
response.ContentLength64 = buffer.Length;
|
|
response.OutputStream.Write(buffer, 0, buffer.Length);
|
|
}
|
|
|
|
void PrepareResponseMessagePack(Data data, HttpListenerResponse response)
|
|
{
|
|
MessagePackData msgPackData = new MessagePackData(data);
|
|
byte[] buffer = MessagePackSerializer.Serialize(msgPackData);
|
|
response.ContentType = "application/x-msgpack";
|
|
response.ContentLength64 = buffer.Length;
|
|
response.OutputStream.Write(buffer, 0, buffer.Length);
|
|
}
|
|
|