Przeglądaj źródła

优化服务端消息处理,服务端的rpc消息处理不需要自己去new Response消息了,不需要try catch ReplyError了
消息处理代码更加简洁

tanghai 6 lat temu
rodzic
commit
e28dae5d56
40 zmienionych plików z 302 dodań i 456 usunięć
  1. 2 2
      Server/Hotfix/Handler/C2G_PlayerInfoHandler.cs
  2. 10 23
      Server/Hotfix/Handler/C2M_ReloadHandler.cs
  3. 3 2
      Server/Hotfix/Handler/C2M_TestRequestHandler.cs
  4. 3 3
      Server/Hotfix/Handler/C2R_PingHandler.cs
  5. 2 1
      Server/Hotfix/Handler/G2C_TestHandler.cs
  6. 4 15
      Server/Hotfix/Handler/M2A_ReloadHandler.cs
  7. 3 11
      Server/Hotfix/Module/ActorLocation/ObjectAddRequestHandler.cs
  8. 6 19
      Server/Hotfix/Module/ActorLocation/ObjectGetRequestHandler.cs
  9. 3 11
      Server/Hotfix/Module/ActorLocation/ObjectLockRequestHandler.cs
  10. 4 11
      Server/Hotfix/Module/ActorLocation/ObjectRemoveRequestHandler.cs
  11. 4 11
      Server/Hotfix/Module/ActorLocation/ObjectUnLockRequestHandler.cs
  12. 4 17
      Server/Hotfix/Module/DB/DBQueryBatchRequestHandler.cs
  13. 5 17
      Server/Hotfix/Module/DB/DBQueryJsonRequestHandler.cs
  14. 4 17
      Server/Hotfix/Module/DB/DBQueryRequestHandler.cs
  15. 8 21
      Server/Hotfix/Module/DB/DBSaveBatchRequestHandler.cs
  16. 7 20
      Server/Hotfix/Module/DB/DBSaveRequestHandler.cs
  17. 27 36
      Server/Hotfix/Module/Demo/Actor_TransferHandler.cs
  18. 9 22
      Server/Hotfix/Module/Demo/C2G_EnterMapHandler.cs
  19. 16 23
      Server/Hotfix/Module/Demo/C2G_LoginGateHandler.cs
  20. 3 2
      Server/Hotfix/Module/Demo/C2M_TestActorRequestHandler.cs
  21. 18 31
      Server/Hotfix/Module/Demo/C2R_LoginHandler.cs
  22. 2 2
      Server/Hotfix/Module/Demo/Frame_ClickMapHandler.cs
  23. 25 39
      Server/Hotfix/Module/Demo/G2M_CreateUnitHandler.cs
  24. 2 1
      Server/Hotfix/Module/Demo/G2M_SessionDisconnectHandler.cs
  25. 11 18
      Server/Hotfix/Module/Demo/M2M_TrasferUnitRequest.cs
  26. 6 13
      Server/Hotfix/Module/Demo/R2G_GetLoginKeyHandler.cs
  27. 9 4
      Server/Model/Module/Actor/AMActorHandler.cs
  28. 19 15
      Server/Model/Module/Actor/AMActorRpcHandler.cs
  29. 10 5
      Server/Model/Module/ActorLocation/AMActorLocationHandler.cs
  30. 19 15
      Server/Model/Module/ActorLocation/AMActorLocationRpcHandler.cs
  31. 20 15
      Server/Model/Module/Message/AMRpcHandler.cs
  32. 2 1
      Unity/Assets/Hotfix/Module/Demo/G2C_TestHotfixHandler.cs
  33. 3 2
      Unity/Assets/Hotfix/Module/Demo/M2C_CreateUnitsHandler.cs
  34. 3 1
      Unity/Assets/Hotfix/Module/Demo/M2C_PathfindingResultHandler.cs
  35. 11 3
      Unity/Assets/Hotfix/Module/Message/AMHandler.cs
  36. 1 1
      Unity/Assets/Hotfix/Module/Message/IMHandler.cs
  37. 11 3
      Unity/Assets/Model/Module/Message/AMHandler.cs
  38. 1 1
      Unity/Assets/Model/Module/Message/IMHandler.cs
  39. 1 1
      Unity/Assets/Model/Module/Message/MessageDispatcherComponent.cs
  40. 1 1
      Unity/Assets/Model/Module/Message/MessageProxy.cs

+ 2 - 2
Server/Hotfix/Handler/C2G_PlayerInfoHandler.cs

@@ -7,7 +7,7 @@ namespace ETHotfix
 	[MessageHandler(AppType.Gate)]
 	public class C2G_PlayerInfoHandler : AMRpcHandler<C2G_PlayerInfo, G2C_PlayerInfo>
 	{
-		protected override void Run(Session session, C2G_PlayerInfo message, Action<G2C_PlayerInfo> reply)
+		protected override async ETTask Run(Session session, C2G_PlayerInfo request, G2C_PlayerInfo response, Action reply)
 		{
 			G2C_PlayerInfo g2CPlayerInfo = new G2C_PlayerInfo();
 			g2CPlayerInfo.PlayerInfo = new PlayerInfo();
@@ -21,7 +21,7 @@ namespace ETHotfix
 			g2CPlayerInfo.TestRepeatedInt64.Add(8);
 			g2CPlayerInfo.TestRepeatedString.Add("9");
 			g2CPlayerInfo.TestRepeatedString.Add("10");
-			reply(g2CPlayerInfo);
+			reply();
 		}
 	}
 }

+ 10 - 23
Server/Hotfix/Handler/C2M_ReloadHandler.cs

