Beispiel #1
0
        public static ETTask <IActorResponse> Call(this ActorMessageSenderComponent self, long actorId, IActorRequest message, bool exception = true)
        {
            if (actorId == 0)
            {
                throw new Exception($"actor id is 0: {MongoHelper.ToJson(message)}");
            }

            var tcs = new ETTaskCompletionSource <IActorResponse>();

            int              process          = IdGenerater.GetProcess(actorId);
            string           address          = StartProcessConfigCategory.Instance.Get(process).InnerAddress;
            Session          session          = NetInnerComponent.Instance.Get(address);
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);

            instanceIdStruct.Process = IdGenerater.Process;
            message.ActorId          = instanceIdStruct.ToLong();
            message.RpcId            = ++self.RpcId;

            self.requestCallback.Add(message.RpcId, new ActorMessageSender((response) =>
            {
                if (exception && ErrorCode.IsRpcNeedThrowException(response.Error))
                {
                    tcs.SetException(new Exception($"Rpc error: {MongoHelper.ToJson(response)}"));
                    return;
                }

                tcs.SetResult(response);
            }));
            session.Send(message);

            return(tcs.Task);
        }
Beispiel #2
0
        public static async ETVoid HandleIActorMessage(Session session, IActorMessage iActorMessage)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(iActorMessage.ActorId);
            int replyId = instanceIdStruct.Process;

            instanceIdStruct.Process = IdGenerater.Process;
            iActorMessage.ActorId    = instanceIdStruct.ToLong();

            Entity entity = Game.EventSystem.Get(iActorMessage.ActorId);

            if (entity == null)
            {
                Log.Error($"not found actor: {MongoHelper.ToJson(iActorMessage)}");
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorMessage}");
                return;
            }

            Session ss = NetInnerComponent.Instance.Get(replyId);
            await mailBoxComponent.Handle(ss, iActorMessage);
        }
Beispiel #3
0
        public ProcessActorId(long actorId)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);

            this.Process             = instanceIdStruct.Process;
            instanceIdStruct.Process = Game.Options.Process;
            this.ActorId             = instanceIdStruct.ToLong();
        }
Beispiel #4
0
        public static async ETVoid HandleIActorRequest(Session session, IActorRequest iActorRequest)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(iActorRequest.ActorId);
            int replyId = instanceIdStruct.Process;

            instanceIdStruct.Process = IdGenerater.Process;
            iActorRequest.ActorId    = instanceIdStruct.ToLong();

            string  address = StartProcessConfigCategory.Instance.Get(replyId).InnerAddress;
            Session ss      = NetInnerComponent.Instance.Get(address);
            Entity  entity  = Game.EventSystem.Get(iActorRequest.ActorId);

            if (entity == null)
            {
                Log.Warning($"not found actor: {MongoHelper.ToJson(iActorRequest)}");
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_NotFoundActor,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorRequest}");
                return;
            }

            await mailBoxComponent.Handle(ss, iActorRequest);
        }
Beispiel #5
0
        public void Handle(Session session, MemoryStream memoryStream)
        {
            ushort opcode = 0;

            try
            {
                long actorId = BitConverter.ToInt64(memoryStream.GetBuffer(), Packet.ActorIdIndex);
                opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);
                Type   type    = null;
                object message = null;

                // 内网收到外网消息,有可能是gateUnit消息,还有可能是gate广播消息
                if (OpcodeTypeComponent.Instance.IsOutrActorMessage(opcode))
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = Game.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();


                    Entity entity = Game.EventSystem.Get(realActorId);
                    if (entity == null)
                    {
                        type    = OpcodeTypeComponent.Instance.GetType(opcode);
                        message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
                        Log.Error($"not found actor: {session.DomainScene().Name}  {opcode} {realActorId} {message}");
                        return;
                    }

                    if (entity is Session gateSession)
                    {
                        // 发送给客户端
                        memoryStream.Seek(Packet.OpcodeIndex, SeekOrigin.Begin);
                        gateSession.Send(0, memoryStream);
                        return;
                    }
                }

                type    = OpcodeTypeComponent.Instance.GetType(opcode);
                message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);

                if (message is IResponse iResponse && !(message is IActorResponse))
                {
                    session.OnRead(opcode, iResponse);
                    return;
                }

                OpcodeHelper.LogMsg(session.DomainZone(), opcode, message);

                // 收到actor消息,放入actor队列
                switch (message)
                {
                case IActorRequest iActorRequest:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    int fromProcess = instanceIdStruct.Process;
                    instanceIdStruct.Process = Game.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();

                    void Reply(IActorResponse response)
                    {
                        Session replySession = NetInnerComponent.Instance.Get(fromProcess);

                        // 发回真实的actorId 做查问题使用
                        replySession.Send(realActorId, response);
                    }

                    InnerMessageDispatcherHelper.HandleIActorRequest(opcode, realActorId, iActorRequest, Reply);
                    return;
                }

                case IActorResponse iActorResponse:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = Game.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();
                    InnerMessageDispatcherHelper.HandleIActorResponse(opcode, realActorId, iActorResponse);
                    return;
                }

                case IActorMessage iactorMessage:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = Game.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();
                    InnerMessageDispatcherHelper.HandleIActorMessage(opcode, realActorId, iactorMessage);
                    return;
                }

                default:
                {
                    MessageDispatcherComponent.Instance.Handle(session, opcode, message);
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Log.Error($"InnerMessageDispatcher error: {opcode}\n{e}");
            }
        }