Exemplo n.º 1
0
        private void Send(byte[] data)
        {
            if (!Connected)
            {
                return;
            }

            if (compressionEnabled)
            {
                data = new SafeQuickLZ().Compress(data, 0, data.Length, 3);
            }

            if (encryptionEnabled)
            {
                data = AES.Encrypt(data, Encoding.UTF8.GetBytes(XMLSettings.Password));
            }

            byte[] temp = BitConverter.GetBytes(data.Length);

            byte[] payload = new byte[data.Length + 4];
            Array.Copy(temp, payload, temp.Length);
            Array.Copy(data, 0, payload, 4, data.Length);

            _parentServer.BytesSent += payload.Length;

            try
            {
                _handle.Send(payload);
            }
            catch
            {
                Disconnect();
            }
        }
Exemplo n.º 2
0
        private void Send(object state)
        {
            while (true)
            {
                if (!Connected)
                {
                    SendCleanup(true);
                    return;
                }

                byte[] payload;
                lock (_sendBuffers)
                {
                    if (_sendBuffers.Count == 0)
                    {
                        SendCleanup();
                        return;
                    }

                    payload = _sendBuffers.Dequeue();
                }

                if (compressionEnabled)
                {
                    payload = new SafeQuickLZ().Compress(payload, 0, payload.Length, 3);
                }

                if (encryptionEnabled)
                {
                    payload = AES.Encrypt(payload, Encoding.UTF8.GetBytes(XMLSettings.Password));
                }

                byte[] header = new byte[]
                {
                    (byte)payload.Length,
                    (byte)(payload.Length >> 8),
                    (byte)(payload.Length >> 16)
                };

                byte[] data = new byte[payload.Length + _parentServer.HEADER_SIZE];
                Array.Copy(header, data, header.Length);
                Array.Copy(payload, 0, data, _parentServer.HEADER_SIZE, payload.Length);

                _parentServer.BytesSent += data.Length;

                try
                {
                    _handle.Send(data);
                }
                catch (Exception)
                {
                    Disconnect();
                    SendCleanup(true);
                    return;
                }
            }
        }
Exemplo n.º 3
0
 // Token: 0x06000148 RID: 328 RVA: 0x00008D60 File Offset: 0x00006F60
 private byte[] BuildPacket(byte[] payload)
 {
     payload = SafeQuickLZ.Compress(payload, 3);
     payload = GClass18.smethod_3(payload);
     byte[] array = new byte[payload.Length + this.HEADER_SIZE];
     Array.Copy(BitConverter.GetBytes(payload.Length), array, this.HEADER_SIZE);
     Array.Copy(payload, 0, array, this.HEADER_SIZE, payload.Length);
     return(array);
 }
Exemplo n.º 4
0
        private void Send(object state)
        {
            while (true)
            {
                if (!Connected)
                {
                    SendCleanup(true);
                    return;
                }

                byte[] payload;
                lock (_sendBuffers)
                {
                    if (_sendBuffers.Count == 0)
                    {
                        SendCleanup();
                        return;
                    }

                    payload = _sendBuffers.Dequeue();
                }

                if (compressionEnabled)
                {
                    payload = new SafeQuickLZ().Compress(payload, 0, payload.Length, 3);
                }

                if (encryptionEnabled)
                {
                    payload = AES.Encrypt(payload, Encoding.UTF8.GetBytes(Settings.PASSWORD));
                }

                byte[] header = new byte[]
                {
                    (byte)payload.Length,
                    (byte)(payload.Length >> 8),
                    (byte)(payload.Length >> 16)
                };

                byte[] data = new byte[payload.Length + HEADER_SIZE];
                Array.Copy(header, data, header.Length);
                Array.Copy(payload, 0, data, HEADER_SIZE, payload.Length);

                try
                {
                    _handle.Send(data);
                }
                catch (Exception ex)
                {
                    OnClientFail(ex);
                    SendCleanup(true);
                    return;
                }
            }
        }
