Example #1
0
        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

            object message;

            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);

                if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
                {
                    Log.Msg(message);
                }
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}");
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }

            RunMessage(opcode, message);
        }
Example #2
0
        public void Run(Session s, byte flag, ushort opcode, MemoryStream memoryStream)
        {
            OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>();
            object instance = opcodeTypeComponent.GetInstance(opcode);
            object message  = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);

            if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
            {
                Log.Msg(message);
            }

            if ((flag & 0x01) > 0)
            {
                IResponse response = message as IResponse;
                if (response == null)
                {
                    throw new Exception($"flag is response, but hotfix message is not! {opcode}");
                }

                Action <IResponse> action;
                if (!this.requestCallback.TryGetValue(response.RpcId, out action))
                {
                    return;
                }
                this.requestCallback.Remove(response.RpcId);

                action(response);
                return;
            }

            Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(s, new MessageInfo(opcode, message));
        }
Example #3
0
        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            byte   flag   = memoryStream.GetBuffer()[Packet.OpcodeIndex];
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

// #if !SERVER
//          if (OpcodeHelper.IsClientHotfixMessage(opcode))
//          {
//              this.GetComponent<SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream);
//              return;
//          }
// #endif

            object message;

            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);

                if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
                {
                    Log.Msg(message);
                }
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}");
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }

            if (!(message is IResponse response))
            {
                this.Network.MessageDispatcher.Dispatch(this, opcode, message);
                return;
            }

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

            Action <IResponse> action;

            if (!this.requestCallback.TryGetValue(response.RpcId, out action))
            {
                throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}");
            }
            this.requestCallback.Remove(response.RpcId);

            action(response);
        }
Example #4
0
        private void Run(Packet packet)
        {
            packet.Flag   = packet.Bytes[Packet.FlagIndex];
            packet.Opcode = BitConverter.ToUInt16(packet.Bytes, Packet.OpcodeIndex);
            packet.Stream.Seek(Packet.MessageIndex, SeekOrigin.Begin);

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

#if !SERVER
            /*if (OpcodeHelper.IsClientHotfixMessage(opcode))
             * {
             *      this.Network.MessageDispatcher.Dispatch(this, packet);
             *      return;
             * }*/
#endif
            Log.Info(flag.ToHex());
            // 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>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                Log.Info(opcode.ToString());
                message = this.Network.MessagePacker.DeserializeFrom(instance, 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.Id);
                return;
            }

            IResponse response = message as IResponse;
            mLatencyComponent.AddAMsgLan(TimeHelper.GetCurrentTimeUnix() - response.Time);


            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);
        }
Example #5
0
        /// <summary>
        /// 读取消息
        /// </summary>
        /// <param name="memoryStream">包体数据</param>
        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            //读取操作码 操作码也是属于包体的一部分
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

#if !SERVER
            //如果是热更模块的操作码 >10000以上的
            if (OpcodeHelper.IsClientHotfixMessage(opcode))
            {
                //提交到热更层Session对象,在其内部注册的方法去处理
                this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream);
                return;
            }
#endif

            object message;
            try
            {
                //通过操作码 获取到实例
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                //反序列化 将内存流剩下的数据反序列化成proto实体
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);

                //需要调试日志的信息 就打印出来
                if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
                {
                    Log.Msg(message);
                }
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"opcode: {opcode} {this.Network.Count} {e}, ip: {this.RemoteAddress}");
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }

            //如果消息不是响应类型的消息 进行派发处理
            if (!(message is IResponse response))
            {
                //进行派发处理
                this.Network.MessageDispatcher.Dispatch(this, opcode, message);
                return;
            }

            //如果是响应类型的消息
            //从requestCallback缓存取出要执行的方法 将message压入到方法中 然后执行该方法 这涉及到ETTask的封装 等待了解
            Action <IResponse> action;
            if (!this.requestCallback.TryGetValue(response.RpcId, out action))
            {
                throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}");
            }
            this.requestCallback.Remove(response.RpcId);

            action(response);
        }
Example #6
0
        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            byte   flag   = memoryStream.GetBuffer()[Packet.FlagIndex];
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

#if !SERVER
            if (OpcodeHelper.IsClientHotfixMessage(opcode))
            {
                this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, flag, opcode, memoryStream);
                return;
            }
#endif

            object message;
            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);
                //Log.Debug($"recv: {JsonHelper.ToJson(message)}");

                if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
                {
                    Log.Msg(message);
                }
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Log.Error($"opcode: {opcode} {this.Network.Count} {e} ");
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }

            // flag第一位为1表示这是rpc返回消息,否则交由MessageDispatcher分发
            if ((flag & 0x01) == 0)
            {
                this.Network.MessageDispatcher.Dispatch(this, opcode, message);
                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);
        }
Example #7
0
        /// <summary>
        /// 处理收到的网络消息
        /// </summary>
        /// <param name="packet">Packet.</param>
        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            byte flag = memoryStream.GetBuffer()[Packet.FlagIndex];
            //得到操作码
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

#if !SERVER
            //如果是客户端  如果是热更新的操作码 就直接把收到的消息派发处理
            if (OpcodeHelper.IsClientHotfixMessage(opcode))
            {
                this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, flag, opcode, memoryStream);
                return;
            }
