Эх сурвалжийг харах

Client连接Realm(验证服务器), Realm随机分配一个Gate,并且向Gate请求一个Key,发回给Client, client拿着key连接Gate
调试通过!

tanghai 9 жил өмнө
parent
commit
fd0b276ff1
27 өөрчлөгдсөн 295 нэмэгдсэн , 81 устгасан
  1. 2 0
      Server/App/Program.cs
  2. 0 17
      Server/Controller/C2S_LoginHandler.cs
  3. 0 19
      Server/Controller/C2S_SubscribeLogHandler.cs
  4. 22 0
      Server/Controller/Message/C2G_LoginGateHandler.cs
  5. 31 0
      Server/Controller/Message/C2R_LoginHandler.cs
  6. 19 0
      Server/Controller/Message/C2R_SubscribeLogHandler.cs
  7. 17 0
      Server/Controller/Message/R2G_GetLoginKeyHandler.cs
  8. 4 2
      Server/Controller/Server.Controller.csproj
  9. 55 0
      Server/Model/Component/GateSessionKeyComponent.cs
  10. 3 1
      Server/Model/Component/LogToClientComponent.cs
  11. 41 0
      Server/Model/Component/RealmGateAddressComponent.cs
  12. 5 0
      Server/Model/Server.Model.csproj
  13. 1 0
      Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs
  14. 8 0
      Unity/Assets/Plugins/Base/Helper/RandomHelper.cs
  15. 1 1
      Unity/Assets/Plugins/Base/Message/AMessage.cs
  16. 2 2
      Unity/Assets/Plugins/Base/Message/RpcException.cs
  17. 5 5
      Unity/Assets/Scripts/Component/MessageComponent.cs
  18. 2 0
      Unity/Assets/Scripts/Message/ErrorCode.cs
  19. 47 10
      Unity/Assets/Scripts/Message/Message.cs
  20. 1 1
      Unity/Assets/Scripts/Other/CommandLine.cs
  21. 2 2
      Unity/Assets/Scripts/Other/CommandLine.cs.meta
  22. 7 3
      Unity/Controller/Event/InitSceneStartEvent_InitGame.cs
  23. 14 0
      Unity/Controller/Message/R2C_ServerLogHandler.cs
  24. 0 14
      Unity/Controller/Message/S2C_ServerLogHandler.cs
  25. 1 1
      Unity/Controller/Unity.Controller.csproj
  26. 4 3
      Unity/Unity.CSharp.Editor.csproj
  27. 1 0
      Unity/Unity.CSharp.csproj

+ 2 - 0
Server/App/Program.cs

