Esempio n. 1
0
        public LineReader(byte[] buffer, WrappedSocket socket, byte[] firstPackge, int index, int length,
                          Func <string, object, bool> onLineRead, Action <Exception, object> onException,
                          Action <byte[], int, int, object> onFinish,
                          Encoding encoding, string delimiter,
                          object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (socket == null)
            {
                throw new ArgumentNullException(nameof(socket));
            }
            if (onLineRead == null)
            {
                throw new ArgumentNullException(nameof(onLineRead));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (delimiter == null)
            {
                throw new ArgumentNullException(nameof(delimiter));
            }

            if (buffer.Length < length)
            {
                throw new ArgumentException("Line buffer length can't less than first package length!", nameof(buffer));
            }

            if (length > 0)
            {
                if (firstPackge == null)
                {
                    throw new ArgumentNullException(nameof(firstPackge));
                }
            }

            _socket      = socket;
            _onLineRead  = onLineRead;
            _onException = onException;
            _onFinish    = onFinish;
            _encoding    = encoding;
            // _delimiter = delimiter;
            _state = state;

            // decode delimiter
            _delimiterBytes = encoding.GetBytes(delimiter);

            if (_delimiterBytes.Length == 0)
            {
                throw new ArgumentException("Too short!", nameof(delimiter));
            }

            if (buffer.Length < _delimiterBytes.Length)
            {
                throw new ArgumentException("Too small!", nameof(buffer));
            }

            _delimiterSearch = new ByteSearch.SearchTarget(_delimiterBytes);

            _lineBuffer = buffer;

            if (length > 0)
            {
                // process first package
                if (buffer == firstPackge)
                {
                    _bufferDataIndex = index;
                }
                else
                {
                    Array.Copy(firstPackge, index, _lineBuffer, 0, length);
                }
                _bufferDataLength = length;

                try
                {
                    NewPackageRecv();
                }
                catch (Exception ex)
                {
                    OnException(ex);
                    OnFinish();
                }
            }
            else
            {
                // start reading
                socket.BeginReceive(_lineBuffer, 0, _lineBuffer.Length, 0, ReceiveCallback, 0);
            }
        }
Esempio n. 2
0
        public LineReader(byte[] buffer, WrappedSocket socket, byte[] firstPackge, int index, int length,
            Func<string, object, bool> onLineRead, Action<Exception, object> onException,
            Action<byte[], int, int, object> onFinish,
            Encoding encoding, string delimiter,
            object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (socket == null)
            {
                throw new ArgumentNullException(nameof(socket));
            }
            if (onLineRead == null)
            {
                throw new ArgumentNullException(nameof(onLineRead));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (delimiter == null)
            {
                throw new ArgumentNullException(nameof(delimiter));
            }

            if (buffer.Length < length)
            {
                throw new ArgumentException("Line buffer length can't less than first package length!", nameof(buffer));
            }

            if (length > 0)
            {
                if (firstPackge == null)
                {
                    throw new ArgumentNullException(nameof(firstPackge));
                }
            }

            _socket = socket;
            _onLineRead = onLineRead;
            _onException = onException;
            _onFinish = onFinish;
            _encoding = encoding;
            // _delimiter = delimiter;
            _state = state;

            // decode delimiter
            _delimiterBytes = encoding.GetBytes(delimiter);

            if (_delimiterBytes.Length == 0)
            {
                throw new ArgumentException("Too short!", nameof(delimiter));
            }

            if (buffer.Length < _delimiterBytes.Length)
            {
                throw new ArgumentException("Too small!", nameof(buffer));
            }

            _delimiterSearch = new ByteSearch.SearchTarget(_delimiterBytes);

            _lineBuffer = buffer;

            if (length > 0)
            {
                // process first package
                if (buffer == firstPackge)
                {
                    _bufferDataIndex = index;
                }
                else
                {
                    Array.Copy(firstPackge, index, _lineBuffer, 0, length);
                }
                _bufferDataLength = length;

                try
                {
                    NewPackageRecv();
                }
                catch (Exception ex)
                {
                    OnException(ex);
                    OnFinish();
                }
            }
            else
            {
                // start reading
                socket.BeginReceive(_lineBuffer, 0, _lineBuffer.Length, 0, ReceiveCallback, 0);
            }
        }