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

1.actor做了一次大修改,主要把名字命名得更规范一些,因为有些命名与erlang对应的概念不
一致导致新人理解困难,ActorComponent改为MailBoxComponent对应actor模型中的
mailbox概念,ActorProxy改为ActorMessageSender
2.上个版本Actor增加了一种知道instanceId发消息的模式,这个版本把gatesession改成了这
种机制,创建unit的时候,带上gateSession的InstanceId(不是gateSession的Id),这样
gateSession不需要注册location,unit直接拿着gateSession的InstanceId就能发消息到
gateSession,减少了一次location查找,这种机制其实就是最原始的actor模型(actorId中
带有地址信息)

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

+ 3 - 3
Server/App/Program.cs

@@ -66,7 +66,7 @@ namespace App
 						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
 						Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
 						Game.Scene.AddComponent<LocationProxyComponent>();
-						Game.Scene.AddComponent<ActorProxyComponent>();
+						Game.Scene.AddComponent<ActorMessageSenderComponent>();
 						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
 					case AppType.Location:
@@ -77,12 +77,12 @@ namespace App
 						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
 						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<LocationProxyComponent>();
-						Game.Scene.AddComponent<ActorProxyComponent>();
+						Game.Scene.AddComponent<ActorMessageSenderComponent>();
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ServerFrameComponent>();
 						break;
 					case AppType.AllServer:
-						Game.Scene.AddComponent<ActorProxyComponent>();
+						Game.Scene.AddComponent<ActorMessageSenderComponent>();
 						Game.Scene.AddComponent<PlayerComponent>();
 						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<DBComponent>();

+ 1 - 1
Server/Hotfix/Handler/Actor_TestHandler.cs

@@ -10,7 +10,7 @@ namespace ETHotfix
 		{
 			Log.Debug(message.Info);
 			await Task.CompletedTask;
-			unit.GetComponent<UnitGateComponent>().GetActorProxy().Send(message);
+			unit.GetComponent<UnitGateComponent>().GetActorMessageSender().Send(message);
 		}
 	}
 }

+ 1 - 1
Server/Hotfix/Handler/M2M_TrasferUnitRequest.cs

@@ -16,7 +16,7 @@ namespace ETHotfix
 				Game.EventSystem.Add(unit);
 				Log.Debug(MongoHelper.ToJson(message.Unit));
 				// 这里不需要注册location,因为unlock会更新位置
-				unit.AddComponent<ActorComponent>();
+				unit.AddComponent<MailBoxComponent>();
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				response.InstanceId = unit.InstanceId;
 				reply(response);

+ 3 - 3
Server/Hotfix/Helper/MessageHelper.cs

@@ -7,11 +7,11 @@ namespace ETHotfix
 		public static void Broadcast(IActorMessage message)
 		{
 			Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
-			ActorProxyComponent actorProxyComponent = Game.Scene.GetComponent<ActorProxyComponent>();
+			ActorMessageSenderComponent actorMessageSenderComponent = Game.Scene.GetComponent<ActorMessageSenderComponent>();
 			foreach (Unit unit in units)
 			{
-				long gateSessionId = unit.GetComponent<UnitGateComponent>().GateSessionId;
-				actorProxyComponent.Get(gateSessionId).Send(message);
+				long gateSessionActorId = unit.GetComponent<UnitGateComponent>().GateSessionActorId;
+				actorMessageSenderComponent.GetWithActorId(gateSessionActorId).Send(message);
 			}
 		}
 	}

+ 49 - 0
Server/Hotfix/Module/Actor/ActorMessageSenderComponentSystem.cs

