Пример #1
0
        public static void Broadcast(Unit unit, IActorMessage message)
        {
            Dictionary <long, AOIEntity> dict = unit.GetBeSeePlayers();

            (ushort _, MemoryStream memoryStream) = MessageSerializeHelper.MessageToStream(message);
            foreach (AOIEntity u in dict.Values)
            {
                ActorMessageSenderComponent.Instance.Send(u.Unit.GetComponent <UnitGateComponent>().GateSessionActorId, memoryStream);
            }
        }
        public static async ETTask <IActorResponse> Call(
            this ActorMessageSenderComponent self,
            long actorId,
            IActorRequest request,
            bool needException = true
            )
        {
            request.RpcId = self.GetRpcId();

            if (actorId == 0)
            {
                throw new Exception($"actor id is 0: {request}");
            }

            (ushort _, MemoryStream stream) = MessageSerializeHelper.MessageToStream(request);

            return(await self.Call(actorId, request.RpcId, stream, needException));
        }
        public static async ETTask <IActorResponse> Call(this ActorLocationSenderComponent self, long entityId, IActorRequest iActorRequest)
        {
            ActorLocationSender actorLocationSender = self.GetOrCreate(entityId);

            // 先序列化好
            int rpcId = ActorMessageSenderComponent.Instance.GetRpcId();

            iActorRequest.RpcId             = rpcId;
            (ushort _, MemoryStream stream) = MessageSerializeHelper.MessageToStream(iActorRequest);

            long actorLocationSenderInstanceId = actorLocationSender.InstanceId;

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ActorLocationSender, entityId))
            {
                if (actorLocationSender.InstanceId != actorLocationSenderInstanceId)
                {
                    throw new RpcException(ErrorCore.ERR_ActorTimeout, $"{stream.ToActorMessage()}");
                }

                // 队列中没处理的消息返回跟上个消息一样的报错
                if (actorLocationSender.Error == ErrorCore.ERR_NotFoundActor)
                {
                    return(ActorHelper.CreateResponse(iActorRequest, actorLocationSender.Error));
                }

                try
                {
                    return(await self.CallInner(actorLocationSender, rpcId, stream));
                }
                catch (RpcException)
                {
                    self.Remove(actorLocationSender.Id);
                    throw;
                }
                catch (Exception e)
                {
                    self.Remove(actorLocationSender.Id);
                    throw new Exception($"{stream.ToActorMessage()}", e);
                }
            }
        }