提交 58714eb1 编写于 作者: T tanghai

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

上级 af9e11aa
......@@ -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;
}
......
......@@ -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;
......
{ "_t" : "GlobalProto", "AssetBundleServerUrl" : "http://172.16.20.27:8080/", "Address" : "172.16.20.27:10002" }
\ No newline at end of file
{ "_t" : "GlobalProto", "AssetBundleServerUrl" : "http://127.0.0.1:8080/", "Address" : "127.0.0.1:10002" }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册