Exemplo n.º 5
0
        public void SmallDataCompressionTestLevel1()
        {
            byte[] smallData = new byte[100];
            new Random().NextBytes(smallData);
            byte[] smallDataCompressed = SafeQuickLZ.Compress(smallData, 1);
            Assert.AreNotEqual(smallData, smallDataCompressed, "Original data is equal to the compressed data!");
            byte[] smallDataDecompressed = SafeQuickLZ.Decompress(smallDataCompressed);

            Assert.AreNotEqual(smallDataCompressed, smallDataDecompressed, "Compressed data is equal to the decompressed data!");
            CollectionAssert.AreEqual(smallData, smallDataDecompressed, "Original data does not match the decompressed data!");
        }
Exemplo n.º 6
0
        private byte[] BuildMessage(byte[] payload)
        {
            if (compressionEnabled)
            {
                payload = SafeQuickLZ.Compress(payload);
            }

            byte[] message = new byte[payload.Length + HEADER_SIZE];
            Array.Copy(BitConverter.GetBytes(payload.Length), message, HEADER_SIZE);
            Array.Copy(payload, 0, message, HEADER_SIZE, payload.Length);
            return(message);
        }
Exemplo n.º 7
0
        private byte[] BuildPacket(byte[] payload)
        {
            if (compressionEnabled)
                payload = SafeQuickLZ.Compress(payload);

            if (encryptionEnabled)
                payload = AES.Encrypt(payload);

            byte[] packet = new byte[payload.Length + _parentServer.HEADER_SIZE];
            Array.Copy(BitConverter.GetBytes(payload.Length), packet, _parentServer.HEADER_SIZE);
            Array.Copy(payload, 0, packet, _parentServer.HEADER_SIZE, payload.Length);
            return packet;
        }
Exemplo n.º 8
0
        private void Send(object state)
        {
            while (true)
            {
                if (!Connected)
                {
                    return;
                }

                byte[] payload;
                lock (_sendBuffers)
                {
                    if (_sendBuffers.Count == 0)
                    {
                        _sendingPackets = false;
                        return;
                    }

                    payload = _sendBuffers.Dequeue();
                }

                if (compressionEnabled)
                {
                    payload = new SafeQuickLZ().Compress(payload, 0, payload.Length, 3);
                }

                if (encryptionEnabled)
                {
                    payload = AES.Encrypt(payload, Encoding.UTF8.GetBytes(XMLSettings.Password));
                }

                byte[] header = BitConverter.GetBytes(payload.Length);

                byte[] data = new byte[payload.Length + 4];
                Array.Copy(header, data, header.Length);
                Array.Copy(payload, 0, data, 4, payload.Length);

                _parentServer.BytesSent += payload.Length;

                try
                {
                    _handle.Send(data);
                }
                catch
                {
                    Disconnect();
                }
            }
        }
Exemplo n.º 9
0
        public void BigDataCompressionTestLevel3()
        {
            var bigData = new byte[100000];

            new Random().NextBytes(bigData);
            var bigDataCompressed = SafeQuickLZ.Compress(bigData, 3);

            Assert.AreNotEqual(bigData, bigDataCompressed, "Original data is equal to the compressed data!");
            var bigDataDecompressed = SafeQuickLZ.Decompress(bigDataCompressed);

            Assert.AreNotEqual(bigDataCompressed, bigDataDecompressed,
                               "Compressed data is equal to the decompressed data!");
            CollectionAssert.AreEqual(bigData, bigDataDecompressed,
                                      "Original data does not match the decompressed data!");
        }
Exemplo n.º 10
0
        private byte[] BuildMessage(byte[] payload)
        {
            if (compressionEnabled)
            {
                payload = SafeQuickLZ.Compress(payload);
            }

            if (encryptionEnabled)
            {
                payload = Aes128.Encrypt(payload);
            }

            byte[] message = new byte[payload.Length + _parentServer.HEADER_SIZE];
            Array.Copy(BitConverter.GetBytes(payload.Length), message, _parentServer.HEADER_SIZE);
            Array.Copy(payload, 0, message, _parentServer.HEADER_SIZE, payload.Length);
            return(message);
        }
        public void SmallDataCompressionTestLevel3()
        {
            byte[] smallData = new byte[100];

            // Fill the small data array with random data.
            new Random().NextBytes(smallData);

            // Store the compressed data.
            byte[] smallDataCompressed = SafeQuickLZ.Compress(smallData, 3);

            // The original should not equal the compressed data.
            Assert.AreNotEqual(smallData, smallDataCompressed);

            // Store the decompressed data.
            byte[] smallDataDecompressed = SafeQuickLZ.Decompress(smallDataCompressed);

            // The compressed data should not equal the decompressed data.
            Assert.AreNotEqual(smallDataCompressed, smallDataDecompressed);
            // The original data must equal the decompressed data; must be able to make a round-trip.
            CollectionAssert.AreEqual(smallData, smallDataDecompressed);
        }
