public override bool Parse(BinaryReader br) { MemoryStream ms = null; BinaryReader brr = null; try { if (br.ReadUInt16() != Tag) { throw new Exception("包头标记错误"); } var len = br.ReadUInt16(); var crc = br.ReadUInt32(); var bytes = br.ReadBytes((int)len - 8); if (Crc32.Compute(bytes) != crc) { throw new Exception("crc32校验错误。"); } ms = new MemoryStream(bytes); ms.Position = 0; brr = new BinaryReader(ms); Seq_Id = brr.ReadUInt32(); Comm_Type = (CommunicationType)brr.ReadByte(); Enc_Type = (EncryptionType)brr.ReadByte(); Major_Ver = brr.ReadByte(); Minor_Ver = brr.ReadByte(); Term_Code = brr.ReadStingWithFixLength(12); var Serv_Type = (ServiceType)brr.ReadUInt16(); var Serv_Code = brr.ReadUInt16(); // IPAddress = brr.ReadBytes(4); // Port = brr.ReadUInt16(); // MAC_Addr = brr.ReadBytes(6); Flags = (TPKGHeadFlags)brr.ReadByte(); Priority = brr.ReadByte(); Reserved = brr.ReadUInt32(); Data = PacketFactory.Create(brr, Serv_Type, Serv_Code); //解析数据域,根据服务类型和服务代码确定是哪个具体功能 if (Flags.HasFlag(TPKGHeadFlags.SIDV)) //附加域 { SessionId = brr.ReadUInt32(); } if (Flags.HasFlag(TPKGHeadFlags.PWV)) { Password = brr.ReadStingWithFixLength(16); } if (Flags.HasFlag(TPKGHeadFlags.TPV)) { var t = brr.ReadBytes(6); Timestamp = new DateTime(t[5], t[4], t[3], t[2], t[1], t[0]); Expire_time = brr.ReadInt32(); } return(true); } catch (Exception e) { log.Error(e); return(false); } finally { if (brr != null) #if WindowsCE { brr.Close(); } #else { brr.Dispose(); } #endif if (ms != null) { ms.Dispose(); } } }
public override bool ToBytes(BinaryWriter bw) { MemoryStream ms = new MemoryStream(); try { BinaryWriter bww = new BinaryWriter(ms); //bww.Write(Tag); bww.Write(Seq_Id); bww.Write((byte)Comm_Type); bww.Write((byte)Enc_Type); bww.Write(Major_Ver); bww.Write(Minor_Ver); bww.WriteStingWithFixLength(Term_Code, 12); bww.Write((ushort)Serv_Type); bww.Write((ushort)Serv_Code); // bww.WriteBytesWithFixLength(IPAddress, 4); // bww.Write(Port); // bww.WriteBytesWithFixLength(MAC_Addr, 6); bww.Write((byte)Flags); bww.Write(Priority); bww.Write(Reserved); if (Data != null) { Data.ToBytes(bww); } if (Flags.HasFlag(TPKGHeadFlags.SIDV)) { bww.Write(SessionId ?? 0); } if (Flags.HasFlag(TPKGHeadFlags.PWV)) { bww.WriteStingWithFixLength(Password, 16); } if (Flags.HasFlag(TPKGHeadFlags.TPV)) { bww.Write(new byte[] { (byte)(Timestamp?.Second ?? 0), (byte)(Timestamp?.Minute ?? 0), (byte)(Timestamp?.Hour ?? 0), (byte)(Timestamp?.Day ?? 0), (byte)(Timestamp?.Minute ?? 0), (byte)(Timestamp?.Year ?? 0), }); bww.Write(Expire_time ?? 0); } var bytes = ms.ToArray(); ms.Dispose(); bw.Write(Tag); bw.Write((ushort)(bytes.Length + 8)); bw.Write(Crc32.Compute(bytes)); bw.Write(bytes); return(true); } catch (Exception e) { log.Error(e); return(false); } finally { if (ms != null) { ms.Dispose(); } } }