mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-17 05:21:15 +08:00
Add Connect*, Disconnect* and Errorccured events to Session
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Renci.SshClient.Common;
|
||||
using Renci.SshClient.Messages;
|
||||
using Renci.SshClient.Messages.Connection;
|
||||
|
||||
@@ -11,6 +12,10 @@ namespace Renci.SshClient.Channels
|
||||
|
||||
private EventWaitHandle _channelWindowAdjustWaitHandle = new AutoResetEvent(false);
|
||||
|
||||
private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(false);
|
||||
|
||||
private EventWaitHandle _disconnectedWaitHandle = new ManualResetEvent(false);
|
||||
|
||||
private uint _initialWindowSize = 0x100000;
|
||||
|
||||
private uint _maximumPacketSize = 0x8000;
|
||||
@@ -164,6 +169,8 @@ namespace Renci.SshClient.Channels
|
||||
this._session.ChannelRequestReceived += OnChannelRequest;
|
||||
this._session.ChannelSuccessReceived += OnChannelSuccess;
|
||||
this._session.ChannelFailureReceived += OnChannelFailure;
|
||||
this._session.ErrorOccured += Session_ErrorOccured;
|
||||
this._session.Disconnected += Session_Disconnected;
|
||||
|
||||
}
|
||||
|
||||
@@ -319,6 +326,16 @@ namespace Renci.SshClient.Channels
|
||||
this._session.WaitHandle(waitHandle);
|
||||
}
|
||||
|
||||
private void Session_Disconnected(object sender, EventArgs e)
|
||||
{
|
||||
this._disconnectedWaitHandle.Set();
|
||||
}
|
||||
|
||||
private void Session_ErrorOccured(object sender, System.IO.ErrorEventArgs e)
|
||||
{
|
||||
this._errorOccuredWaitHandle.Set();
|
||||
}
|
||||
|
||||
#region Channel message event handlers
|
||||
|
||||
private void OnChannelOpen(object sender, MessageEventArgs<ChannelOpenMessage> e)
|
||||
|
||||
+1
-2
@@ -1,5 +1,4 @@
|
||||
|
||||
namespace Renci.SshClient.Channels
|
||||
namespace Renci.SshClient.Common
|
||||
{
|
||||
internal class ChannelDataEventArgs : ChannelEventArgs
|
||||
{
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace Renci.SshClient.Channels
|
||||
namespace Renci.SshClient.Common
|
||||
{
|
||||
internal class ChannelEventArgs : EventArgs
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace Renci.SshClient.Channels
|
||||
namespace Renci.SshClient.Common
|
||||
{
|
||||
internal class ChannelOpenFailedEventArgs : ChannelEventArgs
|
||||
{
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using Renci.SshClient.Messages.Connection;
|
||||
|
||||
namespace Renci.SshClient.Channels
|
||||
namespace Renci.SshClient.Common
|
||||
{
|
||||
internal class ChannelRequestEventArgs : EventArgs
|
||||
{
|
||||
@@ -60,10 +60,10 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Channels\ChannelDataEventArgs.cs" />
|
||||
<Compile Include="Channels\ChannelEventArgs.cs" />
|
||||
<Compile Include="Channels\ChannelOpenFailedEventArgs.cs" />
|
||||
<Compile Include="Channels\ChannelRequestEventArgs.cs" />
|
||||
<Compile Include="Common\ChannelDataEventArgs.cs" />
|
||||
<Compile Include="Common\ChannelEventArgs.cs" />
|
||||
<Compile Include="Common\ChannelOpenFailedEventArgs.cs" />
|
||||
<Compile Include="Common\ChannelRequestEventArgs.cs" />
|
||||
<Compile Include="Channels\ChannelSessionShell.cs" />
|
||||
<Compile Include="Messages\Connection\ChannelOpen\ChannelOpenInfo.cs" />
|
||||
<Compile Include="Messages\Connection\ChannelOpen\DirectTcpipChannelInfo.cs" />
|
||||
|
||||
@@ -217,6 +217,16 @@ namespace Renci.SshClient
|
||||
/// <value>The connection info.</value>
|
||||
public ConnectionInfo ConnectionInfo { get; private set; }
|
||||
|
||||
public event EventHandler<ErrorEventArgs> ErrorOccured;
|
||||
|
||||
public event EventHandler<EventArgs> Connecting;
|
||||
|
||||
public event EventHandler<EventArgs> Connected;
|
||||
|
||||
public event EventHandler<EventArgs> Disconnecting;
|
||||
|
||||
public event EventHandler<EventArgs> Disconnected;
|
||||
|
||||
#region Message events
|
||||
|
||||
/// <summary>
|
||||
@@ -387,6 +397,12 @@ namespace Renci.SshClient
|
||||
if (this.IsConnected)
|
||||
return;
|
||||
|
||||
if (this.Connecting != null)
|
||||
{
|
||||
// TODO: Create custom ConnectingEventArgs to allow to pass different connection parameters
|
||||
this.Connecting(this, new EventArgs());
|
||||
}
|
||||
|
||||
lock (this)
|
||||
{
|
||||
// If connected dont connect again
|
||||
@@ -517,6 +533,12 @@ namespace Renci.SshClient
|
||||
|
||||
Monitor.Pulse(this);
|
||||
}
|
||||
|
||||
if (this.Connected != null)
|
||||
{
|
||||
// TODO: Create custom ConnectingEventArgs to allow to pass different connection parameters
|
||||
this.Connected(this, new EventArgs());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -531,6 +553,11 @@ namespace Renci.SshClient
|
||||
{
|
||||
this._isDisconnecting = true;
|
||||
|
||||
if (this.Disconnecting != null)
|
||||
{
|
||||
this.Disconnecting(this, new EventArgs());
|
||||
}
|
||||
|
||||
this.SendDisconnect(DisconnectReasons.ByApplication, "Connection terminated by the client.");
|
||||
}
|
||||
|
||||
@@ -936,6 +963,11 @@ namespace Renci.SshClient
|
||||
{
|
||||
this.DisconnectReceived(this, new MessageEventArgs<DisconnectMessage>(message));
|
||||
}
|
||||
|
||||
if (this.Disconnected != null)
|
||||
{
|
||||
this.Disconnected(this, new EventArgs());
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnIgnoreReceived(IgnoreMessage message)
|
||||
@@ -1328,9 +1360,7 @@ namespace Renci.SshClient
|
||||
this.SendDisconnect(exp.DisconnectReason, exp.ToString());
|
||||
}
|
||||
|
||||
this._exceptionToThrow = exp;
|
||||
|
||||
this._exceptionWaitHandle.Set();
|
||||
this.RaiseError(exp);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
@@ -1338,15 +1368,25 @@ namespace Renci.SshClient
|
||||
|
||||
this.SendDisconnect(DisconnectReasons.ByApplication, exp.ToString());
|
||||
|
||||
this._exceptionToThrow = exp;
|
||||
|
||||
this._exceptionWaitHandle.Set();
|
||||
this.RaiseError(exp);
|
||||
}
|
||||
}
|
||||
|
||||
this._listenerWaitHandle.Set();
|
||||
}
|
||||
|
||||
private void RaiseError(Exception exp)
|
||||
{
|
||||
this._exceptionToThrow = exp;
|
||||
|
||||
this._exceptionWaitHandle.Set();
|
||||
|
||||
if (this.ErrorOccured != null)
|
||||
{
|
||||
this.ErrorOccured(this, new ErrorEventArgs(exp));
|
||||
}
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
private bool _disposed = false;
|
||||
|
||||
Reference in New Issue
Block a user