コード例 #1
0
        public async ETVoid DispatchAsync(Session session, ushort opcode, object message)
        {
            // 根据消息接口判断是不是Actor消息,不同的接口做不同的处理
            switch (message)
            {
            case IActorLocationRequest actorLocationRequest:                     // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            {
                long actorId = DispatchActorId(session, opcode);
                if (actorId == 0L)
                {
                    var retOpcode = MessageHelper.GetResponseOpcode(opcode);
                    var instance  = Game.Scene.GetComponent <OpcodeTypeComponent>().GetInstance(retOpcode) as IResponse;
                    instance.RpcId = actorLocationRequest.RpcId;
                    instance.Error = ErrorCode.ERR_NotFoundActor;
                    session.Reply(instance);
                    return;
                }
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(actorId);

                int       rpcId      = actorLocationRequest.RpcId;              // 这里要保存客户端的rpcId
                long      instanceId = session.InstanceId;
                IResponse response   = await actorLocationSender.Call(actorLocationRequest);

                response.RpcId = rpcId;

                // session可能已经断开了,所以这里需要判断
                if (session.InstanceId == instanceId)
                {
                    session.Reply(response);
                }

                break;
            }

            case IActorLocationMessage actorLocationMessage:
            {
                long actorId = DispatchActorId(session, opcode);
                if (actorId == 0L)
                {
                    return;
                }
                ActorLocationSender actorLocationSender = Game.Scene.GetComponent <ActorLocationSenderComponent>().Get(actorId);
                actorLocationSender.Send(actorLocationMessage);
                break;
            }

            case IActorRequest actorRequest:                      // 分发IActorRequest消息,目前没有用到,需要的自己添加
            {
                break;
            }

            case IActorMessage actorMessage:                      // 分发IActorMessage消息,目前没有用到,需要的自己添加
            {
                Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
                break;
            }

            default:
            {
                // 非Actor消息
                Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
                break;
            }
            }
        }