using System; using System.Globalization; using System.Text; using System.Threading; using Renci.SshNet.Channels; using Renci.SshNet.Common; namespace Renci.SshNet.Sftp { /// /// Base class for SSH subsystem implementations /// internal abstract class SubsystemSession : IDisposable { private readonly ISession _session; private readonly string _subsystemName; private IChannelSession _channel; private Exception _exception; private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(false); private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(false); /// /// Specifies a timeout to wait for operation to complete /// protected TimeSpan _operationTimeout; /// /// Occurs when an error occurred. /// public event EventHandler ErrorOccurred; /// /// Occurs when session has been disconnected form the server. /// public event EventHandler Disconnected; /// /// Gets the channel associated with this session. /// /// /// The channel associated with this session. /// internal IChannelSession Channel { get { return _channel; } } /// /// Gets the character encoding to use. /// protected Encoding Encoding { get; private set; } /// /// Initializes a new instance of the SubsystemSession class. /// /// The session. /// Name of the subsystem. /// The operation timeout. /// The character encoding to use. /// or or is null. protected SubsystemSession(ISession session, string subsystemName, TimeSpan operationTimeout, Encoding encoding) { if (session == null) throw new ArgumentNullException("session"); if (subsystemName == null) throw new ArgumentNullException("subsystemName"); if (encoding == null) throw new ArgumentNullException("encoding"); this._session = session; this._subsystemName = subsystemName; this._operationTimeout = operationTimeout; this.Encoding = encoding; } /// /// Connects subsystem on SSH channel. /// public void Connect() { this._channel = this._session.CreateChannelSession(); this._session.ErrorOccured += Session_ErrorOccured; this._session.Disconnected += Session_Disconnected; this._channel.DataReceived += Channel_DataReceived; this._channel.Closed += Channel_Closed; this._channel.Open(); this._channel.SendSubsystemRequest(_subsystemName); this.OnChannelOpen(); } /// /// Disconnects subsystem channel. /// public void Disconnect() { this._channel.SendEof(); this._channel.Close(); } /// /// Sends data to the subsystem. /// /// The data to be sent. public void SendData(byte[] data) { this._channel.SendData(data); } /// /// Called when channel is open. /// protected abstract void OnChannelOpen(); /// /// Called when data is received. /// /// The data type code. /// The data. protected abstract void OnDataReceived(uint dataTypeCode, byte[] data); /// /// Raises the error. /// /// The error. protected void RaiseError(Exception error) { this._exception = error; var errorOccuredWaitHandle = _errorOccuredWaitHandle; if (errorOccuredWaitHandle != null) errorOccuredWaitHandle.Set(); SignalErrorOccurred(error); } private void Channel_DataReceived(object sender, ChannelDataEventArgs e) { this.OnDataReceived(e.DataTypeCode, e.Data); } private void Channel_Closed(object sender, ChannelEventArgs e) { var channelClosedWaitHandle = _channelClosedWaitHandle; if (channelClosedWaitHandle != null) channelClosedWaitHandle.Set(); } internal void WaitOnHandle(WaitHandle waitHandle, TimeSpan operationTimeout) { var waitHandles = new[] { this._errorOccuredWaitHandle, this._channelClosedWaitHandle, waitHandle }; switch (WaitHandle.WaitAny(waitHandles, operationTimeout)) { case 0: throw this._exception; case 1: throw new SshException("Channel was closed."); case WaitHandle.WaitTimeout: throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Operation has timed out.")); } } private void Session_Disconnected(object sender, EventArgs e) { SignalDisconnected(); this.RaiseError(new SshException("Connection was lost")); } private void Session_ErrorOccured(object sender, ExceptionEventArgs e) { this.RaiseError(e.Exception); } private void SignalErrorOccurred(Exception error) { var errorOccurred = ErrorOccurred; if (errorOccurred != null) { errorOccurred(this, new ExceptionEventArgs(error)); } } private void SignalDisconnected() { var disconnected = Disconnected; if (disconnected != null) { disconnected(this, new EventArgs()); } } #region IDisposable Members private bool _isDisposed; /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._isDisposed) { if (this._channel != null) { this._channel.DataReceived -= Channel_DataReceived; this._channel.Closed -= Channel_Closed; this._channel.Dispose(); this._channel = null; } this._session.ErrorOccured -= Session_ErrorOccured; this._session.Disconnected -= Session_Disconnected; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (this._errorOccuredWaitHandle != null) { this._errorOccuredWaitHandle.Dispose(); this._errorOccuredWaitHandle = null; } if (this._channelClosedWaitHandle != null) { this._channelClosedWaitHandle.Dispose(); this._channelClosedWaitHandle = null; } } // Note disposing has been done. _isDisposed = true; } } /// /// Finalizes an instance of the class. /// ~SubsystemSession() { // Do not re-create Dispose clean-up code here. // Calling Dispose(false) is optimal in terms of // readability and maintainability. Dispose(false); } #endregion } }