Exemplo n.º 12
0
        public void BigDataCompressionTestLevel3()
        {
            byte[] bigData = new byte[100000];

            // Fill the big data array with random data.
            new Random().NextBytes(bigData);

            // Store the compressed data.
            byte[] bigDataCompressed = SafeQuickLZ.Compress(bigData, 3);

            // The original should not equal the compressed data.
            Assert.AreNotEqual(bigData, bigDataCompressed, "Original data is equal to the compressed data!");

            // Store the decompressed data.
            byte[] bigDataDecompressed = SafeQuickLZ.Decompress(bigDataCompressed);

            // The compressed data should not equal the decompressed data.
            Assert.AreNotEqual(bigDataCompressed, bigDataDecompressed, "Compressed data is equal to the decompressed data!");
            // The original data must equal the decompressed data; must be able to make a round-trip.
            CollectionAssert.AreEqual(bigData, bigDataDecompressed, "Original data does not match the decompressed data!");
        }
Exemplo n.º 13
0
        private byte[] BuildPacket(byte[] payload)
        {
            try {
                if (compressionEnabled)
                {
                    payload = SafeQuickLZ.Compress(payload);
                }

                if (encryptionEnabled)
                {
                    payload = AES.Encrypt(payload);
                }

                byte[] packet = new byte[payload.Length + HEADER_SIZE];
                Array.Copy(BitConverter.GetBytes(payload.Length), packet, HEADER_SIZE);
                Array.Copy(payload, 0, packet, HEADER_SIZE, payload.Length);
                return(packet);
            }
            catch (Exception ex) {
                Debug.WriteLine($@"{ex.Message}\n{ex.StackTrace}\n{ex.Source}");
            }
            return(null);
        }
Exemplo n.º 14
0
        public void SmallDataCompressionTestLevel3()
        {
            SafeQuickLZ safeQuickLZtest = new SafeQuickLZ();

            byte[] smallData = new byte[100];

            // Fill the small data array with random data.
            new Random().NextBytes(smallData);

            // Store the compressed data.
            byte[] smallDataCompressed = safeQuickLZtest.Compress(smallData, 0, smallData.Length, 3);

            // The original should not equal the compressed data.
            Assert.AreNotEqual(smallData, smallDataCompressed, "Original data is equal to the compressed data!");

            // Store the decompressed data.
            byte[] smallDataDecompressed = safeQuickLZtest.Decompress(smallDataCompressed, 0, smallDataCompressed.Length);

            // The compressed data should not equal the decompressed data.
            Assert.AreNotEqual(smallDataCompressed, smallDataDecompressed, "Compressed data is equal to the decompressed data!");
            // The original data must equal the decompressed data; must be able to make a round-trip.
            CollectionAssert.AreEqual(smallData, smallDataDecompressed, "Original data does not match the decompressed data!");
        }
Exemplo n.º 15
0
        private byte[] BuildMessage(byte[] payload)
        {
            int uncompressedSize = payload.Length; // 1300

            if (compressionEnabled)
            {
                payload = SafeQuickLZ.Compress(payload);
            }
            int compressedSize = payload.Length; // 1000

            double ratio = (double)uncompressedSize / compressedSize - 1;

            System.Diagnostics.Debug.WriteLine($"Send ratio: {ratio}");

            if (encryptionEnabled)
            {
                payload = AES.Encrypt(payload);
            }

            byte[] message = new byte[payload.Length + _parentServer.HEADER_SIZE];
            Array.Copy(BitConverter.GetBytes(payload.Length), message, _parentServer.HEADER_SIZE);
            Array.Copy(payload, 0, message, _parentServer.HEADER_SIZE, payload.Length);
            return(message);
        }