@@ -33,8 +33,10 @@ namespace App
 				switch (options.AppType)
 				{
 					case "Realm":
+						Game.Scene.AddComponent<RealmGateAddressComponent>();
 						break;
 					case "Gate":
+						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
 					default:
 						throw new Exception($"命令行参数没有设置正确的AppType: {options.AppType}");

+ 0 - 17
Server/Controller/C2S_LoginHandler.cs

@@ -1,17 +0,0 @@
-using System;
-using Base;
-using Model;
-
-namespace Controller
-{
-	[MessageHandler(AppType.Realm)]
-	public class C2S_LoginHandler: AMRpcEvent<C2S_Login, S2C_Login>
-	{
-		protected override void Run(Entity scene, C2S_Login message, Action<S2C_Login> reply)
-		{
-			Log.Info(MongoHelper.ToJson(message));
-
-			reply(new S2C_Login());
-		}
-	}
-}

+ 0 - 19
Server/Controller/C2S_SubscribeLogHandler.cs

@@ -1,19 +0,0 @@
-using System;
-using Base;
-using Model;
-
-namespace Controller
-{
-	[MessageHandler(AppType.Realm)]
-	public class C2S_SubscribeLogHandler : AMRpcEvent<C2S_SubscribeLog, S2C_SubscribeLog>
-	{
-		protected override void Run(Entity entity, C2S_SubscribeLog message, Action<S2C_SubscribeLog> reply)
-		{
-			Log.Info(MongoHelper.ToJson(message));
-
-			entity.AddComponent<LogToClientComponent>();
-
-			reply(new S2C_SubscribeLog());
-		}
-	}
-}

+ 22 - 0
Server/Controller/Message/C2G_LoginGateHandler.cs

@@ -0,0 +1,22 @@
+using System;
+using Base;
+using Model;
+
+namespace Controller
+{
+	[MessageHandler(AppType.Gate)]
+	public class C2G_LoginGateHandler : AMRpcEvent<C2G_LoginGate, G2C_LoginGate>
+	{
+		protected override void Run(Entity scene, C2G_LoginGate message, Action<G2C_LoginGate> reply)
+		{
+			bool isCheckOK = Game.Scene.GetComponent<GateSessionKeyComponent>().Check(message.Key);
+			G2C_LoginGate g2CLoginGate = new G2C_LoginGate();
+			if (!isCheckOK)
+			{
+				g2CLoginGate.Error = ErrorCode.ERR_ConnectGateKeyError;
+				g2CLoginGate.Message = "Gate key验证失败!";
+			}
+			reply(g2CLoginGate);
+		}
+	}
+}

+ 31 - 0
Server/Controller/Message/C2R_LoginHandler.cs

@@ -0,0 +1,31 @@
+using System;
+using Base;
+using Model;
+
+namespace Controller
+{
+	[MessageHandler(AppType.Realm)]
+	public class C2R_LoginHandler: AMRpcEvent<C2R_Login, R2C_Login>
+	{
+		protected override async void Run(Entity session, C2R_Login message, Action<R2C_Login> reply)
+		{
+			R2C_Login r2CLogin;
+			if (message.Account != "abcdef" || message.Password != "111111")
+			{
+				r2CLogin = new R2C_Login {Error = ErrorCode.ERR_AccountOrPasswordError, Message = "账号名或者密码错误!"};
+				reply(r2CLogin);
+				return;
+			}
+
+			// 随机分配一个Gate
+			string gateAddress = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
+			Entity gateSession = Game.Scene.GetComponent<NetworkComponent>().Get(gateAddress);
+			
+			// 向gate请求一个key,客户端可以拿着这个key连接gate
+			G2R_GetLoginKey g2RGetLoginKey = await gateSession.GetComponent<MessageComponent>().Call<R2G_GetLoginKey, G2R_GetLoginKey>(new R2G_GetLoginKey());
+			
+			r2CLogin = new R2C_Login {Address = gateAddress, Key = g2RGetLoginKey.Key};
+			reply(r2CLogin);
+		}
+	}
+}

+ 19 - 0
Server/Controller/Message/C2R_SubscribeLogHandler.cs

@@ -0,0 +1,19 @@
+using System;
+using Base;
+using Model;
+
+namespace Controller
+{
+	[MessageHandler(AppType.Realm)]
+	public class C2R_SubscribeLogHandler : AMRpcEvent<C2R_SubscribeLog, R2C_SubscribeLog>
+	{
+		protected override void Run(Entity entity, C2R_SubscribeLog message, Action<R2C_SubscribeLog> reply)
+		{
+			Log.Info(MongoHelper.ToJson(message));
+
+			//entity.AddComponent<LogToClientComponent>();
+
+			reply(new R2C_SubscribeLog());
+		}
+	}
+}

+ 17 - 0
Server/Controller/Message/R2G_GetLoginKeyHandler.cs

@@ -0,0 +1,17 @@
+using System;
+using Base;
+using Model;
+
+namespace Controller
+{
+	[MessageHandler(AppType.Gate)]
+	public class R2G_GetLoginKeyHandler : AMRpcEvent<R2G_GetLoginKey, G2R_GetLoginKey>
+	{
+		protected override void Run(Entity scene, R2G_GetLoginKey message, Action<G2R_GetLoginKey> reply)
+		{
+			long key = Game.Scene.GetComponent<GateSessionKeyComponent>().Get();
+			G2R_GetLoginKey g2RGetLoginKey = new G2R_GetLoginKey(key);
+			reply(g2RGetLoginKey);
+		}
+	}
+}

+ 4 - 2
Server/Controller/Server.Controller.csproj

@@ -34,9 +34,11 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="C2S_SubscribeLogHandler.cs" />
+    <Compile Include="Message\C2G_LoginGateHandler.cs" />
+    <Compile Include="Message\R2G_GetLoginKeyHandler.cs" />
+    <Compile Include="Message\C2R_SubscribeLogHandler.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="C2S_LoginHandler.cs" />
+    <Compile Include="Message\C2R_LoginHandler.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Base\Server.Base.csproj">

+ 55 - 0
Server/Model/Component/GateSessionKeyComponent.cs

@@ -0,0 +1,55 @@
+using System.Collections.Generic;
+using Base;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class GateSessionKeyComponentEvent : ObjectEvent<GateSessionKeyComponent>, IAwake
+	{
+		public void Awake()
+		{
+			this.GetValue().Awake();
+		}
+	}
+
+	public class GateSessionKeyComponent : Component
+	{
+		private TimerComponent timerComponent;
+
+		private readonly HashSet<long> sessionKey = new HashSet<long>();
+
+		public void Awake()
+		{
+			this.timerComponent = Game.Scene.GetComponent<TimerComponent>();
+		}
+
+		public long Get()
+		{
+			long key = RandomHelper.RandInt64();
+			this.sessionKey.Add(key);
+			this.TimeoutRemoveKey(key);
+			return key;
+		}
+
+		public bool Check(long key)
+		{
+			bool ret = this.sessionKey.Contains(key);
+			if (ret)
+			{
+				this.sessionKey.Remove(key);
+			}
+			return ret;
+		}
+
+		public void Remove(long key)
+		{
+			this.sessionKey.Remove(key);
+		}
+
+		private async void TimeoutRemoveKey(long key)
+		{
+			await this.timerComponent.WaitAsync(20000);
+			this.sessionKey.Remove(key);
+		}
+	}
+}

