Ejemplo n.º 1
0
        /// <summary>
        /// 网络事件处理
        /// </summary>
        public void OnNetworkClient(long conn_idx, ushort header, ByteArray data)
        {
            ClientSession session = ClientSessionManager.Instance.GetSession(conn_idx);

            if (session != null)
            {
                ///1.判断cd
                if (!session.CheckCooldown(header))
                {
                    //Log.Debug("协议cd不够:" + header);
                    return;
                }

                ///2.消息解密
                if (GlobalID.ENABLE_PACKET_ENCRYPT)
                {
                    //id有效性校验
                    uint packet_idx = data.ReadUInt();
                    packet_idx = PacketEncrypt.DecrpytPacketIndex(packet_idx, PacketEncrypt.Encrypt_Key);
                    if (packet_idx < session.last_packet_idx)
                    {
                        Log.Warning("协议索引校验失败conn_idx:" + conn_idx + " header:" + header);
                        ClientSessionManager.Instance.KickoutSession(conn_idx);
                        return;
                    }
                    session.last_packet_idx = packet_idx;

                    //验证数据的有效性:防止被修改
                    ushort send_verify_data = data.ReadUShort();
                    ushort verify_data      = PacketEncrypt.CalcPacketDataVerify(data.Buffer, 6, data.Available, packet_idx, PacketEncrypt.Encrypt_Key);
                    if (send_verify_data != verify_data)
                    {
                        Log.Warning("数据有效性校验失败conn_idx:" + conn_idx + " header:" + header);
                        ClientSessionManager.Instance.KickoutSession(conn_idx);
                        return;
                    }
                }

                ///3.是否转发消息
                if (header >= ProtocolID.MSG_BASE_C2SS && header < ProtocolID.MSG_BASE_C2SS + ProtocolID.MSG_APPLAYER_PER_INTERVAL)
                {
                    HandleProxyMsgToSS(session, header, data);
                }
                else if (header >= ProtocolID.MSG_BASE_C2WS && header < ProtocolID.MSG_BASE_C2WS + ProtocolID.MSG_APPLAYER_PER_INTERVAL)
                {
                    HandleProxyMsgToWS(session, header, data);
                }
                else if (header >= ProtocolID.MSG_BASE_C2FS && header < ProtocolID.MSG_BASE_C2FS + ProtocolID.MSG_APPLAYER_PER_INTERVAL)
                {
                    HandleProxyMsgToFS(session, header, data);
                }
                else
                {
                    PacketBase packet = PacketPools.Get(header);
                    packet.Read(data);

                    MsgProcFunction fun;
                    if (m_msg_proc.TryGetValue(packet.header, out fun))
                    {
                        try
                        {
                            fun(session, packet);
                        }
                        catch (Exception e)
                        {
                            Log.Exception(e);
                        }
                    }
                    else
                    {
                        Log.Warning("未注册消息处理函数id:" + header);
                    }
                    PacketPools.Recover(packet);
                }
            }
            else
            {
                PacketBase packet = PacketPools.Get(header);
                packet.Read(data);
                switch (packet.header)
                {
                case c2gs.msg.ENCRYPT:
                    OnClientEncrypt(conn_idx, packet);
                    break;

                default:    //没有session,又不是握手协议,说明发错协议了,直接踢掉
                    ClientSessionManager.Instance.KickoutSession(conn_idx);
                    break;
                }
                PacketPools.Recover(packet);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加速检测
        /// </summary>
        private void OnSpeedCheck(ClientSession session, PacketBase packet)
        {
            c2gs.SpeedCheck msg = packet as c2gs.SpeedCheck;

            session.speed_checker.OnRecvTrapMsg(msg.check_sn);
        }