public static void Run(ActorMessageSender self, IActorResponse response) { if (response.Error == ErrorCore.ERR_ActorTimeout) { self.Tcs.SetException(new Exception($"Rpc error: request, 注意Actor消息超时,请注意查看是否死锁或者没有reply: actorId: {self.ActorId} {self.MemoryStream.ToActorMessage()}, response: {response}")); return; } if (self.NeedException && ErrorCore.IsRpcNeedThrowException(response.Error)) { self.Tcs.SetException(new Exception($"Rpc error: actorId: {self.ActorId} request: {self.MemoryStream.ToActorMessage()}, response: {response}")); return; } self.Tcs.SetResult(response); }
public void OnRead(ushort opcode, IResponse response) { OpcodeHelper.LogMsg(this.DomainZone(), opcode, response); if (!this.requestCallbacks.TryGetValue(response.RpcId, out var action)) { return; } this.requestCallbacks.Remove(response.RpcId); if (ErrorCore.IsRpcNeedThrowException(response.Error)) { action.Tcs.SetException(new Exception($"Rpc error, request: {action.Request} response: {response}")); return; } action.Tcs.SetResult(response); }
private static async ETTask <IActorResponse> CallInner(this ActorLocationSenderComponent self, ActorLocationSender actorLocationSender, int rpcId, MemoryStream memoryStream) { int failTimes = 0; long instanceId = actorLocationSender.InstanceId; actorLocationSender.LastSendOrRecvTime = TimeHelper.ServerNow(); while (true) { if (actorLocationSender.ActorId == 0) { actorLocationSender.ActorId = await LocationProxyComponent.Instance.Get(actorLocationSender.Id); if (actorLocationSender.InstanceId != instanceId) { throw new RpcException(ErrorCore.ERR_ActorLocationSenderTimeout2, $"{memoryStream.ToActorMessage()}"); } } if (actorLocationSender.ActorId == 0) { IActorRequest iActorRequest = (IActorRequest)memoryStream.ToActorMessage(); return(ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor)); } IActorResponse response = await ActorMessageSenderComponent.Instance.Call(actorLocationSender.ActorId, rpcId, memoryStream, false); if (actorLocationSender.InstanceId != instanceId) { throw new RpcException(ErrorCore.ERR_ActorLocationSenderTimeout3, $"{memoryStream.ToActorMessage()}"); } switch (response.Error) { case ErrorCore.ERR_NotFoundActor: { // 如果没找到Actor,重试 ++failTimes; if (failTimes > 20) { Log.Debug($"actor send message fail, actorid: {actorLocationSender.Id}"); actorLocationSender.Error = ErrorCore.ERR_NotFoundActor; // 这里不能删除actor,要让后面等待发送的消息也返回ERR_NotFoundActor,直到超时删除 return(response); } // 等待0.5s再发送 await TimerComponent.Instance.WaitAsync(500); if (actorLocationSender.InstanceId != instanceId) { throw new RpcException(ErrorCore.ERR_ActorLocationSenderTimeout4, $"{memoryStream.ToActorMessage()}"); } actorLocationSender.ActorId = 0; continue; } case ErrorCore.ERR_ActorTimeout: { throw new RpcException(response.Error, $"{memoryStream.ToActorMessage()}"); } } if (ErrorCore.IsRpcNeedThrowException(response.Error)) { throw new RpcException(response.Error, $"Message: {response.Message} Request: {memoryStream.ToActorMessage()}"); } return(response); } }