Example #1
0
        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());
        }
Example #2
0
        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());
            }
        }
Example #3
0
        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());
        }
Example #4
0
 public HMessage(ushort Header, HDestinations Destination, HProtocols Protocol, params object[] Chunks)
     : this(Construct(Header, Destination, Protocol, Chunks), Destination)
 {
     LogWriting = true;
     _Appended.AddRange(Chunks);
 }