@@ -0,0 +1,49 @@
+using System.Collections.Generic;
+using ETModel;
+
+namespace ETHotfix
+{
+    [ObjectSystem]
+    public class ActorMessageSenderComponentSystem : StartSystem<ActorMessageSenderComponent>
+    {
+        // 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟
+        public override async void Start(ActorMessageSenderComponent self)
+        {
+            List<long> timeoutActorProxyIds = new List<long>();
+
+            while (true)
+            {
+                await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10000);
+
+                if (self.IsDisposed)
+                {
+                    return;
+                }
+
+                timeoutActorProxyIds.Clear();
+
+                long timeNow = TimeHelper.Now();
+                foreach (long id in self.ActorMessageSenders.Keys)
+                {
+                    ActorMessageSender actorMessageSender = self.Get(id);
+                    if (actorMessageSender == null)
+                    {
+                        continue;
+                    }
+
+                    if (timeNow < actorMessageSender.LastSendTime + 60 * 1000)
+                    {
+                        continue;
+                    }
+					
+                    timeoutActorProxyIds.Add(id);
+                }
+
+                foreach (long id in timeoutActorProxyIds)
+                {
+                    self.Remove(id);
+                }
+            }
+        }
+    }
+}

+ 198 - 0
Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs

@@ -0,0 +1,198 @@
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+using ETModel;
+
+namespace ETHotfix
+{
+	[ObjectSystem]
+	public class ActorMessageSenderAwakeSystem : AwakeSystem<ActorMessageSender>
+	{
+		public override void Awake(ActorMessageSender self)
+		{
+			self.LastSendTime = TimeHelper.Now();
+			self.tcs = null;
+			self.FailTimes = 0;
+			self.MaxFailTimes = 5;
+			self.ActorId = 0;
+		}
+	}
+	
+	[ObjectSystem]
+	public class ActorMessageSenderAwake2System : AwakeSystem<ActorMessageSender, long>
+	{
+		public override void Awake(ActorMessageSender self, long actorId)
+		{
+			self.LastSendTime = TimeHelper.Now();
+			self.tcs = null;
+			self.FailTimes = 0;
+			self.MaxFailTimes = 0;
+			self.ActorId = actorId;
+		}
+	}
+
+	[ObjectSystem]
+	public class ActorMessageSenderStartSystem : StartSystem<ActorMessageSender>
+	{
+		public override async void Start(ActorMessageSender self)
+		{
+			if (self.ActorId == 0)
+			{
+				self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
+			}
+
+			self.Address = Game.Scene.GetComponent<StartConfigComponent>()
+					.Get(IdGenerater.GetAppIdFromId(self.ActorId))
+					.GetComponent<InnerConfig>().IPEndPoint;
+
+			self.UpdateAsync();
+		}
+	}
+	
+	[ObjectSystem]
+	public class ActorMessageSenderDestroySystem : DestroySystem<ActorMessageSender>
+	{
+		public override void Destroy(ActorMessageSender self)
+		{
+			self.LastSendTime = 0;
+			self.Address = null;
+
+			while (self.WaitingTasks.Count > 0)
+			{
+				ActorTask actorTask = self.WaitingTasks.Dequeue();
+				actorTask.RunFail(ErrorCode.ERR_NotFoundActor);
+			}
+
+			self.ActorId = 0;
+			self.FailTimes = 0;
+			var t = self.tcs;
+			self.tcs = null;
+			t?.SetResult(new ActorTask());
+		}
+	}
+
+	public static class ActorMessageSenderEx
+	{
+		private static void Add(this ActorMessageSender self, ActorTask task)
+		{
+			if (self.IsDisposed)
+			{
+				throw new Exception("ActorProxy Disposed! dont hold actorproxy");
+			}
+
+			self.WaitingTasks.Enqueue(task);
+			// failtimes > 0表示正在重试,这时候不能加到正在发送队列
+			if (self.FailTimes == 0)
+			{
+				self.AllowGet();
+			}
+		}
+
+		private static void AllowGet(this ActorMessageSender self)
+		{
+			if (self.tcs == null || self.WaitingTasks.Count <= 0)
+			{
+				return;
+			}
+
+			ActorTask task = self.WaitingTasks.Peek();
+
+			var t = self.tcs;
+			self.tcs = null;
+			t.SetResult(task);
+		}
+
+		private static Task<ActorTask> GetAsync(this ActorMessageSender self)
+		{
+			if (self.WaitingTasks.Count > 0)
+			{
+				ActorTask task = self.WaitingTasks.Peek();
+				return Task.FromResult(task);
+			}
+
+			self.tcs = new TaskCompletionSource<ActorTask>();
+			return self.tcs.Task;
+		}
+
+		public static async void UpdateAsync(this ActorMessageSender self)
+		{
+			try
+			{
+				while (true)
+				{
+					ActorTask actorTask = await self.GetAsync();
+					if (self.IsDisposed)
+					{
+						return;
+					}
+
+					await self.RunTask(actorTask);
+				}
+			}
+			catch (Exception e)
+			{
+				Log.Error(e);
+			}
+		}
+
+		private static async Task RunTask(this ActorMessageSender self, ActorTask task)
+		{
+			IResponse response = await task.Run();
+
+			// 如果没找到Actor,重试
+			if (response.Error == ErrorCode.ERR_NotFoundActor)
+			{
+				++self.FailTimes;
+
+				// 失败MaxFailTimes次则清空actor发送队列,返回失败
+				if (self.FailTimes > self.MaxFailTimes)
+				{
+					// 失败直接删除actorproxy
+					Log.Info($"actor send message fail, actorid: {self.Id}");
+					self.GetParent<ActorMessageSenderComponent>().Remove(self.Id);
+					return;
+				}
+
+				// 等待1s再发送
+				await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
+				self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
+				self.Address = Game.Scene.GetComponent<StartConfigComponent>()
+						.Get(IdGenerater.GetAppIdFromId(self.ActorId))
+						.GetComponent<InnerConfig>().IPEndPoint;
+				self.AllowGet();
+				return;
+			}
+
+			// 发送成功
+			self.LastSendTime = TimeHelper.Now();
+			self.FailTimes = 0;
+
+			self.WaitingTasks.Dequeue();
+		}
+
+		public static void Send(this ActorMessageSender self, IActorMessage message)
+		{
+			ActorTask task = new ActorTask { message = message, MessageSender = self };
+			self.Add(task);
+		}
+
+		public static Task<IResponse> Call(this ActorMessageSender self, IActorRequest request)
+		{
+			ActorTask task = new ActorTask { message = request, MessageSender = self, Tcs = new TaskCompletionSource<IResponse>() };
+			self.Add(task);
+			return task.Tcs.Task;
+		}
+
+		public static string DebugQueue(this ActorMessageSender self, Queue<ActorTask> tasks)
+		{
+			string s = "";
+			foreach (ActorTask task in tasks)
+			{
+				s += $" {task.message.GetType().Name}";
+			}
+
+			return s;
+		}
+	}
+}

