This commit is contained in:
qornwh1
2026-03-03 15:01:53 +09:00
11 changed files with 763 additions and 91 deletions

View File

@@ -18,6 +18,7 @@ public class DummyClients
// seq → 송신 타임스탬프 (Stopwatch tick)
private ConcurrentDictionary<int, long> pendingPings = new();
private int seqNumber;
private const int MaxPendingPings = 1000;
// 유닛 테스트용 (0 = 제한 없음)
public int TestCount
@@ -117,9 +118,20 @@ public class DummyClients
int seq = seqNumber++;
pendingPings[seq] = Stopwatch.GetTimestamp();
// 응답 없는 오래된 ping 정리 (패킷 유실 시 메모리 누수 방지)
if (pendingPings.Count > MaxPendingPings)
{
int cutoff = seq - MaxPendingPings;
foreach (int key in pendingPings.Keys)
{
if (key < cutoff)
pendingPings.TryRemove(key, out _);
}
}
PacketHeader packetHeader = new PacketHeader();
packetHeader.Code = 0;
packetHeader.BodyLength = $"seq:{seq}".Length;
packetHeader.BodyLength = $"Echo seq:{seq}".Length;
writer.Put((short)packetHeader.Code);
writer.Put((short)packetHeader.BodyLength);
writer.Put($"Echo seq:{seq}");

View File

@@ -7,22 +7,21 @@ namespace ClientTester.Packet
public static class PacketSerializer
{
// 직렬화: 객체 → byte[]
public static byte[] Serialize<T>(ushort type, ushort size, T packet)
// 직렬화: 객체 → byte[] (size는 payload 크기로 자동 계산)
public static byte[] Serialize<T>(ushort type, T packet)
{
MemoryStream ms = new MemoryStream();
using MemoryStream payloadMs = new MemoryStream();
Serializer.Serialize(payloadMs, packet);
byte[] payload = payloadMs.ToArray();
ushort size = (ushort)payload.Length;
// 2바이트 패킷 타입 헤더
ms.WriteByte((byte)(type & 0xFF));
ms.WriteByte((byte)(type >> 8));
// 2바이트 패킷 길이 헤더
ms.WriteByte((byte)(size & 0xFF));
ms.WriteByte((byte)(size >> 8));
// protobuf 페이로드
Serializer.Serialize(ms, packet);
return ms.ToArray();
byte[] result = new byte[4 + payload.Length];
result[0] = (byte)(type & 0xFF);
result[1] = (byte)(type >> 8);
result[2] = (byte)(size & 0xFF);
result[3] = (byte)(size >> 8);
Buffer.BlockCopy(payload, 0, result, 4, payload.Length);
return result;
}
// 역직렬화: byte[] → (PacketType, payload bytes)
@@ -34,19 +33,25 @@ namespace ClientTester.Packet
return (0, 0, null)!;
}
// 길이체크도 필요함
ushort type = (ushort)(data[0] | (data[1] << 8));
ushort size = (ushort)(data[2] | (data[3] << 8));
byte[] payload = new byte[data.Length - 4];
Buffer.BlockCopy(data, 4, payload, 0, payload.Length);
int actualPayloadLen = data.Length - 4;
if (size > actualPayloadLen)
{
Log.Warning("[PacketSerializer] 페이로드 크기 불일치 HeaderSize={Size} ActualSize={Actual}", size, actualPayloadLen);
return (0, 0, null)!;
}
byte[] payload = new byte[size];
Buffer.BlockCopy(data, 4, payload, 0, size);
return (type, size, payload);
}
// 페이로드 → 특정 타입으로 역직렬화
public static T DeserializePayload<T>(byte[] payload)
{
MemoryStream ms = new MemoryStream(payload);
using MemoryStream ms = new MemoryStream(payload);
return Serializer.Deserialize<T>(ms);
}
}

View File

@@ -3,7 +3,7 @@ using Serilog;
class EcoClientTester
{
public static readonly string SERVER_IP = "localhost";
public static readonly string SERVER_IP = "tolelom.xyz";
public static readonly int SERVER_PORT = 9500;
public static readonly string CONNECTION_KEY = "test";
public static readonly int CLIENT_COUNT = 100;
@@ -29,10 +29,10 @@ class EcoClientTester
};
DummyClientService service = new DummyClientService(CLIENT_COUNT, SERVER_IP, SERVER_PORT, CONNECTION_KEY, 100);
service.OnAllDisconnected += async () =>
service.OnAllDisconnected += () =>
{
Log.Warning("[SHUTDOWN] 종료 이벤트 발생, 종료 중...");
await cts.CancelAsync();
cts.Cancel();
};
// service.IsTest = true;