From 248344f800f34fbefe0afbf91f32ccafdfec0873 Mon Sep 17 00:00:00 2001
From: Li
Date: Wed, 31 Aug 2022 20:38:08 +1200
Subject: [PATCH] Make RandomId Thread Safe
---
HorseIsleServer/LibHISP/Security/RandomID.cs | 14 ++++++++++----
HorseIsleServer/LibHISP/Server/GameClient.cs | 1 -
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/HorseIsleServer/LibHISP/Security/RandomID.cs b/HorseIsleServer/LibHISP/Security/RandomID.cs
index 20ec693..20e1da5 100755
--- a/HorseIsleServer/LibHISP/Security/RandomID.cs
+++ b/HorseIsleServer/LibHISP/Security/RandomID.cs
@@ -1,19 +1,25 @@
-namespace HISP.Security
+using System.Threading;
+
+namespace HISP.Security
{
public class RandomID
{
+ private static Mutex rndmIdMutex = new Mutex();
private static int prevId = 0;
public static int NextRandomId(int randomId=-1)
{
int rndmId = 0;
-
+ rndmIdMutex.WaitOne();
+
if (randomId == -1)
rndmId = prevId+1;
else
rndmId = randomId;
-
+
if (rndmId >= prevId)
- prevId = rndmId;
+ prevId = rndmId;
+
+ rndmIdMutex.ReleaseMutex();
return rndmId;
}
diff --git a/HorseIsleServer/LibHISP/Server/GameClient.cs b/HorseIsleServer/LibHISP/Server/GameClient.cs
index 108efc4..ac0cb95 100755
--- a/HorseIsleServer/LibHISP/Server/GameClient.cs
+++ b/HorseIsleServer/LibHISP/Server/GameClient.cs
@@ -50,7 +50,6 @@ namespace HISP.Server
private Timer minuteTimer;
private bool isDisconnecting = false;
-
private int timeoutInterval = 95 * 1000;
private int totalMinutesElapsed = 0;