Пример #1
0
        public async Task Handle(Session session, Entity entity, uint rpcId, ActorRequest message)
        {
            Message msg = message.AMessage as Message;

            if (msg == null)
            {
                Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof (Message).Name}");
                return;
            }
            E e = entity as E;

            if (e == null)
            {
                Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
                return;
            }

            await this.Run(e, msg);

            // 等回调回来,session可以已经断开了,所以需要判断session id是否为0
            if (session.IsDisposed)
            {
                return;
            }
            ActorResponse response = new ActorResponse();

            session.Reply(rpcId, response);
        }
Пример #2
0
 public async Task Handle(Session session, Entity entity, uint rpcId, ActorRequest message)
 {
     try
     {
         Request request = message.AMessage as Request;
         if (request == null)
         {
             Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof (Request).Name}");
             return;
         }
         E e = entity as E;
         if (e == null)
         {
             Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
             return;
         }
         await this.Run(e, request, response =>
         {
             // 等回调回来,session可以已经断开了,所以需要判断session id是否为0
             if (session.IsDisposed)
             {
                 return;
             }
             ActorResponse actorResponse = new ActorResponse
             {
                 AMessage = response
             };
             session.Reply(rpcId, actorResponse);
         });
     }
     catch (Exception e)
     {
         throw new Exception($"解释消息失败: {message.GetType().FullName}", e);
     }
 }
Пример #3
0
        public async Task <IResponse> Run()
        {
            Session             session             = Game.Scene.GetComponent <NetInnerComponent>().Get(this.proxy.Address);
            OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();

            ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType());

            byte[] requestBytes = session.Network.MessagePacker.SerializeToByteArray(message);

            ActorRequest actorRequest = new ActorRequest()
            {
                Id = this.proxy.Id, Op = opcode, AMessage = requestBytes
            };

            ActorResponse actorResponse = (ActorResponse)await session.Call(actorRequest, this.proxy.CancellationTokenSource.Token);

            if (actorResponse.Error != ErrorCode.ERR_NotFoundActor)
            {
                if (this.Tcs != null)
                {
                    Type      type     = opcodeTypeComponent.GetType(actorResponse.Op);
                    IResponse response = (IResponse)session.Network.MessagePacker.DeserializeFrom(type, actorResponse.AMessage);
                    this.Tcs?.SetResult(response);
                }
            }
            return(actorResponse);
        }
Пример #4
0
        public async Task Handle(Session session, Entity entity, ActorRequest actorRequest, IMessage message)
        {
            if (!this.handlers.TryGetValue(message.GetType(), out IMActorHandler handler))
            {
                throw new Exception($"not found message handler: {MongoHelper.ToJson(message)}");
            }

            await handler.Handle(session, entity, actorRequest, message);
        }
Пример #5
0
        public async Task <IResponse> Run()
        {
            ActorRequest request = new ActorRequest()
            {
                Id = this.proxy.Id, AMessage = this.message
            };
            ActorResponse response = (ActorResponse)await this.proxy.RealCall(request, this.proxy.CancellationTokenSource.Token);

            if (response.Error != ErrorCode.ERR_NotFoundActor)
            {
                this.Tcs?.SetResult((IResponse)response.AMessage);
            }
            return(response);
        }
        public async Task Handle(Session session, Entity entity, ActorRequest actorRequest, object message)
        {
            try
            {
                Request request = message as Request;
                if (request == null)
                {
                    Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof (Request).Name}");
                    return;
                }
                E e = entity as E;
                if (e == null)
                {
                    Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
                    return;
                }

                int rpcId = request.RpcId;
                await this.Run(e, request, response =>
                {
                    // 等回调回来,session可以已经断开了,所以需要判断session id是否为0
                    if (session.IsDisposed)
                    {
                        return;
                    }

                    response.RpcId = rpcId;

                    OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();
                    ushort opcode         = opcodeTypeComponent.GetOpcode(response.GetType());
                    byte[] repsponseBytes = session.Network.MessagePacker.SerializeToByteArray(response);

                    ActorResponse actorResponse = new ActorResponse
                    {
                        Flag     = 0x01,
                        Op       = opcode,
                        AMessage = repsponseBytes
                    };
                    actorResponse.RpcId = actorRequest.RpcId;
                    session.Reply(actorResponse);
                });
            }
            catch (Exception e)
            {
                throw new Exception($"解释消息失败: {message.GetType().FullName}", e);
            }
        }
Пример #7
0
        public async Task <IResponse> RealCall(ActorRequest request, CancellationToken cancellationToken)
        {
            try
            {
                //Log.Debug($"realcall {MongoHelper.ToJson(request)} {this.Address}");
                request.Id = this.Id;
                Session   session  = Game.Scene.GetComponent <NetInnerComponent>().Get(this.Address);
                IResponse response = await session.Call(request, cancellationToken);

                return(response);
            }
            catch (RpcException e)
            {
                Log.Error($"{this.Address} {e}");
                throw;
            }
        }