提交 b3f92f73 编写于 作者: T tanghai

ActorProxyComponent增加了一个GetWithActorId方法,假如已经知道了actorId(即actor的instanceId),就可以...

ActorProxyComponent增加了一个GetWithActorId方法,假如已经知道了actorId(即actor的instanceId),就可以使用此方法,可以不用向location server的请求
上级 3858c529
...@@ -11,7 +11,24 @@ namespace ETHotfix ...@@ -11,7 +11,24 @@ namespace ETHotfix
{ {
public override void Awake(ActorProxy self) public override void Awake(ActorProxy self)
{ {
self.Awake(); 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;
} }
} }
...@@ -20,9 +37,13 @@ namespace ETHotfix ...@@ -20,9 +37,13 @@ namespace ETHotfix
{ {
public override async void Start(ActorProxy self) public override async void Start(ActorProxy self)
{ {
self.ActorInstanceId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id); if (self.ActorId == 0)
{
self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
}
self.Address = Game.Scene.GetComponent<StartConfigComponent>() self.Address = Game.Scene.GetComponent<StartConfigComponent>()
.Get(IdGenerater.GetAppIdFromId(self.ActorInstanceId)) .Get(IdGenerater.GetAppIdFromId(self.ActorId))
.GetComponent<InnerConfig>().IPEndPoint; .GetComponent<InnerConfig>().IPEndPoint;
self.UpdateAsync(); self.UpdateAsync();
...@@ -43,7 +64,8 @@ namespace ETHotfix ...@@ -43,7 +64,8 @@ namespace ETHotfix
actorTask.RunFail(ErrorCode.ERR_NotFoundActor); actorTask.RunFail(ErrorCode.ERR_NotFoundActor);
} }
self.failTimes = 0; self.ActorId = 0;
self.FailTimes = 0;
var t = self.tcs; var t = self.tcs;
self.tcs = null; self.tcs = null;
t?.SetResult(new ActorTask()); t?.SetResult(new ActorTask());
...@@ -52,14 +74,6 @@ namespace ETHotfix ...@@ -52,14 +74,6 @@ namespace ETHotfix
public static class ActorProxyEx public static class ActorProxyEx
{ {
public static void Awake(this ActorProxy self)
{
self.LastSendTime = TimeHelper.Now();
self.tcs = null;
self.failTimes = 0;
self.CancellationTokenSource = new CancellationTokenSource();
}
private static void Add(this ActorProxy self, ActorTask task) private static void Add(this ActorProxy self, ActorTask task)
{ {
if (self.IsDisposed) if (self.IsDisposed)
...@@ -69,7 +83,7 @@ namespace ETHotfix ...@@ -69,7 +83,7 @@ namespace ETHotfix
self.WaitingTasks.Enqueue(task); self.WaitingTasks.Enqueue(task);
// failtimes > 0表示正在重试,这时候不能加到正在发送队列 // failtimes > 0表示正在重试,这时候不能加到正在发送队列
if (self.failTimes == 0) if (self.FailTimes == 0)
{ {
self.AllowGet(); self.AllowGet();
} }
...@@ -132,11 +146,10 @@ namespace ETHotfix ...@@ -132,11 +146,10 @@ namespace ETHotfix
// 如果没找到Actor,重试 // 如果没找到Actor,重试
if (response.Error == ErrorCode.ERR_NotFoundActor) if (response.Error == ErrorCode.ERR_NotFoundActor)
{ {
self.CancellationTokenSource.Cancel(); ++self.FailTimes;
++self.failTimes;
// 失败10次则清空actor发送队列,返回失败 // 失败10次则清空actor发送队列,返回失败
if (self.failTimes > 10) if (self.FailTimes > self.MaxFailTimes)
{ {
// 失败直接删除actorproxy // 失败直接删除actorproxy
Log.Info($"actor send message fail, actorid: {self.Id}"); Log.Info($"actor send message fail, actorid: {self.Id}");
...@@ -146,18 +159,17 @@ namespace ETHotfix ...@@ -146,18 +159,17 @@ namespace ETHotfix
// 等待1s再发送 // 等待1s再发送
await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000); await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
self.ActorInstanceId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id); self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
self.Address = Game.Scene.GetComponent<StartConfigComponent>() self.Address = Game.Scene.GetComponent<StartConfigComponent>()
.Get(IdGenerater.GetAppIdFromId(self.ActorInstanceId)) .Get(IdGenerater.GetAppIdFromId(self.ActorId))
.GetComponent<InnerConfig>().IPEndPoint; .GetComponent<InnerConfig>().IPEndPoint;
self.CancellationTokenSource = new CancellationTokenSource();
self.AllowGet(); self.AllowGet();
return; return;
} }
// 发送成功 // 发送成功
self.LastSendTime = TimeHelper.Now(); self.LastSendTime = TimeHelper.Now();
self.failTimes = 0; self.FailTimes = 0;
self.WaitingTasks.Dequeue(); self.WaitingTasks.Dequeue();
} }
......
...@@ -10,7 +10,7 @@ namespace ETModel ...@@ -10,7 +10,7 @@ namespace ETModel
// actor的地址 // actor的地址
public IPEndPoint Address; public IPEndPoint Address;
public long ActorInstanceId; public long ActorId;
// 还没发送的消息 // 还没发送的消息
public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>(); public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>();
...@@ -20,8 +20,8 @@ namespace ETModel ...@@ -20,8 +20,8 @@ namespace ETModel
public TaskCompletionSource<ActorTask> tcs; public TaskCompletionSource<ActorTask> tcs;
public CancellationTokenSource CancellationTokenSource; public int FailTimes;
public int failTimes; public int MaxFailTimes;
} }
} }
\ No newline at end of file
...@@ -75,6 +75,20 @@ namespace ETModel ...@@ -75,6 +75,20 @@ namespace ETModel
this.ActorProxys[id] = actorProxy; this.ActorProxys[id] = actorProxy;
return 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) public void Remove(long id)
{ {
......
...@@ -14,8 +14,8 @@ namespace ETModel ...@@ -14,8 +14,8 @@ namespace ETModel
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.proxy.Address); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.proxy.Address);
this.message.ActorId = this.proxy.ActorInstanceId; this.message.ActorId = this.proxy.ActorId;
IResponse response = await session.Call(message, this.proxy.CancellationTokenSource.Token); IResponse response = await session.Call(message);
if (response.Error != ErrorCode.ERR_NotFoundActor) if (response.Error != ErrorCode.ERR_NotFoundActor)
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册