+ 0 - 206
Server/Hotfix/Module/Actor/ActorProxySystem.cs

@@ -1,206 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Threading;
-using System.Threading.Tasks;
-using ETModel;
-
-namespace ETHotfix
-{
-	[ObjectSystem]
-	public class ActorProxyAwakeSystem : AwakeSystem<ActorProxy>
-	{
-		public override void Awake(ActorProxy self)
-		{
-			self.LastSendTime = TimeHelper.Now();
-			self.tcs = null;
-			self.FailTimes = 0;
-			self.MaxFailTimes = 5;
-			self.ActorId = 0;
-		}
-	}
-	
-	[ObjectSystem]
-	public class ActorProxyAwake2System : AwakeSystem<ActorProxy, long>
-	{
-		public override void Awake(ActorProxy self, long actorId)
-		{
-			self.LastSendTime = TimeHelper.Now();
-			self.tcs = null;
-			self.FailTimes = 0;
-			self.MaxFailTimes = 0;
-			self.ActorId = actorId;
-		}
-	}
-
-	[ObjectSystem]
-	public class ActorProxyStartSystem : StartSystem<ActorProxy>
-	{
-		public override async void Start(ActorProxy self)
-		{
-			if (self.ActorId == 0)
-			{
-				self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
-			}
-
-			self.Address = Game.Scene.GetComponent<StartConfigComponent>()
-					.Get(IdGenerater.GetAppIdFromId(self.ActorId))
-					.GetComponent<InnerConfig>().IPEndPoint;
-
-			self.UpdateAsync();
-		}
-	}
-	
-	[ObjectSystem]
-	public class ActorProxyDestroySystem : DestroySystem<ActorProxy>
-	{
-		public override void Destroy(ActorProxy self)
-		{
-			self.LastSendTime = 0;
-			self.Address = null;
-
-			while (self.WaitingTasks.Count > 0)
-			{
-				ActorTask actorTask = self.WaitingTasks.Dequeue();
-				actorTask.RunFail(ErrorCode.ERR_NotFoundActor);
-			}
-
-			self.ActorId = 0;
-			self.FailTimes = 0;
-			var t = self.tcs;
-			self.tcs = null;
-			t?.SetResult(new ActorTask());
-		}
-	}
-
-	public static class ActorProxyEx
-	{
-		private static void Add(this ActorProxy self, ActorTask task)
-		{
-			if (self.IsDisposed)
-			{
-				throw new Exception("ActorProxy Disposed! dont hold actorproxy");
-			}
-
-			self.WaitingTasks.Enqueue(task);
-			// failtimes > 0表示正在重试,这时候不能加到正在发送队列
-			if (self.FailTimes == 0)
-			{
-				self.AllowGet();
-			}
-		}
-
-		private static void AllowGet(this ActorProxy self)
-		{
-			if (self.tcs == null || self.WaitingTasks.Count <= 0)
-			{
-				return;
-			}
-
-			ActorTask task = self.WaitingTasks.Peek();
-
-			var t = self.tcs;
-			self.tcs = null;
-			t.SetResult(task);
-		}
-
-		private static Task<ActorTask> GetAsync(this ActorProxy self)
-		{
-			if (self.WaitingTasks.Count > 0)
-			{
-				ActorTask task = self.WaitingTasks.Peek();
-				return Task.FromResult(task);
-			}
-
-			self.tcs = new TaskCompletionSource<ActorTask>();
-			return self.tcs.Task;
-		}
-
-		public static async void UpdateAsync(this ActorProxy self)
-		{
-			while (true)
-			{
-				ActorTask actorTask = await self.GetAsync();
-				if (self.IsDisposed)
-				{
-					return;
-				}
-
-				try
-				{
-					await self.RunTask(actorTask);
-				}
-				catch (Exception e)
-				{
-					Log.Error(e);
-					return;
-				}
-			}
-		}
-
-		private static async Task RunTask(this ActorProxy self, ActorTask task)
-		{
-			try
-			{
-				IResponse response = await task.Run();
-
-				// 如果没找到Actor,重试
-				if (response.Error == ErrorCode.ERR_NotFoundActor)
-				{
-					++self.FailTimes;
-
-					// 失败10次则清空actor发送队列,返回失败
-					if (self.FailTimes > self.MaxFailTimes)
-					{
-						// 失败直接删除actorproxy
-						Log.Info($"actor send message fail, actorid: {self.Id}");
-						Game.Scene.GetComponent<ActorProxyComponent>().Remove(self.Id);
-						return;
-					}
-
-					// 等待1s再发送
-					await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
-					self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
-					self.Address = Game.Scene.GetComponent<StartConfigComponent>()
-							.Get(IdGenerater.GetAppIdFromId(self.ActorId))
-							.GetComponent<InnerConfig>().IPEndPoint;
-					self.AllowGet();
-					return;
-				}
-
-				// 发送成功
-				self.LastSendTime = TimeHelper.Now();
-				self.FailTimes = 0;
-
-				self.WaitingTasks.Dequeue();
-			}
-			catch (Exception e)
-			{
-				Log.Error(e);
-			}
-		}
-
-		public static void Send(this ActorProxy self, IActorMessage message)
-		{
-			ActorTask task = new ActorTask { message = message, proxy = self };
-			self.Add(task);
-		}
-
-		public static Task<IResponse> Call(this ActorProxy self, IActorRequest request)
-		{
-			ActorTask task = new ActorTask { message = request, proxy = self, Tcs = new TaskCompletionSource<IResponse>() };
-			self.Add(task);
-			return task.Tcs.Task;
-		}
-
-		public static string DebugQueue(this ActorProxy self, Queue<ActorTask> tasks)
-		{
-			string s = "";
-			foreach (ActorTask task in tasks)
-			{
-				s += $" {task.message.GetType().Name}";
-			}
-
-			return s;
-		}
-	}
-}

