public HMessage(byte[] data, HDestination destination) : this() { if (data == null) { throw new NullReferenceException(); } if (data.Length < 6) { throw new Exception("Insufficient data, minimum length is '6'(Six). [Length{4}][Header{2}]"); } Destination = destination; IsCorrupted = (BigEndian.DecypherInt(data) != data.Length - 4); if (!IsCorrupted) { Header = BigEndian.DecypherShort(data, 4); _body.AddRange(data); _body.RemoveRange(0, 6); Reconstruct(); } else { Length = data.Length; _toBytesCache = data; } }
public virtual ushort ReadShort(ref int index) { if (index >= Body.Length || index + 2 > Body.Length) { throw new Exception("Not enough data at the current position to begin reading a UInt16 type object."); } ushort value = BigEndian.DecypherShort(Body[index++], Body[index++]); AddToRead(value); return(value); }
public int ReadShort(ref int Index) { if (IsCorrupted) { return(0); } switch (Protocol) { case HProtocols.Modern: return(BigEndian.DecypherShort(Body[Index++], Body[Index++])); case HProtocols.Ancient: return(Ancient.DecypherShort(Body[Index++], Body[Index++])); default: return(0); } }
public HMessage(byte[] Data, HDestinations Destination) : this() { if (Data == null) { throw new NullReferenceException(); } if (Data.Length < 1) { throw new Exception("The minimum amount of bytes required to initialize an HMessage instance is 1(One). If the amount of bytes passed is < 3(Three), and >= 1(One), it will be immediately be identified as a corrupted packet. { IsCorrupted = true }"); } this.Destination = Destination; bool HasByteZero = Data.Contains(byte.MinValue); bool IsAncientHeader = !HasByteZero && Data.Length == 2 && Data[1] != 1; if (!IsAncientHeader && Data.Length >= 6 && BigEndian.DecypherInt(Data) == Data.Length - 4) { Protocol = HProtocols.Modern; _Header = BigEndian.DecypherShort(Data, 4); Append(ByteUtils.CopyBlock(Data, 4, Data.Length - 4)); if (Data.Length == 6) { LogWriting = true; } } else if ((Destination == HDestinations.Server && IsAncientHeader) || (!HasByteZero && Data.Length >= 5 && Ancient.DecypherShort(Data, 1) == Data.Length - 3)) { this.Destination = HDestinations.Server; Protocol = HProtocols.Ancient; _Header = Ancient.DecypherShort(Data, IsAncientHeader ? 0 : 3); Append(IsAncientHeader ? Data : ByteUtils.CopyBlock(Data, 3, Data.Length - 3)); if (Data.Length == 5 || IsAncientHeader) { LogWriting = true; } } else if (IsAncientHeader || (!HasByteZero && Data.Length >= 3 && Data[Data.Length - 1] == 1)) { this.Destination = HDestinations.Client; Protocol = HProtocols.Ancient; if (IsAncientHeader) { Data = new byte[3] { Data[0], Data[1], 1 } } ; _Header = Ancient.DecypherShort(Data); Append(Data); if (Data.Length == 3 || IsAncientHeader) { LogWriting = true; } } else { Body = Data; BCache = Data; IsCorrupted = true; Length = Data.Length; Buffer.AddRange(Data); SCache = ToString(Data); } }