Esempio n. 1
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="Stream"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (_isDisposed)
            {
                return;
            }

            if (disposing)
            {
                UnsubscribeFromSessionEvents(_session);

                if (_channel != null)
                {
                    _channel.DataReceived -= Channel_DataReceived;
                    _channel.Closed       -= Channel_Closed;
                    _channel.Dispose();
                    _channel = null;
                }

                if (_dataReceived != null)
                {
                    _dataReceived.Dispose();
                    _dataReceived = null;
                }

                _isDisposed = true;
            }
            else
            {
                UnsubscribeFromSessionEvents(_session);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Stops this shell.
        /// </summary>
        /// <exception cref="SshException">Shell is not started.</exception>
        public void Stop()
        {
            if (!IsStarted)
            {
                throw new SshException("Shell is not started.");
            }

            if (_channel != null)
            {
                _channel.Dispose();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new <see cref="ShellStream"/> instance.
        /// </summary>
        /// <param name="session">The SSH session.</param>
        /// <param name="terminalName">The <c>TERM</c> environment variable.</param>
        /// <param name="columns">The terminal width in columns.</param>
        /// <param name="rows">The terminal width in rows.</param>
        /// <param name="width">The terminal height in pixels.</param>
        /// <param name="height">The terminal height in pixels.</param>
        /// <param name="terminalModeValues">The terminal mode values.</param>
        /// <param name="bufferSize">The size of the buffer.</param>
        /// <exception cref="SshException">The channel could not be opened.</exception>
        /// <exception cref="SshException">The pseudo-terminal request was not accepted by the server.</exception>
        /// <exception cref="SshException">The request to start a shell was not accepted by the server.</exception>
        internal ShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary <TerminalModes, uint> terminalModeValues, int bufferSize)
        {
            _encoding   = session.ConnectionInfo.Encoding;
            _session    = session;
            _bufferSize = bufferSize;
            _incoming   = new Queue <byte>();
            _outgoing   = new Queue <byte>();

            _channel = _session.CreateChannelSession();
            _channel.DataReceived += Channel_DataReceived;
            _channel.Closed       += Channel_Closed;
            _session.Disconnected += Session_Disconnected;
            _session.ErrorOccured += Session_ErrorOccured;

            try
            {
                _channel.Open();
                if (!_channel.SendPseudoTerminalRequest(terminalName, columns, rows, width, height, terminalModeValues))
                {
                    throw new SshException("The pseudo-terminal request was not accepted by the server. Consult the server log for more information.");
                }
                if (!_channel.SendShellRequest())
                {
                    throw new SshException("The request to start a shell was not accepted by the server. Consult the server log for more information.");
                }
            }
            catch
            {
                UnsubscribeFromSessionEvents(session);
                _channel.Dispose();
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!_disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged ResourceMessages.
                if (disposing)
                {
                    if (_channelClosedWaitHandle != null)
                    {
                        _channelClosedWaitHandle.Dispose();
                        _channelClosedWaitHandle = null;
                    }

                    if (_channel != null)
                    {
                        _channel.Dispose();
                        _channel = null;
                    }

                    if (_dataReaderTaskCompleted != null)
                    {
                        _dataReaderTaskCompleted.Dispose();
                        _dataReaderTaskCompleted = null;
                    }
                }

                // Note disposing has been done.
                _disposed = true;
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Cancels command execution in asynchronous scenarios.
 /// </summary>
 public void CancelAsync()
 {
     if (_channel != null && _channel.IsOpen && _asyncResult != null)
     {
         // TODO: check with Oleg if we shouldn't dispose the channel and uninitialize it ?
         _channel.Dispose();
     }
 }
Esempio n. 6
0
        private void UnsubscribeFromEventsAndDisposeChannel()
        {
            // unsubscribe from events as we do not want to be signaled should these get fired
            // during the dispose of the channel
            _channel.DataReceived         -= Channel_DataReceived;
            _channel.ExtendedDataReceived -= Channel_ExtendedDataReceived;
            _channel.RequestReceived      -= Channel_RequestReceived;
            _channel.Closed -= Channel_Closed;

            // actually dispose the channel
            _channel.Dispose();
        }
Esempio n. 7
0
        /// <summary>
        /// Disconnects the subsystem channel.
        /// </summary>
        public void Disconnect()
        {
            if (_session != null)
            {
                _session.ErrorOccured -= Session_ErrorOccured;
                _session.Disconnected -= Session_Disconnected;
            }

            if (_channel != null)
            {
                _channel.DataReceived -= Channel_DataReceived;
                _channel.Exception    -= Channel_Exception;
                _channel.Closed       -= Channel_Closed;
                _channel.Close();
                _channel.Dispose();
                _channel = null;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (_session != null)
            {
                _session.Disconnected -= Session_Disconnected;
                _session.ErrorOccured -= Session_ErrorOccured;
            }

            if (_channel != null)
            {
                _channel.DataReceived -= Channel_DataReceived;
                _channel.Closed       -= Channel_Closed;
                _channel.Dispose();
                _channel = null;
            }

            if (_dataReceived != null)
            {
                _dataReceived.Dispose();
                _dataReceived = null;
            }
        }