服务端回的RPC消息需要继承这个抽象类
Inheritance: AMessage
Beispiel #1
0
        private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
        {
            Type   messageType = this.network.Owner.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            object message     = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);

            //Log.Debug($"recv: {JsonHelper.ToJson(message)}");

            AResponse response = message as AResponse;

            if (response != null)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                Action <object> action;
                if (!this.requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);
                action(message);
                return;
            }

            this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
        }
Beispiel #2
0
        /// <summary>
        /// Rpc调用
        /// </summary>
        public Task <AResponse> Call(ARequest request, bool isHotfix, CancellationToken cancellationToken)
        {
            request.RpcId = ++RpcId;
            this.SendMessage(request);

            var tcs = new TaskCompletionSource <AResponse>();

            this.requestCallback[RpcId] = (message) =>
            {
                try
                {
                    AResponse response = (AResponse)message;
                    if (response.Error > 100)
                    {
                        tcs.SetException(new RpcException(response.Error, response.Message));
                        return;
                    }
                    //Log.Debug($"recv: {MongoHelper.ToJson(response)}");
                    tcs.SetResult(response);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception($"Rpc Error: {message.GetType().FullName}", e));
                }
            };

            cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });

            return(tcs.Task);
        }
        public async void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
        {
            // gate session收到actor消息直接转发给actor自己去处理
            if (message is AActorMessage)
            {
                long       unitId     = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorProxy actorProxy = Game.Scene.GetComponent <ActorProxyComponent>().Get(unitId);
                actorProxy.Send(message);
                return;
            }

            // gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
            if (message is AActorRequest aActorRequest)
            {
                long       unitId     = session.GetComponent <SessionPlayerComponent>().Player.UnitId;
                ActorProxy actorProxy = Game.Scene.GetComponent <ActorProxyComponent>().Get(unitId);
                uint       rpcId      = aActorRequest.RpcId;
                AResponse  response   = await actorProxy.Call <AResponse>(aActorRequest);

                response.RpcId = rpcId;
                session.Reply(response);
                return;
            }

            if (message != null)
            {
                Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, message);
                return;
            }

            throw new Exception($"message type error: {message.GetType().FullName}");
        }
Beispiel #4
0
        private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
        {
            Type   messageType = this.network.Owner.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            object message     = messagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);


            // 普通消息或者是Rpc请求消息
            if (message is AMessage || message is ARequest)
            {
                MessageInfo messageInfo = new MessageInfo(opcode, message);
                Game.Scene.GetComponent <CrossComponent>().Run(CrossIdType.MessageDeserializeFinish, messageInfo);
                return;
            }

            AResponse response = message as AResponse;

            Log.Debug($"aaaaaaaaaaaaaaaaaaaaaaaaaaa {JsonHelper.ToJson(response)}");
            if (response != null)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                Action <object> action;
                if (!this.requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);
                action(message);
                return;
            }

            throw new Exception($"message type error: {message.GetType().FullName}");
        }
Beispiel #5
0
        private async void RunTask(ActorTask task)
        {
            try
            {
                AResponse response = await task.Run();

                // 如果没找到Actor,发送窗口减少为1,重试
                if (response.Error == ErrorCode.ERR_NotFoundActor)
                {
                    this.CancellationTokenSource.Cancel();
                    this.WindowSize = 1;
                    ++this.failTimes;

                    while (this.WaitingTasks.Count > 0)
                    {
                        ActorTask actorTask = this.WaitingTasks.Dequeue();
                        this.RunningTasks.Enqueue(actorTask);
                    }
                    ObjectHelper.Swap(ref this.RunningTasks, ref this.WaitingTasks);

                    // 失败3次则清空actor发送队列,返回失败
                    if (this.failTimes > 3)
                    {
                        while (this.WaitingTasks.Count > 0)
                        {
                            ActorTask actorTask = this.WaitingTasks.Dequeue();
                            actorTask.RunFail(response.Error);
                        }

                        // 失败直接删除actorproxy
                        Game.Scene.GetComponent <ActorProxyComponent>().Remove(this.Id);
                        return;
                    }
                    // 等待一会再发送
                    await Game.Scene.GetComponent <TimerComponent>().WaitAsync(this.failTimes * 500);

                    int appId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(this.Id);

                    this.Address = Game.Scene.GetComponent <StartConfigComponent>().Get(appId).GetComponent <InnerConfig>().IPEndPoint;
                    this.CancellationTokenSource = new CancellationTokenSource();
                    this.AllowGet();
                    return;
                }

                // 发送成功
                this.LastSendTime = TimeHelper.Now();
                this.failTimes    = 0;
                if (this.WindowSize < MaxWindowSize)
                {
                    ++this.WindowSize;
                }
                this.Remove();
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
Beispiel #6
0
        /// <summary>
        /// Rpc调用
        /// </summary>
        public void CallWithAction(ARequest request, Action <AResponse> action)
        {
            request.RpcId = ++RpcId;
            this.SendMessage(request);

            this.requestCallback[RpcId] = (message) =>
            {
                try
                {
                    AResponse response = (AResponse)message;
                    action(response);
                }
                catch (Exception e)
                {
                    Log.Error(e.ToString());
                }
            };
        }
Beispiel #7
0
        private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
        {
            object message;
            Opcode op;

            try
            {
                op = (Opcode)opcode;
                Type messageType = this.network.Entity.GetComponent <OpcodeTypeComponent>().GetType(op);
                message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
            }
            catch (Exception e)
            {
                Log.Error($"message deserialize error, ip: {this.RemoteAddress} {opcode} {e}");
                this.network.Remove(this.Id);
                return;
            }

            //Log.Debug($"recv: {MongoHelper.ToJson(message)}");

            AResponse response = message as AResponse;

            if (response != null)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                Action <object> action;
                if (!this.requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);
                action(message);
                return;
            }

            this.network.MessageDispatcher.Dispatch(this, op, offset, messageBytes, (AMessage)message);
        }
Beispiel #8
0
        private void RunDecompressedBytes(ushort nOpcode, byte[] rMessageBytes, int nOffset)
        {
            Type   rMessageType = NetworkOpcodeType.Instance.GetType(nOpcode);
            object message      = this.mNetwork.MessagePacker.DeserializeFrom(rMessageType, rMessageBytes, nOffset, rMessageBytes.Length - nOffset);

            //Log.Debug($"recv: {MongoHelper.ToJson(message)}");

            AResponse response = message as AResponse;

            if (response != null)
            {
                // rpcFlag>0 表示这是一个rpc响应消息
                // Rpc回调有找不着的可能,因为client可能取消Rpc调用
                Action <object> rAction;
                if (!this.mRequestCallback.TryGetValue(response.RpcId, out rAction))
                {
                    return;
                }
                this.mRequestCallback.Remove(response.RpcId);
                rAction(message);
                return;
            }
            this.mNetwork.MessageDispatcher.Dispatch(this, nOpcode, nOffset, rMessageBytes, (AMessage)message);
        }