Преглед на файлове

增加actorproxy回收机制,每分钟扫描过期5分钟的actorproxy

tanghai преди 8 години
родител
ревизия
58714eb1ef
променени са 3 файла, в които са добавени 57 реда и са изтрити 5 реда
  1. 50 4
      Server/Model/Component/ActorProxyComponent.cs
  2. 6 0
      Server/Model/Entity/ActorProxy.cs
  3. 1 1
      Unity/Assets/Res/Config/GlobalProto.txt

+ 50 - 4
Server/Model/Component/ActorProxyComponent.cs

@@ -2,26 +2,72 @@
 
 namespace Model
 {
+	[ObjectEvent]
+	public class ActorProxyComponentEvent : ObjectEvent<ActorProxyComponent>, IStart
+	{
+		// 每分钟扫描一次过期的actorproxy进行回收,过期时间是5分钟
+		public async void Start()
+		{
+			ActorProxyComponent self = this.Get();
+
+			List<long> timeoutActorProxyIds = new List<long>();
+
+			while (true)
+			{
+				await Game.Scene.GetComponent<TimerComponent>().WaitAsync(60000);
+
+				if (self.Id == 0)
+				{
+					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 + 5 * 60000)
+					{
+						continue;
+					}
+					
+					timeoutActorProxyIds.Add(id);
+				}
+
+				foreach (long id in timeoutActorProxyIds)
+				{
+					self.Remove(id);
+				}
+			}
+		}
+	}
+
 	public class ActorProxyComponent: Component
 	{
-		private readonly Dictionary<long, ActorProxy> dictionary = new Dictionary<long, ActorProxy>();
+		public readonly Dictionary<long, ActorProxy> ActorProxys = new Dictionary<long, ActorProxy>();
 
 		public ActorProxy Get(long id)
 		{
-			if (this.dictionary.TryGetValue(id, out ActorProxy actorProxy))
+			if (this.ActorProxys.TryGetValue(id, out ActorProxy actorProxy))
 			{
 				return actorProxy;
 			}
 			
 			actorProxy = EntityFactory.CreateWithId<ActorProxy>(id);
-			this.dictionary[id] = actorProxy;
+			this.ActorProxys[id] = actorProxy;
 			return actorProxy;
 		}
 
 		public void Remove(long id)
 		{
 			ActorProxy actorProxy;
-			if (!this.dictionary.TryGetValue(id, out actorProxy))
+			if (!this.ActorProxys.TryGetValue(id, out actorProxy))
 			{
 				return;
 			}

+ 6 - 0
Server/Model/Entity/ActorProxy.cs

@@ -105,6 +105,9 @@ namespace Model
 		// 最大窗口
 		public const int MaxWindowSize = 1;
 
+		// 最近发送消息的时间
+		public long LastSendTime;
+
 		private TaskCompletionSource<ActorTask> tcs;
 
 		public CancellationTokenSource CancellationTokenSource;
@@ -113,6 +116,7 @@ namespace Model
 		
 		public void Awake()
 		{
+			this.LastSendTime = TimeHelper.Now();
 			this.RunningTasks = new EQueue<ActorTask>();
 			this.WaitingTasks = new EQueue<ActorTask>();
 			this.WindowSize = 1;
@@ -254,12 +258,14 @@ namespace Model
 
 		public void Send(AMessage message)
 		{
+			this.LastSendTime = TimeHelper.Now();
 			ActorMessageTask task = new ActorMessageTask(this, message);
 			this.Add(task);
 		}
 
 		public Task<Response> Call<Response>(ARequest request)where Response : AResponse
 		{
+			this.LastSendTime = TimeHelper.Now();
 			ActorRpcTask<Response> task = new ActorRpcTask<Response>(this, request);
 			this.Add(task);
 			return task.Tcs.Task;

+ 1 - 1
Unity/Assets/Res/Config/GlobalProto.txt

@@ -1 +1 @@
-{ "_t" : "GlobalProto", "AssetBundleServerUrl" : "http://172.16.20.27:8080/", "Address" : "172.16.20.27:10002" }
+{ "_t" : "GlobalProto", "AssetBundleServerUrl" : "http://127.0.0.1:8080/", "Address" : "127.0.0.1:10002" }