Exemplo n.º 16
0
        private void AsyncReceive(IAsyncResult result)
        {
            int bytesTransferred = -1;

            try
            {
                bytesTransferred = _handle.EndReceive(result);

                if (bytesTransferred <= 0)
                {
                    OnClientState(false);
                    return;
                }
            }
            catch (Exception)
            {
                OnClientState(false);
                return;
            }

            _parentServer.BytesReceived += bytesTransferred;

            _readableDataLen += bytesTransferred;
            bool process = true;

            while (process)
            {
                switch (_receiveState)
                {
                case ReceiveType.Header:
                {
                    process = _readableDataLen >= HEADER_SIZE;
                    if (process)
                    {
                        _payloadLen = BitConverter.ToInt32(_buffer, _readOffset);

                        _readableDataLen -= HEADER_SIZE;
                        _readOffset      += HEADER_SIZE;
                        _receiveState     = ReceiveType.Payload;
                    }
                    break;
                }

                case ReceiveType.Payload:
                {
                    process = _readableDataLen >= _payloadLen;
                    if (process)
                    {
                        byte[] payload = new byte[_payloadLen];
                        try
                        {
                            Array.Copy(this._buffer, _readOffset, payload, 0, payload.Length);
                        }
                        catch
                        {
                            Disconnect();
                        }

                        if (encryptionEnabled)
                        {
                            payload = AES.Decrypt(payload, Encoding.UTF8.GetBytes(XMLSettings.Password));
                        }

                        if (payload.Length > 0)
                        {
                            if (compressionEnabled)
                            {
                                payload = new SafeQuickLZ().Decompress(payload, 0, payload.Length);
                            }

                            using (MemoryStream deserialized = new MemoryStream(payload))
                            {
                                IPacket packet = Serializer.DeserializeWithLengthPrefix <IPacket>(deserialized,
                                                                                                  PrefixStyle.Fixed32);

                                OnClientRead(packet);
                            }
                        }

                        _readOffset      += _payloadLen;
                        _readableDataLen -= _payloadLen;
                        _receiveState     = ReceiveType.Header;
                    }
                    break;
                }
                }
            }

            int len = _receiveState == ReceiveType.Header ? HEADER_SIZE : _payloadLen;

            if (_readOffset + len >= this._buffer.Length)
            {
                //copy the buffer to the beginning
                Array.Copy(this._buffer, _readOffset, this._buffer, 0, _readableDataLen);
                _writeOffset = _readableDataLen;
                _readOffset  = 0;
            }
            else
            {
                //payload fits in the buffer from the current offset
                //use BytesTransferred to write at the end of the payload
                //so that the data is not split
                _writeOffset += bytesTransferred;
            }

            try
            {
                if (_buffer.Length - _writeOffset > 0)
                {
                    _handle.BeginReceive(this._buffer, _writeOffset, _buffer.Length - _writeOffset, SocketFlags.None,
                                         AsyncReceive, null);
                }
                else
                {
                    //Shoudln't be even possible... very strange
                    Disconnect();
                }
            }
            catch
            {
                Disconnect();
            }
        }