#endif

            object message;
            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); //操作码和类的映射关系
                object instance = opcodeTypeComponent.GetInstance(opcode);                                          //根据操作码得到类
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);                       //将包里面的数据根据操作码找到的类型反序列化为object
                                                                                                                    //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.Id);
                return;
            }

            // flag第一位为1表示这是rpc返回消息  如果是0 说明这个只是普通的消息 由MessageDispatcher处理
            if ((flag & 0x01) == 0)
            {
                this.Network.MessageDispatcher.Dispatch(this, opcode, message);
                return;
            }
            //是RPC消息  把消息体转换为	IResponse 类型
            IResponse response = message as IResponse;
            if (response == null)             //做验证保护
            {
                throw new Exception($"flag is response, but message is not! {opcode}");
            }
            Action <IResponse> action;              //如果回来的是RPC消息  查看RPC消息列表
            if (!this.requestCallback.TryGetValue(response.RpcId, out action))
            {
                return;                                  //没有就滚蛋
            }
            this.requestCallback.Remove(response.RpcId); //从字典中移除这个RPC消息

            action(response);                            //处理这个消息 RPC消息
        }
Example #8
0
        private void Run(Packet packet)
        {
            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>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(instance, 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.Id);
                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);
        }
Example #9
0
        public void Dispatch(Session session, Packet packet)
        {
            Debug.Log(packet.Opcode);
            object message;

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

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

            // 如果是帧同步消息,交给ClientFrameComponent处理

            /*FrameMessage frameMessage = message as FrameMessage;
             *          if (frameMessage != null)
             *          {
             *                  Game.Scene.GetComponent<ClientFrameComponent>().Add(session, frameMessage);
             *                  return;
             *          }*/
            // 普通消息或者是Rpc请求消息
            MessageInfo messageInfo = new MessageInfo(packet.Opcode, message);

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, messageInfo);
        }
Example #10
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.Message.Count; ++i)
            {
                OneFrameMessage oneFrameMessage = sessionFrameMessage.FrameMessage.Message[i];

                Session             session             = sessionFrameMessage.Session;
                OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent <OpcodeTypeComponent>();
                ushort opcode   = (ushort)oneFrameMessage.Op;
                object instance = opcodeTypeComponent.GetInstance(opcode);

                byte[]           bytes   = ByteString.Unsafe.GetBuffer(oneFrameMessage.AMessage);
                ETModel.IMessage message = (ETModel.IMessage)session.Network.MessagePacker.DeserializeFrom(instance, bytes, 0, bytes.Length);
                Game.Scene.GetComponent <MessageDispatherComponent>().Handle(sessionFrameMessage.Session, new MessageInfo((ushort)oneFrameMessage.Op, message));
            }
        }
Example #11
0
        //------------//[MyAppend end]------------

        private void Run(MemoryStream memoryStream)
        {
            memoryStream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
            ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);

            //------------//[MyAppend begin]------------
            if (!checkOpcode(opcode))
            {
                myDebug(opcode);
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }
            //------------//[MyAppend end]------------

#if !SERVER
            if (OpcodeHelper.IsClientHotfixMessage(opcode))
            {
                this.GetComponent <SessionCallbackComponent>().MessageCallback.Invoke(this, opcode, memoryStream);
                return;
            }
#endif

            object message;
            try
            {
                OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>();
                object instance = opcodeTypeComponent.GetInstance(opcode);
                message = this.Network.MessagePacker.DeserializeFrom(instance, memoryStream);
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                //                Log.Error($"opcode: {opcode} {this.Network.Count} {e} " + myDebug(memoryStream));
                //Log.Error(e + ", opcode: " + opcode + ", this.Network.Count: " + this.Network.Count + ", " + myDebug(memoryStream));
                this.Error = ErrorCode.ERR_PacketParserError;
                this.Network.Remove(this.Id);
                return;
            }

            IResponse response = message as IResponse;
            if (response == null)
            {
                //屏蔽心跳日志输出 10008
                //屏蔽广播玩家下注日志 10050
                //屏蔽广播推送的消息 request
                //1011数据存储
                if (OpcodeHelper.IsNeedDebugLogMessage(opcode) &&
                    opcode != 10008 &&
                    opcode != 10050 &&
                    opcode != 1011 &&
                    opcode != 1012 &&
                    opcode != 1009 &&
                    opcode != 1010 &&
                    (message as IActorMessage) == null
                    )
                {
                    Log.Msg(message);
                }
                this.Network.MessageDispatcher.Dispatch(this, opcode, message);
                return;
            }

            Action <IResponse> action;
            if (!this.requestCallback.TryGetValue(response.RpcId, out action))
            {
                throw new Exception($"not found rpc, response message: {StringHelper.MessageToStr(response)}");
            }
            this.requestCallback.Remove(response.RpcId);

            action(response);

            //屏蔽心跳日志输出 10008
            //屏蔽广播玩家下注日志 10050
            if (OpcodeHelper.IsNeedDebugLogMessage(opcode) && opcode != 10008 && opcode != 10050 && opcode != 1011 && opcode != 1012 && opcode != 1010 && opcode != 1009)
            {
                Log.Msg(message);
            }
        }