Ejemplo n.º 1
0
        protected int ReadInt(ref int index, HProtocol protocol)
        {
            if (index >= Body.Length)
            {
                return(0);
            }

            switch (Protocol)
            {
            case HProtocol.Modern:
            {
                if (index + 4 > Body.Length)
                {
                    return(0);
                }
                return(Modern.DecypherInt(Body[index++], Body[index++], Body[index++], Body[index++]));
            }

            case HProtocol.Ancient:
            {
                int length = (Body[index] >> 3) & 7;
                if (length < 1)
                {
                    length++;
                }
                if (index + length > Body.Length)
                {
                    return(0);
                }

                int value = Ancient.DecypherInt(Body, index);
                index += length;

                return(value);
            }

            default: return(0);
            }
        }
Ejemplo n.º 2
0
        public HMessage(byte[] data, HDestination 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 }");
            }

            Destination = destination;
            bool hasByteZero     = data.Contains(byte.MinValue);
            bool isAncientHeader = !hasByteZero && data.Length == 2 && data[1] != 1;

            if (!isAncientHeader && data.Length >= 6 && Modern.DecypherInt(data) == data.Length - 4)
            {
                Protocol = HProtocol.Modern;

                _header = Modern.DecypherShort(data, 4);
                Append(ByteUtils.CopyBlock(data, 4, data.Length - 4));

                if (data.Length == 6)
                {
                    _logWriting = true;
                }
            }
            else if ((destination == HDestination.Server && isAncientHeader) || (!hasByteZero && data.Length >= 5 && Ancient.DecypherShort(data, 1) == data.Length - 3))
            {
                Destination = HDestination.Server;
                Protocol    = HProtocol.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 && Destination != HDestination.Server))
            {
                Destination = HDestination.Client;
                Protocol    = HProtocol.Ancient;

                if (isAncientHeader)
                {
                    data = new byte[] { data[0], data[1], 1 }
                }
                ;
                _header = Ancient.DecypherShort(data);
                Append(data);

                if (data.Length == 3 || isAncientHeader)
                {
                    _logWriting = true;
                }
            }
            else
            {
                Body         = data;
                _bufferCache = data;
                IsCorrupted  = true;
                Length       = data.Length;
                _buffer.AddRange(data);
                _stringCache = ToString(data);
            }
        }
Ejemplo n.º 3
0
        public static byte[][] Split(ref byte[] cache, byte[] data, HDestination destination, HProtocol protocol)
        {
            lock (SplitLock)
            {
                if (cache != null)
                {
                    data  = Merge(cache, data);
                    cache = null;
                }

                var chunks = new List <byte[]>();
                if (protocol == HProtocol.Ancient && destination == HDestination.Client)
                {
                    if (!data.Contains((byte)1))
                    {
                        cache = data;
                    }
                    else
                    {
                        var 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 == HProtocol.Ancient);
                    int  offset    = isAncient ? 3 : 4;
                    int  length    = isAncient ? Ancient.DecypherShort(data, 1) : Modern.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) : Modern.DecypherInt(data);
                            }
                        }while (data.Length != 0);
                    }
                }
                return(chunks.ToArray());
            }
        }