This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubsystemSession.cs
263 lines (216 loc) · 8.35 KB
/
SubsystemSession.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Sftp.Requests;
using Renci.SshNet.Messages.Connection;
namespace Renci.SshNet.Sftp
{
/// <summary>
/// Base class for SSH subsystem implementations
/// </summary>
public abstract class SubsystemSession : IDisposable
{
private Session _session;
private string _subsystemName;
private ChannelSession _channel;
private Exception _exception;
private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(false);
private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(false);
/// <summary>
/// Specifies a timeout to wait for operation to complete
/// </summary>
protected TimeSpan _operationTimeout;
/// <summary>
/// Occurs when an error occurred.
/// </summary>
public event EventHandler<ExceptionEventArgs> ErrorOccurred;
/// <summary>
/// Occurs when session has been disconnected form the server.
/// </summary>
public event EventHandler<EventArgs> Disconnected;
/// <summary>
/// Gets the channel number.
/// </summary>
protected uint ChannelNumber { get; private set; }
protected Encoding Encoding { get; private set; }
/// <summary>
/// Initializes a new instance of the SubsystemSession class.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="subsystemName">Name of the subsystem.</param>
/// <param name="operationTimeout">The operation timeout.</param>
/// <exception cref="System.ArgumentNullException">session</exception>
/// <exception cref="ArgumentNullException"><paramref name="session" /> or <paramref name="subsystemName" /> is null.</exception>
public SubsystemSession(Session session, string subsystemName, TimeSpan operationTimeout, Encoding encoding)
{
if (session == null)
throw new ArgumentNullException("session");
if (subsystemName == null)
throw new ArgumentNullException("subsystemName");
this._session = session;
this._subsystemName = subsystemName;
this._operationTimeout = operationTimeout;
this.Encoding = encoding;
}
/// <summary>
/// Connects subsystem on SSH channel.
/// </summary>
public void Connect()
{
this._channel = this._session.CreateChannel<ChannelSession>();
this._session.ErrorOccured += Session_ErrorOccured;
this._session.Disconnected += Session_Disconnected;
this._channel.DataReceived += Channel_DataReceived;
this._channel.Closed += Channel_Closed;
this._channel.Open();
this.ChannelNumber = this._channel.RemoteChannelNumber;
this._channel.SendSubsystemRequest(_subsystemName);
this.OnChannelOpen();
}
/// <summary>
/// Disconnects subsystem channel.
/// </summary>
public void Disconnect()
{
this._channel.SendEof();
this._channel.Close();
}
/// <summary>
/// Sends data to the subsystem.
/// </summary>
/// <param name="data">The data to be sent.</param>
public void SendData(byte[] data)
{
this._channel.SendData(data);
}
/// <summary>
/// Called when channel is open.
/// </summary>
protected abstract void OnChannelOpen();
/// <summary>
/// Called when data is received.
/// </summary>
/// <param name="dataTypeCode">The data type code.</param>
/// <param name="data">The data.</param>
protected abstract void OnDataReceived(uint dataTypeCode, byte[] data);
/// <summary>
/// Raises the error.
/// </summary>
/// <param name="error">The error.</param>
protected void RaiseError(Exception error)
{
this._exception = error;
this._errorOccuredWaitHandle.Set();
if (this.ErrorOccurred != null)
{
this.ErrorOccurred(this, new ExceptionEventArgs(error));
}
}
private void Channel_DataReceived(object sender, Common.ChannelDataEventArgs e)
{
this.OnDataReceived(e.DataTypeCode, e.Data);
}
private void Channel_Closed(object sender, Common.ChannelEventArgs e)
{
this._channelClosedWaitHandle.Set();
}
internal void WaitHandle(WaitHandle waitHandle, TimeSpan operationTimeout)
{
var waitHandles = new WaitHandle[]
{
this._errorOccuredWaitHandle,
this._channelClosedWaitHandle,
waitHandle,
};
switch (EventWaitHandle.WaitAny(waitHandles, operationTimeout))
{
case 0:
throw this._exception;
case 1:
throw new SshException("Channel was closed.");
case System.Threading.WaitHandle.WaitTimeout:
throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Operation has timed out."));
default:
break;
}
}
private void Session_Disconnected(object sender, EventArgs e)
{
if (this.Disconnected != null)
{
this.Disconnected(this, new EventArgs());
}
this.RaiseError(new SshException("Connection was lost"));
}
private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
{
this.RaiseError(e.Exception);
}
#region IDisposable Members
private bool _isDisposed = false;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!this._isDisposed)
{
if (this._channel != null)
{
this._channel.DataReceived -= Channel_DataReceived;
this._channel.Dispose();
this._channel = null;
}
this._session.ErrorOccured -= Session_ErrorOccured;
this._session.Disconnected -= Session_Disconnected;
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
if (this._errorOccuredWaitHandle != null)
{
this._errorOccuredWaitHandle.Dispose();
this._errorOccuredWaitHandle = null;
}
if (this._channelClosedWaitHandle != null)
{
this._channelClosedWaitHandle.Dispose();
this._channelClosedWaitHandle = null;
}
}
// Note disposing has been done.
_isDisposed = true;
}
}
/// <summary>
/// Finalizes an instance of the <see cref="SubsystemSession" /> class.
/// </summary>
~SubsystemSession()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
#endregion
}
}