Channel.cs 25 KB

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