Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 public SmModel(SmPackageType type)
 {
     this.PackageType = type;
 }