+ 3 - 1
Server/Model/Component/LogToClientComponent.cs

@@ -14,10 +14,12 @@ namespace Model
 	public class LogToClientComponent : Component
 	{
 		private string appType;
+		private int appId;
 
 		public void Awake()
 		{
 			this.appType = Game.Scene.GetComponent<OptionsComponent>().Options.AppType;
+			this.appId = Game.Scene.GetComponent<OptionsComponent>().Options.Id;
 			Log.Callback.Add(this.Id, this.LogToClient);
 		}
 
@@ -27,7 +29,7 @@ namespace Model
 			{
 				return;
 			}
-			this.GetComponent<MessageComponent>().Send(new S2C_ServerLog { AppType = this.appType, Type = type, Log = message });
+			this.GetComponent<MessageComponent>().Send(new R2C_ServerLog { AppType = this.appType, AppId = this.appId, Type = type, Log = message });
 		}
 
 		public override void Dispose()

+ 41 - 0
Server/Model/Component/RealmGateAddressComponent.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Base;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class RealmGateAddressComponentEvent : ObjectEvent<RealmGateAddressComponent>, IAwake
+	{
+		public void Awake()
+		{
+			this.GetValue().Awake();
+		}
+	}
+
+	public class RealmGateAddressComponent : Component
+	{
+		private readonly List<string> GateAddress = new List<string>();
+
+		public void Awake()
+		{
+			string s = File.ReadAllText("./CommandLineConfig.txt");
+			CommandLines commandLines = MongoHelper.FromJson<CommandLines>(s);
+			foreach (CommandLine commandLine in commandLines.Commands)
+			{
+				if (commandLine.Options.AppType != "Gate")
+				{
+					continue;
+				}
+				this.GateAddress.Add($"{commandLine.Options.Host}:{commandLine.Options.Port}");
+			}
+		}
+
+		public string GetAddress()
+		{
+			int n = RandomHelper.RandomNumber(0, this.GateAddress.Count);
+			return this.GateAddress[n];
+		}
+	}
+}

+ 5 - 0
Server/Model/Server.Model.csproj

@@ -74,10 +74,15 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Message\OpcodeHelper.cs">
       <Link>Message\OpcodeHelper.cs</Link>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Other\CommandLine.cs">
+      <Link>Other\CommandLine.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Other\Options.cs">
       <Link>Other\Options.cs</Link>
     </Compile>
     <Compile Include="Component\LogToClientComponent.cs" />
+    <Compile Include="Component\GateSessionKeyComponent.cs" />
+    <Compile Include="Component\RealmGateAddressComponent.cs" />
     <Compile Include="Component\OptionsComponent.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 1 - 0
Unity/Assets/Editor/ServerCommandLineEditor/UI/ServerCommandLineEditor.cs

@@ -2,6 +2,7 @@
 using System.Diagnostics;
 using System.IO;
 using Base;
+using Model;
 using UnityEditor;
 using UnityEngine;
 

+ 8 - 0
Unity/Assets/Plugins/Base/Helper/RandomHelper.cs

@@ -12,6 +12,14 @@ namespace Base
 			return BitConverter.ToUInt64(bytes, 0);
 		}
 
+		public static Int64 RandInt64()
+		{
+			var bytes = new byte[8];
+			Random random = new Random();
+			random.NextBytes(bytes);
+			return BitConverter.ToInt64(bytes, 0);
+		}
+
 		/// <summary>
 		/// 获取lower与Upper之间的随机数
 		/// </summary>

+ 1 - 1
Unity/Assets/Plugins/Base/Message/AMessage.cs

