public LineReader(int maxLineBytes, WrappedSocket socket, Func <string, object, bool> onLineRead,
                   Action <Exception, object> onException, Action <byte[], int, int, object> onFinish, Encoding encoding,
                   string delimiter, object state)
     : this(
         new byte[maxLineBytes], socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter,
         state)
 {
 }
        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);
        }
Beispiel #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);
        }
            public void Start(byte[] firstPacket, int length, Socket socket, int targetPort)
            {
                this._firstPacket = firstPacket;
                this._firstPacketLength = length;
                this._local = socket;
                try
                {
                    EndPoint remoteEP = SocketUtil.GetEndPoint("127.0.0.1", targetPort);

                    // Connect to the remote endpoint.
                    _remote = new WrappedSocket();
                    _remote.BeginConnect(remoteEP, ConnectCallback, null);
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                    this.Close();
                }
            }
Beispiel #5
0
 public HttpHandler(ShadowsocksController controller, Configuration config, TCPRelay tcprelay, Socket socket)
     : base(controller, config, tcprelay, socket)
 {
     _localSocket = new WrappedSocket(socket);
 }
        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);
            }
        }
Beispiel #7
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);
            }
        }
Beispiel #8
0
 public LineReader(int maxLineBytes, WrappedSocket socket, Func<string, object, bool> onLineRead,
     Action<Exception, object> onException, Action<byte[], int, int, object> onFinish, Encoding encoding,
     string delimiter, object state)
     : this(new byte[maxLineBytes], socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter,
         state)
 {
 }