+ 15 - 15
Server/Hotfix/Module/Actor/ActorComponentSystem.cs → Server/Hotfix/Module/Actor/MailBoxComponentSystem.cs

@@ -6,9 +6,9 @@ using ETModel;
 namespace ETHotfix
 {
 	[ObjectSystem]
-	public class ActorComponentAwakeSystem : AwakeSystem<ActorComponent>
+	public class MailBoxComponentAwakeSystem : AwakeSystem<MailBoxComponent>
 	{
-		public override void Awake(ActorComponent self)
+		public override void Awake(MailBoxComponent self)
 		{
 			self.ActorType = ActorType.Common;
 			self.Queue.Clear();
@@ -16,9 +16,9 @@ namespace ETHotfix
 	}
 
 	[ObjectSystem]
-	public class ActorComponentAwake1System : AwakeSystem<ActorComponent, string>
+	public class MailBoxComponentAwake1System : AwakeSystem<MailBoxComponent, string>
 	{
-		public override void Awake(ActorComponent self, string actorType)
+		public override void Awake(MailBoxComponent self, string actorType)
 		{
 			self.ActorType = actorType;
 			self.Queue.Clear();
@@ -26,38 +26,38 @@ namespace ETHotfix
 	}
 
 	[ObjectSystem]
-	public class ActorComponentStartSystem : StartSystem<ActorComponent>
+	public class MailBoxComponentStartSystem : StartSystem<MailBoxComponent>
 	{
-		public override void Start(ActorComponent self)
+		public override void Start(MailBoxComponent self)
 		{
 			self.HandleAsync();
 		}
 	}
 
 	[ObjectSystem]
-	public class ActorComponentDestroySystem : DestroySystem<ActorComponent>
+	public class MailBoxComponentDestroySystem : DestroySystem<MailBoxComponent>
 	{
-		public override void Destroy(ActorComponent self)
+		public override void Destroy(MailBoxComponent self)
 		{
 		}
 	}
 
 	/// <summary>
-	/// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
+	/// 挂上这个组件表示该Entity是一个Actor, 接收的消息将会队列处理
 	/// </summary>
-	public static class ActorComponentEx
+	public static class MailBoxComponentEx
 	{
-		public static async Task AddLocation(this ActorComponent self)
+		public static async Task AddLocation(this MailBoxComponent self)
 		{
 			await Game.Scene.GetComponent<LocationProxyComponent>().Add(self.Entity.Id, self.Entity.InstanceId);
 		}
 
-		public static async Task RemoveLocation(this ActorComponent self)
+		public static async Task RemoveLocation(this MailBoxComponent self)
 		{
 			await Game.Scene.GetComponent<LocationProxyComponent>().Remove(self.Entity.Id);
 		}
 
-		public static void Add(this ActorComponent self, ActorMessageInfo info)
+		public static void Add(this MailBoxComponent self, ActorMessageInfo info)
 		{
 			self.Queue.Enqueue(info);
 
@@ -71,7 +71,7 @@ namespace ETHotfix
 			t.SetResult(self.Queue.Dequeue());
 		}
 
-		private static Task<ActorMessageInfo> GetAsync(this ActorComponent self)
+		private static Task<ActorMessageInfo> GetAsync(this MailBoxComponent self)
 		{
 			if (self.Queue.Count > 0)
 			{
@@ -82,7 +82,7 @@ namespace ETHotfix
 			return self.Tcs.Task;
 		}
 
-		public static async void HandleAsync(this ActorComponent self)
+		public static async void HandleAsync(this MailBoxComponent self)
 		{
 			ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent<ActorMessageDispatherComponent>();
 			while (true)

+ 1 - 1
Server/Hotfix/Module/FrameSync/C2G_EnterMapHandler.cs

@@ -16,7 +16,7 @@ namespace ETHotfix
 				// 在map服务器上创建战斗Unit
 				IPEndPoint mapAddress = Game.Scene.GetComponent<StartConfigComponent>().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.Id });
+				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;
 				response.Count = createUnit.Count;

+ 1 - 1
Server/Hotfix/Module/FrameSync/C2G_LoginGateHandler.cs

@@ -22,7 +22,7 @@ namespace ETHotfix
 				Player player = ComponentFactory.Create<Player, string>(account);
 				Game.Scene.GetComponent<PlayerComponent>().Add(player);
 				session.AddComponent<SessionPlayerComponent>().Player = player;
-				await session.AddComponent<ActorComponent, string>(ActorType.GateSession).AddLocation();
+				session.AddComponent<MailBoxComponent, string>(ActorType.GateSession);
 
 				response.PlayerId = player.Id;
 				reply(response);

+ 1 - 1
Server/Hotfix/Module/FrameSync/G2M_CreateUnitHandler.cs

@@ -13,7 +13,7 @@ namespace ETHotfix
 			{
 				Unit unit = ComponentFactory.Create<Unit>();
 
-				await unit.AddComponent<ActorComponent>().AddLocation();
+				await unit.AddComponent<MailBoxComponent>().AddLocation();
 				unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				response.UnitId = unit.Id;

+ 14 - 1
Server/Hotfix/Module/Message/InnerMessageDispatcher.cs

@@ -26,8 +26,21 @@ namespace ETHotfix
 					session.Reply(response);
 					return;
 				}
+
+				MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
+				if (mailBoxComponent == null)
+				{
+					ActorResponse response = new ActorResponse
+					{
+						Error = ErrorCode.ERR_NotFoundActor,
+						RpcId = iActorMessage.RpcId
+					};
+					session.Reply(response);
+					Log.Error($"actor没有挂载ActorComponent组件: {entity.GetType().Name} {entity.Id}");
+					return;
+				}
 				
-				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = iActorMessage });
+				mailBoxComponent.Add(new ActorMessageInfo() { Session = session, Message = iActorMessage });
 				return;
 			}
 			

+ 6 - 6
Server/Hotfix/Module/Message/OuterMessageDispatcher.cs

@@ -18,7 +18,7 @@ namespace ETHotfix
 				case IFrameMessage iFrameMessage: // 如果是帧消息,构造成OneFrameMessage发给对应的unit
 				{
 					long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
-					ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
+					ActorMessageSender actorMessageSender = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(unitId);
 
 					// 这里设置了帧消息的id,防止客户端伪造
 					iFrameMessage.Id = unitId;
@@ -28,16 +28,16 @@ namespace ETHotfix
 						Op = opcode,
 						AMessage = session.Network.MessagePacker.SerializeToByteArray(iFrameMessage)
 					};
-					actorProxy.Send(oneFrameMessage);
+					actorMessageSender.Send(oneFrameMessage);
 					return;
 				}
 				case IActorRequest iActorRequest: // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
 				{
 					long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
-					ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
+					ActorMessageSender actorMessageSender = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(unitId);
 
 					int rpcId = iActorRequest.RpcId; // 这里要保存客户端的rpcId
-					IResponse response = await actorProxy.Call(iActorRequest);
+					IResponse response = await actorMessageSender.Call(iActorRequest);
 					response.RpcId = rpcId;
 
 					session.Reply(response);
@@ -46,8 +46,8 @@ namespace ETHotfix
 				case IActorMessage iActorMessage: // gate session收到actor消息直接转发给actor自己去处理
 				{
 					long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
-					ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(unitId);
-					actorProxy.Send(iActorMessage);
+					ActorMessageSender actorMessageSender = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(unitId);
+					actorMessageSender.Send(iActorMessage);
 					return;
 				}
 			}

