示例#1
0
        private void ProcessData()
        {
            while (true)
            {
                // If the parser is already handling a message
                // and if the reader has all the bytes we need
                if (_currentMessageLength != 0 && DataLength >= _currentMessageLength)
                {
                    var bytes = _reader.ReadBytes(_currentMessageLength);
                    _currentMessageLength = 0;

                    MessageParsed?.Invoke(bytes);
                    continue;
                }

                // Otherwise check if we can read the message's length
                if (DataLength >= 4)
                {
                    _currentMessageLength = NetworkUtils.BytesToIntBigEndian(_reader.ReadBytes(4));
                    continue;
                }

                // If we can't read anything anymore, might aswell clear the stream
                if (DataLength == 0)
                {
                    _reader.Dispose();
                    _reader = new BinaryReader(new MemoryStream());
                }

                break;
            }
        }