|
|
@@ -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;
|
|
|
}
|