@@ -13,7 +13,7 @@
 	/// </summary>
 	public abstract class AResponse: AMessage
 	{
-		public int ErrorCode = 0;
+		public int Error = 0;
 		public string Message = "";
 	}
 }

+ 2 - 2
Unity/Assets/Plugins/Base/Message/RpcException.cs

@@ -10,12 +10,12 @@ namespace Base
 	{
 		public int Error { get; private set; }
 
-		public RpcException(int error, string message) : base($"{error} : {message}")
+		public RpcException(int error, string message) : base($"Error: {error} Message: {message}")
 		{
 			this.Error = error;
 		}
 
-		public RpcException(int error, string message, Exception e) : base($"{error} : {message}", e)
+		public RpcException(int error, string message, Exception e) : base($"Error: {error} Message: {message}", e)
 		{
 			this.Error = error;
 		}

+ 5 - 5
Unity/Assets/Scripts/Component/MessageComponent.cs

@@ -140,9 +140,9 @@ namespace Model
 				try
 				{
 					Response response = MongoHelper.FromBson<Response>(bytes, offset, count);
-					if (response.ErrorCode != 0)
+					if (response.Error != 0)
 					{
-						tcs.SetException(new RpcException(response.ErrorCode, response.Message));
+						tcs.SetException(new RpcException(response.Error, response.Message));
 						return;
 					}
 					tcs.SetResult(response);
@@ -172,9 +172,9 @@ namespace Model
 				try
 				{
 					Response response = MongoHelper.FromBson<Response>(bytes, offset, count);
-					if (response.ErrorCode != 0)
+					if (response.Error != 0)
 					{
-						tcs.SetException(new RpcException(response.ErrorCode,  response.Message));
+						tcs.SetException(new RpcException(response.Error,  response.Message));
 						return;
 					}
 					tcs.SetResult(response);
@@ -219,7 +219,7 @@ namespace Model
 			}
 
 			byte[] seqBytes = BitConverter.GetBytes(rpcId);
-
+			
 			channel.Send(new List<byte[]> { opcodeBytes, seqBytes, messageBytes });
 		}
 

+ 2 - 0
Unity/Assets/Scripts/Message/ErrorCode.cs

@@ -3,5 +3,7 @@ namespace Model
 	public static class ErrorCode
 	{
 		public const int ERR_Success = 0;
+		public const int ERR_AccountOrPasswordError = 1;
+		public const int ERR_ConnectGateKeyError = 2;
 	}
 }

+ 47 - 10
Unity/Assets/Scripts/Message/Message.cs

