Просмотр исходного кода

Session提供回调形式,热更层不使用async await

tanghai 8 лет назад
Родитель
Сommit
f80b41fe25

+ 3 - 81
Unity/Assets/Scripts/Entity/Session.cs

@@ -135,104 +135,26 @@ namespace Model
 			this.network.MessageDispatcher.Dispatch(this, op, offset, messageBytes, (AMessage)message);
 		}
 		
-		/// <summary>
-		/// Rpc调用,发送一个消息,等待返回一个消息
-		/// </summary>
-		public Task<AResponse> Call(ARequest request)
-		{
-			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
-			var tcs = new TaskCompletionSource<AResponse>();
-			this.requestCallback[RpcId] = (message) =>
-			{
-				try
-				{
-					AResponse response = (AResponse)message;
-					if (response.Error > 100)
-					{
-						tcs.SetException(new RpcException(response.Error, response.Message));
-						return;
-					}
-					//Log.Debug($"recv: {MongoHelper.ToJson(response)}");
-					tcs.SetResult(response);
-				}
-				catch (Exception e)
-				{
-					tcs.SetException(new Exception($"Rpc Error: {message.GetType().FullName}", e));
-				}
-			};
-
-			return tcs.Task;
-		}
-		
 		/// <summary>
 		/// Rpc调用
 		/// </summary>
-		public Task<AResponse> Call(ARequest request, CancellationToken cancellationToken)
+		public void CallWithAction(ARequest request, Action<AResponse> action)
 		{
 			request.RpcId = ++RpcId;
 			this.SendMessage(request);
 
-			var tcs = new TaskCompletionSource<AResponse>();
-
 			this.requestCallback[RpcId] = (message) =>
 			{
 				try
 				{
 					AResponse response = (AResponse)message;
-					if (response.Error > 100)
-					{
-						tcs.SetException(new RpcException(response.Error, response.Message));
-						return;
-					}
-					//Log.Debug($"recv: {MongoHelper.ToJson(response)}");
-					tcs.SetResult(response);
+					action(response);
 				}
 				catch (Exception e)
 				{
-					tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName}", e));
-				}
-			};
-
-			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
-
-			return tcs.Task;
-		}
-
-		/// <summary>
-		/// Rpc调用
-		/// </summary>
-		public Task<Response> Call<Response>(ARequest request, CancellationToken cancellationToken)
-			where Response : AResponse
-		{
-			request.RpcId = ++RpcId;
-			this.SendMessage(request);
-
-			var tcs = new TaskCompletionSource<Response>();
-
-			this.requestCallback[RpcId] = (message) =>
-			{
-				try
-				{
-					Response response = (Response)message;
-					if (response.Error > 100)
-					{
-						tcs.SetException(new RpcException(response.Error, response.Message));
-						return;
-					}
-					//Log.Debug($"recv: {MongoHelper.ToJson(response)}");
-					tcs.SetResult(response);
-				}
-				catch (Exception e)
-				{
-					tcs.SetException(new Exception($"Rpc Error: {typeof(Response).FullName}", e));
+					Log.Error(e.ToString());
 				}
 			};
-
-			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
-
-			return tcs.Task;
 		}
 
 		/// <summary>

+ 7 - 0
Unity/Assets/Scripts/Helper/ILHelper.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Reflection;
 using ILRuntime.Runtime.Enviorment;
+using UnityEngine;
 
 namespace Model
 {
@@ -21,6 +22,7 @@ namespace Model
 			// 注册委托
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
 			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
+			Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AResponse>();
 
 
 			// 注册适配器
@@ -41,5 +43,10 @@ namespace Model
 				Init.Instance.AppDomain.RegisterCrossBindingAdaptor(adaptor);
 			}
 		}
+
+		public static void AvoidAot(GameObject gameObject)
+		{
+			Input input = gameObject.Get<Input>("11");
+		}
 	}
 }

+ 38 - 24
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -28,36 +28,50 @@ namespace Hotfix
 			this.account = rc.Get<GameObject>("Account");
 		}
 
-		private async void OnLogin()
+		private void OnLogin()
 		{
 			Session session = null;
-			try
-			{
-				session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
-				string text = this.account.GetComponent<InputField>().text;
-				R2C_Login r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = text, Password = "111111" });
-				Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
-				Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
-
-				G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() {Key = r2CLogin.Key});
-				Log.Info("登陆gate成功!");
-
-				// 创建Player
-				Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
-				PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
-				playerComponent.MyPlayer = player;
-
-				Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
-				Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
-			}
-			catch (Exception e)
+			session = Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
+			string text = this.account.GetComponent<InputField>().text;
+			session.CallWithAction(new C2R_Login() { Account = text, Password = "111111" }, (response) => LoginOK(response));
+		}
+
+		private void LoginOK(AResponse response)
+		{
+			R2C_Login r2CLogin = (R2C_Login) response;
+			if (r2CLogin.Error != ErrorCode.ERR_Success)
 			{
-				Log.Error(e.ToStr());
+				Log.Error(r2CLogin.Error.ToString());
+				return;
 			}
-			finally
+
+			Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
+			Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
+
+			SessionComponent.Instance.Session.CallWithAction(new C2G_LoginGate() { Key = r2CLogin.Key },
+				(response2)=>LoginGateOk(response2)
+			);
+
+		}
+
+		private void LoginGateOk(AResponse response)
+		{
+			G2C_LoginGate g2CLoginGate = (G2C_LoginGate) response;
+			if (g2CLoginGate.Error != ErrorCode.ERR_Success)
 			{
-				session?.Dispose();
+				Log.Error(g2CLoginGate.Error.ToString());
+				return;
 			}
+
+			Log.Info("登陆gate成功!");
+
+			// 创建Player
+			Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
+			PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
+			playerComponent.MyPlayer = player;
+
+			Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
+			Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
 		}
 	}
 }