浏览代码

Improve PortForwarding and disconnect

olegkap_cp 13 年之前
父节点
当前提交
d89e0e055e

+ 12 - 4
Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.NET.cs

@@ -13,6 +13,7 @@ namespace Renci.SshNet
     public partial class ForwardedPortDynamic
     {
         private TcpListener _listener;
+        private object _listenerLocker = new object();
 
         partial void InternalStart()
         {
@@ -38,6 +39,12 @@ namespace Renci.SshNet
                 {
                     while (true)
                     {
+                        lock (this._listenerLocker)
+                        {
+                            if (this._listener == null)
+                                break;
+                        }
+
                         var socket = this._listener.AcceptSocket();
 
                         this.ExecuteThread(() =>
@@ -104,11 +111,14 @@ namespace Renci.SshNet
             if (!this.IsStarted)
                 return;
 
-            this._listener.Stop();
+            lock (this._listenerLocker)
+            {
+                this._listener.Stop();
+                this._listener = null;
+            }
             this._listenerTaskCompleted.WaitOne(this.Session.ConnectionInfo.Timeout);
             this._listenerTaskCompleted.Dispose();
             this._listenerTaskCompleted = null;
-
             this.IsStarted = false;
         }
 
@@ -271,7 +281,5 @@ namespace Renci.SshNet
             }
             return text.ToString();
         }
-
-
     }
 }

+ 2 - 0
Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.cs

@@ -90,6 +90,8 @@ namespace Renci.SshNet
             // 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)

+ 12 - 1
Renci.SshClient/Renci.SshNet/ForwardedPortLocal.NET.cs

@@ -12,6 +12,7 @@ namespace Renci.SshNet
     public partial class ForwardedPortLocal
     {
         private TcpListener _listener;
+        private object _listenerLocker = new object();
 
         partial void InternalStart()
         {
@@ -31,6 +32,12 @@ namespace Renci.SshNet
                 {
                     while (true)
                     {
+                        lock (this._listenerLocker)
+                        {
+                            if (this._listener == null)
+                                break;
+                        }
+
                         var socket = this._listener.AcceptSocket();
 
                         this.ExecuteThread(() =>
@@ -80,7 +87,11 @@ namespace Renci.SshNet
             if (!this.IsStarted)
                 return;
 
-            this._listener.Stop();
+            lock (this._listenerLocker)
+            {
+                this._listener.Stop();
+                this._listener = null;
+            }
             this._listenerTaskCompleted.WaitOne(this.Session.ConnectionInfo.Timeout);
             this._listenerTaskCompleted.Dispose();
             this._listenerTaskCompleted = null;