feat : 맵 바운딩 박스 처리 / echo 패킷 구조 재 구성 / 더미 클라이언트 랜덤 이동 작업

This commit is contained in:
qornwh1
2026-03-04 14:51:43 +09:00
parent 241820846d
commit 053c5d23b9
16 changed files with 262 additions and 121 deletions

View File

@@ -11,10 +11,18 @@ public class DummyClientService
// 모든거 강종
public event Action? OnAllDisconnected;
public DummyClientService(int count, string ip, int port, string key, int sendIntervalMs = 1000)
public DummyClientService(int count, string ip, int port, string key, int sendIntervalMs = 1000, MapBounds? mapBounds = null)
{
sendInterval = sendIntervalMs;
clients = Enumerable.Range(1, count + 1).Select(i => new DummyClients(i, ip, port, key)).ToList();
clients = Enumerable.Range(1, count).Select(i =>
{
DummyClients client = new DummyClients(i, ip, port, key);
if (mapBounds != null)
{
client.Map = mapBounds;
}
return client;
}).ToList();
Log.Information("[SERVICE] {Count}개 클라이언트 생성 → {Ip}:{Port}", count, ip, port);
}
@@ -30,30 +38,23 @@ public class DummyClientService
{
foreach (DummyClients c in clients)
{
c.PollEvents();
try
{
c.PollEvents();
}
catch (Exception ex)
{
Log.Error("[Client {ClientId}] PollEvents 예외: {Ex}", c.clientId, ex.Message);
}
}
try
{
await Task.Delay(10, ct);
}
catch (OperationCanceledException)
{
break;
}
await Task.Delay(10);
}
}
private async Task SendLoopAsync(CancellationToken ct)
{
try
{
await Task.Delay(500, ct);
}
catch (OperationCanceledException)
{
return;
}
await Task.Delay(500);
int tick = 0;
@@ -85,14 +86,7 @@ public class DummyClientService
Log.Debug("[TICK {Tick:000}] {Sent}/{Total} 전송", tick, sent, total);
tick++;
try
{
await Task.Delay(sendInterval, ct);
}
catch (OperationCanceledException)
{
break;
}
await Task.Delay(sendInterval);
}
}

View File

