Пример #1
0
        /// <summary>
        /// 所有包协议一致,通过协议来检验是否为一个包
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static bool AsserIsPackage(byte[] buffer, int offset, int count)
        {
            //帧头,帧尾,长度判断
            byte startFrame = buffer[offset];
            int  endFrame   = buffer[count + offset - 1];

            if (count < (int)SmSup.MinLength ||
                (startFrame != (byte)SmFrame.Start) ||
                (endFrame != (byte)SmFrame.End))
            {
                return(false);
            }
            List <byte> byList = buffer.ToList();
            //固定帧校验
            byte fixedFrame = byList[SmTool.GetSocketIndex(SmIndex.Fixed, offset)];

            if (fixedFrame != (byte)SmFrame.Fixed)
            {
                return(false);
            }

            //crc校验
            int crsFrameStart = offset + count - 3;

            byte[] crcBytes = byList.GetRange(crsFrameStart, 2).ToArray().Reverse().ToArray();
            int    crc      = BitConverter.ToUInt16(crcBytes, 0);
            var    crcCalc  = SmCrc16.CrcCalc(buffer, offset, count - 3);

            if (crc != crcCalc)
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 判断包类型,这是个辅助方法
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <param name="type"></param>
        /// <param name="isPackage"></param>
        /// <returns></returns>
        internal static bool AssertPackageType(byte[] buffer, int offset, int count, SmPackageType type, bool isPackage)
        {
            if (!isPackage)
            {
                if (!AsserIsPackage(buffer, offset, count))
                {
                    return(false);
                }
            }
            byte cmdFrame = buffer[SmTool.GetSocketIndex(SmIndex.Cmd, offset)];

            return(cmdFrame == (byte)type);
        }
Пример #3
0
        /// <summary>
        /// 所有包都能提取的信息
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private SmModel processPackage(byte[] buffer, int offset, SmPackageType type)
        {
            List <byte> byList = buffer.ToList();

            byList = byList.GetRange(offset, byList.Count);
            var emSocket = new SmModel(type);

            //解码
            emSocket.ModuleAddr = byList.GetRange(SmTool.GetSocketIndex(SmIndex.MachineAddrStart), (int)SmIndex.MacineAddrCount);
            emSocket.Cmd        = byList[SmTool.GetSocketIndex(SmIndex.Cmd)];
            emSocket.AimType    = byList[SmTool.GetSocketIndex(SmIndex.AimType)];
            emSocket.Buffer     = buffer;
            return(emSocket);
        }
Пример #4
0
        /// <summary>
        /// 检验是否为心跳包
        /// 一定要先运行AsserIsPackage
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        internal static bool AssertIsHeartbeatPackage(byte[] buffer, int offset, int count, bool isPackage = false)
        {
            if (!isPackage)
            {
                if (!AsserIsPackage(buffer, offset, count))
                {
                    return(false);
                }
            }
            byte cmdFrame = buffer[SmTool.GetSocketIndex(SmIndex.Cmd, offset)];

            if (cmdFrame == (byte)SmCmdFrame.Heartbeat)
            {
                return(true);
            }
            return(false);
        }
Пример #5
0
 /// <summary>
 /// 判断缓存中的数据是什么包
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 internal static SmPackageType GetPackageType(byte[] buffer, int offset, int count)
 {
     try {
         if (AsserIsPackage(buffer, offset, count))
         {
             byte cmdFrame = buffer[SmTool.GetSocketIndex(SmIndex.Cmd, offset)];
             if (cmdFrame == (byte)SmCmdFrame.Param)
             {
                 return(SmPackageType.ParamPackage);
             }
             else if (cmdFrame == (byte)SmCmdFrame.Heartbeat)
             {
                 return(SmPackageType.HeartbeatPackage);
             }
             else if (cmdFrame == (byte)SmCmdFrame.ClientReplyCmd)
             {
                 return(SmPackageType.ClientReplyCmd);
             }
         }
     } catch {
     }
     return(SmPackageType.ErrorPackage);
 }