BeginReceive() 공개 메소드

public BeginReceive ( byte buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
size int
socketFlags SocketFlags
callback AsyncCallback
state object
리턴 IAsyncResult
예제 #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            int length = (int)ar.AsyncState;

            try
            {
                var bytesRead = _socket.EndReceive(ar);

                if (bytesRead == 0)
                {
                    OnFinish(length);
                    return;
                }

                length += bytesRead;

                int i;
                while ((i = IndexOf(_lineBuffer, _bufferIndex, length, _delimiterBytes, _delimiterSearchOffsetTable,
                                    _delimiterSearchCharTable)) != -1)
                {
                    var    decodeLen = i - _bufferIndex;
                    string line      = _encoding.GetString(_lineBuffer, _bufferIndex, decodeLen);

                    _bufferIndex = i + _delimiterBytes.Length;
                    length      -= decodeLen;
                    length      -= _delimiterBytes.Length;

                    var stop = _onLineRead(line, _state);
                    if (stop)
                    {
                        OnFinish(length);
                        return;
                    }
                }
                if (length == _lineBuffer.Length)
                {
                    OnException(new IndexOutOfRangeException("LineBuffer full! Try increace maxLineBytes!"));
                    OnFinish(length);

                    return;
                }

                if (_bufferIndex > 0)
                {
                    Buffer.BlockCopy(_lineBuffer, _bufferIndex, _lineBuffer, 0, length);
                    _bufferIndex = 0;
                }

                _socket.BeginReceive(_lineBuffer, length, _lineBuffer.Length - length, 0, ReceiveCallback, length);
            }
            catch (Exception ex)
            {
                OnException(ex);
                OnFinish(length);
            }
        }
예제 #2
0
        public LineReader(WrappedSocket socket, Func<string, object, bool> onLineRead, Action<Exception, object> onException,
            Action<byte[], int, int, object> onFinish, Encoding encoding, string delimiter, int maxLineBytes, object state)
        {
            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));
            }

            _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 (maxLineBytes < _delimiterBytes.Length)
            {
                throw new ArgumentException("Too small!", nameof(maxLineBytes));
            }

            _delimiterSearchCharTable = MakeCharTable(_delimiterBytes);
            _delimiterSearchOffsetTable = MakeOffsetTable(_delimiterBytes);

            _lineBuffer = new byte[maxLineBytes];

            // start reading
            socket.BeginReceive(_lineBuffer, 0, maxLineBytes, 0, ReceiveCallback, 0);
        }
예제 #3
0
        public LineReader(WrappedSocket socket, Func <string, object, bool> onLineRead, Action <Exception, object> onException,
                          Action <byte[], int, int, object> onFinish, Encoding encoding, string delimiter, int maxLineBytes, object state)
        {
            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));
            }

            _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 (maxLineBytes < _delimiterBytes.Length)
            {
                throw new ArgumentException("Too small!", nameof(maxLineBytes));
            }

            _delimiterSearchCharTable   = MakeCharTable(_delimiterBytes);
            _delimiterSearchOffsetTable = MakeOffsetTable(_delimiterBytes);

            _lineBuffer = new byte[maxLineBytes];

            // start reading
            socket.BeginReceive(_lineBuffer, 0, maxLineBytes, 0, ReceiveCallback, 0);
        }
예제 #4
0
        private void NewPackageRecv()
        {
            int i;

            while ((i = _delimiterSearch.SearchIn(_lineBuffer, _bufferDataIndex, _bufferDataLength)) != -1)
            {
                var    decodeLen = i - _bufferDataIndex;
                string line      = _encoding.GetString(_lineBuffer, _bufferDataIndex, decodeLen);

                _bufferDataIndex   = i + _delimiterBytes.Length;
                _bufferDataLength -= decodeLen;
                _bufferDataLength -= _delimiterBytes.Length;

                var stop = _onLineRead(line, _state);
                if (stop)
                {
                    OnFinish();
                    return;
                }
            }
            if (_bufferDataLength == _lineBuffer.Length)
            {
                OnException(new IndexOutOfRangeException("LineBuffer full! Try increace maxLineBytes!"));
                OnFinish();

                return;
            }

            if (_bufferDataIndex > 0)
            {
                Buffer.BlockCopy(_lineBuffer, _bufferDataIndex, _lineBuffer, 0, _bufferDataLength);
                _bufferDataIndex = 0;
            }

            _socket.BeginReceive(_lineBuffer, _bufferDataLength, _lineBuffer.Length - _bufferDataLength, 0, ReceiveCallback, _bufferDataLength);
        }
예제 #5
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);
            }
        }
예제 #6
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);
            }
        }