Channel.cs 27 KB

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