public bool IsKeepAlivePackage(object _object, object _socket = null) { LztpPackage _packageCurrent = (LztpPackage)_object; LztpPackage _packageKeepAlive = (LztpPackage)this.KeepAlivePackage; return(_packageCurrent.Command1 == _packageKeepAlive.Command1 && _packageCurrent.Command2 == _packageKeepAlive.Command2 && _packageCurrent.CommandId == _packageKeepAlive.CommandId); }
/// <summary> /// 数据解包 /// </summary> /// <param name="_byteArray">解包的数据流</param> /// <param name="_packageSize">输出数据包整体大小</param> /// <returns>解包后的对象</returns> public object DePackage(byte[] _byteArray, out uint _packageSize, bool _completely, SocketSession _session = null) { if (!this.Check(_byteArray, _completely)) { throw new Exception("Can not depackage this stream."); } LztpPackage _package = new LztpPackage(); byte[] _packageHeader = new byte[64]; Array.Copy(_byteArray, 0, _packageHeader, 0, 64); byte[] _packageVersion = new byte[4]; Array.Copy(_packageHeader, 4, _packageVersion, 0, 4); byte[] _packageLength = new byte[4]; Array.Copy(_packageHeader, 8, _packageLength, 0, 4); byte[] _packageBodyCRC32 = new byte[4]; Array.Copy(_packageHeader, 12, _packageBodyCRC32, 0, 4); byte[] _packageCommand1 = new byte[2]; Array.Copy(_packageHeader, 16, _packageCommand1, 0, 2); byte[] _packageCommand2 = new byte[2]; Array.Copy(_packageHeader, 18, _packageCommand2, 0, 2); byte[] _packageCommandId = new byte[4]; Array.Copy(_packageHeader, 20, _packageCommandId, 0, 4); byte[] _packageTimestampSend = new byte[8]; Array.Copy(_packageHeader, 24, _packageTimestampSend, 0, 8); byte[] _packageTimestampReceive = new byte[8]; Array.Copy(_packageHeader, 32, _packageTimestampReceive, 0, 8); byte[] _packageTimestampResponse = new byte[8]; Array.Copy(_packageHeader, 40, _packageTimestampResponse, 0, 8); byte[] _packageHeaderCRC32 = new byte[4]; Array.Copy(_packageHeader, 60, _packageHeaderCRC32, 0, 4); _package.Version = this.ByteArrayToUInt32(_packageVersion); _package.Length = this.ByteArrayToUInt32(_packageLength); _package.BodyHeadCRC32 = this.ByteArrayToUInt32(_packageBodyCRC32); _package.Command1 = this.ByteArrayToUInt16(_packageCommand1); _package.Command2 = this.ByteArrayToUInt16(_packageCommand2); _package.CommandId = this.ByteArrayToUInt32(_packageCommandId); _package.TimestampSend = this.ByteArrayToDouble(_packageTimestampSend); _package.TimestampReceive = this.GetTimestamp(DateTime.Now); _package.TimestampResponse = this.ByteArrayToDouble(_packageTimestampResponse); _package.HeadCRC32 = this.ByteArrayToUInt32(_packageHeaderCRC32); _package.BodyType = new byte(); _package.BodyLength = 0; _package.BodyCRC32 = 0; _package.FieldCount = 0; _package.Fields = new object[0]; _package.FieldTypes = new byte[0]; _package.ReceivedLength = _byteArray.Length - 64; if (_package.ReceivedLength > _package.Length - 64) { _package.ReceivedLength = int.Parse(_package.Length.ToString()) - 64; } if (_completely && _package.Length > 64) { _package.BodyType = _byteArray[64]; //string _byteString = ""; //for (int i = 0; i < _byteArray.Length; i++) //{ // _byteString += i > 0 && i % 8 == 0 ? "\n" : ""; // _byteString += _byteArray[i].ToString("X2") + " "; //} if (_package.BodyType == 0x00) { #region 0x00 数据包 byte[] _bodyLength = new byte[4]; Array.Copy(_byteArray, 65, _bodyLength, 0, 4); _package.BodyLength = this.ByteArrayToUInt32(_bodyLength); byte[] _bodyCRC32 = new byte[4]; Array.Copy(_byteArray, 69, _bodyCRC32, 0, 4); _package.BodyCRC32 = this.ByteArrayToUInt32(_bodyCRC32); byte[] _fieldCount = new byte[4]; Array.Copy(_byteArray, 73, _fieldCount, 0, 4); _package.FieldCount = this.ByteArrayToUInt32(_fieldCount); byte[] _fieldTypes = new byte[_package.FieldCount]; Array.Copy(_byteArray, 77, _fieldTypes, 0, _fieldTypes.Length); _package.FieldTypes = _fieldTypes; if (_package.FieldCount != _package.FieldTypes.Length) { throw new Exception("Field count and type length not match."); } if (_package.FieldCount > 100000) { throw new Exception("Field count can not more then 100000."); } _package.Fields = new object[_package.FieldCount]; uint _position = 77 + (uint)_package.FieldTypes.Length; uint _fieldSize = 0; for (UInt32 i = 0; i < _fieldTypes.Length; i++) { _package.Fields[i] = this.GetFromByteArray(_byteArray, _position, _package.FieldTypes[i], ref _fieldSize); _position += _fieldSize; } #endregion } else if (_package.BodyType == 0x01) { _packageSize = 0; } } _packageSize = _package.Length; return(_package); }
/// <summary> /// 数据打包 /// </summary> /// <param name="_objects">需要打包的对象</param> /// <returns>打包后的数据</returns> public byte[] EnPackage(object _object, SocketSession _session = null) { if (_object == null) { return(new byte[0]); } LztpPackage _package = (LztpPackage)_object; if (_package.TimestampSend == 0) { _package.TimestampSend = this.GetTimestamp(DateTime.UtcNow); } else { _package.TimestampResponse = this.GetTimestamp(DateTime.UtcNow); } MemoryStream _body_stream = new MemoryStream(); byte[] _body_Fields = new byte[_package.Fields.Length]; #region Body_Fields,Body_Content for (int i = 0; i < _package.Fields.Length; i++) { _body_Fields[i] = this.AppendToByteArray(ref _body_stream, _package.Fields[i]); } byte[] _body_Content = _body_stream.ToArray(); _body_stream.Close(); #endregion byte[] _body_Header = new byte[13 + _body_Fields.Length]; #region Body_Header _body_Header[0] = 0x00; Array.Copy(this.UInt32ToByteArray(UInt32.Parse(_body_Content.Length.ToString())), 0, _body_Header, 1, 4); Array.Copy(this.UInt32ToByteArray(this.GetCRC32(_body_Content)), 0, _body_Header, 5, 4); Array.Copy(this.UInt32ToByteArray(UInt32.Parse(_package.Fields.Length.ToString())), 0, _body_Header, 9, 4); Array.Copy(_body_Fields, 0, _body_Header, 13, _body_Fields.Length); #endregion byte[] _packageArray = new byte[64 + _body_Header.Length + _body_Content.Length]; byte[] _head_Content = new byte[60]; #region Head_Content Array.Copy(this.Head, 0, _head_Content, 0, 4); Array.Copy(this.UInt32ToByteArray(_package.Version), 0, _head_Content, 4, 4); Array.Copy(this.UInt32ToByteArray(UInt32.Parse(_packageArray.Length.ToString())), 0, _head_Content, 8, 4); Array.Copy(this.UInt32ToByteArray(this.GetCRC32(_body_Header)), 0, _head_Content, 12, 4); Array.Copy(this.UInt16ToByteArray(_package.Command1), 0, _head_Content, 16, 2); Array.Copy(this.UInt16ToByteArray(_package.Command2), 0, _head_Content, 18, 2); Array.Copy(this.UInt32ToByteArray(_package.CommandId), 0, _head_Content, 20, 4); Array.Copy(this.DoubleToByteArray(_package.TimestampSend), 0, _head_Content, 24, 8); Array.Copy(this.DoubleToByteArray(_package.TimestampReceive), 0, _head_Content, 32, 8); Array.Copy(this.DoubleToByteArray(_package.TimestampResponse), 0, _head_Content, 40, 8); for (int i = 48; i < 60; i++) { _head_Content[i] = 0x0; } #endregion byte[] _head_CRC32 = new byte[4]; #region Head_CRC32 Array.Copy(this.UInt32ToByteArray(this.GetCRC32(_head_Content)), 0, _head_CRC32, 0, 4); #endregion Array.Copy(_head_Content, 0, _packageArray, 0, _head_Content.Length); Array.Copy(_head_CRC32, 0, _packageArray, _head_Content.Length, _head_CRC32.Length); Array.Copy(_body_Header, 0, _packageArray, _head_Content.Length + _head_CRC32.Length, _body_Header.Length); Array.Copy(_body_Content, 0, _packageArray, _head_Content.Length + _head_CRC32.Length + _body_Header.Length, _body_Content.Length); return(_packageArray); }