<PackageReference Include="SSH.NET" Version="2016.0.0" />

SubsystemSession

Base class for SSH subsystem implementations
using Renci.SshNet.Channels; using Renci.SshNet.Common; using System; using System.Globalization; using System.Text; using System.Threading; namespace Renci.SshNet { internal abstract class SubsystemSession : ISubsystemSession, IDisposable { private ISession _session; private readonly string _subsystemName; private IChannelSession _channel; private Exception _exception; private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(false); private EventWaitHandle _sessionDisconnectedWaitHandle = new ManualResetEvent(false); private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(false); private bool _isDisposed; protected TimeSpan OperationTimeout { get; set; } internal IChannelSession Channel { get { EnsureNotDisposed(); return _channel; } } public bool IsOpen { get { if (_channel != null) return _channel.IsOpen; return false; } } protected Encoding Encoding { get; set; } public event EventHandler<ExceptionEventArgs> ErrorOccurred; public event EventHandler<EventArgs> Disconnected; 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"); _session = session; _subsystemName = subsystemName; OperationTimeout = operationTimeout; Encoding = encoding; } public void Connect() { EnsureNotDisposed(); if (IsOpen) throw new InvalidOperationException("The session is already connected."); _errorOccuredWaitHandle.Reset(); _sessionDisconnectedWaitHandle.Reset(); _sessionDisconnectedWaitHandle.Reset(); _channelClosedWaitHandle.Reset(); _session.ErrorOccured += Session_ErrorOccured; _session.Disconnected += Session_Disconnected; _channel = _session.CreateChannelSession(); _channel.DataReceived += Channel_DataReceived; _channel.Exception += Channel_Exception; _channel.Closed += Channel_Closed; _channel.Open(); _channel.SendSubsystemRequest(_subsystemName); OnChannelOpen(); } public void Disconnect() { UnsubscribeFromSessionEvents(_session); IChannelSession channel = _channel; if (channel != null) { channel.DataReceived -= Channel_DataReceived; channel.Exception -= Channel_Exception; channel.Closed -= Channel_Closed; channel.Close(); channel.Dispose(); _channel = null; } } public void SendData(byte[] data) { EnsureNotDisposed(); EnsureSessionIsOpen(); _channel.SendData(data); } protected abstract void OnChannelOpen(); protected abstract void OnDataReceived(byte[] data); protected void RaiseError(Exception error) { _exception = error; _errorOccuredWaitHandle?.Set(); SignalErrorOccurred(error); } private void Channel_DataReceived(object sender, ChannelDataEventArgs e) { try { OnDataReceived(e.Data); } catch (Exception error) { RaiseError(error); } } private void Channel_Exception(object sender, ExceptionEventArgs e) { RaiseError(e.Exception); } private void Channel_Closed(object sender, ChannelEventArgs e) { _channelClosedWaitHandle?.Set(); } public void WaitOnHandle(WaitHandle waitHandle, TimeSpan operationTimeout) { switch (WaitHandle.WaitAny(new WaitHandle[4] { _errorOccuredWaitHandle, _sessionDisconnectedWaitHandle, _channelClosedWaitHandle, waitHandle }, operationTimeout)) { case 0: throw _exception; case 1: throw new SshException("Connection was closed by the server."); case 2: throw new SshException("Channel was closed."); case 258: throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Operation has timed out.")); } } private void Session_Disconnected(object sender, EventArgs e) { _sessionDisconnectedWaitHandle?.Set(); SignalDisconnected(); } private void Session_ErrorOccured(object sender, ExceptionEventArgs e) { RaiseError(e.Exception); } private void SignalErrorOccurred(Exception error) { this.ErrorOccurred?.Invoke(this, new ExceptionEventArgs(error)); } private void SignalDisconnected() { this.Disconnected?.Invoke(this, new EventArgs()); } private void EnsureSessionIsOpen() { if (!IsOpen) throw new InvalidOperationException("The session is not open."); } private void UnsubscribeFromSessionEvents(ISession session) { if (session != null) { session.Disconnected -= Session_Disconnected; session.ErrorOccured -= Session_ErrorOccured; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_isDisposed && disposing) { Disconnect(); _session = null; EventWaitHandle errorOccuredWaitHandle = _errorOccuredWaitHandle; if (errorOccuredWaitHandle != null) { errorOccuredWaitHandle.Dispose(); _errorOccuredWaitHandle = null; } EventWaitHandle sessionDisconnectedWaitHandle = _sessionDisconnectedWaitHandle; if (sessionDisconnectedWaitHandle != null) { sessionDisconnectedWaitHandle.Dispose(); _sessionDisconnectedWaitHandle = null; } EventWaitHandle channelClosedWaitHandle = _channelClosedWaitHandle; if (channelClosedWaitHandle != null) { channelClosedWaitHandle.Dispose(); _channelClosedWaitHandle = null; } _isDisposed = true; } } ~SubsystemSession() { Dispose(false); } private void EnsureNotDisposed() { if (_isDisposed) throw new ObjectDisposedException(GetType().FullName); } } }