using System;
namespace Renci.SshNet
{
///
/// Provides functionality for local port forwarding
///
public partial class ForwardedPortLocal : ForwardedPort, IDisposable
{
private ForwardedPortStatus _status;
///
/// Gets the bound host.
///
public string BoundHost { get; private set; }
///
/// Gets the bound port.
///
public uint BoundPort { get; private set; }
///
/// Gets the forwarded host.
///
public string Host { get; private set; }
///
/// Gets the forwarded port.
///
public uint Port { get; private set; }
///
/// Gets a value indicating whether port forwarding is started.
///
///
/// true if port forwarding is started; otherwise, false.
///
public override bool IsStarted
{
get { return _status == ForwardedPortStatus.Started; }
}
///
/// Initializes a new instance of the class.
///
/// The bound port.
/// The host.
/// The port.
/// is greater than .
/// is null.
/// is greater than .
///
///
///
public ForwardedPortLocal(uint boundPort, string host, uint port)
: this(string.Empty, boundPort, host, port)
{
}
///
/// Initializes a new instance of the class.
///
/// The bound host.
/// The host.
/// The port.
/// is null.
/// is null.
/// is greater than .
public ForwardedPortLocal(string boundHost, string host, uint port)
: this(boundHost, 0, host, port)
{
}
///
/// Initializes a new instance of the class.
///
/// The bound host.
/// The bound port.
/// The host.
/// The port.
/// is null.
/// is null.
/// is greater than .
/// is greater than .
public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
{
if (boundHost == null)
throw new ArgumentNullException("boundHost");
if (host == null)
throw new ArgumentNullException("host");
boundPort.ValidatePort("boundPort");
port.ValidatePort("port");
BoundHost = boundHost;
BoundPort = boundPort;
Host = host;
Port = port;
_status = ForwardedPortStatus.Stopped;
}
///
/// Starts local port forwarding.
///
protected override void StartPort()
{
if (!ForwardedPortStatus.ToStarting(ref _status))
return;
try
{
InternalStart();
}
catch (Exception)
{
_status = ForwardedPortStatus.Stopped;
throw;
}
}
///
/// Stops local port forwarding, and waits for the specified timeout until all pending
/// requests are processed.
///
/// The maximum amount of time to wait for pending requests to finish processing.
protected override void StopPort(TimeSpan timeout)
{
if (!ForwardedPortStatus.ToStopping(ref _status))
return;
// signal existing channels that the port is closing
base.StopPort(timeout);
// prevent new requests from getting processed
StopListener();
// wait for open channels to close
InternalStop(timeout);
// mark port stopped
_status = ForwardedPortStatus.Stopped;
}
///
/// Ensures the current instance is not disposed.
///
/// The current instance is disposed.
protected override void CheckDisposed()
{
if (_isDisposed)
throw new ObjectDisposedException(GetType().FullName);
}
partial void InternalStart();
///
/// Interrupts the listener, and waits for the listener loop to finish.
///
///
/// When the forwarded port is stopped, then any further action is skipped.
///
partial void StopListener();
partial void InternalStop(TimeSpan timeout);
#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);
}
partial void InternalDispose(bool disposing);
///
/// Releases unmanaged and - optionally - managed resources
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool disposing)
{
if (_isDisposed)
return;
base.Dispose(disposing);
InternalDispose(disposing);
_isDisposed = true;
}
///
/// Releases unmanaged resources and performs other cleanup operations before the
/// is reclaimed by garbage collection.
///
~ForwardedPortLocal()
{
Dispose(false);
}
#endregion
}
}