public static byte[] Construct(ushort Header, HDestinations Destination, HProtocols Protocol, params object[] Chunks) { if (Protocol == HProtocols.Unknown) { throw new Exception("You must specify a supported HProtocols value for this method. ( Ancient / Modern )"); } if (Protocol == HProtocols.Ancient && Destination == HDestinations.Unknown) { throw new Exception("Cannot construct the body of an Ancient type packet without a valid HDestinations value. ( Client / Server )"); } List <byte> Buffer = new List <byte>(); bool IsAncient = Protocol == HProtocols.Ancient; if (IsAncient && Destination == HDestinations.Server) { Buffer.Add(64); } Buffer.AddRange(IsAncient ? Ancient.CypherShort(Header) : BigEndian.CypherShort(Header)); Buffer.AddRange(ConstructBody(Destination, Protocol, Chunks)); if (!IsAncient || Destination == HDestinations.Server) { Buffer.InsertRange(IsAncient ? 1 : 0, IsAncient ? Ancient.CypherShort((ushort)(Buffer.Count - 1)) : BigEndian.CypherInt(Buffer.Count)); } else if (Buffer[Buffer.Count - 1] != 1) { Buffer.Add(1); } return(Buffer.ToArray()); }
public DataToEventArgs(byte[] Data, HDestinations Destination, int Step) : base() { this.Step = Step; Packet = new HMessage(Data, Destination); }
public static byte[][] Split(ref byte[] Cache, byte[] Data, HDestinations Destination, HProtocols Protocol) { lock (SplitLock) { if (Cache != null) { Data = Merge(Cache, Data); Cache = null; } List <byte[]> Chunks = new List <byte[]>(); if (Protocol == HProtocols.Ancient && Destination == HDestinations.Client) { if (!Data.Contains((byte)1)) { Cache = Data; } else { List <byte> Buffer = new List <byte>(); foreach (byte Value in Data) { Buffer.Add(Value); if (Value == 1) { Chunks.Add(Buffer.ToArray()); Buffer.Clear(); } } if (Buffer.Count > 0) { Cache = Buffer.ToArray(); } } } else { bool IsAncient = Protocol == HProtocols.Ancient; int Offset = IsAncient ? 3 : 4; int Length = IsAncient ? Ancient.DecypherShort(Data, 1) : BigEndian.DecypherInt(Data); if (Length == Data.Length - Offset) { Chunks.Add(Data); } else { do { if (Length > Data.Length - Offset) { Cache = Data; break; } Chunks.Add(CutBlock(ref Data, 0, Length + Offset)); if (Data.Length >= Offset) { Length = IsAncient ? Ancient.DecypherShort(Data, 1) : BigEndian.DecypherInt(Data); } }while (Data.Length != 0); } } return(Chunks.ToArray()); } }
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); } }
public HMessage(string Packet, HDestinations Destination) : this(ToBytes(Packet), Destination) { }
public static byte[] ConstructBody(HDestinations Destination, HProtocols Protocol, params object[] Chunks) { if (Protocol == HProtocols.Unknown) { throw new Exception("You must specify a supported HProtocols value for this method. ( Ancient / Modern )"); } if (Protocol == HProtocols.Ancient && Destination == HDestinations.Unknown) { throw new Exception("Cannot construct the body of an Ancient type packet without a valid HDestinations value. ( Client / Server )"); } List <byte> Buffer = new List <byte>(); bool IsAncient = Protocol == HProtocols.Ancient; for (int i = 0; i < Chunks.Length; i++) { object Chunk = Chunks[i]; if (Chunk == null) { throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i)); } if (Chunk is byte[]) { Buffer.AddRange((byte[])Chunk); } else { switch (Type.GetTypeCode(Chunk.GetType())) { case TypeCode.Int32: { int Value = (int)Chunk; Buffer.AddRange(IsAncient ? Ancient.CypherInt(Value) : BigEndian.CypherInt(Value)); break; } default: case TypeCode.String: { string Value = Chunk.ToString(); if (!IsAncient || Destination == HDestinations.Server) { Buffer.AddRange(IsAncient ? Ancient.CypherShort((ushort)Value.Length) : BigEndian.CypherShort((ushort)Value.Length)); Buffer.AddRange(Encoding.Default.GetBytes(Value)); } else { Buffer.AddRange(Encoding.Default.GetBytes(Value)); Buffer.Add(2); } break; } case TypeCode.Boolean: { bool Value = (bool)Chunk; Buffer.Add(IsAncient ? (byte)(Value ? 73 : 72) : Convert.ToByte(Value)); break; } case TypeCode.Byte: { byte Value = (byte)Chunk; Buffer.Add(Value); break; } } } } return(Buffer.ToArray()); }
public HMessage(ushort Header, HDestinations Destination, HProtocols Protocol, params object[] Chunks) : this(Construct(Header, Destination, Protocol, Chunks), Destination) { LogWriting = true; _Appended.AddRange(Chunks); }