mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-19 23:30:23 +08:00
Fix and remove orphan threads when using port forwarding.
This commit is contained in:
@@ -519,8 +519,18 @@ namespace Renci.SshNet.Channels
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDisconnected()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnErrorOccured(Exception exp)
|
||||
{
|
||||
}
|
||||
|
||||
private void Session_Disconnected(object sender, EventArgs e)
|
||||
{
|
||||
this.OnDisconnected();
|
||||
|
||||
// If objected is disposed or being disposed don't handle this event
|
||||
if (this._isDisposed)
|
||||
return;
|
||||
@@ -530,6 +540,8 @@ namespace Renci.SshNet.Channels
|
||||
|
||||
private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
|
||||
{
|
||||
this.OnErrorOccured(e.Exception);
|
||||
|
||||
// If objected is disposed or being disposed don't handle this event
|
||||
if (this._isDisposed)
|
||||
return;
|
||||
|
||||
@@ -194,6 +194,22 @@ namespace Renci.SshNet.Channels
|
||||
this._channelEof.Set();
|
||||
}
|
||||
|
||||
protected override void OnErrorOccured(Exception exp)
|
||||
{
|
||||
base.OnErrorOccured(exp);
|
||||
|
||||
// If error occured, no more data can be received
|
||||
this._channelEof.Set();
|
||||
}
|
||||
|
||||
protected override void OnDisconnected()
|
||||
{
|
||||
base.OnDisconnected();
|
||||
|
||||
// If disconnected, no more data can be received
|
||||
this._channelEof.Set();
|
||||
}
|
||||
|
||||
partial void ExecuteThread(Action action);
|
||||
|
||||
partial void InternalSocketReceive(byte[] buffer, ref int read);
|
||||
|
||||
@@ -27,6 +27,9 @@ namespace Renci.SshNet
|
||||
this._listener = new TcpListener(ep);
|
||||
this._listener.Start();
|
||||
|
||||
this.Session.ErrorOccured += Session_ErrorOccured;
|
||||
this.Session.Disconnected += Session_Disconnected;
|
||||
|
||||
this._listenerTaskCompleted = new ManualResetEvent(false);
|
||||
this.ExecuteThread(() =>
|
||||
{
|
||||
@@ -102,5 +105,15 @@ namespace Renci.SshNet
|
||||
|
||||
this.IsStarted = false;
|
||||
}
|
||||
|
||||
private void Session_ErrorOccured(object sender, Common.ExceptionEventArgs e)
|
||||
{
|
||||
this._listener.Stop();
|
||||
}
|
||||
|
||||
private void Session_Disconnected(object sender, EventArgs e)
|
||||
{
|
||||
this._listener.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user