@@ -28,6 +28,9 @@ public class DummyClients
private float dirX = 0f;
private float dirZ = 0f;
// 맵 경계
public MapBounds Map { get; set; } = new MapBounds(-50f, 50f, -50f, 50f);
// 통계
public int SentCount
{
@@ -71,15 +74,6 @@ public class DummyClients
listener.NetworkReceiveEvent += (peer, reader, channel, deliveryMethod) =>
{
short code = reader.GetShort();
short bodyLength = reader.GetShort();
string? msg = reader.GetString();
if (msg != null)
{
RttCount++;
}
ReceivedCount++;
reader.Recycle();
};
@@ -104,8 +98,12 @@ public class DummyClients
// 남은 거리가 없으면 새 방향·목표 거리 설정
if (distance <= 0f)
{
// 현재 각도에서 -30~+30도 범위로 회전
rotY = (rotY + Random.Shared.Next(-30, 31) + 360) % 360;
// 벽에 붙어있으면 반대 방향 강제, 아니면 ±30도 회전
int wallRotY = Map.GetRotYAwayFromWall(posX, posZ);
rotY = wallRotY >= 0
? (wallRotY + Random.Shared.Next(-30, 31) + 360) % 360
: (rotY + Random.Shared.Next(-30, 31) + 360) % 360;
float rad = rotY * MathF.PI / 180f;
dirX = MathF.Sin(rad);
dirZ = MathF.Cos(rad);
@@ -117,9 +115,14 @@ public class DummyClients
// 이번 틱 이동량 (남은 거리 초과 방지)
float step = MathF.Min(moveSpeed * delta, distance);
posX += dirX * step;
posZ += dirZ * step;
distance -= step;
float nextX = posX + dirX * step;
float nextZ = posZ + dirZ * step;
// 벽 충돌 시 clamp + 다음 틱에 방향 재설정
bool hitWall = Map.Clamp(ref nextX, ref nextZ);
posX = nextX;
posZ = nextZ;
distance = hitWall ? 0f : distance - step;
// 정수 Vector3 갱신
position.X = (int)MathF.Round(posX);

View File

@@ -0,0 +1,116 @@
namespace ClientTester.DummyService;
public class MapBounds
{
public float MinX
{
get;
set;
}
public float MaxX
{
get;
set;
}
public float MinZ
{
get;
set;
}
public float MaxZ
{
get;
set;
}
public MapBounds(float minX, float maxX, float minZ, float maxZ)
{
MinX = minX;
MaxX = maxX;
MinZ = minZ;
MaxZ = maxZ;
}
// 위치가 경계 안에 있는지 체크
public bool IsInside(float x, float z)
{
return x >= MinX && x <= MaxX && z >= MinZ && z <= MaxZ;
}
// 경계 초과 시 clamp, 벽에 부딪혔으면 true 반환
public bool Clamp(ref float x, ref float z)
{
bool hit = false;
if (x < MinX)
{
x = MinX;
hit = true;
}
else if (x > MaxX)
{
x = MaxX;
hit = true;
}
if (z < MinZ)
{
z = MinZ;
hit = true;
}
else if (z > MaxZ)
{
z = MaxZ;
hit = true;
}
return hit;
}
// 현재 위치 기준으로 벽 반대 방향 rotY 계산 (벽 없으면 -1)
public int GetRotYAwayFromWall(float x, float z, float margin = 0.5f)
{
bool atLeft = x <= MinX + margin;
bool atRight = x >= MaxX - margin;
bool atBottom = z <= MinZ + margin;
bool atTop = z >= MaxZ - margin;
if (!atLeft && !atRight && !atBottom && !atTop)
{
return -1; // 벽 근처 아님
}
// 벽 반대 방향 벡터 합산
float awayX = 0f, awayZ = 0f;
if (atLeft)
{
awayX += 1f;
}
if (atRight)
{
awayX -= 1f;
}
if (atBottom)
{
awayZ += 1f;
}
if (atTop)
{
awayZ -= 1f;
}
// 정규화
float len = MathF.Sqrt(awayX * awayX + awayZ * awayZ);
awayX /= len;
awayZ /= len;
// 방향 벡터 → rotY (degree)
return ((int)(MathF.Atan2(awayX, awayZ) * 180f / MathF.PI) + 360) % 360;
}
}

View File

@@ -59,27 +59,13 @@ public class EchoDummyClientService
c.PollEvents();
}
try
{
await Task.Delay(10, ct);
}
catch (OperationCanceledException)
{
break;
}
await Task.Delay(10);
}
}
private async Task SendLoopAsync(CancellationToken ct)
{
try
{
await Task.Delay(500, ct);
}
catch (OperationCanceledException)
{
return;
}
await Task.Delay(500);
int tick = 0;
@@ -111,14 +97,7 @@ public class EchoDummyClientService
Log.Debug("[TICK {Tick:000}] {Sent}/{Total} 전송", tick, sent, total);
tick++;
try
{
await Task.Delay(sendInterval, ct);
}
catch (OperationCanceledException)
{
break;
}
await Task.Delay(sendInterval);
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
using ClientTester.Packet;
using LiteNetLib;
using LiteNetLib.Utils;
@@ -131,12 +132,12 @@ public class EchoDummyClients
}
}
PacketHeader packetHeader = new PacketHeader();
packetHeader.Code = 0;
packetHeader.BodyLength = (ushort)$"Echo seq:{seq}".Length;
writer.Put((short)packetHeader.Code);
writer.Put((short)packetHeader.BodyLength);
writer.Put($"Echo seq:{seq}");
// string → raw bytes (길이 prefix 방지)
EchoPacket echoPacket = new EchoPacket();
echoPacket.Str = $"Echo seq:{seq}";
byte[] data = PacketSerializer.Serialize<EchoPacket>((ushort)PacketCode.ECHO, echoPacket);
writer.Put(data);
// 순서보장 안함 HOL Blocking 제거
peer.Send(writer, DeliveryMethod.ReliableUnordered);
SentCount++;

View File

@@ -4,6 +4,22 @@ using ProtoBuf;
namespace ClientTester.Packet;
// ============================================================
// 에코용
// ============================================================
// ECHO
[ProtoContract]
public class EchoPacket
{
[ProtoMember(1)]
public string Str
{
get;
set;
}
}
// ============================================================
// 공통 타입
// ============================================================

View File

@@ -2,6 +2,8 @@ namespace ClientTester.Packet;
public enum PacketCode : ushort
{
ECHO = 0,
// 초기 클라이언트 시작시 jwt토큰 받아옴
ACC_TOKEN = 1,

View File

@@ -11,7 +11,7 @@ namespace ClientTester.Packet
public static byte[] Serialize<T>(ushort type, T packet)
{
using MemoryStream payloadMs = new MemoryStream();
Serializer.Serialize(payloadMs, packet);
Serializer.Serialize<T>(payloadMs, packet);
byte[] payload = payloadMs.ToArray();
ushort size = (ushort)payload.Length;

View File

@@ -7,7 +7,7 @@ class EcoClientTester
public static readonly string SERVER_IP = "localhost";
public static readonly int SERVER_PORT = 9500;
public static readonly string CONNECTION_KEY = "test";
public static readonly int CLIENT_COUNT = 100;
public static readonly int CLIENT_COUNT = 10;
private async Task StartEchoDummyTest()
{