Esempio n. 1
0
        protected string ReadString(ref int index, HProtocol protocol, HDestination destination)
        {
            if (index >= Body.Length)
            {
                return(string.Empty);
            }

            if (destination == HDestination.Server || protocol == HProtocol.Modern)
            {
                ushort length = ReadShort(ref index, protocol);
                byte[] data   = ByteUtils.CopyBlock(Body, (index += length) - length, length);
                return(Encoding.Default.GetString(data));
            }
            else
            {
                string chunk = _rawBody.Substring(index).Split((char)2)[0];
                index += chunk.Length + 1;
                return(chunk);
            }
        }
Esempio n. 2
0
 public string ReadString(ref int Index)
 {
     if (IsCorrupted)
     {
         return(string.Empty);
     }
     if (Protocol == HProtocols.Modern || (Protocol == HProtocols.Ancient && Destination == HDestinations.Server))
     {
         int    SLength = ReadShort(ref Index);
         byte[] SData   = ByteUtils.CopyBlock(Body, (Index += SLength) - SLength, SLength);
         return(Encoding.Default.GetString(SData));
     }
     else if (Protocol == HProtocols.Ancient && Destination == HDestinations.Client)
     {
         string Chunk = RawBody.Substring(Index).Split((char)2)[0];
         Index += Chunk.Length + 1;
         return(Chunk);
     }
     else
     {
         return(string.Empty);
     }
 }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
            }
        }