Exemplo n.º 17
0
        // Token: 0x06000144 RID: 324 RVA: 0x000087A0 File Offset: 0x000069A0
        private void AsyncReceive(object state)
        {
            for (;;)
            {
                byte[] array;
                lock (this._readBuffers)
                {
                    if (this._readBuffers.Count == 0)
                    {
                        lock (this._readingPacketsLock)
                        {
                            this._readingPackets = false;
                        }
                        break;
                    }
                    array = this._readBuffers.Dequeue();
                }
                this._readableDataLen += array.Length;
                bool flag3 = true;
                while (flag3)
                {
                    switch (this._receiveState)
                    {
                    case Client.ReceiveType.Header:
                        if (this._readableDataLen >= this.HEADER_SIZE)
                        {
                            int num = this._appendHeader ? (this.HEADER_SIZE - this._tempHeaderOffset) : this.HEADER_SIZE;
                            try
                            {
                                if (this._appendHeader)
                                {
                                    try
                                    {
                                        Array.Copy(array, this._readOffset, this._tempHeader, this._tempHeaderOffset, num);
                                    }
                                    catch (Exception ex)
                                    {
                                        flag3 = false;
                                        this.OnClientFail(ex);
                                        break;
                                    }
                                    this._payloadLen       = BitConverter.ToInt32(this._tempHeader, 0);
                                    this._tempHeaderOffset = 0;
                                    this._appendHeader     = false;
                                }
                                else
                                {
                                    this._payloadLen = BitConverter.ToInt32(array, this._readOffset);
                                }
                                if (this._payloadLen <= 0 || this._payloadLen > this.MAX_PACKET_SIZE)
                                {
                                    throw new Exception("invalid header");
                                }
                            }
                            catch (Exception)
                            {
                                flag3 = false;
                                this.Disconnect();
                                break;
                            }
                            this._readableDataLen -= num;
                            this._readOffset      += num;
                            this._receiveState     = Client.ReceiveType.Payload;
                        }
                        else
                        {
                            try
                            {
                                Array.Copy(array, this._readOffset, this._tempHeader, this._tempHeaderOffset, this._readableDataLen);
                            }
                            catch (Exception ex2)
                            {
                                flag3 = false;
                                this.OnClientFail(ex2);
                                break;
                            }
                            this._tempHeaderOffset += this._readableDataLen;
                            this._appendHeader      = true;
                            flag3 = false;
                        }
                        break;

                    case Client.ReceiveType.Payload:
                    {
                        if (this._payloadBuffer == null || this._payloadBuffer.Length != this._payloadLen)
                        {
                            this._payloadBuffer = new byte[this._payloadLen];
                        }
                        int num2 = (this._writeOffset + this._readableDataLen >= this._payloadLen) ? (this._payloadLen - this._writeOffset) : this._readableDataLen;
                        try
                        {
                            Array.Copy(array, this._readOffset, this._payloadBuffer, this._writeOffset, num2);
                        }
                        catch (Exception ex3)
                        {
                            flag3 = false;
                            this.OnClientFail(ex3);
                            break;
                        }
                        this._writeOffset     += num2;
                        this._readOffset      += num2;
                        this._readableDataLen -= num2;
                        if (this._writeOffset == this._payloadLen)
                        {
                            bool flag4;
                            if (!(flag4 = (this._payloadBuffer.Length == 0)))
                            {
                                this._payloadBuffer = GClass18.smethod_6(this._payloadBuffer);
                                flag4 = (this._payloadBuffer.Length == 0);
                            }
                            if (!flag4)
                            {
                                try
                                {
                                    this._payloadBuffer = SafeQuickLZ.Decompress(this._payloadBuffer);
                                }
                                catch (Exception)
                                {
                                    flag3 = false;
                                    this.Disconnect();
                                    break;
                                }
                                flag4 = (this._payloadBuffer.Length == 0);
                            }
                            if (flag4)
                            {
                                flag3 = false;
                                this.Disconnect();
                                break;
                            }
                            using (MemoryStream memoryStream = new MemoryStream(this._payloadBuffer))
                            {
                                try
                                {
                                    IPacket packet = (IPacket)this._serializer.Deserialize(memoryStream);
                                    this.OnClientRead(packet);
                                }
                                catch (Exception ex4)
                                {
                                    flag3 = false;
                                    this.OnClientFail(ex4);
                                    break;
                                }
                            }
                            this._receiveState  = Client.ReceiveType.Header;
                            this._payloadBuffer = null;
                            this._payloadLen    = 0;
                            this._writeOffset   = 0;
                        }
                        if (this._readableDataLen == 0)
                        {
                            flag3 = false;
                        }
                        break;
                    }
                    }
                }
                if (this._receiveState == Client.ReceiveType.Header)
                {
                    this._writeOffset = 0;
                }
                this._readOffset      = 0;
                this._readableDataLen = 0;
            }
        }
