public TCPPacketFlags GetPacketFlags() { TCPPacketFlags F = default(TCPPacketFlags); this.Header.Seek(4, System.IO.SeekOrigin.Begin); F = (TCPPacketFlags)this.Header.BR.ReadByte(); return(F); }
public byte[] GetPacketDgram() { TCPPacketFlags Flags = this.GetPacketFlags(); byte[] Dgram = null; try { if ((Flags & TCPPacketFlags.GZipCompressed) > 0) { this.Content.Seek(0, System.IO.SeekOrigin.Begin); int OriginalLength = this.Content.BR.ReadInt32(); System.IO.Compression.GZipStream GZip = new System.IO.Compression.GZipStream(this.Content, System.IO.Compression.CompressionMode.Decompress); //Dim DecompressedBuffer As Byte() = New Byte(OriginalLength - 1) {} //Dim totalCount As Integer = TCPPacketBuilder.ReadAllBytesFromStream(GZip, DecompressedBuffer) Dgram = new byte[OriginalLength]; GZip.Read(Dgram, 0, OriginalLength); GZip.Close(); } else { //Dgram = New Byte(totalCount - 1) {} //Array.Copy(DecompressedBuffer, 0, Dgram, 0, totalCount) Dgram = this.Content.ToArray(); } this.Header.Close(); this.Content.Close(); this.SetState(PacketState.Empty); } catch (Exception ex) { throw ex; } return(Dgram); }
public byte[] BuildPacket(byte[] dgram, TCPPacketFlags Flags) { DataStream OutDS = null; DataStream InputDS = null; byte[] ret = null; try { OutDS = new DataStream(); InputDS = new DataStream(); //build input dgram if ((Flags & TCPPacketFlags.GZipCompressed) > 0) { InputDS.BW.Write((int)dgram.Length); System.IO.Compression.GZipStream GZip = new System.IO.Compression.GZipStream(InputDS, System.IO.Compression.CompressionMode.Compress, true); GZip.Write(dgram, 0, dgram.Length); GZip.Close(); //versneltruck: kijk of de gecompressed versie wel echt kleiner is if (InputDS.Length > dgram.Length) { //groter ==> geen compressie gebruiken return(this.BuildPacket(dgram, Flags ^ TCPPacketFlags.GZipCompressed)); } } else { InputDS.Write(dgram, 0, dgram.Length); } //header: OutDS.BW.Write((int)InputDS.Length); OutDS.BW.Write((byte)Flags); //content: InputDS.WriteTo(OutDS); ret = OutDS.ToArray(); InputDS.Close(); OutDS.Close(); } catch (Exception ex) { throw; } finally { if (InputDS != null) { InputDS.Close(); InputDS = null; } if (OutDS != null) { OutDS.Close(); OutDS = null; } } return(ret); }