@@ -5,7 +5,7 @@ namespace Model
 {
 	[Message(1)]
 	[BsonIgnoreExtraElements]
-	public class C2S_Login: ARequest
+	public class C2R_Login: ARequest
 	{
 		public string Account;
 		public string Password;
@@ -13,30 +13,67 @@ namespace Model
 
 	[Message(2)]
 	[BsonIgnoreExtraElements]
-	public class S2C_Login: AResponse
+	public class R2C_Login: AResponse
 	{
-		public string Host;
-		public int Port;
+		public string Address { get; set; }
+		public long Key { get; set; }
 	}
 
 	[Message(3)]
 	[BsonIgnoreExtraElements]
-	public class S2C_ServerLog: AMessage
+	public class R2C_ServerLog: AMessage
 	{
-		public string AppType;
-		public LogType Type;
-		public string Log;
+		public string AppType { get; set; }
+		public int AppId { get; set; }
+		public LogType Type { get; set; }
+		public string Log { get; set; }
 	}
 
 	[Message(4)]
 	[BsonIgnoreExtraElements]
-	public class C2S_SubscribeLog: ARequest
+	public class C2R_SubscribeLog: ARequest
 	{
 	}
 
 	[Message(5)]
 	[BsonIgnoreExtraElements]
-	public class S2C_SubscribeLog: AResponse
+	public class R2C_SubscribeLog: AResponse
+	{
+	}
+
+	[Message(6)]
+	[BsonIgnoreExtraElements]
+	public class R2G_GetLoginKey : ARequest
+	{
+	}
+
+	[Message(7)]
+	[BsonIgnoreExtraElements]
+	public class G2R_GetLoginKey : AResponse
+	{
+		public long Key;
+
+		public G2R_GetLoginKey(long key)
+		{
+			this.Key = key;
+		}
+	}
+
+	[Message(8)]
+	[BsonIgnoreExtraElements]
+	public class C2G_LoginGate : ARequest
+	{
+		public long Key;
+
+		public C2G_LoginGate(long key)
+		{
+			this.Key = key;
+		}
+	}
+
+	[Message(9)]
+	[BsonIgnoreExtraElements]
+	public class G2C_LoginGate : AResponse
 	{
 	}
 }

+ 1 - 1
Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs → Unity/Assets/Scripts/Other/CommandLine.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using Base;
 using Model;
 
-namespace MyEditor
+namespace Model
 {
 	public class CommandLine: ICloneable
 	{

+ 2 - 2
Unity/Assets/Editor/ServerCommandLineEditor/Component/CommandLine.cs.meta → Unity/Assets/Scripts/Other/CommandLine.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 4f4981289b5c3cc4ca1604a6ba39f3a8
-timeCreated: 1476674012
+guid: c181dc38f15288746960812611a7767e
+timeCreated: 1476862821
 licenseType: Pro
 MonoImporter:
   serializedVersion: 2

+ 7 - 3
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs

@@ -19,9 +19,13 @@ namespace Controller
 			try
 			{
 				// 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端
-				await session.GetComponent<MessageComponent>().Call<C2S_SubscribeLog, S2C_SubscribeLog>(new C2S_SubscribeLog());
-				S2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2S_Login, S2C_Login>(new C2S_Login {Account = "tanghai", Password = "1111111"});
-				Log.Info(MongoHelper.ToJson(s2CLogin));
+				await session.GetComponent<MessageComponent>().Call<C2R_SubscribeLog, R2C_SubscribeLog>(new C2R_SubscribeLog());
+				R2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2R_Login, R2C_Login>(new C2R_Login {Account = "111", Password = "111111"});
+
+				// 连接Gate
+				Entity gateSession = networkComponent.Get(s2CLogin.Address);
+				await gateSession.GetComponent<MessageComponent>().Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
+				Log.Info("连接Gate验证成功!");
 			}
 			catch (RpcException e)
 			{

+ 14 - 0
Unity/Controller/Message/R2C_ServerLogHandler.cs

@@ -0,0 +1,14 @@
+using Base;
+using Model;
+
+namespace Controller
+{
+	[MessageHandler(AppType.Client)]
+	public class R2C_ServerLogHandler: AMEvent<R2C_ServerLog>
+	{
+		protected override void Run(Entity scene, R2C_ServerLog message)
+		{
+			Log.Debug($"[{message.AppType}][{message.AppId}] [{message.Type}] {message.Log}");
+		}
+	}
+}

+ 0 - 14
Unity/Controller/Message/S2C_ServerLogHandler.cs

@@ -1,14 +0,0 @@
-using Base;
-using Model;
-
-namespace Controller
-{
-	[MessageHandler(AppType.Client)]
-	public class S2C_ServerLogHandler: AMEvent<S2C_ServerLog>
-	{
-		protected override void Run(Entity scene, S2C_ServerLog message)
-		{
-			Log.Debug($"[{message.AppType}] [{message.Type}] {message.Log}");
-		}
-	}
-}

+ 1 - 1
Unity/Controller/Unity.Controller.csproj

@@ -54,7 +54,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Event\InitSceneStartEvent_InitGame.cs" />
-    <Compile Include="Message\S2C_ServerLogHandler.cs" />
+    <Compile Include="Message\R2C_ServerLogHandler.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>

+ 4 - 3
Unity/Unity.CSharp.Editor.csproj

@@ -13,11 +13,13 @@
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
     <TargetFrameworkProfile>Unity Full v3.5</TargetFrameworkProfile>
-    <CompilerResponseFile></CompilerResponseFile>
+    <CompilerResponseFile>
+    </CompilerResponseFile>
     <UnityProjectType>Editor:5</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
     <UnityVersion>5.4.2f1</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -112,7 +114,6 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" />
-    <Compile Include="Assets\Editor\ServerCommandLineEditor\Component\CommandLine.cs" />
     <Compile Include="Assets\Editor\ServerCommandLineEditor\UI\ServerCommandLineEditor.cs" />
   </ItemGroup>
   <ItemGroup>

+ 1 - 0
Unity/Unity.CSharp.csproj

@@ -102,6 +102,7 @@
     <Compile Include="Assets\Scripts\Message\ErrorCode.cs" />
     <Compile Include="Assets\Scripts\Message\Message.cs" />
     <Compile Include="Assets\Scripts\Message\OpcodeHelper.cs" />
+    <Compile Include="Assets\Scripts\Other\CommandLine.cs" />
     <Compile Include="Assets\Scripts\Other\EntityType.cs" />
     <Compile Include="Assets\Scripts\Other\GameException.cs" />
     <Compile Include="Assets\Scripts\Other\Options.cs" />