Example #1
0
        /// <summary>
        /// 构建动态加密包,此包要分三次发给嵌入式
        /// </summary>
        /// <returns></returns>
        public static List <byte[]> BuildEncryptPackages(List <byte> moduleAddr, bool isUpdateEncryptTable = false, byte aimType = (byte)SmEncryptFrame.AimType)
        {
            List <byte[]> packages = new List <byte[]>();

            if (moduleAddr.Count != (int)SmEncryptIndex.ModuleAddrCount)
            {
                throw new Exception("加密包module addr 长度不对");
            }
            //更新表
            if (isUpdateEncryptTable)
            {
                SmEncrypt.UpdateEncriptyTable();
            }
            List <byte> tables = SmEncrypt.GetEncryptTable();
            //对加密表进行切分
            //0-84
            List <byte> firstTable = tables.GetRange(0, 85);
            //85 - 169
            List <byte> secondTable = tables.GetRange(85, 85);
            //170-255
            List <byte> thirdTable = tables.GetRange(170, 86);
            //切分好的表添加到一个表中好遍历
            List <List <byte> > tableList = new List <List <byte> >();

            tableList.Add(firstTable);
            tableList.Add(secondTable);
            tableList.Add(thirdTable);
            int completedLen = 0;

            //将切分的包构建成package
            tableList.ForEach(data => {
                byte[] encryptPackage = buildEncryptPackage(data, moduleAddr, completedLen, aimType);
                completedLen         += data.Count;
                packages.Add(encryptPackage);
            });
            return(packages);
        }
Example #2
0
        /// <summary>
        /// 缓存数据==>可用的包
        /// 对ProcessBuffer函数进行了极大的优化
        /// 目前效率 250次循环 61ms 50个单元单元测试包
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private Dictionary <SmPackageType, List <byte[]> > processBuffer2(byte[] buffer, int offset, int count)
        {
            //解密
            buffer = SmEncrypt.DecodeArray(buffer, offset, count);

            Dictionary <SmPackageType, List <byte[]> > packageDictionary = new Dictionary <SmPackageType, List <byte[]> >();

            try {
                //fixed: 2018-01-04
                // 某些机台这里会抛异常
                socketBuffer.WriteBuffer(buffer, offset, count);
            } catch (Exception e) {
                //Console.WriteLine("DynamicBuffer WriteBuffer 异常" + e.Message);
                Logger.Error($"DynamicBuffer WriteBuffer 异常: DataCount: {socketBuffer.GetDataCount()},BufferSize: {socketBuffer.BufferSize}", e);
                //清空缓存
                socketBuffer.Clear();
            }
            List <byte[]> normalPackages         = new List <byte[]>();
            List <byte[]> heartbeatPackages      = new List <byte[]>();
            List <byte[]> clientReplyCmdPackages = new List <byte[]>();
            SmPackageType smPackageType          = SmPackageType.ErrorPackage;
            int           startFrameCount        = 0;

            for (int i = offset; i < offset + socketBuffer.GetDataCount(); ++i)
            {
                //检验帧头
                if (socketBuffer[i] == (byte)SmFrame.Start)
                {
                    ++startFrameCount;
                    //找到长度帧
                    byte[] datalenBytes = { socketBuffer[i + (int)SmIndex.TotalLenStart], socketBuffer[(int)SmIndex.TotalLenStart + 1 + i] };
                    datalenBytes = datalenBytes.Reverse().ToArray();
                    //数据长度(该位后面(不包括结束符,CRC)的长度)
                    Int16 propLen = BitConverter.ToInt16(datalenBytes, 0);
                    //整个包长度
                    int packageLen =
                        +(int)SmIndex.TotalLenStart
                        + (int)SmIndex.TotalLenCount
                        + propLen
                        + 3;
                    int index = i + packageLen - 1;
                    //加上长度是否为结束帧
                    if (propLen > 0 && index < socketBuffer.GetDataCount() &&
                        socketBuffer[index] == (byte)SmFrame.End)
                    {
                        //获取包类型
                        smPackageType = SmPackage.GetPackageType(socketBuffer.Buffer, i, packageLen);
                        if (smPackageType != SmPackageType.ErrorPackage)
                        {
                            byte[] bytes = copyPackageAndClearOrigin2(ref i, packageLen);
                            if (smPackageType == SmPackageType.ParamPackage)
                            {
                                normalPackages.Add(bytes);
                            }
                            else if (smPackageType == SmPackageType.HeartbeatPackage)
                            {
                                heartbeatPackages.Add(bytes);
                            }
                            else if (smPackageType == SmPackageType.ClientReplyCmd)
                            {
                                clientReplyCmdPackages.Add(bytes);
                            }
                        }
                    }
                    else
                    {
                        ////缓存中出现了超过1000个字节
                        ////全删掉
                        if (startFrameCount >= 1000)
                        {
                            //socketBuffer.Clear(0, i);
                            Console.WriteLine("缓存中超过1000个坏包");
                        }
                    }
                }
            }
            //存包
            packageDictionary[SmPackageType.ParamPackage]     = normalPackages;
            packageDictionary[SmPackageType.HeartbeatPackage] = heartbeatPackages;
            packageDictionary[SmPackageType.ClientReplyCmd]   = clientReplyCmdPackages;
            return(packageDictionary);
        }