@@ -6,35 +6,22 @@ namespace ETHotfix
 	[MessageHandler(AppType.Manager)]
 	public class C2M_ReloadHandler: AMRpcHandler<C2M_Reload, M2C_Reload>
 	{
-		protected override void Run(Session session, C2M_Reload message, Action<M2C_Reload> reply)
+		protected override async ETTask Run(Session session, C2M_Reload request, M2C_Reload response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		private async ETVoid RunAsync(Session session, C2M_Reload message, Action<M2C_Reload> reply)
-		{
-			M2C_Reload response = new M2C_Reload();
-			if (message.Account != "panda" && message.Password != "panda")
+			if (request.Account != "panda" && request.Password != "panda")
 			{
-				Log.Error($"error reload account and password: {MongoHelper.ToJson(message)}");
+				Log.Error($"error reload account and password: {MongoHelper.ToJson(request)}");
 				return;
 			}
-			try
-			{
-				StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
-				NetInnerComponent netInnerComponent = Game.Scene.GetComponent<NetInnerComponent>();
-				foreach (StartConfig startConfig in startConfigComponent.GetAll())
-				{
-					InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
-					Session serverSession = netInnerComponent.Get(innerConfig.IPEndPoint);
-					await serverSession.Call(new M2A_Reload());
-				}
-				reply(response);
-			}
-			catch (Exception e)
+			StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
+			NetInnerComponent netInnerComponent = Game.Scene.GetComponent<NetInnerComponent>();
+			foreach (StartConfig startConfig in startConfigComponent.GetAll())
 			{
-				ReplyError(response, e, reply);
+				InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
+				Session serverSession = netInnerComponent.Get(innerConfig.IPEndPoint);
+				await serverSession.Call(new M2A_Reload());
 			}
+			reply();
 		}
 	}
 }

+ 3 - 2
Server/Hotfix/Handler/C2M_TestRequestHandler.cs

@@ -6,10 +6,11 @@ namespace ETHotfix
 	[ActorMessageHandler(AppType.Map)]
 	public class C2M_TestRequestHandler : AMActorLocationRpcHandler<Unit, C2M_TestRequest, M2C_TestResponse>
 	{
-		protected override async ETTask Run(Unit unit, C2M_TestRequest message, Action<M2C_TestResponse> reply)
+		protected override async ETTask Run(Unit unit, C2M_TestRequest message, M2C_TestResponse response, Action reply)
 		{
+			response.Response = "response actor rpc";
+			reply();
 			await ETTask.CompletedTask;
-			reply(new M2C_TestResponse() {Response = "response actor rpc"});
 		}
 	}
 }

+ 3 - 3
Server/Hotfix/Handler/C2R_PingHandler.cs

