private string ReadToEnd(
            SshChannelBase channel,
            Encoding encoding)
        {
            channel.WaitForEndOfStream();

            var  text   = new StringBuilder();
            var  buffer = new byte[1024];
            uint bytesRead;

            while ((bytesRead = channel.Read(buffer)) > 0)
            {
                text.Append(encoding.GetString(buffer, 0, (int)bytesRead));
            }

            return(text.ToString());
        }
        private string ReadUntil(
            SshChannelBase channel,
            string delimiter,
            Encoding encoding)
        {
            var text   = new StringBuilder();
            var buffer = new byte[1];

            while ((channel.Read(buffer)) > 0)
            {
                var ch = encoding.GetString(buffer, 0, 1);
                text.Append(ch);

                if (text.ToString().EndsWith(delimiter))
                {
                    return(text.ToString());
                }
            }

            return(text.ToString());
        }
Пример #3
0
        //---------------------------------------------------------------------
        // Receiving overrides.
        //---------------------------------------------------------------------

        protected override void Receive(SshChannelBase channel)
        {
            //
            // NB. receiveFunc() can throw an exception, in which case
            // we do not complete the current packet. If the exception
            // is bad, we'll receive a OnReceiveError callback later.
            //
            // NB. This method is always called on the same thread, so it's ok
            // to reuse the same buffer.
            //

            var bytesReceived = channel.Read(this.receiveBuffer);

            this.receiveDataHandler(this.receiveBuffer, 0, bytesReceived);

            //
            // In non-blocking mode, we're not always receive a final
            // zero-length read.
            //
            if (bytesReceived > 0 && channel.IsEndOfStream)
            {
                this.receiveDataHandler(Array.Empty <byte>(), 0, 0);
            }
        }