-
-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathSshCommand.cs
More file actions
739 lines (646 loc) · 29 KB
/
SshCommand.cs
File metadata and controls
739 lines (646 loc) · 29 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#nullable enable
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Messages.Transport;
namespace Renci.SshNet
{
/// <summary>
/// Represents an SSH command that can be executed.
/// </summary>
public sealed class SshCommand : IDisposable
{
private readonly ISession _session;
private readonly Encoding _encoding;
private IChannelSession _channel;
private TaskCompletionSource<object>? _tcs;
private CancellationTokenSource? _cts;
private CancellationTokenRegistration _tokenRegistration;
private string? _stdOut;
private string? _stdErr;
private bool _hasError;
private bool _isDisposed;
private ChannelInputStream? _inputStream;
private TimeSpan _commandTimeout;
/// <summary>
/// The token supplied as an argument to <see cref="ExecuteAsync(CancellationToken)"/>.
/// </summary>
private CancellationToken _userToken;
/// <summary>
/// Whether <see cref="CancelAsync(bool, int)"/> has been called
/// (either by a token or manually).
/// </summary>
private bool _cancellationRequested;
private int _exitStatus;
private volatile bool _haveExitStatus; // volatile to prevent re-ordering of reads/writes of _exitStatus.
/// <summary>
/// Gets the command text.
/// </summary>
public string CommandText { get; private set; }
/// <summary>
/// Gets or sets the command timeout.
/// </summary>
/// <value>
/// The command timeout.
/// </value>
public TimeSpan CommandTimeout
{
get
{
return _commandTimeout;
}
set
{
value.EnsureValidTimeout(nameof(CommandTimeout));
_commandTimeout = value;
}
}
/// <summary>
/// Gets the number representing the exit status of the command, if applicable,
/// otherwise <see langword="null"/>.
/// </summary>
/// <remarks>
/// The value is not <see langword="null"/> when an exit status code has been returned
/// from the server. If the command terminated due to a signal, <see cref="ExitSignal"/>
/// may be not <see langword="null"/> instead.
/// </remarks>
/// <seealso cref="ExitSignal"/>
public int? ExitStatus
{
get
{
return _haveExitStatus ? _exitStatus : null;
}
}
/// <summary>
/// Gets the name of the signal due to which the command
/// terminated violently, if applicable, otherwise <see langword="null"/>.
/// </summary>
/// <remarks>
/// The value (if it exists) is supplied by the server and is usually one of the
/// following, as described in https://datatracker.ietf.org/doc/html/rfc4254#section-6.10:
/// ABRT, ALRM, FPE, HUP, ILL, INT, KILL, PIPE, QUIT, SEGV, TER, USR1, USR2.
/// </remarks>
public string? ExitSignal { get; private set; }
/// <summary>
/// Gets the output stream.
/// </summary>
public Stream OutputStream { get; private set; }
/// <summary>
/// Gets the extended output stream.
/// </summary>
public Stream ExtendedOutputStream { get; private set; }
/// <summary>
/// Creates and returns the input stream for the command.
/// </summary>
/// <returns>
/// The stream that can be used to transfer data to the command's input stream.
/// </returns>
/// <remarks>
/// Callers should ensure that <see cref="Stream.Dispose()"/> is called on the
/// returned instance in order to notify the command that no more data will be sent.
/// Failure to do so may result in the command executing indefinitely.
/// </remarks>
/// <example>
/// This example shows how to stream some data to 'cat' and have the server echo it back.
/// <code>
/// using (SshCommand command = mySshClient.CreateCommand("cat"))
/// {
/// Task executeTask = command.ExecuteAsync(CancellationToken.None);
///
/// using (Stream inputStream = command.CreateInputStream())
/// {
/// inputStream.Write("Hello World!"u8);
/// }
///
/// await executeTask;
///
/// Console.WriteLine(command.ExitStatus); // 0
/// Console.WriteLine(command.Result); // "Hello World!"
/// }
/// </code>
/// </example>
public Stream CreateInputStream()
{
if (!_channel.IsOpen)
{
throw new InvalidOperationException("The input stream can be used only during execution.");
}
if (_inputStream != null)
{
throw new InvalidOperationException("The input stream already exists.");
}
_inputStream = new ChannelInputStream(_channel);
return _inputStream;
}
/// <summary>
/// Gets the standard output of the command by reading <see cref="OutputStream"/>.
/// </summary>
public string Result
{
get
{
if (_stdOut is not null)
{
return _stdOut;
}
if (_tcs is null)
{
return string.Empty;
}
using (var sr = new StreamReader(OutputStream, _encoding))
{
return _stdOut = sr.ReadToEnd();
}
}
}
/// <summary>
/// Gets the standard error of the command by reading <see cref="ExtendedOutputStream"/>,
/// when extended data has been sent which has been marked as stderr.
/// </summary>
public string Error
{
get
{
if (_stdErr is not null)
{
return _stdErr;
}
if (_tcs is null || !_hasError)
{
return string.Empty;
}
using (var sr = new StreamReader(ExtendedOutputStream, _encoding))
{
return _stdErr = sr.ReadToEnd();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SshCommand"/> class.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="commandText">The command text.</param>
/// <param name="encoding">The encoding to use for the results.</param>
/// <exception cref="ArgumentNullException">Either <paramref name="session"/>, <paramref name="commandText"/> is <see langword="null"/>.</exception>
internal SshCommand(ISession session, string commandText, Encoding encoding)
{
ArgumentNullException.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(commandText);
ArgumentNullException.ThrowIfNull(encoding);
_session = session;
CommandText = commandText;
_encoding = encoding;
CommandTimeout = Timeout.InfiniteTimeSpan;
OutputStream = new PipeStream();
ExtendedOutputStream = new PipeStream();
_session.Disconnected += Session_Disconnected;
_session.ErrorOccured += Session_ErrorOccurred;
_channel = _session.CreateChannelSession();
}
/// <summary>
/// Executes the command asynchronously.
/// </summary>
/// <param name="cancellationToken">
/// The <see cref="CancellationToken"/>. When triggered, attempts to terminate the
/// remote command by sending a signal.
/// </param>
/// <returns>A <see cref="Task"/> representing the lifetime of the command.</returns>
/// <exception cref="InvalidOperationException">Command is already executing. Thrown synchronously.</exception>
/// <exception cref="ObjectDisposedException">Instance has been disposed. Thrown synchronously.</exception>
/// <exception cref="OperationCanceledException">The <see cref="Task"/> has been cancelled.</exception>
/// <exception cref="SshOperationTimeoutException">The command timed out according to <see cref="CommandTimeout"/>.</exception>
#pragma warning disable CA1849 // Call async methods when in an async method; PipeStream.DisposeAsync would complete synchronously anyway.
public Task ExecuteAsync(CancellationToken cancellationToken = default)
{
ObjectDisposedException.ThrowIf(_isDisposed, this);
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
if (_tcs is not null)
{
if (!_tcs.Task.IsCompleted)
{
throw new InvalidOperationException("Asynchronous operation is already in progress.");
}
UnsubscribeFromChannelEvents(dispose: true);
OutputStream.Dispose();
ExtendedOutputStream.Dispose();
// Initialise output streams. We already initialised them for the first
// execution in the constructor (to allow passing them around before execution)
// so we just need to reinitialise them for subsequent executions.
OutputStream = new PipeStream();
ExtendedOutputStream = new PipeStream();
_channel = _session.CreateChannelSession();
}
_exitStatus = default;
_haveExitStatus = false;
ExitSignal = null;
_stdOut = null;
_stdErr = null;
_hasError = false;
_tokenRegistration.Dispose();
_tokenRegistration = default;
_cts?.Dispose();
_cts = null;
_cancellationRequested = false;
_tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
_userToken = cancellationToken;
_channel.DataReceived += Channel_DataReceived;
_channel.ExtendedDataReceived += Channel_ExtendedDataReceived;
_channel.RequestReceived += Channel_RequestReceived;
_channel.Closed += Channel_Closed;
_channel.Open();
_ = _channel.SendExecRequest(CommandText);
if (CommandTimeout != Timeout.InfiniteTimeSpan)
{
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_cts.CancelAfter(CommandTimeout);
cancellationToken = _cts.Token;
}
if (cancellationToken.CanBeCanceled)
{
_tokenRegistration = cancellationToken.Register(static cmd =>
{
try
{
((SshCommand)cmd!).CancelAsync();
}
catch
{
// Swallow exceptions which would otherwise be unhandled.
}
},
this);
}
return _tcs.Task;
}
#pragma warning restore CA1849
/// <summary>
/// Begins an asynchronous command execution.
/// </summary>
/// <returns>
/// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pending.
/// </returns>
/// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception>
/// <exception cref="SshException">Invalid operation.</exception>
/// <exception cref="ArgumentException">CommandText property is empty.</exception>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public IAsyncResult BeginExecute()
{
return BeginExecute(callback: null, state: null);
}
/// <summary>
/// Begins an asynchronous command execution.
/// </summary>
/// <param name="callback">An optional asynchronous callback, to be called when the command execution is complete.</param>
/// <returns>
/// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pending.
/// </returns>
/// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception>
/// <exception cref="SshException">Invalid operation.</exception>
/// <exception cref="ArgumentException">CommandText property is empty.</exception>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public IAsyncResult BeginExecute(AsyncCallback? callback)
{
return BeginExecute(callback, state: null);
}
/// <summary>
/// Begins an asynchronous command execution.
/// </summary>
/// <param name="callback">An optional asynchronous callback, to be called when the command execution is complete.</param>
/// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests.</param>
/// <returns>
/// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pending.
/// </returns>
/// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception>
/// <exception cref="SshException">Invalid operation.</exception>
/// <exception cref="ArgumentException">CommandText property is empty.</exception>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public IAsyncResult BeginExecute(AsyncCallback? callback, object? state)
{
return TaskToAsyncResult.Begin(ExecuteAsync(), callback, state);
}
/// <summary>
/// Begins an asynchronous command execution.
/// </summary>
/// <param name="commandText">The command text.</param>
/// <param name="callback">An optional asynchronous callback, to be called when the command execution is complete.</param>
/// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests.</param>
/// <returns>
/// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pending.
/// </returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public IAsyncResult BeginExecute(string commandText, AsyncCallback? callback, object? state)
{
ArgumentNullException.ThrowIfNull(commandText);
CommandText = commandText;
return BeginExecute(callback, state);
}
/// <summary>
/// Waits for the pending asynchronous command execution to complete.
/// </summary>
/// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param>
/// <returns><see cref="Result"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="asyncResult"/> does not correspond to the currently executing command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public string EndExecute(IAsyncResult asyncResult)
{
var executeTask = TaskToAsyncResult.Unwrap(asyncResult);
if (executeTask != _tcs?.Task)
{
throw new ArgumentException("Argument does not correspond to the currently executing command.", nameof(asyncResult));
}
executeTask.GetAwaiter().GetResult();
return Result;
}
/// <summary>
/// Cancels a running command by sending a signal to the remote process.
/// </summary>
/// <param name="forceKill">if true send SIGKILL instead of SIGTERM.</param>
/// <param name="millisecondsTimeout">Time to wait for the server to reply.</param>
/// <remarks>
/// <para>
/// This method stops the command running on the server by sending a SIGTERM
/// (or SIGKILL, depending on <paramref name="forceKill"/>) signal to the remote
/// process. When the server implements signals, it will send a response which
/// populates <see cref="ExitSignal"/> with the signal with which the command terminated.
/// </para>
/// <para>
/// When the server does not implement signals, it may send no response. As a fallback,
/// this method waits up to <paramref name="millisecondsTimeout"/> for a response
/// and then completes the <see cref="SshCommand"/> object anyway if there was none.
/// </para>
/// <para>
/// If the command has already finished (with or without cancellation), this method does
/// nothing.
/// </para>
/// </remarks>
/// <exception cref="InvalidOperationException">Command has not been started.</exception>
public void CancelAsync(bool forceKill = false, int millisecondsTimeout = 500)
{
if (_tcs is null)
{
throw new InvalidOperationException("Command has not been started.");
}
if (_tcs.Task.IsCompleted)
{
return;
}
_cancellationRequested = true;
Interlocked.MemoryBarrier(); // ensure fresh read in SetAsyncComplete (possibly unnecessary)
try
{
// Try to send the cancellation signal.
if (_channel?.SendSignalRequest(forceKill ? "KILL" : "TERM") is null)
{
// Command has completed (in the meantime since the last check).
return;
}
// Having sent the "signal" message, we expect to receive "exit-signal"
// and then a close message. But since a server may not implement signals,
// we can't guarantee that, so we wait a short time for that to happen and
// if it doesn't, just complete the task ourselves to unblock waiters.
_ = _tcs.Task.Wait(millisecondsTimeout);
}
catch (AggregateException)
{
// We expect to be here from the call to Wait if the server implements signals.
// But we don't want to propagate the exception on the task from here.
}
finally
{
SetAsyncComplete();
}
}
private static string? GetSignalName(CommandSignal signal)
{
#if NETCOREAPP
return Enum.GetName(signal);
#else
// Boxes signal, but Enum.GetName does not have a non-boxing overload prior to .NET Core.
return Enum.GetName(typeof(CommandSignal), signal);
#endif
}
/// <summary>
/// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
/// </summary>
/// <param name="signal">The signal to send</param>
/// <returns>If the signal was sent.</returns>
public bool TrySendSignal(CommandSignal signal)
{
var signalName = GetSignalName(signal);
if (signalName is null)
{
return false;
}
if (_tcs is null || _tcs.Task.IsCompleted || _channel?.IsOpen != true)
{
return false;
}
try
{
// Try to send the cancellation signal.
return _channel.SendSignalRequest(signalName);
}
catch (Exception)
{
// Exception can be ignored since we are in a Try method
// Possible exceptions here: InvalidOperationException, SshConnectionException, SshOperationTimeoutException
}
return false;
}
/// <summary>
/// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
/// </summary>
/// <param name="signal">The signal to send</param>
/// <exception cref="ArgumentException">Signal was not a valid CommandSignal.</exception>
/// <exception cref="SshConnectionException">The client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
/// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
/// <exception cref="InvalidOperationException">Command has not been started.</exception>
public void SendSignal(CommandSignal signal)
{
var signalName = GetSignalName(signal);
if (signalName is null)
{
throw new ArgumentException("Signal was not a valid CommandSignal.");
}
if (_tcs is null || _tcs.Task.IsCompleted || _channel?.IsOpen != true)
{
throw new InvalidOperationException("Command has not been started.");
}
_ = _channel.SendSignalRequest(signalName);
}
/// <summary>
/// Executes the command specified by <see cref="CommandText"/>.
/// </summary>
/// <returns><see cref="Result"/>.</returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public string Execute()
{
ExecuteAsync().GetAwaiter().GetResult();
return Result;
}
/// <summary>
/// Executes the specified command.
/// </summary>
/// <param name="commandText">The command text.</param>
/// <returns><see cref="Result"/>.</returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public string Execute(string commandText)
{
CommandText = commandText;
return Execute();
}
private void Session_Disconnected(object? sender, EventArgs e)
{
_ = _tcs?.TrySetException(new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost));
SetAsyncComplete(setResult: false);
}
private void Session_ErrorOccurred(object? sender, ExceptionEventArgs e)
{
_ = _tcs?.TrySetException(e.Exception);
SetAsyncComplete(setResult: false);
}
private void SetAsyncComplete(bool setResult = true)
{
Interlocked.MemoryBarrier(); // ensure fresh read of _cancellationRequested (possibly unnecessary)
if (setResult)
{
Debug.Assert(_tcs is not null, "Should only be completing the task if we've started one.");
if (_userToken.IsCancellationRequested)
{
_ = _tcs.TrySetCanceled(_userToken);
}
else if (_cts?.Token.IsCancellationRequested == true)
{
_ = _tcs.TrySetException(new SshOperationTimeoutException($"Command '{CommandText}' timed out. ({nameof(CommandTimeout)}: {CommandTimeout})."));
}
else if (_cancellationRequested)
{
_ = _tcs.TrySetCanceled();
}
else
{
_ = _tcs.TrySetResult(null!);
}
}
// We don't dispose the channel here to avoid a race condition
// where SSH_MSG_CHANNEL_CLOSE arrives before _channel starts
// waiting for a response in _channel.SendExecRequest().
UnsubscribeFromChannelEvents(dispose: false);
OutputStream.Dispose();
ExtendedOutputStream.Dispose();
}
private void Channel_Closed(object? sender, ChannelEventArgs e)
{
SetAsyncComplete();
}
private void Channel_RequestReceived(object? sender, ChannelRequestEventArgs e)
{
if (e.Info is ExitStatusRequestInfo exitStatusInfo)
{
_exitStatus = (int)exitStatusInfo.ExitStatus;
_haveExitStatus = true;
Debug.Assert(!exitStatusInfo.WantReply, "exit-status is want_reply := false by definition.");
}
else if (e.Info is ExitSignalRequestInfo exitSignalInfo)
{
ExitSignal = exitSignalInfo.SignalName;
Debug.Assert(!exitSignalInfo.WantReply, "exit-signal is want_reply := false by definition.");
}
else if (e.Info.WantReply && sender is IChannel { RemoteChannelNumber: uint remoteChannelNumber })
{
var replyMessage = new ChannelFailureMessage(remoteChannelNumber);
_session.SendMessage(replyMessage);
}
}
private void Channel_ExtendedDataReceived(object? sender, ChannelExtendedDataEventArgs e)
{
ExtendedOutputStream.Write(e.Data.Array!, e.Data.Offset, e.Data.Count);
if (e.DataTypeCode == 1)
{
_hasError = true;
}
}
private void Channel_DataReceived(object? sender, ChannelDataEventArgs e)
{
OutputStream.Write(e.Data.Array!, e.Data.Offset, e.Data.Count);
}
/// <summary>
/// Unsubscribes the current <see cref="SshCommand"/> from channel events, and optionally,
/// disposes <see cref="_channel"/>.
/// </summary>
private void UnsubscribeFromChannelEvents(bool dispose)
{
var channel = _channel;
// unsubscribe from events as we do not want to be signaled should these get fired
// during the dispose of the channel
channel.DataReceived -= Channel_DataReceived;
channel.ExtendedDataReceived -= Channel_ExtendedDataReceived;
channel.RequestReceived -= Channel_RequestReceived;
channel.Closed -= Channel_Closed;
if (dispose)
{
channel.Dispose();
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
private void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}
if (disposing)
{
// unsubscribe from session events to ensure other objects that we're going to dispose
// are not accessed while disposing
_session.Disconnected -= Session_Disconnected;
_session.ErrorOccured -= Session_ErrorOccurred;
// unsubscribe from channel events to ensure other objects that we're going to dispose
// are not accessed while disposing
UnsubscribeFromChannelEvents(dispose: true);
_inputStream?.Dispose();
_inputStream = null;
OutputStream.Dispose();
ExtendedOutputStream.Dispose();
_tokenRegistration.Dispose();
_tokenRegistration = default;
_cts?.Dispose();
_cts = null;
if (_tcs is { Task.IsCompleted: false } tcs)
{
// In case an operation is still running, try to complete it with an ObjectDisposedException.
_ = tcs.TrySetException(new ObjectDisposedException(GetType().FullName));
}
_isDisposed = true;
}
}
}
}