@@ -6,10 +6,10 @@ namespace ETHotfix
 	[MessageHandler(AppType.AllServer)]
 	public class C2R_PingHandler : AMRpcHandler<C2R_Ping, R2C_Ping>
 	{
-		protected override void Run(Session session, C2R_Ping message, Action<R2C_Ping> reply)
+		protected override async ETTask Run(Session session, C2R_Ping request, R2C_Ping response, Action reply)
 		{
-			R2C_Ping r2CPing = new R2C_Ping();
-			reply(r2CPing);
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 2 - 1
Server/Hotfix/Handler/G2C_TestHandler.cs

@@ -6,9 +6,10 @@ namespace ETHotfix
     public class G2C_TestHandler: AMHandler<G2C_Test>
     {
         public static int count = 0;
-        protected override void Run(Session session, G2C_Test message)
+        protected override async ETTask Run(Session session, G2C_Test message)
         {
             count++;
+            await ETTask.CompletedTask;
         }
     }
 }

+ 4 - 15
Server/Hotfix/Handler/M2A_ReloadHandler.cs

@@ -6,22 +6,11 @@ namespace ETHotfix
 	[MessageHandler(AppType.AllServer)]
 	public class M2A_ReloadHandler : AMRpcHandler<M2A_Reload, A2M_Reload>
 	{
-		protected override void Run(Session session, M2A_Reload message, Action<A2M_Reload> reply)
+		protected override async ETTask Run(Session session, M2A_Reload request, A2M_Reload response, Action reply)
 		{
-			A2M_Reload response = new A2M_Reload();
-			try
-			{
-				Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				response.Error = ErrorCode.ERR_ReloadFail;
-				StartConfig myStartConfig = StartConfigComponent.Instance.StartConfig;
-				InnerConfig innerConfig = myStartConfig.GetComponent<InnerConfig>();
-				response.Message = $"{innerConfig.IPEndPoint} reload fail, {e}";
-				reply(response);
-			}
+			Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 3 - 11
Server/Hotfix/Module/ActorLocation/ObjectAddRequestHandler.cs

@@ -6,18 +6,10 @@ namespace ETHotfix
 	[MessageHandler(AppType.Location)]
 	public class ObjectAddRequestHandler : AMRpcHandler<ObjectAddRequest, ObjectAddResponse>
 	{
-		protected override void Run(Session session, ObjectAddRequest message, Action<ObjectAddResponse> reply)
+		protected override async ETTask Run(Session session, ObjectAddRequest request, ObjectAddResponse response, Action reply)
 		{
-			ObjectAddResponse response = new ObjectAddResponse();
-			try
-			{
-				Game.Scene.GetComponent<LocationComponent>().Add(message.Key, message.InstanceId);
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Game.Scene.GetComponent<LocationComponent>().Add(request.Key, request.InstanceId);
+			reply();
 		}
 	}
 }

+ 6 - 19
Server/Hotfix/Module/ActorLocation/ObjectGetRequestHandler.cs

@@ -6,28 +6,15 @@ namespace ETHotfix
 	[MessageHandler(AppType.Location)]
 	public class ObjectGetRequestHandler : AMRpcHandler<ObjectGetRequest, ObjectGetResponse>
 	{
-		protected override void Run(Session session, ObjectGetRequest message, Action<ObjectGetResponse> reply)
+		protected override async ETTask Run(Session session, ObjectGetRequest request, ObjectGetResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		private async ETVoid RunAsync(Session session, ObjectGetRequest message, Action<ObjectGetResponse> reply)
-		{
-			ObjectGetResponse response = new ObjectGetResponse();
-			try
-			{
-				long instanceId = await Game.Scene.GetComponent<LocationComponent>().GetAsync(message.Key);
-				if (instanceId == 0)
-				{
-					response.Error = ErrorCode.ERR_ActorLocationNotFound;
-				}
-				response.InstanceId = instanceId;
-				reply(response);
-			}
-			catch (Exception e)
+			long instanceId = await Game.Scene.GetComponent<LocationComponent>().GetAsync(request.Key);
+			if (instanceId == 0)
 			{
-				ReplyError(response, e, reply);
+				response.Error = ErrorCode.ERR_ActorLocationNotFound;
 			}
+			response.InstanceId = instanceId;
+			reply();
 		}
 	}
 }

+ 3 - 11
Server/Hotfix/Module/ActorLocation/ObjectLockRequestHandler.cs

@@ -6,18 +6,10 @@ namespace ETHotfix
 	[MessageHandler(AppType.Location)]
 	public class ObjectLockRequestHandler : AMRpcHandler<ObjectLockRequest, ObjectLockResponse>
 	{
-		protected override void Run(Session session, ObjectLockRequest message, Action<ObjectLockResponse> reply)
+		protected override async ETTask Run(Session session, ObjectLockRequest request, ObjectLockResponse response, Action reply)
 		{
-			ObjectLockResponse response = new ObjectLockResponse();
-			try
-			{
-				Game.Scene.GetComponent<LocationComponent>().Lock(message.Key, message.InstanceId, message.Time).Coroutine();
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Game.Scene.GetComponent<LocationComponent>().Lock(request.Key, request.InstanceId, request.Time).Coroutine();
+			reply();
 		}
 	}
 }

+ 4 - 11
Server/Hotfix/Module/ActorLocation/ObjectRemoveRequestHandler.cs

@@ -6,18 +6,11 @@ namespace ETHotfix
 	[MessageHandler(AppType.Location)]
 	public class ObjectRemoveRequestHandler : AMRpcHandler<ObjectRemoveRequest, ObjectRemoveResponse>
 	{
-		protected override void Run(Session session, ObjectRemoveRequest message, Action<ObjectRemoveResponse> reply)
+		protected override async ETTask Run(Session session, ObjectRemoveRequest request, ObjectRemoveResponse response, Action reply)
 		{
-			ObjectRemoveResponse response = new ObjectRemoveResponse();
-			try
-			{
-				Game.Scene.GetComponent<LocationComponent>().Remove(message.Key);
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Game.Scene.GetComponent<LocationComponent>().Remove(request.Key);
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 4 - 11
Server/Hotfix/Module/ActorLocation/ObjectUnLockRequestHandler.cs

@@ -6,18 +6,11 @@ namespace ETHotfix
 	[MessageHandler(AppType.Location)]
 	public class ObjectUnLockRequestHandler : AMRpcHandler<ObjectUnLockRequest, ObjectUnLockResponse>
 	{
-		protected override void Run(Session session, ObjectUnLockRequest message, Action<ObjectUnLockResponse> reply)
+		protected override async ETTask Run(Session session, ObjectUnLockRequest request, ObjectUnLockResponse response, Action reply)
 		{
-			ObjectUnLockResponse response = new ObjectUnLockResponse();
-			try
-			{
-				Game.Scene.GetComponent<LocationComponent>().UnLockAndUpdate(message.Key, message.OldInstanceId, message.InstanceId);
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Game.Scene.GetComponent<LocationComponent>().UnLockAndUpdate(request.Key, request.OldInstanceId, request.InstanceId);
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 4 - 17
Server/Hotfix/Module/DB/DBQueryBatchRequestHandler.cs

@@ -6,25 +6,12 @@ namespace ETHotfix
 	[MessageHandler(AppType.DB)]
 	public class DBQueryBatchRequestHandler : AMRpcHandler<DBQueryBatchRequest, DBQueryBatchResponse>
 	{
-		protected override void Run(Session session, DBQueryBatchRequest message, Action<DBQueryBatchResponse> reply)
+		protected override async ETTask Run(Session session, DBQueryBatchRequest request, DBQueryBatchResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, DBQueryBatchRequest message, Action<DBQueryBatchResponse> reply)
-		{
-			DBQueryBatchResponse response = new DBQueryBatchResponse();
-			try
-			{
-				DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-				response.Components = await dbComponent.GetBatch(message.CollectionName, message.IdList);
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+			response.Components = await dbComponent.GetBatch(request.CollectionName, request.IdList);
 
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			reply();
 		}
 	}
 }

+ 5 - 17
Server/Hotfix/Module/DB/DBQueryJsonRequestHandler.cs

@@ -7,25 +7,13 @@ namespace ETHotfix
 	[MessageHandler(AppType.DB)]
 	public class DBQueryJsonRequestHandler : AMRpcHandler<DBQueryJsonRequest, DBQueryJsonResponse>
 	{
-		protected override void Run(Session session, DBQueryJsonRequest message, Action<DBQueryJsonResponse> reply)
+		protected override async ETTask Run(Session session, DBQueryJsonRequest request, DBQueryJsonResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, DBQueryJsonRequest message, Action<DBQueryJsonResponse> reply)
-		{
-			DBQueryJsonResponse response = new DBQueryJsonResponse();
-			try
-			{
-				List<ComponentWithId> components = await Game.Scene.GetComponent<DBComponent>().GetJson(message.CollectionName, message.Json);
-				response.Components = components;
+			List<ComponentWithId> components = await Game.Scene.GetComponent<DBComponent>().GetJson(request.CollectionName, request.Json);
+			response.Components = components;
 
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 4 - 17
Server/Hotfix/Module/DB/DBQueryRequestHandler.cs

@@ -6,26 +6,13 @@ namespace ETHotfix
 	[MessageHandler(AppType.DB)]
 	public class DBQueryRequestHandler : AMRpcHandler<DBQueryRequest, DBQueryResponse>
 	{
-		protected override void Run(Session session, DBQueryRequest message, Action<DBQueryResponse> reply)
+		protected override async ETTask Run(Session session, DBQueryRequest request, DBQueryResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, DBQueryRequest message, Action<DBQueryResponse> reply)
-		{
-			DBQueryResponse response = new DBQueryResponse();
-			try
-			{
-				ComponentWithId component = await Game.Scene.GetComponent<DBComponent>().Get(message.CollectionName, message.Id);
+			ComponentWithId component = await Game.Scene.GetComponent<DBComponent>().Get(request.CollectionName, request.Id);
 
-				response.Component = component;
+			response.Component = component;
 
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			reply();
 		}
 	}
 }

+ 8 - 21
Server/Hotfix/Module/DB/DBSaveBatchRequestHandler.cs

@@ -6,31 +6,18 @@ namespace ETHotfix
 	[MessageHandler(AppType.DB)]
 	public class DBSaveBatchRequestHandler : AMRpcHandler<DBSaveBatchRequest, DBSaveBatchResponse>
 	{
-		protected override void Run(Session session, DBSaveBatchRequest message, Action<DBSaveBatchResponse> reply)
+		protected override async ETTask Run(Session session, DBSaveBatchRequest request, DBSaveBatchResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, DBSaveBatchRequest message, Action<DBSaveBatchResponse> reply)
-		{
-			DBSaveBatchResponse response = new DBSaveBatchResponse();
-			try
-			{
-				DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-
-				if (string.IsNullOrEmpty(message.CollectionName))
-				{
-					message.CollectionName = message.Components[0].GetType().Name;
-				}
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
 
-				await dbComponent.AddBatch(message.Components, message.CollectionName);
-
-				reply(response);
-			}
-			catch (Exception e)
+			if (string.IsNullOrEmpty(request.CollectionName))
 			{
-				ReplyError(response, e, reply);
+				request.CollectionName = request.Components[0].GetType().Name;
 			}
+
+			await dbComponent.AddBatch(request.Components, request.CollectionName);
+
+			reply();
 		}
 	}
 }

+ 7 - 20
Server/Hotfix/Module/DB/DBSaveRequestHandler.cs

@@ -6,29 +6,16 @@ namespace ETHotfix
 	[MessageHandler(AppType.DB)]
 	public class DBSaveRequestHandler : AMRpcHandler<DBSaveRequest, DBSaveResponse>
 	{
-		protected override void Run(Session session, DBSaveRequest message, Action<DBSaveResponse> reply)
+		protected override async ETTask Run(Session session, DBSaveRequest request, DBSaveResponse response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, DBSaveRequest message, Action<DBSaveResponse> reply)
-		{
-			DBSaveResponse response = new DBSaveResponse();
-			try
-			{
-				DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-				if (string.IsNullOrEmpty(message.CollectionName))
-				{
-					message.CollectionName = message.Component.GetType().Name;
-				}
-
-				await dbComponent.Add(message.Component, message.CollectionName);
-				reply(response);
-			}
-			catch (Exception e)
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+			if (string.IsNullOrEmpty(request.CollectionName))
 			{
-				ReplyError(response, e, reply);
+				request.CollectionName = request.Component.GetType().Name;
 			}
+
+			await dbComponent.Add(request.Component, request.CollectionName);
+			reply();
 		}
 	}
 }

+ 27 - 36
Server/Hotfix/Module/Demo/Actor_TransferHandler.cs

@@ -7,51 +7,42 @@ namespace ETHotfix
 	[ActorMessageHandler(AppType.Map)]
 	public class Actor_TransferHandler : AMActorRpcHandler<Unit, Actor_TransferRequest, Actor_TransferResponse>
 	{
-		protected override async ETTask Run(Unit unit, Actor_TransferRequest message, Action<Actor_TransferResponse> reply)
+		protected override async ETTask Run(Unit unit, Actor_TransferRequest request, Actor_TransferResponse response, Action reply)
 		{
-			Actor_TransferResponse response = new Actor_TransferResponse();
+			long unitId = unit.Id;
 
-			try
-			{
-				long unitId = unit.Id;
-
-				// 先在location锁住unit的地址
-				await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId, unit.InstanceId);
+			// 先在location锁住unit的地址
+			await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId, unit.InstanceId);
 
-				// 删除unit,让其它进程发送过来的消息找不到actor,重发
-				Game.EventSystem.Remove(unitId);
-				
-				long instanceId = unit.InstanceId;
-				
-				int mapIndex = message.MapIndex;
+			// 删除unit,让其它进程发送过来的消息找不到actor,重发
+			Game.EventSystem.Remove(unitId);
+			
+			long instanceId = unit.InstanceId;
+			
+			int mapIndex = request.MapIndex;
 
-				StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
+			StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
 
-				// 考虑AllServer情况
-				if (startConfigComponent.Count == 1)
-				{
-					mapIndex = 0;
-				}
+			// 考虑AllServer情况
+			if (startConfigComponent.Count == 1)
+			{
+				mapIndex = 0;
+			}
 
-				// 传送到map
-				StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
-				IPEndPoint address = mapConfig.GetComponent<InnerConfig>().IPEndPoint;
-				Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
+			// 传送到map
+			StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
+			IPEndPoint address = mapConfig.GetComponent<InnerConfig>().IPEndPoint;
+			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
 
-				// 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
-				Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
-				M2M_TrasferUnitResponse m2m_TrasferUnitResponse = (M2M_TrasferUnitResponse)await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
-				unit.Dispose();
+			// 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
+			Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
+			M2M_TrasferUnitResponse m2m_TrasferUnitResponse = (M2M_TrasferUnitResponse)await session.Call(new M2M_TrasferUnitRequest() { Unit = unit });
+			unit.Dispose();
 
-				// 解锁unit的地址,并且更新unit的instanceId
-				await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, instanceId, m2m_TrasferUnitResponse.InstanceId);
+			// 解锁unit的地址,并且更新unit的instanceId
+			await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, instanceId, m2m_TrasferUnitResponse.InstanceId);
 
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			reply();
 		}
 	}
 }

+ 9 - 22
Server/Hotfix/Module/Demo/C2G_EnterMapHandler.cs

@@ -7,29 +7,16 @@ namespace ETHotfix
 	[MessageHandler(AppType.Gate)]
 	public class C2G_EnterMapHandler : AMRpcHandler<C2G_EnterMap, G2C_EnterMap>
 	{
-		protected override void Run(Session session, C2G_EnterMap message, Action<G2C_EnterMap> reply)
+		protected override async ETTask Run(Session session, C2G_EnterMap request, G2C_EnterMap response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, C2G_EnterMap message, Action<G2C_EnterMap> reply)
-		{
-			G2C_EnterMap response = new G2C_EnterMap();
-			try
-			{
-				Player player = session.GetComponent<SessionPlayerComponent>().Player;
-				// 在map服务器上创建战斗Unit
-				IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
-				Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
-				M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
-				player.UnitId = createUnit.UnitId;
-				response.UnitId = createUnit.UnitId;
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Player player = session.GetComponent<SessionPlayerComponent>().Player;
+			// 在map服务器上创建战斗Unit
+			IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
+			Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
+			M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
+			player.UnitId = createUnit.UnitId;
+			response.UnitId = createUnit.UnitId;
+			reply();
 		}
 	}
 }

+ 16 - 23
Server/Hotfix/Module/Demo/C2G_LoginGateHandler.cs

@@ -6,33 +6,26 @@ namespace ETHotfix
 	[MessageHandler(AppType.Gate)]
 	public class C2G_LoginGateHandler : AMRpcHandler<C2G_LoginGate, G2C_LoginGate>
 	{
-		protected override void Run(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply)
+		protected override async ETTask Run(Session session, C2G_LoginGate request, G2C_LoginGate response, Action reply)
 		{
-			G2C_LoginGate response = new G2C_LoginGate();
-			try
+			string account = Game.Scene.GetComponent<GateSessionKeyComponent>().Get(request.Key);
+			if (account == null)
 			{
-				string account = Game.Scene.GetComponent<GateSessionKeyComponent>().Get(message.Key);
-				if (account == null)
-				{
-					response.Error = ErrorCode.ERR_ConnectGateKeyError;
-					response.Message = "Gate key验证失败!";
-					reply(response);
-					return;
-				}
-				Player player = ComponentFactory.Create<Player, string>(account);
-				Game.Scene.GetComponent<PlayerComponent>().Add(player);
-				session.AddComponent<SessionPlayerComponent>().Player = player;
-				session.AddComponent<MailBoxComponent, string>(MailboxType.GateSession);
+				response.Error = ErrorCode.ERR_ConnectGateKeyError;
+				response.Message = "Gate key验证失败!";
+				reply();
+				return;
+			}
+			Player player = ComponentFactory.Create<Player, string>(account);
+			Game.Scene.GetComponent<PlayerComponent>().Add(player);
+			session.AddComponent<SessionPlayerComponent>().Player = player;
+			session.AddComponent<MailBoxComponent, string>(MailboxType.GateSession);
 
-				response.PlayerId = player.Id;
-				reply(response);
+			response.PlayerId = player.Id;
+			reply();
 
-				session.Send(new G2C_TestHotfixMessage() { Info = "recv hotfix message success" });
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			session.Send(new G2C_TestHotfixMessage() { Info = "recv hotfix message success" });
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 3 - 2
Server/Hotfix/Module/Demo/C2M_TestActorRequestHandler.cs

@@ -6,9 +6,10 @@ namespace ETHotfix
 	[ActorMessageHandler(AppType.Map)]
 	public class C2M_TestActorRequestHandler : AMActorLocationRpcHandler<Unit, C2M_TestActorRequest, M2C_TestActorResponse>
 	{
-		protected override async ETTask Run(Unit unit, C2M_TestActorRequest message, Action<M2C_TestActorResponse> reply)
+		protected override async ETTask Run(Unit unit, C2M_TestActorRequest message, M2C_TestActorResponse response, Action reply)
 		{
-			reply(new M2C_TestActorResponse(){Info = "actor rpc response"});
+			response.Info = "actor rpc response";
+			reply();
 			await ETTask.CompletedTask;
 		}
 	}

+ 18 - 31
Server/Hotfix/Module/Demo/C2R_LoginHandler.cs

@@ -7,42 +7,29 @@ namespace ETHotfix
 	[MessageHandler(AppType.Realm)]
 	public class C2R_LoginHandler : AMRpcHandler<C2R_Login, R2C_Login>
 	{
-		protected override void Run(Session session, C2R_Login message, Action<R2C_Login> reply)
+		protected override async ETTask Run(Session session, C2R_Login request, R2C_Login response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-
-		private async ETVoid RunAsync(Session session, C2R_Login message, Action<R2C_Login> reply)
-		{
-			R2C_Login response = new R2C_Login();
-			try
-			{
-				//if (message.Account != "abcdef" || message.Password != "111111")
-				//{
-				//	response.Error = ErrorCode.ERR_AccountOrPasswordError;
-				//	reply(response);
-				//	return;
-				//}
+			//if (message.Account != "abcdef" || message.Password != "111111")
+			//{
+			//	response.Error = ErrorCode.ERR_AccountOrPasswordError;
+			//	reply(response);
+			//	return;
+			//}
 
-				// 随机分配一个Gate
-				StartConfig config = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
-				//Log.Debug($"gate address: {MongoHelper.ToJson(config)}");
-				IPEndPoint innerAddress = config.GetComponent<InnerConfig>().IPEndPoint;
-				Session gateSession = Game.Scene.GetComponent<NetInnerComponent>().Get(innerAddress);
+			// 随机分配一个Gate
+			StartConfig config = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
+			//Log.Debug($"gate address: {MongoHelper.ToJson(config)}");
+			IPEndPoint innerAddress = config.GetComponent<InnerConfig>().IPEndPoint;
+			Session gateSession = Game.Scene.GetComponent<NetInnerComponent>().Get(innerAddress);
 
-				// 向gate请求一个key,客户端可以拿着这个key连接gate
-				G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey() {Account = message.Account});
+			// 向gate请求一个key,客户端可以拿着这个key连接gate
+			G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey() {Account = request.Account});
 
-				string outerAddress = config.GetComponent<OuterConfig>().Address2;
+			string outerAddress = config.GetComponent<OuterConfig>().Address2;
 
-				response.Address = outerAddress;
-				response.Key = g2RGetLoginKey.Key;
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			response.Address = outerAddress;
+			response.Key = g2RGetLoginKey.Key;
+			reply();
 		}
 	}
 }

+ 2 - 2
Server/Hotfix/Module/Demo/Frame_ClickMapHandler.cs

@@ -7,11 +7,11 @@ namespace ETHotfix
 	[ActorMessageHandler(AppType.Map)]
 	public class Frame_ClickMapHandler : AMActorLocationHandler<Unit, Frame_ClickMap>
 	{
-		protected override void Run(Unit unit, Frame_ClickMap message)
+		protected override async ETTask Run(Unit unit, Frame_ClickMap message)
 		{
 			Vector3 target = new Vector3(message.X, message.Y, message.Z);
 			unit.GetComponent<UnitPathComponent>().MoveTo(target).Coroutine();
-			
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 25 - 39
Server/Hotfix/Module/Demo/G2M_CreateUnitHandler.cs

@@ -8,48 +8,34 @@ namespace ETHotfix
 	[MessageHandler(AppType.Map)]
 	public class G2M_CreateUnitHandler : AMRpcHandler<G2M_CreateUnit, M2G_CreateUnit>
 	{
-		protected override void Run(Session session, G2M_CreateUnit message, Action<M2G_CreateUnit> reply)
+		protected override async ETTask Run(Session session, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
 		{
-			RunAsync(session, message, reply).Coroutine();
-		}
-		
-		protected async ETVoid RunAsync(Session session, G2M_CreateUnit message, Action<M2G_CreateUnit> reply)
-		{
-			M2G_CreateUnit response = new M2G_CreateUnit();
-			try
-			{
-				Unit unit = ComponentFactory.CreateWithId<Unit>(IdGenerater.GenerateId());
-				unit.AddComponent<MoveComponent>();
-				unit.AddComponent<UnitPathComponent>();
-				unit.Position = new Vector3(-10, 0, -10);
-				
-				await unit.AddComponent<MailBoxComponent>().AddLocation();
-				unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
-				Game.Scene.GetComponent<UnitComponent>().Add(unit);
-				response.UnitId = unit.Id;
-				
-				
-				// 广播创建的unit
-				M2C_CreateUnits createUnits = new M2C_CreateUnits();
-				Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
-				foreach (Unit u in units)
-				{
-					UnitInfo unitInfo = new UnitInfo();
-					unitInfo.X = u.Position.x;
-					unitInfo.Y = u.Position.y;
-					unitInfo.Z = u.Position.z;
-					unitInfo.UnitId = u.Id;
-					createUnits.Units.Add(unitInfo);
-				}
-				MessageHelper.Broadcast(createUnits);
-				
-				
-				reply(response);
-			}
-			catch (Exception e)
+			Unit unit = ComponentFactory.CreateWithId<Unit>(IdGenerater.GenerateId());
+			unit.AddComponent<MoveComponent>();
+			unit.AddComponent<UnitPathComponent>();
+			unit.Position = new Vector3(-10, 0, -10);
+			
+			await unit.AddComponent<MailBoxComponent>().AddLocation();
+			unit.AddComponent<UnitGateComponent, long>(request.GateSessionId);
+			Game.Scene.GetComponent<UnitComponent>().Add(unit);
+			response.UnitId = unit.Id;
+			
+			
+			// 广播创建的unit
+			M2C_CreateUnits createUnits = new M2C_CreateUnits();
+			Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
+			foreach (Unit u in units)
 			{
-				ReplyError(response, e, reply);
+				UnitInfo unitInfo = new UnitInfo();
+				unitInfo.X = u.Position.x;
+				unitInfo.Y = u.Position.y;
+				unitInfo.Z = u.Position.z;
+				unitInfo.UnitId = u.Id;
+				createUnits.Units.Add(unitInfo);
 			}
+			MessageHelper.Broadcast(createUnits);
+			
+			reply();
 		}
 	}
 }

+ 2 - 1
Server/Hotfix/Module/Demo/G2M_SessionDisconnectHandler.cs

@@ -5,9 +5,10 @@ namespace ETHotfix
 	[ActorMessageHandler(AppType.Map)]
 	public class G2M_SessionDisconnectHandler : AMActorLocationHandler<Unit, G2M_SessionDisconnect>
 	{
-		protected override void Run(Unit unit, G2M_SessionDisconnect message)
+		protected override async ETTask Run(Unit unit, G2M_SessionDisconnect message)
 		{
 			unit.GetComponent<UnitGateComponent>().IsDisconnect = true;
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 11 - 18
Server/Hotfix/Module/Demo/M2M_TrasferUnitRequest.cs

@@ -6,25 +6,18 @@ namespace ETHotfix
 	[MessageHandler(AppType.Map)]
 	public class M2M_TrasferUnitRequestHandler : AMRpcHandler<M2M_TrasferUnitRequest, M2M_TrasferUnitResponse>
 	{
-		protected override void Run(Session session, M2M_TrasferUnitRequest message, Action<M2M_TrasferUnitResponse> reply)
+		protected override async ETTask Run(Session session, M2M_TrasferUnitRequest request, M2M_TrasferUnitResponse response, Action reply)
 		{
-			M2M_TrasferUnitResponse response = new M2M_TrasferUnitResponse();
-			try
-			{
-				Unit unit = message.Unit;
-				// 将unit加入事件系统
-				Game.EventSystem.Add(unit);
-				Log.Debug(MongoHelper.ToJson(message.Unit));
-				// 这里不需要注册location,因为unlock会更新位置
-				unit.AddComponent<MailBoxComponent>();
-				Game.Scene.GetComponent<UnitComponent>().Add(unit);
-				response.InstanceId = unit.InstanceId;
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			Unit unit = request.Unit;
+			// 将unit加入事件系统
+			Game.EventSystem.Add(unit);
+			Log.Debug(MongoHelper.ToJson(request.Unit));
+			// 这里不需要注册location,因为unlock会更新位置
+			unit.AddComponent<MailBoxComponent>();
+			Game.Scene.GetComponent<UnitComponent>().Add(unit);
+			response.InstanceId = unit.InstanceId;
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 6 - 13
Server/Hotfix/Module/Demo/R2G_GetLoginKeyHandler.cs

@@ -6,20 +6,13 @@ namespace ETHotfix
 	[MessageHandler(AppType.Gate)]
 	public class R2G_GetLoginKeyHandler : AMRpcHandler<R2G_GetLoginKey, G2R_GetLoginKey>
 	{
-		protected override void Run(Session session, R2G_GetLoginKey message, Action<G2R_GetLoginKey> reply)
+		protected override async ETTask Run(Session session, R2G_GetLoginKey request, G2R_GetLoginKey response, Action reply)
 		{
-			G2R_GetLoginKey response = new G2R_GetLoginKey();
-			try
-			{
-				long key = RandomHelper.RandInt64();
-				Game.Scene.GetComponent<GateSessionKeyComponent>().Add(key, message.Account);
-				response.Key = key;
-				reply(response);
-			}
-			catch (Exception e)
-			{
-				ReplyError(response, e, reply);
-			}
+			long key = RandomHelper.RandInt64();
+			Game.Scene.GetComponent<GateSessionKeyComponent>().Add(key, request.Account);
+			response.Key = key;
+			reply();
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 9 - 4
Server/Model/Module/Actor/AMActorHandler.cs

@@ -4,7 +4,7 @@ namespace ETModel
 {
 	public abstract class AMActorHandler<E, Message>: IMActorHandler where E: Entity where Message : class, IActorMessage
 	{
-		protected abstract void Run(E entity, Message message);
+		protected abstract ETTask Run(E entity, Message message);
 
 		public async ETTask Handle(Session session, Entity entity, object actorMessage)
 		{
@@ -21,9 +21,14 @@ namespace ETModel
 				return;
 			}
 
-			this.Run(e, msg);
-
-			await ETTask.CompletedTask;
+			try
+			{
+				await this.Run(e, msg);
+			}
+			catch (Exception exception)
+			{
+				Log.Error(exception);
+			}
 		}
 
 		public Type GetMessageType()

+ 19 - 15
Server/Model/Module/Actor/AMActorRpcHandler.cs

@@ -4,15 +4,7 @@ namespace ETModel
 {
 	public abstract class AMActorRpcHandler<E, Request, Response>: IMActorHandler where E: Entity where Request: class, IActorRequest where Response : class, IActorResponse
 	{
-		protected static void ReplyError(Response response, Exception e, Action<Response> reply)
-		{
-			Log.Error(e);
-			response.Error = ErrorCode.ERR_RpcFail;
-			response.Message = e.ToString();
-			reply(response);
-		}
-
-		protected abstract ETTask Run(E unit, Request message, Action<Response> reply);
+		protected abstract ETTask Run(E unit, Request request, Response response, Action reply);
 
 		public async ETTask Handle(Session session, Entity entity, object actorMessage)
 		{
@@ -32,24 +24,36 @@ namespace ETModel
 				}
 
 				int rpcId = request.RpcId;
-				
 				long instanceId = session.InstanceId;
-				
-				await this.Run(e, request, response =>
+				Response response = Activator.CreateInstance<Response>();
+
+				void Reply()
 				{
 					// 等回调回来,session可以已经断开了,所以需要判断session InstanceId是否一样
 					if (session.InstanceId != instanceId)
 					{
 						return;
 					}
+
 					response.RpcId = rpcId;
-					
 					session.Reply(response);
-				});
+				}
+
+				try
+				{
+					await this.Run(e, request, response, Reply);
+				}
+				catch (Exception exception)
+				{
+					Log.Error(exception);
+					response.Error = ErrorCode.ERR_RpcFail;
+					response.Message = e.ToString();
+					Reply();
+				}
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"解释消息失败: {actorMessage.GetType().FullName}", e);
+				Log.Error($"解释消息失败: {actorMessage.GetType().FullName}\n{e}");
 			}
 		}
 

+ 10 - 5
Server/Model/Module/ActorLocation/AMActorLocationHandler.cs

@@ -4,7 +4,7 @@ namespace ETModel
 {
 	public abstract class AMActorLocationHandler<E, Message>: IMActorHandler where E: Entity where Message : class, IActorLocationMessage
 	{
-		protected abstract void Run(E entity, Message message);
+		protected abstract ETTask Run(E entity, Message message);
 
 		public async ETTask Handle(Session session, Entity entity, object actorMessage)
 		{
@@ -24,10 +24,15 @@ namespace ETModel
 			ActorResponse actorResponse = new ActorResponse();
 			actorResponse.RpcId = msg.RpcId;
 			session.Reply(actorResponse);
-			
-			this.Run(e, msg);
-			
-			await ETTask.CompletedTask;
+
+			try
+			{
+				await this.Run(e, msg);
+			}
+			catch (Exception exception)
+			{
+				Log.Error(exception);
+			}
 		}
 
 		public Type GetMessageType()

+ 19 - 15
Server/Model/Module/ActorLocation/AMActorLocationRpcHandler.cs

@@ -4,15 +4,7 @@ namespace ETModel
 {
 	public abstract class AMActorLocationRpcHandler<E, Request, Response>: IMActorHandler where E: Entity where Request: class, IActorLocationRequest where Response : class, IActorLocationResponse
 	{
-		protected static void ReplyError(Response response, Exception e, Action<Response> reply)
-		{
-			Log.Error(e);
-			response.Error = ErrorCode.ERR_RpcFail;
-			response.Message = e.ToString();
-			reply(response);
-		}
-
-		protected abstract ETTask Run(E unit, Request message, Action<Response> reply);
+		protected abstract ETTask Run(E unit, Request message, Response response, Action reply);
 
 		public async ETTask Handle(Session session, Entity entity, object actorMessage)
 		{
@@ -32,24 +24,36 @@ namespace ETModel
 				}
 
 				int rpcId = request.RpcId;
-				
 				long instanceId = session.InstanceId;
-				
-				await this.Run(e, request, response =>
+				Response response = Activator.CreateInstance<Response>();
+
+				void Reply()
 				{
 					// 等回调回来,session可以已经断开了,所以需要判断session InstanceId是否一样
 					if (session.InstanceId != instanceId)
 					{
 						return;
 					}
+
 					response.RpcId = rpcId;
-					
 					session.Reply(response);
-				});
+				}
+
+				try
+				{
+					await this.Run(e, request, response, Reply);
+				}
+				catch (Exception exception)
+				{
+					Log.Error(exception);
+					response.Error = ErrorCode.ERR_RpcFail;
+					response.Message = e.ToString();
+					Reply();
+				}
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"解释消息失败: {actorMessage.GetType().FullName}", e);
+				Log.Error($"解释消息失败: {actorMessage.GetType().FullName}\n{e}");
 			}
 		}
 

+ 20 - 15
Server/Model/Module/Message/AMRpcHandler.cs

@@ -4,17 +4,9 @@ namespace ETModel
 {
 	public abstract class AMRpcHandler<Request, Response>: IMHandler where Request : class, IRequest where Response : class, IResponse 
 	{
-		protected static void ReplyError(Response response, Exception e, Action<Response> reply)
-		{
-			Log.Error(e);
-			response.Error = ErrorCode.ERR_RpcFail;
-			response.Message = e.ToString();
-			reply(response);
-		}
+		protected abstract ETTask Run(Session session, Request request, Response response, Action reply);
 
-		protected abstract void Run(Session session, Request message, Action<Response> reply);
-
-		public void Handle(Session session, object message)
+		public async ETVoid Handle(Session session, object message)
 		{
 			try
 			{
@@ -25,10 +17,10 @@ namespace ETModel
 				}
 
 				int rpcId = request.RpcId;
-
 				long instanceId = session.InstanceId;
-				
-				this.Run(session, request, response =>
+				Response response = Activator.CreateInstance<Response>();
+
+				void Reply()
 				{
 					// 等回调回来,session可以已经断开了,所以需要判断session InstanceId是否一样
 					if (session.InstanceId != instanceId)
@@ -38,11 +30,24 @@ namespace ETModel
 
 					response.RpcId = rpcId;
 					session.Reply(response);
-				});
+				}
+
+				try
+				{
+					await this.Run(session, request, response, Reply);
+				}
+				catch (Exception e)
+				{
+					Log.Error(e);
+					response.Error = ErrorCode.ERR_RpcFail;
+					response.Message = e.ToString();
+					Reply();
+				}
+				
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"解释消息失败: {message.GetType().FullName}", e);
+				Log.Error($"解释消息失败: {message.GetType().FullName}\n{e}");
 			}
 		}
 

+ 2 - 1
Unity/Assets/Hotfix/Module/Demo/G2C_TestHotfixHandler.cs

@@ -5,9 +5,10 @@ namespace ETHotfix
 	[MessageHandler]
 	public class G2C_TestHotfixMessageHandler : AMHandler<G2C_TestHotfixMessage>
 	{
-		protected override void Run(ETModel.Session session, G2C_TestHotfixMessage message)
+		protected override async ETTask Run(ETModel.Session session, G2C_TestHotfixMessage message)
 		{
 			Log.Debug(message.Info);
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 3 - 2
Unity/Assets/Hotfix/Module/Demo/M2C_CreateUnitsHandler.cs

@@ -1,5 +1,4 @@
 using ETModel;
-using PF;
 using Vector3 = UnityEngine.Vector3;
 
 namespace ETHotfix
@@ -7,7 +6,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class M2C_CreateUnitsHandler : AMHandler<M2C_CreateUnits>
 	{
-		protected override void Run(ETModel.Session session, M2C_CreateUnits message)
+		protected override async ETTask Run(ETModel.Session session, M2C_CreateUnits message)
 		{	
 			UnitComponent unitComponent = ETModel.Game.Scene.GetComponent<UnitComponent>();
 			
@@ -20,6 +19,8 @@ namespace ETHotfix
 				Unit unit = UnitFactory.Create(unitInfo.UnitId);
 				unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z);
 			}
+
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 3 - 1
Unity/Assets/Hotfix/Module/Demo/M2C_PathfindingResultHandler.cs

@@ -6,7 +6,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class M2C_PathfindingResultHandler : AMHandler<M2C_PathfindingResult>
 	{
-		protected override void Run(ETModel.Session session, M2C_PathfindingResult message)
+		protected override async ETTask Run(ETModel.Session session, M2C_PathfindingResult message)
 		{
 			Unit unit = ETModel.Game.Scene.GetComponent<UnitComponent>().Get(message.Id);
 			
@@ -22,6 +22,8 @@ namespace ETHotfix
 			{
 				GizmosDebug.Instance.Path.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i]));
 			}
+
+			await ETTask.CompletedTask;
 		}
 	}
 }

+ 11 - 3
Unity/Assets/Hotfix/Module/Message/AMHandler.cs

@@ -5,16 +5,24 @@ namespace ETHotfix
 {
 	public abstract class AMHandler<Message> : IMHandler where Message: class
 	{
-		protected abstract void Run(ETModel.Session session, Message message);
+		protected abstract ETTask Run(ETModel.Session session, Message message);
 
-		public void Handle(ETModel.Session session, object msg)
+		public async ETVoid Handle(ETModel.Session session, object msg)
 		{
 			Message message = msg as Message;
 			if (message == null)
 			{
 				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
 			}
-			this.Run(session, message);
+
+			try
+			{
+				await this.Run(session, message);
+			}
+			catch (Exception e)
+			{
+				Log.Error(e);
+			}
 		}
 
 		public Type GetMessageType()

+ 1 - 1
Unity/Assets/Hotfix/Module/Message/IMHandler.cs

@@ -6,7 +6,7 @@ namespace ETHotfix
 #if ILRuntime
 	public interface IMHandler
 	{
-		void Handle(ETModel.Session session, object message);
+		ETVoid Handle(ETModel.Session session, object message);
 		Type GetMessageType();
 	}
 #else

+ 11 - 3
Unity/Assets/Model/Module/Message/AMHandler.cs

@@ -4,9 +4,9 @@ namespace ETModel
 {
 	public abstract class AMHandler<Message> : IMHandler where Message: class
 	{
-		protected abstract void Run(Session session, Message message);
+		protected abstract ETTask Run(Session session, Message message);
 
-		public void Handle(Session session, object msg)
+		public async ETVoid Handle(Session session, object msg)
 		{
 			Message message = msg as Message;
 			if (message == null)
@@ -19,7 +19,15 @@ namespace ETModel
 				Log.Error($"session disconnect {msg}");
 				return;
 			}
-			this.Run(session, message);
+
+			try
+			{
+				await this.Run(session, message);
+			}
+			catch (Exception e)
+			{
+				Log.Error(e);
+			}
 		}
 
 		public Type GetMessageType()

+ 1 - 1
Unity/Assets/Model/Module/Message/IMHandler.cs

@@ -4,7 +4,7 @@ namespace ETModel
 {
 	public interface IMHandler
 	{
-		void Handle(Session session, object message);
+		ETVoid Handle(Session session, object message);
 		Type GetMessageType();
 	}
 }

+ 1 - 1
Unity/Assets/Model/Module/Message/MessageDispatcherComponent.cs

@@ -87,7 +87,7 @@ namespace ETModel
 			{
 				try
 				{
-					ev.Handle(session, messageInfo.Message);
+					ev.Handle(session, messageInfo.Message).Coroutine();
 				}
 				catch (Exception e)
 				{

+ 1 - 1
Unity/Assets/Model/Module/Message/MessageProxy.cs

@@ -13,7 +13,7 @@ namespace ETModel
 			this.action = action;
 		}
 		
-		public void Handle(Session session, object message)
+		public async ETVoid Handle(Session session, object message)
 		{
 			this.action.Invoke(session, message);
 		}