using System;
using System.Threading;
namespace Renci.SshNet
{
    /// 
    /// Provides functionality for dynamic port forwarding
    /// 
    public partial class ForwardedPortDynamic : ForwardedPort, IDisposable
    {
        private EventWaitHandle _listenerTaskCompleted;
        /// 
        /// Gets the bound host.
        /// 
        public string BoundHost { get; protected set; }
        /// 
        /// Gets the bound port.
        /// 
        public uint BoundPort { get; protected set; }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The port.
        public ForwardedPortDynamic(uint port)
            : this(string.Empty, port)
        {
        }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The host.
        /// The port.
        public ForwardedPortDynamic(string host, uint port)
        {
            this.BoundHost = host;
            this.BoundPort = port;
        }
        /// 
        /// Starts local port forwarding.
        /// 
        public override void Start()
        {
            this.InternalStart();
        }
        /// 
        /// Stops local port forwarding.
        /// 
        public override void Stop()
        {
            base.Stop();
            this.InternalStop();
        }
        partial void InternalStart();
        partial void InternalStop();
        partial void ExecuteThread(Action action);
        #region IDisposable Members
        private bool _isDisposed;
        /// 
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
        /// 
        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 ResourceMessages.
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._isDisposed)
            {
                this.InternalStop();
                // If disposing equals true, dispose all managed
                // and unmanaged ResourceMessages.
                if (disposing)
                {
                    // Dispose managed ResourceMessages.
                    if (this._listenerTaskCompleted != null)
                    {
                        this._listenerTaskCompleted.Dispose();
                        this._listenerTaskCompleted = null;
                    }
                }
                // Note disposing has been done.
                _isDisposed = true;
            }
        }
        /// 
        /// Releases unmanaged resources and performs other cleanup operations before the
        ///  is reclaimed by garbage collection.
        /// 
        ~ForwardedPortDynamic()
        {
            // Do not re-create Dispose clean-up code here.
            // Calling Dispose(false) is optimal in terms of
            // readability and maintainability.
            Dispose(false);
        }
        #endregion
    }
}