Example #1
0
        /// <summary>
        /// 解析指定的字节流。
        /// </summary>
        public void ParseBytes(byte[] bytes)
        {
            if (bytes == null || bytes.Length < AleFrame.HeadLength)
            {
                throw new AleFrameParsingException("无法将指定的字节流解析为AleFrame,长度不够。");
            }

            int startIndex = 0;

            // 包长度
            var pktLen = RsspEncoding.ToHostUInt16(bytes, startIndex);

            startIndex += AleFrame.SizeofHeadLen;

            // 版本
            this.Version = bytes[startIndex++];

            // 应用类型
            this.ApplicationType = bytes[startIndex++];

            // 传输序列号
            this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex);
            startIndex     += 2;

            // N/R标志
            this.IsNormal = (bytes[startIndex++] == 1);

            // 包类型
            this.FrameType = (AleFrameType)bytes[startIndex++];

            // 校验和
            if (this.FrameType != AleFrameType.KAA && this.FrameType != AleFrameType.KANA) // 生命信息不需要校验
            {
                //var actualCrcValue = BitConverter.ToUInt16(bytes, startIndex);
                var actualCrcValue   = RsspEncoding.ToHostUInt16(bytes, startIndex);
                var expectedCrcValue = CrcTool.CaculateCCITT16(bytes, 0, 8);
                if (actualCrcValue != expectedCrcValue)
                {
                    throw new AleFrameParsingException(string.Format("解析Ale协议帧时发生异常,CRC检验不一致,期望值是{0},实际值是{1}。",
                                                                     expectedCrcValue, actualCrcValue));
                }
            }
            startIndex += 2;

            // 用户数据
            var userDataLen = pktLen - (AleFrame.HeadLength - AleFrame.SizeofHeadLen);

            if (userDataLen < 0)
            {
                throw new AleFrameParsingException(string.Format("将字节流解析为ALE帧时发生异常,用户数据长度为负值{0}。", userDataLen));
            }
            this.UserData = AleUserData.Parse(this.FrameType, bytes, startIndex, startIndex + userDataLen - 1);
        }
Example #2
0
        public AleFrame(byte appType, UInt16 seqNo, bool isNormal, AleUserData userData)
            : this()
        {
            if (userData == null)
            {
                throw new ArgumentNullException();
            }

            this.ApplicationType = appType;
            this.SequenceNo      = seqNo;
            this.IsNormal        = isNormal;
            this.FrameType       = userData.FrameType;
            this.UserData        = userData;
        }
Example #3
0
        public static AleUserData Parse(AleFrameType frameType, byte[] bytes, int startIndex, int endIndex)
        {
            AleUserData result = null;

            if (frameType == AleFrameType.ConnectionRequest)
            {
                result = new AleConnectionRequest();
            }
            else if (frameType == AleFrameType.ConnectionConfirm)
            {
                result = new AleConnectionConfirm();
            }
            else if (frameType == AleFrameType.DataTransmission)
            {
                result = new AleDataTransmission();
            }
            else if (frameType == AleFrameType.Disconnect)
            {
                result = new AleDisconnect();
            }
            else if (frameType == AleFrameType.SwitchN2R)
            {
                result = new AleSwitchN2R();
            }
            else if (frameType == AleFrameType.SwitchR2N)
            {
                result = new AleSwitchR2N();
            }
            else if (frameType == AleFrameType.KANA)
            {
                result = new AleKeepAliveOnNonActiveLink();
            }
            else if (frameType == AleFrameType.KAA)
            {
                result = new AleKeepAliveOnActiveLink();
            }
            else
            {
                throw new AleFrameParsingException(string.Format("无法将指定的字节流解析为ALE层的用户数据,类型 = {0}。", frameType));
            }

            result.ParseBytes(bytes, startIndex, endIndex);

            return(result);
        }