예제 #1
0
파일: Session.cs 프로젝트: taigacon/ET
        public void Send(byte flag, IMessage message)
        {
            OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
            ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType());

            byte[] bytes = this.Network.MessagePacker.SerializeToByteArray(message);

            Send(flag, opcode, bytes);
        }
예제 #2
0
파일: Session.cs 프로젝트: taigacon/ET
        private async void Run(Packet packet)
        {
            //交由Unity线程执行
            await Awaiters.NextFrame;

            byte   flag   = packet.Flag;
            ushort opcode = packet.Opcode;

#if !SERVER
            if (OpcodeHelper.IsClientHotfixMessage(opcode))
            {
                this.Network.MessageDispatcher.Dispatch(this, packet);
                return;
            }
#endif

            // flag第一位为1表示这是rpc返回消息,否则交由MessageDispatcher分发
            if ((flag & 0x01) == 0)
            {
                this.Network.MessageDispatcher.Dispatch(this, packet);
                return;
            }

            object message;
            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                Type responseType = opcodeTypeComponent.GetType(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(responseType, packet.Stream);
                //Log.Debug($"recv: {JsonHelper.ToJson(message)}");
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"opcode: {opcode} {this.Network.Count} {e} ");
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.InstanceId);
                return;
            }

            IResponse response = message as IResponse;
            if (response == null)
            {
                throw new Exception($"flag is response, but message is not! {opcode}");
            }
            Action <IResponse> action;
            if (!this.requestCallback.TryGetValue(response.RpcId, out action))
            {
                return;
            }
            this.requestCallback.Remove(response.RpcId);

            action(response);
        }
예제 #3
0
        public void Load()
        {
            this.handlers.Clear();

            BK.MessageDispatherComponent messageDispatherComponent = BK.Game.MessageDispatherComponent;
            BK.OpcodeTypeComponent       opcodeTypeComponent       = BK.Game.OpcodeTypeComponent;

            List <Type> types = BK.Game.Hotfix.GetHotfixTypes();

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }

                Type   messageType = iMHandler.GetMessageType();
                ushort opcode      = this.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType);
                if (opcode != 0)
                {
                    this.RegisterHandler(opcode, iMHandler);
                }

                // 尝试注册到mono层
                if (messageDispatherComponent != null && opcodeTypeComponent != null)
                {
                    ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType);
                    if (monoOpcode == 0)
                    {
                        continue;
                    }

                    MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
                    messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy);
                }
            }
        }
예제 #4
0
        public void Update()
        {
            if (this.Queue.Count == 0)
            {
                return;
            }
            SessionFrameMessage sessionFrameMessage = this.Queue.Dequeue();

            this.Frame = sessionFrameMessage.FrameMessage.Frame;

            for (int i = 0; i < sessionFrameMessage.FrameMessage.Messages.Count; ++i)
            {
                OneFrameMessage oneFrameMessage = sessionFrameMessage.FrameMessage.Messages[i];

                Session             session             = sessionFrameMessage.Session;
                OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();
                Type type = opcodeTypeComponent.GetType(oneFrameMessage.Op);

                IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(type, oneFrameMessage.AMessage);
                Game.MessageDispatherComponent.Handle(sessionFrameMessage.Session, new MessageInfo(oneFrameMessage.Op, message));
            }
        }
예제 #5
0
        public void Dispatch(Session session, Packet packet)
        {
            object message;

            try
            {
                if (OpcodeHelper.IsClientHotfixMessage(packet.Opcode))
                {
                    session.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(session, packet);
                    return;
                }

                OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();
                Type responseType = opcodeTypeComponent.GetType(packet.Opcode);
                message = session.Network.MessagePacker.DeserializeFrom(responseType, packet.Stream);
            }
            catch (Exception e)
            {
                // 出现任何解析消息异常都要断开Session,防止客户端伪造消息
                Log.Error(e);
                session.Error = ErrorCode.ERR_PacketParserError;
                session.Network.Remove(session.InstanceId);
                return;
            }

            // 如果是帧同步消息,交给ClientFrameComponent处理
            FrameMessage frameMessage = message as FrameMessage;

            if (frameMessage != null)
            {
                Game.ClientFrameComponent.Add(session, frameMessage);
                return;
            }

            // 普通消息或者是Rpc请求消息
            MessageInfo messageInfo = new MessageInfo(packet.Opcode, message);

            Game.MessageDispatherComponent.Handle(session, messageInfo);
        }