+ 1 - 1
Server/Model/Module/Actor/ActorProxy.cs → Server/Model/Module/Actor/ActorMessageSender.cs

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
 
 namespace ETModel
 {
-	public sealed class ActorProxy : ComponentWithId
+	public sealed class ActorMessageSender : ComponentWithId
 	{
 		// actor的地址
 		public IPEndPoint Address;

+ 60 - 0
Server/Model/Module/Actor/ActorMessageSenderComponent.cs

@@ -0,0 +1,60 @@
+using System.Collections.Generic;
+
+namespace ETModel
+{
+	public class ActorMessageSenderComponent: Component
+	{
+		public readonly Dictionary<long, ActorMessageSender> ActorMessageSenders = new Dictionary<long, ActorMessageSender>();
+
+		public override void Dispose()
+		{
+			if (this.IsDisposed)
+			{
+				return;
+			}
+			base.Dispose();
+			foreach (ActorMessageSender actorMessageSender in this.ActorMessageSenders.Values)
+			{
+				actorMessageSender.Dispose();
+			}
+			this.ActorMessageSenders.Clear();
+		}
+
+		public ActorMessageSender Get(long id)
+		{
+			if (this.ActorMessageSenders.TryGetValue(id, out ActorMessageSender actorMessageSender))
+			{
+				return actorMessageSender;
+			}
+			
+			actorMessageSender = ComponentFactory.CreateWithId<ActorMessageSender>(id);
+			actorMessageSender.Parent = this;
+			this.ActorMessageSenders[id] = actorMessageSender;
+			return actorMessageSender;
+		}
+		
+		public ActorMessageSender GetWithActorId(long actorId)
+		{
+			if (this.ActorMessageSenders.TryGetValue(actorId, out ActorMessageSender actorMessageSender))
+			{
+				return actorMessageSender;
+			}
+			
+			actorMessageSender = ComponentFactory.CreateWithId<ActorMessageSender, long>(actorId, actorId);
+			actorMessageSender.Parent = this;
+			this.ActorMessageSenders[actorId] = actorMessageSender;
+			return actorMessageSender;
+		}
+
+		public void Remove(long id)
+		{
+			ActorMessageSender actorMessageSender;
+			if (!this.ActorMessageSenders.TryGetValue(id, out actorMessageSender))
+			{
+				return;
+			}
+			this.ActorMessageSenders.Remove(id);
+			actorMessageSender.Dispose();
+		}
+	}
+}

+ 0 - 104
Server/Model/Module/Actor/ActorProxyComponent.cs

@@ -1,104 +0,0 @@
-using System.Collections.Generic;
-
-namespace ETModel
-{
-	[ObjectSystem]
-	public class ActorProxyComponentSystem : StartSystem<ActorProxyComponent>
-	{
-		// 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟
-		public override async void Start(ActorProxyComponent self)
-		{
-			List<long> timeoutActorProxyIds = new List<long>();
-
-			while (true)
-			{
-				await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10000);
-
-				if (self.IsDisposed)
-				{
-					return;
-				}
-
-				timeoutActorProxyIds.Clear();
-
-				long timeNow = TimeHelper.Now();
-				foreach (long id in self.ActorProxys.Keys)
-				{
-					ActorProxy actorProxy = self.Get(id);
-					if (actorProxy == null)
-					{
-						continue;
-					}
-
-					if (timeNow < actorProxy.LastSendTime + 60 * 1000)
-					{
-						continue;
-					}
-					
-					timeoutActorProxyIds.Add(id);
-				}
-
-				foreach (long id in timeoutActorProxyIds)
-				{
-					self.Remove(id);
-				}
-			}
-		}
-	}
-
-	public class ActorProxyComponent: Component
-	{
-		public readonly Dictionary<long, ActorProxy> ActorProxys = new Dictionary<long, ActorProxy>();
-
-		public override void Dispose()
-		{
-			if (this.IsDisposed)
-			{
-				return;
-			}
-			base.Dispose();
-			foreach (ActorProxy actorProxy in this.ActorProxys.Values)
-			{
-				actorProxy.Dispose();
-			}
-			this.ActorProxys.Clear();
-		}
-
-		public ActorProxy Get(long id)
-		{
-			if (this.ActorProxys.TryGetValue(id, out ActorProxy actorProxy))
-			{
-				return actorProxy;
-			}
-			
-			actorProxy = ComponentFactory.CreateWithId<ActorProxy>(id);
-			this.ActorProxys[id] = actorProxy;
-			return actorProxy;
-		}
-		
-		public ActorProxy GetWithActorId(long actorId)
-		{
-			if (this.ActorProxys.TryGetValue(actorId, out ActorProxy actorProxy))
-			{
-				return actorProxy;
-			}
-			
-			actorProxy = ComponentFactory.CreateWithId<ActorProxy>(actorId);
-			actorProxy.ActorId = actorId;
-			actorProxy.MaxFailTimes = 0;
-			this.ActorProxys[actorId] = actorProxy;
-			return actorProxy;
-		}
-
-		public void Remove(long id)
-		{
-			ActorProxy actorProxy;
-			if (!this.ActorProxys.TryGetValue(id, out actorProxy))
-			{
-				return;
-			}
-			this.ActorProxys.Remove(id);
-			actorProxy.Dispose();
-		}
-	}
-}

+ 3 - 3
Server/Model/Module/Actor/ActorTask.cs

@@ -4,7 +4,7 @@ namespace ETModel
 {
 	public struct ActorTask
 	{
-		public ActorProxy proxy;
+		public ActorMessageSender MessageSender;
 		
 		public IActorMessage message;
 		
@@ -12,9 +12,9 @@ namespace ETModel
 
 		public async Task<IResponse> Run()
 		{
-			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.proxy.Address);
+			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.MessageSender.Address);
 
-			this.message.ActorId = this.proxy.ActorId;
+			this.message.ActorId = this.MessageSender.ActorId;
 			IResponse response = await session.Call(message);
 
 			if (response.Error != ErrorCode.ERR_NotFoundActor)

+ 2 - 2
Server/Model/Module/Actor/ActorComponent.cs → Server/Model/Module/Actor/MailBoxComponent.cs

@@ -10,9 +10,9 @@ namespace ETModel
 	}
 
 	/// <summary>
-	/// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
+	/// 挂上这个组件表示该Entity是一个Actor,接收的消息将会队列处理
 	/// </summary>
-	public class ActorComponent: Component
+	public class MailBoxComponent: Component
 	{
 		public string ActorType;
 

+ 4 - 4
Server/Model/Module/FrameSync/UnitGateComponent.cs

@@ -11,16 +11,16 @@
 
 	public class UnitGateComponent : Component, ISerializeToEntity
 	{
-		public long GateSessionId;
+		public long GateSessionActorId;
 
 		public void Awake(long gateSessionId)
 		{
-			this.GateSessionId = gateSessionId;
+			this.GateSessionActorId = gateSessionId;
 		}
 
-		public ActorProxy GetActorProxy()
+		public ActorMessageSender GetActorMessageSender()
 		{
-			return Game.Scene.GetComponent<ActorProxyComponent>().Get(this.GateSessionId);
+			return Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(this.GateSessionActorId);
 		}
 	}
 }

BIN
Tools/Config/RiderSettings.jar


+ 1 - 0
Unity/Assets/Scripts/Module/Message/ErrorCode.cs

@@ -4,6 +4,7 @@ namespace ETModel
 	{
 		public const int ERR_Success = 0;
 		public const int ERR_NotFoundActor = 2;
+		public const int ERR_ActorNoActorComponent = 3;
 
 		public const int ERR_AccountOrPasswordError = 102;
 		public const int ERR_SessionActorError = 103;