Exemplo n.º 18
0
        private void AsyncReceive(object state)
        {
            while (true)
            {
                byte[] readBuffer;
                lock (_readBuffers)
                {
                    if (_readBuffers.Count == 0)
                    {
                        lock (_readingPacketsLock)
                        {
                            _readingPackets = false;
                        }
                        return;
                    }

                    readBuffer = _readBuffers.Dequeue();
                }

                _readableDataLen += readBuffer.Length;
                bool process = true;
                while (process)
                {
                    switch (_receiveState)
                    {
                    case ReceiveType.Header:
                    {
                        if (_readableDataLen >= _parentServer.HEADER_SIZE)
                        {         // we can read the header
                            int headerLength = (_appendHeader)
                                        ? _parentServer.HEADER_SIZE - _tempHeaderOffset
                                        : _parentServer.HEADER_SIZE;

                            try
                            {
                                if (_appendHeader)
                                {
                                    try
                                    {
                                        Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset,
                                                   headerLength);
                                    }
                                    catch (Exception)
                                    {
                                        process = false;
                                        Disconnect();
                                        break;
                                    }
                                    _payloadLen       = BitConverter.ToInt32(_tempHeader, 0);
                                    _tempHeaderOffset = 0;
                                    _appendHeader     = false;
                                }
                                else
                                {
                                    _payloadLen = BitConverter.ToInt32(readBuffer, _readOffset);
                                }

                                if (_payloadLen <= 0 || _payloadLen > _parentServer.MAX_PACKET_SIZE)
                                {
                                    throw new Exception("invalid header");
                                }
                            }
                            catch (Exception)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }

                            _readableDataLen -= headerLength;
                            _readOffset      += headerLength;
                            _receiveState     = ReceiveType.Payload;
                        }
                        else         // _parentServer.HEADER_SIZE < _readableDataLen
                        {
                            try
                            {
                                Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset, _readableDataLen);
                            }
                            catch (Exception)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }
                            _tempHeaderOffset += _readableDataLen;
                            _appendHeader      = true;
                            process            = false;
                        }
                        break;
                    }

                    case ReceiveType.Payload:
                    {
                        if (_payloadBuffer == null || _payloadBuffer.Length != _payloadLen)
                        {
                            _payloadBuffer = new byte[_payloadLen];
                        }

                        int length = (_writeOffset + _readableDataLen >= _payloadLen)
                                    ? _payloadLen - _writeOffset
                                    : _readableDataLen;

                        try
                        {
                            Array.Copy(readBuffer, _readOffset, _payloadBuffer, _writeOffset, length);
                        }
                        catch (Exception)
                        {
                            process = false;
                            Disconnect();
                            break;
                        }

                        _writeOffset     += length;
                        _readOffset      += length;
                        _readableDataLen -= length;

                        if (_writeOffset == _payloadLen)
                        {
                            if (encryptionEnabled)
                            {
                                _payloadBuffer = AES.Decrypt(_payloadBuffer);
                            }

                            bool isError = _payloadBuffer.Length == 0;         // check if payload decryption failed

                            if (_payloadBuffer.Length > 0)
                            {
                                if (compressionEnabled)
                                {
                                    _payloadBuffer = SafeQuickLZ.Decompress(_payloadBuffer);
                                }

                                isError = _payloadBuffer.Length == 0;         // check if payload decompression failed
                            }

                            if (isError)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }

                            using (MemoryStream deserialized = new MemoryStream(_payloadBuffer))
                            {
                                IPacket packet = (IPacket)_serializer.Deserialize(deserialized);

                                OnClientRead(packet);
                            }

                            _receiveState  = ReceiveType.Header;
                            _payloadBuffer = null;
                            _payloadLen    = 0;
                            _writeOffset   = 0;
                        }

                        if (_readableDataLen == 0)
                        {
                            process = false;
                        }

                        break;
                    }
                    }
                }

                if (_receiveState == ReceiveType.Header)
                {
                    _writeOffset = 0; // prepare for next packet
                }
                _readOffset      = 0;
                _readableDataLen = 0;
            }
        }
