36 lines
809 B
C#
36 lines
809 B
C#
namespace MMOserver.Utils;
|
|
|
|
public class UuidGenerator
|
|
{
|
|
// 0 ~ 1000 은 더미 클라이언트 예약 범위
|
|
private const int DUMMY_RANGE_MAX = 1000;
|
|
|
|
private readonly object idLock = new();
|
|
private readonly HashSet<int> usedIds = new();
|
|
|
|
// 고유 랜덤 int ID 발급 (1001번 이상, 충돌 시 재생성)
|
|
public int Create()
|
|
{
|
|
lock (idLock)
|
|
{
|
|
int id;
|
|
do
|
|
{
|
|
id = Random.Shared.Next(DUMMY_RANGE_MAX + 1, int.MaxValue);
|
|
} while (usedIds.Contains(id));
|
|
|
|
usedIds.Add(id);
|
|
return id;
|
|
}
|
|
}
|
|
|
|
// 로그아웃 / 세션 만료 시 ID 반납
|
|
public bool Release(int id)
|
|
{
|
|
lock (idLock)
|
|
{
|
|
return usedIds.Remove(id);
|
|
}
|
|
}
|
|
}
|