2
0

Channel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. using System;
  2. using System.Threading;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Messages;
  5. using Renci.SshNet.Messages.Connection;
  6. using System.Globalization;
  7. namespace Renci.SshNet.Channels
  8. {
  9. /// <summary>
  10. /// Represents base class for SSH channel implementations.
  11. /// </summary>
  12. internal abstract class Channel : IDisposable
  13. {
  14. private EventWaitHandle _channelClosedWaitHandle = new AutoResetEvent(false);
  15. private EventWaitHandle _channelWindowAdjustWaitHandle = new AutoResetEvent(false);
  16. private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(false);
  17. private EventWaitHandle _disconnectedWaitHandle = new ManualResetEvent(false);
  18. private bool _closeMessageSent = false;
  19. private uint _initialWindowSize = 0x100000;
  20. private uint _maximumPacketSize = 0x8000;
  21. private Session _session;
  22. /// <summary>
  23. /// Gets the type of the channel.
  24. /// </summary>
  25. /// <value>
  26. /// The type of the channel.
  27. /// </value>
  28. public abstract ChannelTypes ChannelType { get; }
  29. /// <summary>
  30. /// Gets the local channel number.
  31. /// </summary>
  32. public uint LocalChannelNumber { get; private set; }
  33. /// <summary>
  34. /// Gets the remote channel number assigned by the server.
  35. /// </summary>
  36. public uint RemoteChannelNumber { get; private set; }
  37. /// <summary>
  38. /// Gets the size of the local window.
  39. /// </summary>
  40. /// <value>
  41. /// The size of the local window.
  42. /// </value>
  43. public uint LocalWindowSize { get; private set; }
  44. /// <summary>
  45. /// Gets or sets the size of the server window.
  46. /// </summary>
  47. /// <value>
  48. /// The size of the server window.
  49. /// </value>
  50. public uint ServerWindowSize { get; protected set; }
  51. /// <summary>
  52. /// Gets the size of the packet.
  53. /// </summary>
  54. /// <value>
  55. /// The size of the packet.
  56. /// </value>
  57. public uint PacketSize { get; private set; }
  58. /// <summary>
  59. /// Gets a value indicating whether this channel is open.
  60. /// </summary>
  61. /// <value>
  62. /// <c>true</c> if this channel is open; otherwise, <c>false</c>.
  63. /// </value>
  64. public bool IsOpen { get; private set; }
  65. #region Message events
  66. /// <summary>
  67. /// Occurs when <see cref="ChannelOpenFailureMessage"/> message received
  68. /// </summary>
  69. public event EventHandler<ChannelOpenFailedEventArgs> OpenFailed;
  70. /// <summary>
  71. /// Occurs when <see cref="ChannelDataMessage"/> message received
  72. /// </summary>
  73. public event EventHandler<ChannelDataEventArgs> DataReceived;
  74. /// <summary>
  75. /// Occurs when <see cref="ChannelExtendedDataMessage"/> message received
  76. /// </summary>
  77. public event EventHandler<ChannelDataEventArgs> ExtendedDataReceived;
  78. /// <summary>
  79. /// Occurs when <see cref="ChannelEofMessage"/> message received
  80. /// </summary>
  81. public event EventHandler<ChannelEventArgs> EndOfData;
  82. /// <summary>
  83. /// Occurs when <see cref="ChannelCloseMessage"/> message received
  84. /// </summary>
  85. public event EventHandler<ChannelEventArgs> Closed;
  86. /// <summary>
  87. /// Occurs when <see cref="ChannelRequestMessage"/> message received
  88. /// </summary>
  89. public event EventHandler<ChannelRequestEventArgs> RequestReceived;
  90. /// <summary>
  91. /// Occurs when <see cref="ChannelSuccessMessage"/> message received
  92. /// </summary>
  93. public event EventHandler<ChannelEventArgs> RequestSuccessed;
  94. /// <summary>
  95. /// Occurs when <see cref="ChannelFailureMessage"/> message received
  96. /// </summary>
  97. public event EventHandler<ChannelEventArgs> RequestFailed;
  98. #endregion
  99. /// <summary>
  100. /// Gets a value indicating whether the session is connected.
  101. /// </summary>
  102. /// <value>
  103. /// <c>true</c> if the session is connected; otherwise, <c>false</c>.
  104. /// </value>
  105. protected bool IsConnected
  106. {
  107. get { return this._session.IsConnected; }
  108. }
  109. /// <summary>
  110. /// Gets the connection info.
  111. /// </summary>
  112. /// <value>The connection info.</value>
  113. protected ConnectionInfo ConnectionInfo
  114. {
  115. get
  116. {
  117. return this._session.ConnectionInfo;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets the session semaphore to control number of session channels
  122. /// </summary>
  123. /// <value>The session semaphore.</value>
  124. protected SemaphoreLight SessionSemaphore
  125. {
  126. get
  127. {
  128. return this._session.SessionSemaphore;
  129. }
  130. }
  131. /// <summary>
  132. /// Initializes a new instance of the <see cref="Channel"/> class.
  133. /// </summary>
  134. internal Channel()
  135. {
  136. }
  137. /// <summary>
  138. /// Initializes the channel.
  139. /// </summary>
  140. /// <param name="session">The session.</param>
  141. /// <param name="serverChannelNumber">The server channel number.</param>
  142. /// <param name="windowSize">Size of the window.</param>
  143. /// <param name="packetSize">Size of the packet.</param>
  144. internal virtual void Initialize(Session session, uint serverChannelNumber, uint windowSize, uint packetSize)
  145. {
  146. this._initialWindowSize = windowSize;
  147. this._maximumPacketSize = Math.Max(packetSize, 0x8000); // Ensure minimum maximum packet size of 0x8000 bytes
  148. this._session = session;
  149. this.LocalWindowSize = this._initialWindowSize; // Initial window size
  150. this.PacketSize = this._maximumPacketSize; // Maximum packet size
  151. this.LocalChannelNumber = session.NextChannelNumber;
  152. this.RemoteChannelNumber = serverChannelNumber;
  153. this._session.ChannelOpenReceived += OnChannelOpen;
  154. this._session.ChannelOpenConfirmationReceived += OnChannelOpenConfirmation;
  155. this._session.ChannelOpenFailureReceived += OnChannelOpenFailure;
  156. this._session.ChannelWindowAdjustReceived += OnChannelWindowAdjust;
  157. this._session.ChannelDataReceived += OnChannelData;
  158. this._session.ChannelExtendedDataReceived += OnChannelExtendedData;
  159. this._session.ChannelEofReceived += OnChannelEof;
  160. this._session.ChannelCloseReceived += OnChannelClose;
  161. this._session.ChannelRequestReceived += OnChannelRequest;
  162. this._session.ChannelSuccessReceived += OnChannelSuccess;
  163. this._session.ChannelFailureReceived += OnChannelFailure;
  164. this._session.ErrorOccured += Session_ErrorOccured;
  165. this._session.Disconnected += Session_Disconnected;
  166. }
  167. /// <summary>
  168. /// Sends the SSH_MSG_CHANNEL_EOF message.
  169. /// </summary>
  170. internal void SendEof()
  171. {
  172. // Send EOF message first when channel need to be closed
  173. this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber));
  174. }
  175. internal void SendData(byte[] buffer)
  176. {
  177. this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer));
  178. }
  179. /// <summary>
  180. /// Closes the channel.
  181. /// </summary>
  182. public virtual void Close()
  183. {
  184. // Send message to close the channel on the server
  185. if (!_closeMessageSent)
  186. {
  187. this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber));
  188. this._closeMessageSent = true;
  189. }
  190. // Wait for channel to be closed
  191. this._session.WaitHandle(this._channelClosedWaitHandle);
  192. }
  193. #region Channel virtual methods
  194. /// <summary>
  195. /// Called when channel need to be open on the client.
  196. /// </summary>
  197. /// <param name="info">Channel open information.</param>
  198. protected virtual void OnOpen(ChannelOpenInfo info)
  199. {
  200. }
  201. /// <summary>
  202. /// Called when channel is opened by the server.
  203. /// </summary>
  204. /// <param name="remoteChannelNumber">The remote channel number.</param>
  205. /// <param name="initialWindowSize">Initial size of the window.</param>
  206. /// <param name="maximumPacketSize">Maximum size of the packet.</param>
  207. protected virtual void OnOpenConfirmation(uint remoteChannelNumber, uint initialWindowSize, uint maximumPacketSize)
  208. {
  209. this.RemoteChannelNumber = remoteChannelNumber;
  210. this.ServerWindowSize = initialWindowSize;
  211. this.PacketSize = maximumPacketSize;
  212. // Chanel consider to be open when confirmation message was received
  213. this.IsOpen = true;
  214. }
  215. /// <summary>
  216. /// Called when channel failed to open.
  217. /// </summary>
  218. /// <param name="reasonCode">The reason code.</param>
  219. /// <param name="description">The description.</param>
  220. /// <param name="language">The language.</param>
  221. protected virtual void OnOpenFailure(uint reasonCode, string description, string language)
  222. {
  223. if (this.OpenFailed != null)
  224. {
  225. this.OpenFailed(this, new ChannelOpenFailedEventArgs(this.LocalChannelNumber, reasonCode, description, language));
  226. }
  227. }
  228. /// <summary>
  229. /// Called when channel window need to be adjust.
  230. /// </summary>
  231. /// <param name="bytesToAdd">The bytes to add.</param>
  232. protected virtual void OnWindowAdjust(uint bytesToAdd)
  233. {
  234. this.ServerWindowSize += bytesToAdd;
  235. }
  236. /// <summary>
  237. /// Called when channel data is received.
  238. /// </summary>
  239. /// <param name="data">The data.</param>
  240. protected virtual void OnData(byte[] data)
  241. {
  242. this.AdjustDataWindow(data);
  243. if (this.DataReceived != null)
  244. {
  245. this.DataReceived(this, new ChannelDataEventArgs(this.LocalChannelNumber, data));
  246. }
  247. }
  248. /// <summary>
  249. /// Called when channel extended data is received.
  250. /// </summary>
  251. /// <param name="data">The data.</param>
  252. /// <param name="dataTypeCode">The data type code.</param>
  253. protected virtual void OnExtendedData(byte[] data, uint dataTypeCode)
  254. {
  255. this.AdjustDataWindow(data);
  256. if (this.ExtendedDataReceived != null)
  257. {
  258. this.ExtendedDataReceived(this, new ChannelDataEventArgs(this.LocalChannelNumber, data, dataTypeCode));
  259. }
  260. }
  261. /// <summary>
  262. /// Called when channel has no more data to receive.
  263. /// </summary>
  264. protected virtual void OnEof()
  265. {
  266. if (this.EndOfData != null)
  267. {
  268. this.EndOfData(this, new ChannelEventArgs(this.LocalChannelNumber));
  269. }
  270. }
  271. /// <summary>
  272. /// Called when channel is closed by the server.
  273. /// </summary>
  274. protected virtual void OnClose()
  275. {
  276. // No more channel messages are allowed after Close message received
  277. this._session.ChannelOpenReceived -= OnChannelOpen;
  278. this._session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
  279. this._session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
  280. this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
  281. this._session.ChannelDataReceived -= OnChannelData;
  282. this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
  283. this._session.ChannelEofReceived -= OnChannelEof;
  284. this._session.ChannelCloseReceived -= OnChannelClose;
  285. this._session.ChannelRequestReceived -= OnChannelRequest;
  286. this._session.ChannelSuccessReceived -= OnChannelSuccess;
  287. this._session.ChannelFailureReceived -= OnChannelFailure;
  288. this._session.ErrorOccured -= Session_ErrorOccured;
  289. this._session.Disconnected -= Session_Disconnected;
  290. // Send close message to channel to confirm channel closing
  291. if (!_closeMessageSent)
  292. {
  293. this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber));
  294. this._closeMessageSent = true;
  295. }
  296. if (this.Closed != null)
  297. {
  298. this.Closed(this, new ChannelEventArgs(this.LocalChannelNumber));
  299. }
  300. }
  301. /// <summary>
  302. /// Called when channel request received.
  303. /// </summary>
  304. /// <param name="info">Channel request information.</param>
  305. protected virtual void OnRequest(RequestInfo info)
  306. {
  307. if (this.RequestReceived != null)
  308. {
  309. this.RequestReceived(this, new ChannelRequestEventArgs(info));
  310. }
  311. }
  312. /// <summary>
  313. /// Called when channel request was successful
  314. /// </summary>
  315. protected virtual void OnSuccess()
  316. {
  317. if (this.RequestSuccessed != null)
  318. {
  319. this.RequestSuccessed(this, new ChannelEventArgs(this.LocalChannelNumber));
  320. }
  321. }
  322. /// <summary>
  323. /// Called when channel request failed.
  324. /// </summary>
  325. protected virtual void OnFailure()
  326. {
  327. if (this.RequestFailed != null)
  328. {
  329. this.RequestFailed(this, new ChannelEventArgs(this.LocalChannelNumber));
  330. }
  331. }
  332. #endregion
  333. /// <summary>
  334. /// Sends SSH message to the server.
  335. /// </summary>
  336. /// <param name="message">The message.</param>
  337. protected void SendMessage(Message message)
  338. {
  339. // Send channel messages only while channel is open
  340. if (!this.IsOpen)
  341. return;
  342. this._session.SendMessage(message);
  343. }
  344. protected void SendMessage(ChannelOpenConfirmationMessage message)
  345. {
  346. // No need to check whether channel is open when trying to open a channel
  347. this._session.SendMessage(message);
  348. // Chanel consider to be open when confirmation message is sent
  349. this.IsOpen = true;
  350. }
  351. /// <summary>
  352. /// Send message to open a channel.
  353. /// </summary>
  354. /// <param name="message">Message to send</param>
  355. protected void SendMessage(ChannelOpenMessage message)
  356. {
  357. // No need to check whether channel is open when trying to open a channel
  358. this._session.SendMessage(message);
  359. }
  360. /// <summary>
  361. /// Sends close channel message to the server
  362. /// </summary>
  363. /// <param name="message">Message to send.</param>
  364. protected void SendMessage(ChannelCloseMessage message)
  365. {
  366. // Send channel messages only while channel is open
  367. if (!this.IsOpen)
  368. return;
  369. this._session.SendMessage(message);
  370. // When channel close message is sent channel considred to be closed
  371. this.IsOpen = false;
  372. }
  373. /// <summary>
  374. /// Sends channel data message to the servers.
  375. /// </summary>
  376. /// <remarks>This method takes care of managing the window size.</remarks>
  377. /// <param name="message">Channel data message.</param>
  378. protected void SendMessage(ChannelDataMessage message)
  379. {
  380. // Send channel messages only while channel is open
  381. if (!this.IsOpen)
  382. return;
  383. if (this.ServerWindowSize < 1)
  384. {
  385. // Wait for window to be adjust
  386. this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
  387. }
  388. this.ServerWindowSize -= (uint)message.Data.Length;
  389. this._session.SendMessage(message);
  390. }
  391. /// <summary>
  392. /// Sends channel extended data message to the servers.
  393. /// </summary>
  394. /// <remarks>This method takes care of managing the window size.</remarks>
  395. /// <param name="message">Channel data message.</param>
  396. protected void SendMessage(ChannelExtendedDataMessage message)
  397. {
  398. // Send channel messages only while channel is open
  399. if (!this.IsOpen)
  400. return;
  401. if (this.ServerWindowSize < 1)
  402. {
  403. // Wait for window to be adjust
  404. this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
  405. }
  406. this.ServerWindowSize -= (uint)message.Data.Length;
  407. this._session.SendMessage(message);
  408. }
  409. /// <summary>
  410. /// Waits for the handle to be signaled or for an error to occurs.
  411. /// </summary>
  412. /// <param name="waitHandle">The wait handle.</param>
  413. protected void WaitHandle(WaitHandle waitHandle)
  414. {
  415. this._session.WaitHandle(waitHandle);
  416. }
  417. private void Session_Disconnected(object sender, EventArgs e)
  418. {
  419. // If objected is disposed or being disposed don't handle this event
  420. if (this._isDisposed)
  421. return;
  422. this._disconnectedWaitHandle.Set();
  423. }
  424. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  425. {
  426. // If objected is disposed or being disposed don't handle this event
  427. if (this._isDisposed)
  428. return;
  429. this._errorOccuredWaitHandle.Set();
  430. }
  431. #region Channel message event handlers
  432. private void OnChannelOpen(object sender, MessageEventArgs<ChannelOpenMessage> e)
  433. {
  434. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  435. {
  436. this.OnOpen(e.Message.Info);
  437. }
  438. }
  439. private void OnChannelOpenConfirmation(object sender, MessageEventArgs<ChannelOpenConfirmationMessage> e)
  440. {
  441. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  442. {
  443. this.OnOpenConfirmation(e.Message.RemoteChannelNumber, e.Message.InitialWindowSize, e.Message.MaximumPacketSize);
  444. }
  445. }
  446. private void OnChannelOpenFailure(object sender, MessageEventArgs<ChannelOpenFailureMessage> e)
  447. {
  448. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  449. {
  450. this.OnOpenFailure(e.Message.ReasonCode, e.Message.Description, e.Message.Language);
  451. }
  452. }
  453. private void OnChannelWindowAdjust(object sender, MessageEventArgs<ChannelWindowAdjustMessage> e)
  454. {
  455. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  456. {
  457. this.OnWindowAdjust(e.Message.BytesToAdd);
  458. this._channelWindowAdjustWaitHandle.Set();
  459. }
  460. }
  461. private void OnChannelData(object sender, MessageEventArgs<ChannelDataMessage> e)
  462. {
  463. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  464. {
  465. this.OnData(e.Message.Data);
  466. }
  467. }
  468. private void OnChannelExtendedData(object sender, MessageEventArgs<ChannelExtendedDataMessage> e)
  469. {
  470. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  471. {
  472. this.OnExtendedData(e.Message.Data, e.Message.DataTypeCode);
  473. }
  474. }
  475. private void OnChannelEof(object sender, MessageEventArgs<ChannelEofMessage> e)
  476. {
  477. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  478. {
  479. this.OnEof();
  480. }
  481. }
  482. private void OnChannelClose(object sender, MessageEventArgs<ChannelCloseMessage> e)
  483. {
  484. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  485. {
  486. this.OnClose();
  487. this._channelClosedWaitHandle.Set();
  488. }
  489. }
  490. private void OnChannelRequest(object sender, MessageEventArgs<ChannelRequestMessage> e)
  491. {
  492. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  493. {
  494. if (this._session.ConnectionInfo.ChannelRequests.ContainsKey(e.Message.RequestName))
  495. {
  496. // Get request specific class
  497. RequestInfo requestInfo = this._session.ConnectionInfo.ChannelRequests[e.Message.RequestName];
  498. // Load request specific data
  499. requestInfo.Load(e.Message.RequestData);
  500. // Raise request specific event
  501. this.OnRequest(requestInfo);
  502. }
  503. else
  504. {
  505. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Request '{0}' is not supported.", e.Message.RequestName));
  506. }
  507. }
  508. }
  509. private void OnChannelSuccess(object sender, MessageEventArgs<ChannelSuccessMessage> e)
  510. {
  511. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  512. {
  513. this.OnSuccess();
  514. }
  515. }
  516. private void OnChannelFailure(object sender, MessageEventArgs<ChannelFailureMessage> e)
  517. {
  518. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  519. {
  520. this.OnFailure();
  521. }
  522. }
  523. #endregion
  524. private void AdjustDataWindow(byte[] messageData)
  525. {
  526. this.LocalWindowSize -= (uint)messageData.Length;
  527. // Adjust window if window size is too low
  528. if (this.LocalWindowSize < this.PacketSize)
  529. {
  530. this.SendMessage(new ChannelWindowAdjustMessage(this.RemoteChannelNumber, this._initialWindowSize - this.LocalWindowSize));
  531. this.LocalWindowSize = this._initialWindowSize;
  532. }
  533. }
  534. #region IDisposable Members
  535. private bool _isDisposed = false;
  536. /// <summary>
  537. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  538. /// </summary>
  539. public void Dispose()
  540. {
  541. this.Dispose(true);
  542. GC.SuppressFinalize(this);
  543. }
  544. /// <summary>
  545. /// Releases unmanaged and - optionally - managed resources
  546. /// </summary>
  547. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  548. protected virtual void Dispose(bool disposing)
  549. {
  550. // Check to see if Dispose has already been called.
  551. if (!this._isDisposed)
  552. {
  553. // If disposing equals true, dispose all managed
  554. // and unmanaged resources.
  555. if (disposing)
  556. {
  557. // Dispose managed resources.
  558. if (this._channelClosedWaitHandle != null)
  559. {
  560. this._channelClosedWaitHandle.Dispose();
  561. this._channelClosedWaitHandle = null;
  562. }
  563. if (this._channelWindowAdjustWaitHandle != null)
  564. {
  565. this._channelWindowAdjustWaitHandle.Dispose();
  566. this._channelWindowAdjustWaitHandle = null;
  567. }
  568. if (this._errorOccuredWaitHandle != null)
  569. {
  570. this._errorOccuredWaitHandle.Dispose();
  571. this._errorOccuredWaitHandle = null;
  572. }
  573. if (this._disconnectedWaitHandle != null)
  574. {
  575. this._disconnectedWaitHandle.Dispose();
  576. this._disconnectedWaitHandle = null;
  577. }
  578. }
  579. // Ensure that all events are detached from current instance
  580. this._session.ChannelOpenReceived -= OnChannelOpen;
  581. this._session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
  582. this._session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
  583. this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
  584. this._session.ChannelDataReceived -= OnChannelData;
  585. this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
  586. this._session.ChannelEofReceived -= OnChannelEof;
  587. this._session.ChannelCloseReceived -= OnChannelClose;
  588. this._session.ChannelRequestReceived -= OnChannelRequest;
  589. this._session.ChannelSuccessReceived -= OnChannelSuccess;
  590. this._session.ChannelFailureReceived -= OnChannelFailure;
  591. this._session.ErrorOccured -= Session_ErrorOccured;
  592. this._session.Disconnected -= Session_Disconnected;
  593. // Note disposing has been done.
  594. _isDisposed = true;
  595. }
  596. }
  597. /// <summary>
  598. /// Releases unmanaged resources and performs other cleanup operations before the
  599. /// <see cref="Channel"/> is reclaimed by garbage collection.
  600. /// </summary>
  601. ~Channel()
  602. {
  603. // Do not re-create Dispose clean-up code here.
  604. // Calling Dispose(false) is optimal in terms of
  605. // readability and maintainability.
  606. this.Dispose(false);
  607. }
  608. #endregion
  609. }
  610. }