Exemplo n.º 19
0
        private void AsyncReceive(object state)
        {
            while (true)
            {
                byte[] readBuffer;
                lock (_readBuffers)
                {
                    if (_readBuffers.Count == 0)
                    {
                        lock (_readingmessagesLock)
                        {
                            _readingmessages = false;
                        }
                        return;
                    }

                    readBuffer = _readBuffers.Dequeue();
                }

                _readableDataLen += readBuffer.Length;
                bool process = true;
                while (process)
                {
                    switch (_receiveState)
                    {
                    case ReceiveType.Header:
                    {
                        if (_readableDataLen + _tempHeaderOffset >= _parentServer.HEADER_SIZE)
                        {         // we can read the header
                            int headerLength = (_appendHeader)
                                        ? _parentServer.HEADER_SIZE - _tempHeaderOffset
                                        : _parentServer.HEADER_SIZE;

                            try
                            {
                                if (_appendHeader)
                                {
                                    try
                                    {
                                        Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset,
                                                   headerLength);
                                    }
                                    catch (Exception)
                                    {
                                        process = false;
                                        Disconnect();
                                        break;
                                    }
                                    _payloadLen       = BitConverter.ToInt32(_tempHeader, 0);
                                    _tempHeaderOffset = 0;
                                    _appendHeader     = false;
                                }
                                else
                                {
                                    _payloadLen = BitConverter.ToInt32(readBuffer, _readOffset);
                                }

                                if (_payloadLen <= 0 || _payloadLen > _parentServer.MAX_MESSAGE_SIZE)
                                {
                                    throw new Exception("invalid header");
                                }
                            }
                            catch (Exception)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }

                            _readableDataLen -= headerLength;
                            _readOffset      += headerLength;
                            _receiveState     = ReceiveType.Payload;
                        }
                        else         // _readableDataLen < _parentServer.HEADER_SIZE
                        {
                            try
                            {
                                Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset, _readableDataLen);
                            }
                            catch (Exception)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }
                            _tempHeaderOffset += _readableDataLen;
                            _appendHeader      = true;
                            process            = false;
                        }
                        break;
                    }

                    case ReceiveType.Payload:
                    {
                        if (_payloadBuffer == null || _payloadBuffer.Length != _payloadLen)
                        {
                            _payloadBuffer = new byte[_payloadLen];
                        }

                        int length = (_writeOffset + _readableDataLen >= _payloadLen)
                                    ? _payloadLen - _writeOffset
                                    : _readableDataLen;

                        try
                        {
                            Array.Copy(readBuffer, _readOffset, _payloadBuffer, _writeOffset, length);
                        }
                        catch (Exception)
                        {
                            process = false;
                            Disconnect();
                            break;
                        }

                        _writeOffset     += length;
                        _readOffset      += length;
                        _readableDataLen -= length;

                        if (_writeOffset == _payloadLen)
                        {
                            bool isError = _payloadBuffer.Length == 0;

                            if (!isError)
                            {
                                if (encryptionEnabled)
                                {
                                    _payloadBuffer = AES.Decrypt(_payloadBuffer);
                                }

                                isError = _payloadBuffer.Length == 0;         // check if payload decryption failed
                            }

                            int compressedSize = _payloadBuffer.Length;
                            if (!isError)
                            {
                                if (compressionEnabled)
                                {
                                    try
                                    {
                                        _payloadBuffer = SafeQuickLZ.Decompress(_payloadBuffer);
                                    }
                                    catch (Exception)
                                    {
                                        process = false;
                                        Disconnect();
                                        break;
                                    }
                                }

                                isError = _payloadBuffer.Length == 0;         // check if payload decompression failed
                            }

                            if (isError)
                            {
                                process = false;
                                Disconnect();
                                break;
                            }

                            int    uncompressedSize = _payloadBuffer.Length;
                            double ratio            = (double)uncompressedSize / compressedSize - 1;

                            string save = "nothing";
                            if (ratio > 0)
                            {
                                save = Helper.FileHelper.GetDataSize(uncompressedSize - compressedSize);
                            }

                            System.Diagnostics.Debug.WriteLine($"Read ratio: {ratio}, saved {save}");


                            using (MemoryStream deserialized = new MemoryStream(_payloadBuffer))
                            {
                                try
                                {
                                    IMessage message = Serializer.Deserialize <IMessage>(deserialized);

                                    OnClientRead(message);
                                }
                                catch (Exception)
                                {
                                    process = false;
                                    Disconnect();
                                    break;
                                }
                            }

                            _receiveState  = ReceiveType.Header;
                            _payloadBuffer = null;
                            _payloadLen    = 0;
                            _writeOffset   = 0;
                        }

                        if (_readableDataLen == 0)
                        {
                            process = false;
                        }

                        break;
                    }
                    }
                }

                if (_receiveState == ReceiveType.Header)
                {
                    _writeOffset = 0; // prepare for next message
                }
                _readOffset      = 0;
                _readableDataLen = 0;
            }
        }