Explorar el Código

唯一id生成规则:16位appId+32位时间+16位自增

tanghai hace 9 años
padre
commit
ec0eef9a95

+ 2 - 0
Server/App/Program.cs

@@ -20,6 +20,8 @@ namespace App
 
 				StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string[]>(args).MyConfig;
 
+				IdGenerater.AppId = startConfig.AppId;
+
 				LogManager.Configuration.Variables["appType"] = startConfig.AppType;
 				LogManager.Configuration.Variables["appId"] = startConfig.AppId.ToString();
 

+ 2 - 2
Server/Model/Component/AppManagerComponent.cs

@@ -39,10 +39,10 @@ namespace Model
 
 #if __MonoCS__
 				const string exe = @"mono";
-				string arguments = $"--debug App.exe --id={startConfig.AppId} --appType={startConfig.AppType}";
+				string arguments = $"--debug App.exe --appId={startConfig.AppId} --appType={startConfig.AppType}";
 #else
 				const string exe = @"App.exe";
-				string arguments = $"--id={startConfig.AppId} --appType={startConfig.AppType}";
+				string arguments = $"--appId={startConfig.AppId} --appType={startConfig.AppType}";
 #endif
 
 				Log.Info($"{exe} {arguments}");

+ 1 - 1
Server/Model/Component/StartConfigComponent.cs

@@ -51,7 +51,7 @@ namespace Model
 				throw new Exception($"命令行格式错误!");
 			}
 
-			this.MyConfig = this.Get(options.Id);
+			this.MyConfig = this.Get(options.AppId);
 		}
 
 		public StartConfig Get(int id)

+ 1 - 1
Server/Run.sh

@@ -6,4 +6,4 @@ cmake ../..
 make
 
 ps -ef | grep App.exe | awk '{print $2}' | xargs kill -9
-mono --debug App.exe --id=1 --appType=Manager
+mono --debug App.exe --appId=1 --appType=Manager

+ 1 - 1
Server/Start.sh

@@ -2,4 +2,4 @@
 
 cd Bin/Debug/
 ps -ef | grep App.exe | awk '{print $2}' | xargs kill -9
-mono --debug App.exe --id=1 --appType=Manager
+mono --debug App.exe --appId=1 --appType=Manager

+ 6 - 2
Unity/Assets/Plugins/Base/Helper/IdGenerater.cs

@@ -2,11 +2,15 @@
 {
 	public static class IdGenerater
 	{
-		private static long value = long.MaxValue;
+		public static long AppId { private get; set; }
+
+		private static ushort value;
 
 		public static long GenerateId()
 		{
-			return --value;
+			long time = TimeHelper.ClientNowSeconds();
+
+			return (AppId << 48) + (time << 16) + ++value;
 		}
 	}
 }

+ 5 - 0
Unity/Assets/Plugins/Base/Helper/TimeHelper.cs

@@ -14,6 +14,11 @@ namespace Base
 			return Convert.ToInt64((DateTime.UtcNow - epoch).TotalMilliseconds);
 		}
 
+		public static long ClientNowSeconds()
+		{
+			return Convert.ToInt64((DateTime.UtcNow - epoch).TotalSeconds);
+		}
+
 		public static long ClientNowTicks()
 		{	
 			return Convert.ToInt64((DateTime.UtcNow - epoch).Ticks);

+ 2 - 2
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -111,7 +111,7 @@ namespace Model
 			}
 			List<Action<Session, MessageInfo>> actions = this.handlers[opcode];
 
-			actions.Add((entity, messageInfo) =>
+			actions.Add((session, messageInfo) =>
 			{
 				Message message;
 				try
@@ -124,7 +124,7 @@ namespace Model
 			        throw new Exception("解释消息失败:" + opcode, ex);
 			    }
 
-				action(entity, message);
+				action(session, message);
 			});
 		}
 		

+ 2 - 2
Unity/Assets/Scripts/Other/Options.cs

@@ -11,9 +11,9 @@ namespace Model
 	public class Options: ICloneable
 	{
 #if SERVER
-		[Option("id", Required = true, HelpText = "Id.")]
+		[Option("appId", Required = true, HelpText = "Id.")]
 #endif
-		public int Id { get; set; }
+		public int AppId { get; set; }
 
 #if SERVER
 		[Option("appType", Required = true, HelpText = "AppType: realm gate")]