Channel.cs 27 KB

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