feat : 에코 서버 / 클라이언트 기능 추가 작업

This commit is contained in:
qornwh1
2026-03-01 01:05:14 +09:00
parent 30457819b1
commit 97f6187f5d
13 changed files with 255 additions and 133 deletions

View File

@@ -1,17 +1,20 @@
using LiteNetLib.Utils;
using LiteNetLib;
using Serilog;
namespace ClientTester.EchoDummyService;
public class DummyClientService
{
private readonly List<DummyClients> _clients;
private readonly int _sendInterval;
private readonly List<DummyClients> clients;
private readonly int sendInterval;
// 모든거 강종
public event Action? OnAllDisconnected;
public DummyClientService(int count, string ip, int port, string key, int sendIntervalMs = 1000)
{
_sendInterval = sendIntervalMs;
_clients = Enumerable.Range(0, count)
sendInterval = sendIntervalMs;
clients = Enumerable.Range(0, count)
.Select(i => new DummyClients(i, ip, port, key))
.ToList();
@@ -30,62 +33,100 @@ public class DummyClientService
{
while (!ct.IsCancellationRequested)
{
foreach (var c in _clients)
foreach (DummyClients c in clients)
{
c.PollEvents();
}
try { await Task.Delay(15, ct); }
catch (OperationCanceledException) { break; }
try
{
await Task.Delay(15, ct);
}
catch (OperationCanceledException)
{
break;
}
}
}
private async Task SendLoopAsync(CancellationToken ct)
{
try { await Task.Delay(500, ct); }
catch (OperationCanceledException) { return; }
try
{
await Task.Delay(500, ct);
}
catch (OperationCanceledException)
{
return;
}
int tick = 0;
while (!ct.IsCancellationRequested)
{
int sent = 0;
int total = clients.Count;
foreach (var client in _clients)
foreach (DummyClients client in clients)
{
client.SendPing();
if (client.Peer != null) sent++;
if (client.peer != null)
{
sent++;
}
else
{
total--;
}
}
Log.Information("[TICK {Tick:000}] {Sent}/{Total} 전송", tick, sent, _clients.Count);
if (total == 0)
{
Log.Information("All Disconnect Clients");
OnAllDisconnected?.Invoke();
break;
}
Log.Information("[TICK {Tick:000}] {Sent}/{Total} 전송", tick, sent, total);
tick++;
try { await Task.Delay(_sendInterval, ct); }
catch (OperationCanceledException) { break; }
try
{
await Task.Delay(sendInterval, ct);
}
catch (OperationCanceledException)
{
break;
}
}
}
public void PrintStats()
{
int totalSent = 0, totalRecv = 0;
int connected = 0;
int totalSent = 0, totalRecv = 0;
int connected = 0;
Log.Information("════════════ Performance Report ════════════");
Log.Information("───────────── Performance Report ─────────────");
double totalAvgRtt = 0;
foreach (var c in _clients)
foreach (DummyClients c in clients)
{
var stats = c.Peer?.Statistics;
long loss = stats?.PacketLoss ?? 0;
NetStatistics? stats = c.peer?.Statistics;
long loss = stats?.PacketLoss ?? 0;
float lossPct = stats?.PacketLossPercent ?? 0f;
Log.Information(
"[Client {ClientId:00}] Sent={Sent} Recv={Recv} | Loss={Loss}({LossPct:F1}%) AvgRTT={AvgRtt:F3}ms LastRTT={LastRtt:F3}ms",
c.ClientId, c.SentCount, c.ReceivedCount, loss, lossPct, c.AvgRttMs, c.LastRttMs);
c.clientId, c.SentCount, c.ReceivedCount, loss, lossPct, c.AvgRttMs, c.LastRttMs);
totalSent += c.SentCount;
totalRecv += c.ReceivedCount;
totalAvgRtt += c.AvgRttMs;
if (c.Peer != null) connected++;
totalSent += c.SentCount;
totalRecv += c.ReceivedCount;
totalAvgRtt += c.AvgRttMs;
if (c.peer != null)
{
connected++;
}
}
double avgRtt = connected > 0 ? totalAvgRtt / connected : 0;
@@ -93,14 +134,16 @@ public class DummyClientService
Log.Information("────────────────────────────────────────────");
Log.Information(
"[TOTAL] Sent={Sent} Recv={Recv} Connected={Connected}/{Total} AvgRTT={AvgRtt:F3}ms",
totalSent, totalRecv, connected, _clients.Count, avgRtt);
Log.Information("════════════════════════════════════════════");
totalSent, totalRecv, connected, clients.Count, avgRtt);
Log.Information("────────────────────────────────────────────");
}
public void Stop()
{
foreach (var c in _clients)
foreach (DummyClients c in clients)
{
c.Stop();
}
Log.Information("[SERVICE] 모든 클라이언트 종료됨.");
}