public override byte[] GetBytes() { int index = 0; var bytes = new byte[MaslAu2Frame.FrameLength]; // ETY + MTI + DF bytes[index++] = this.GetHeaderByte(); // 消息发起方ID var tempBuf = RsspEncoding.ToNetUInt32(this.ServerID); Array.Copy(tempBuf, 1, bytes, index, 3); index += 3; // 安全特征 bytes[index++] = (byte)this.EncryAlgorithm; // 随机数 Array.Copy(this.RandomA, 0, bytes, index, 8); index += 8; // MAC Array.Copy(this.MAC, 0, bytes, index, 8); index += 8; return(bytes); }
public override byte[] GetBytes() { var bytes = new byte[this.Length]; int startIndex = 0; // 主叫编号 var tempBuf = RsspEncoding.ToNetUInt32(this.InitiatorID); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // 被叫编号 tempBuf = RsspEncoding.ToNetUInt32(this.ResponderID); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // 服务类型 bytes[startIndex++] = (byte)this.ServiceType; // 用户数据 if (this.UserData != null) { Array.Copy(this.UserData, 0, bytes, startIndex, this.UserData.Length); } return(bytes); }
public override void ParseBytes(byte[] bytes, int startIndex, int endIndex) { if ((endIndex - startIndex + 1) < MaslAu2Frame.FrameLength) { throw new Au2LengthException(); } var orgStartIndex = startIndex; // ETY + MTI + DF this.ParseHeaderByte(bytes[startIndex++]); // 消息发起方ID this.ServerID = (RsspEncoding.ToHostUInt32(bytes, orgStartIndex) & 0xFFFFFF); startIndex += 3; // 安全特征 this.EncryAlgorithm = (EncryptionAlgorithm)bytes[startIndex++]; // 随机数 Array.Copy(bytes, startIndex, this.RandomA, 0, 8); startIndex += 8; // MAC Array.Copy(bytes, startIndex, this.MAC, 0, 8); startIndex += 8; }
public override byte[] GetBytes() { var bytes = new byte[25]; int startIndex = 0; // 消息类型 bytes[startIndex++] = (byte)this.FrameType; // 序列号 var tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // Padding startIndex += SaiFrame.TtsPaddingLength; // 初始值 tempBuf = RsspEncoding.ToNetUInt32(this.InitialValue); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // 版本 tempBuf = RsspEncoding.ToNetUInt32(this.Version); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // EC周期 tempBuf = RsspEncoding.ToNetUInt16(this.Interval); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); return(bytes); }
public override byte[] GetBytes() { var bytes = new byte[15]; int startIndex = 0; // 消息类型 bytes[startIndex++] = (byte)this.FrameType; // 序列号 var tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // sender timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // Responsor last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.ReceiverLastSendTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // sender last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderLastRecvTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; return(bytes); }
public override void ParseBytes(byte[] bytes) { int startIndex = 0; // 消息类型 this.FrameType = (SaiFrameType)bytes[startIndex++]; // 序列号 this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex); startIndex += 2; // sender timestamp this.SenderTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // Responsor last timestamp this.ReceiverLastSendTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // sender last timestamp this.SenderLastRecvTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // valid this.Valid = (bytes[startIndex++] == 1); }
public override void ParseBytes(byte[] bytes) { int startIndex = 0; // 消息类型 this.FrameType = (SaiFrameType)bytes[startIndex++]; // 序列号 this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex); startIndex += 2; // Padding startIndex += SaiFrame.TtsPaddingLength; // 初始值 this.InitialValue = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // 版本 this.Version = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // EC周期 this.Interval = RsspEncoding.ToHostUInt16(bytes, startIndex); }
public override void ParseBytes(byte[] bytes) { int startIndex = 0; // 消息类型 this.FrameType = (SaiFrameType)bytes[startIndex++]; // 序列号 this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex); startIndex += 2; // sender timestamp this.SenderTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // Responsor last timestamp this.ReceiverLastSendTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // sender last timestamp this.SenderLastRecvTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // user data var len = bytes.Length - startIndex; if (len > 0) { this.UserData = new byte[len]; Array.Copy(bytes, startIndex, this.UserData, 0, len); } }
public override byte[] GetBytes() { if (this.UserDataLength > SaiFrame.MaxUserDataLength) { throw new ArgumentException(string.Format("SAI层用户数据长度不能超过{0}。", SaiFrame.MaxUserDataLength)); } var bytes = new byte[19 + this.UserDataLength]; int startIndex = 0; // 消息类型 bytes[startIndex++] = (byte)this.FrameType; // 序列号 var tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // Padding startIndex += SaiFrame.TtsPaddingLength; // EC计数 tempBuf = RsspEncoding.ToNetUInt32(this.EcValue); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // user data if (this.UserData != null) { Array.Copy(this.UserData, 0, bytes, startIndex, this.UserData.Length); } return(bytes); }
public override void ParseBytes(byte[] bytes) { int startIndex = 0; // 消息类型 this.FrameType = (SaiFrameType)bytes[startIndex++]; // 序列号 this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex); startIndex += 2; // Padding startIndex += SaiFrame.TtsPaddingLength; // EC计数 this.EcValue = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // user data var len = bytes.Length - startIndex; if (len > 0) { this.UserData = new byte[len]; Array.Copy(bytes, startIndex, this.UserData, 0, len); } }
public static bool ContainsFileStartFlag(byte[] data, int dataCount) { // 20:一个估算值,用于防止传输的用户数据中包含文件起始标志。 return(dataCount >= CustomFile.StartFlagLen && dataCount < (CustomFile.StartFlagLen + 20) && CustomFile.StartFlag == RsspEncoding.ToHostString(data, 0, CustomFile.StartFlagLen)); }
private ushort GetPacketLength(byte[] buffer, int startIndex) { if ((buffer == null) || (buffer.Length - startIndex < 2)) { return(0); } return(RsspEncoding.ToHostUInt16(buffer, startIndex)); }
/// <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); }
public override void ParseBytes(byte[] bytes, int startIndex, int endIndex) { // 应答方编号 this.ServerID = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // 用户数据 var len = endIndex - startIndex + 1; if (len > 0) { this.UserData = new byte[len]; Array.Copy(bytes, startIndex, this.UserData, 0, len); } }
public override void ParseBytes(byte[] bytes) { int startIndex = 0; // 消息类型 this.FrameType = (SaiFrameType)bytes[startIndex++]; // 序列号 this.SequenceNo = RsspEncoding.ToHostUInt16(bytes, startIndex); startIndex += 2; // sender timestamp this.SenderTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // Responsor last timestamp this.ReceiverLastSendTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // sender last timestamp this.SenderLastRecvTimestamp = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // 偏移标志 bool negative = (bytes[startIndex] == 1); startIndex++; // |最小偏移值| this.OffsetMin = RsspEncoding.ToHostUInt32(bytes, startIndex); if (negative) { this.OffsetMin = -this.OffsetMin; } startIndex += 4; // 偏移标志 negative = (bytes[startIndex] == 1); startIndex++; // |最大偏移值| this.OffsetMax = RsspEncoding.ToHostUInt32(bytes, startIndex); if (negative) { this.OffsetMax = -this.OffsetMax; } startIndex += 4; }
public override byte[] GetBytes() { var bytes = new byte[25]; int startIndex = 0; // 消息类型 bytes[startIndex++] = (byte)this.FrameType; // 序列号 var tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // sender timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // Responsor last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.ReceiverLastSendTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // sender last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderLastRecvTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // 偏移标志 bytes[startIndex++] = (this.OffsetMin >= 0) ? (byte)0 : (byte)1; // |最小偏移值| tempBuf = RsspEncoding.ToNetUInt32((uint)Math.Abs(this.OffsetMin)); Array.Copy(tempBuf, 0, bytes, startIndex, 4); startIndex += 4; // 偏移标志 bytes[startIndex++] = (this.OffsetMax >= 0) ? (byte)0 : (byte)1; // |最大偏移值| tempBuf = RsspEncoding.ToNetUInt32((uint)Math.Abs(this.OffsetMax)); Array.Copy(tempBuf, 0, bytes, startIndex, 4); startIndex += 4; return(bytes); }
/// <summary> /// 获取序列化后的字节流。 /// </summary> public byte[] GetBytes() { var bytes = new byte[AleFrame.HeadLength + this.UserDataLength]; var startIndex = 0; // 包长度 var tempBuf = RsspEncoding.ToNetUInt16(this.PacketLength); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += AleFrame.SizeofHeadLen; // 版本 bytes[startIndex++] = this.Version; // 应用类型 bytes[startIndex++] = this.ApplicationType; // 传输序列号 tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // N/R标志 bytes[startIndex++] = (byte)(this.IsNormal ? 1 : 0); // 包类型 bytes[startIndex++] = (byte)this.FrameType; // 校验和 var crcValue = CrcTool.CaculateCCITT16(bytes, 0, 8); //tempBuf = BitConverter.GetBytes(crcValue); tempBuf = RsspEncoding.ToNetUInt16(crcValue); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // 用户数据 var userData = this.UserData.GetBytes(); if (userData != null) { Array.Copy(userData, 0, bytes, startIndex, this.UserDataLength); } return(bytes); }
public override byte[] GetBytes() { var bytes = new byte[this.Length]; int startIndex = 0; // 应答方编号 var tempBuf = RsspEncoding.ToNetUInt32(this.ServerID); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // 用户数据 if (this.UserData != null) { Array.Copy(this.UserData, 0, bytes, startIndex, this.UserData.Length); } return(bytes); }
public override byte[] GetBytes() { if (this.UserDataLength > SaiFrame.MaxUserDataLength) { throw new ArgumentException(string.Format("SAI层用户数据长度不能超过{0}。", SaiFrame.MaxUserDataLength)); } var bytes = new byte[15 + this.UserDataLength]; int startIndex = 0; // 消息类型 bytes[startIndex++] = (byte)this.FrameType; // 序列号 var tempBuf = RsspEncoding.ToNetUInt16(this.SequenceNo); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 2; // sender timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // Responsor last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.ReceiverLastSendTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // sender last timestamp tempBuf = RsspEncoding.ToNetUInt32(this.SenderLastRecvTimestamp); Array.Copy(tempBuf, 0, bytes, startIndex, tempBuf.Length); startIndex += 4; // user data if (this.UserData != null) { Array.Copy(this.UserData, 0, bytes, startIndex, this.UserData.Length); } return(bytes); }
public override void ParseBytes(byte[] bytes, int startIndex, int endIndex) { // 主叫编号 this.InitiatorID = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // 被叫编号 this.ResponderID = RsspEncoding.ToHostUInt32(bytes, startIndex); startIndex += 4; // 服务类型 this.ServiceType = (ServiceType)bytes[startIndex++]; // 用户数据 var len = endIndex - startIndex + 1; if (len > 0) { this.UserData = new byte[len]; Array.Copy(bytes, startIndex, this.UserData, 0, len); } }
/// <summary> /// 保存收到的用户数据 /// </summary> public void SaveUserData(uint remoteID, byte[] userData) { try { var dataLength = userData.Length; if (CustomFile.ContainsFileStartFlag(userData, dataLength)) { // 先关闭旧的文件 this.RemoveFile(remoteID); // 创建新的业务文件 var ext = RsspEncoding.ToHostString(userData, CustomFile.StartFlagLen, dataLength - CustomFile.StartFlagLen); var bdfile = new CustomFile(remoteID.ToString(), ext); this.AddFile(remoteID, bdfile); } else if (CustomFile.ContainsFileEndFlag(userData, dataLength)) { // 关闭业务文件 this.RemoveFile(remoteID); } else { // 保存收到的数据 var theFile = this.GetFile(remoteID); if (theFile != null) { theFile.Write(userData); } } } catch (System.Exception ex) { LogUtility.Error(ex); } }
private void SendCustomFile() { FileStream fs = null; BinaryReader br = null; Byte[] userData = null; var dest = this.GetDest(); try { // Create the reader for data. fs = new FileStream(_fileNameToSend, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); for (int n = 0; n < _sendTimes; n++) { try { // 发送文件起始标志与扩展名 if (_fileBeginFlagEnabled) { userData = RsspEncoding.ToNetString(CustomFile.StartFlag + Path.GetExtension(_fileNameToSend)); var outPkg = new OutgoingPackage(dest, userData); _nodeComm.Send(outPkg); } // Read and send data fs.Position = 0; do { userData = br.ReadBytes(_packetSize); var outPkg = new OutgoingPackage(dest, userData); _nodeComm.Send(outPkg); // 如果文件发送完成,则立即结束 if (userData.Length < _packetSize) { break; } // 发送间隔 if (_sendExitEvent.WaitOne(_sendInterval, false)) { break; } } while (fs.Length != fs.Position); } finally { userData = RsspEncoding.ToNetString(CustomFile.EndFlag); // 发送文件传送结束标志 if (_fileBeginFlagEnabled) { var outPkg = new OutgoingPackage(dest, userData); _nodeComm.Send(outPkg); } } } } catch (Exception /*ex*/) { } finally { if (fs != null) { fs.Close(); } _sendThread = null; } }
public static bool ContainsFileEndFlag(byte[] data, int dataCount) { return(dataCount == CustomFile.EndFlagLen && CustomFile.EndFlag == RsspEncoding.ToHostString(data, 0, CustomFile.EndFlagLen)); }