Channel.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. /// Sends the SSH_MSG_CHANNEL_EOF message.
  168. /// </summary>
  169. internal void SendEof()
  170. {
  171. // Send EOF message first when channel need to be closed
  172. this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber));
  173. }
  174. /// <summary>
  175. /// Closes the channel.
  176. /// </summary>
  177. public virtual void Close()
  178. {
  179. // Send message to close the channel on the server
  180. this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber));
  181. // Wait for channel to be closed
  182. this._session.WaitHandle(this._channelClosedWaitHandle);
  183. this.Dispose();
  184. }
  185. #region Channel virtual methods
  186. /// <summary>
  187. /// Called when channel need to be open on the client.
  188. /// </summary>
  189. /// <param name="info">Channel open information.</param>
  190. protected virtual void OnOpen(ChannelOpenInfo info)
  191. {
  192. }
  193. /// <summary>
  194. /// Called when channel is opened by the server.
  195. /// </summary>
  196. /// <param name="remoteChannelNumber">The remote channel number.</param>
  197. /// <param name="initialWindowSize">Initial size of the window.</param>
  198. /// <param name="maximumPacketSize">Maximum size of the packet.</param>
  199. protected virtual void OnOpenConfirmation(uint remoteChannelNumber, uint initialWindowSize, uint maximumPacketSize)
  200. {
  201. this.RemoteChannelNumber = remoteChannelNumber;
  202. this.ServerWindowSize = initialWindowSize;
  203. this.PacketSize = maximumPacketSize;
  204. // Chanel consider to be open when confirmation message was received
  205. this.IsOpen = true;
  206. }
  207. /// <summary>
  208. /// Called when channel failed to open.
  209. /// </summary>
  210. /// <param name="reasonCode">The reason code.</param>
  211. /// <param name="description">The description.</param>
  212. /// <param name="language">The language.</param>
  213. protected virtual void OnOpenFailure(uint reasonCode, string description, string language)
  214. {
  215. if (this.OpenFailed != null)
  216. {
  217. this.OpenFailed(this, new ChannelOpenFailedEventArgs(this.LocalChannelNumber, reasonCode, description, language));
  218. }
  219. }
  220. /// <summary>
  221. /// Called when channel window need to be adjust.
  222. /// </summary>
  223. /// <param name="bytesToAdd">The bytes to add.</param>
  224. protected virtual void OnWindowAdjust(uint bytesToAdd)
  225. {
  226. this.ServerWindowSize += bytesToAdd;
  227. }
  228. /// <summary>
  229. /// Called when channel data is received.
  230. /// </summary>
  231. /// <param name="data">The data.</param>
  232. protected virtual void OnData(byte[] data)
  233. {
  234. this.AdjustDataWindow(data);
  235. if (this.DataReceived != null)
  236. {
  237. this.DataReceived(this, new ChannelDataEventArgs(this.LocalChannelNumber, data));
  238. }
  239. }
  240. /// <summary>
  241. /// Called when channel extended data is received.
  242. /// </summary>
  243. /// <param name="data">The data.</param>
  244. /// <param name="dataTypeCode">The data type code.</param>
  245. protected virtual void OnExtendedData(byte[] data, uint dataTypeCode)
  246. {
  247. this.AdjustDataWindow(data);
  248. if (this.ExtendedDataReceived != null)
  249. {
  250. this.ExtendedDataReceived(this, new ChannelDataEventArgs(this.LocalChannelNumber, data, dataTypeCode));
  251. }
  252. }
  253. /// <summary>
  254. /// Called when channel has no more data to receive.
  255. /// </summary>
  256. protected virtual void OnEof()
  257. {
  258. if (this.EndOfData != null)
  259. {
  260. this.EndOfData(this, new ChannelEventArgs(this.LocalChannelNumber));
  261. }
  262. }
  263. /// <summary>
  264. /// Called when channel is closed by the server.
  265. /// </summary>
  266. protected virtual void OnClose()
  267. {
  268. // No more channel messages are allowed after Close message received
  269. this._session.ChannelOpenReceived -= OnChannelOpen;
  270. this._session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
  271. this._session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
  272. this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
  273. this._session.ChannelDataReceived -= OnChannelData;
  274. this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
  275. this._session.ChannelEofReceived -= OnChannelEof;
  276. this._session.ChannelCloseReceived -= OnChannelClose;
  277. this._session.ChannelRequestReceived -= OnChannelRequest;
  278. this._session.ChannelSuccessReceived -= OnChannelSuccess;
  279. this._session.ChannelFailureReceived -= OnChannelFailure;
  280. this._session.ErrorOccured -= Session_ErrorOccured;
  281. this._session.Disconnected -= Session_Disconnected;
  282. // Send close message to channel to confirm channel closing
  283. this.SendMessage(new ChannelCloseMessage(this.RemoteChannelNumber));
  284. if (this.Closed != null)
  285. {
  286. this.Closed(this, new ChannelEventArgs(this.LocalChannelNumber));
  287. }
  288. }
  289. /// <summary>
  290. /// Called when channel request received.
  291. /// </summary>
  292. /// <param name="info">Channel request information.</param>
  293. protected virtual void OnRequest(RequestInfo info)
  294. {
  295. if (this.RequestReceived != null)
  296. {
  297. this.RequestReceived(this, new ChannelRequestEventArgs(info));
  298. }
  299. }
  300. /// <summary>
  301. /// Called when channel request was successful
  302. /// </summary>
  303. protected virtual void OnSuccess()
  304. {
  305. if (this.RequestSuccessed != null)
  306. {
  307. this.RequestSuccessed(this, new ChannelEventArgs(this.LocalChannelNumber));
  308. }
  309. }
  310. /// <summary>
  311. /// Called when channel request failed.
  312. /// </summary>
  313. protected virtual void OnFailure()
  314. {
  315. if (this.RequestFailed != null)
  316. {
  317. this.RequestFailed(this, new ChannelEventArgs(this.LocalChannelNumber));
  318. }
  319. }
  320. #endregion
  321. /// <summary>
  322. /// Sends SSH message to the server.
  323. /// </summary>
  324. /// <param name="message">The message.</param>
  325. protected void SendMessage(Message message)
  326. {
  327. // Send channel messages only while channel is open
  328. if (!this.IsOpen)
  329. return;
  330. this._session.SendMessage(message);
  331. }
  332. protected void SendMessage(ChannelOpenConfirmationMessage message)
  333. {
  334. // No need to check whether channel is open when trying to open a channel
  335. this._session.SendMessage(message);
  336. // Chanel consider to be open when confirmation message is sent
  337. this.IsOpen = true;
  338. }
  339. /// <summary>
  340. /// Send message to open a channel.
  341. /// </summary>
  342. /// <param name="message">Message to send</param>
  343. protected void SendMessage(ChannelOpenMessage message)
  344. {
  345. // No need to check whether channel is open when trying to open a channel
  346. this._session.SendMessage(message);
  347. }
  348. /// <summary>
  349. /// Sends close channel message to the server
  350. /// </summary>
  351. /// <param name="message">Message to send.</param>
  352. protected void SendMessage(ChannelCloseMessage message)
  353. {
  354. // Send channel messages only while channel is open
  355. if (!this.IsOpen)
  356. return;
  357. this._session.SendMessage(message);
  358. // When channel close message is sent channel considred to be closed
  359. this.IsOpen = false;
  360. }
  361. /// <summary>
  362. /// Sends channel data message to the servers.
  363. /// </summary>
  364. /// <remarks>This method takes care of managing the window size.</remarks>
  365. /// <param name="message">Channel data message.</param>
  366. protected void SendMessage(ChannelDataMessage message)
  367. {
  368. // Send channel messages only while channel is open
  369. if (!this.IsOpen)
  370. return;
  371. if (this.ServerWindowSize < 1)
  372. {
  373. // Wait for window to be adjust
  374. this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
  375. }
  376. this.ServerWindowSize -= (uint)message.Data.Length;
  377. this._session.SendMessage(message);
  378. }
  379. /// <summary>
  380. /// Sends channel extended data message to the servers.
  381. /// </summary>
  382. /// <remarks>This method takes care of managing the window size.</remarks>
  383. /// <param name="message">Channel data message.</param>
  384. protected void SendMessage(ChannelExtendedDataMessage message)
  385. {
  386. // Send channel messages only while channel is open
  387. if (!this.IsOpen)
  388. return;
  389. if (this.ServerWindowSize < 1)
  390. {
  391. // Wait for window to be adjust
  392. this._session.WaitHandle(this._channelWindowAdjustWaitHandle);
  393. }
  394. this.ServerWindowSize -= (uint)message.Data.Length;
  395. this._session.SendMessage(message);
  396. }
  397. /// <summary>
  398. /// Waits for the handle to be signaled or for an error to occurs.
  399. /// </summary>
  400. /// <param name="waitHandle">The wait handle.</param>
  401. protected void WaitHandle(WaitHandle waitHandle)
  402. {
  403. this._session.WaitHandle(waitHandle);
  404. }
  405. private void Session_Disconnected(object sender, EventArgs e)
  406. {
  407. this._disconnectedWaitHandle.Set();
  408. }
  409. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  410. {
  411. this._errorOccuredWaitHandle.Set();
  412. }
  413. #region Channel message event handlers
  414. private void OnChannelOpen(object sender, MessageEventArgs<ChannelOpenMessage> e)
  415. {
  416. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  417. {
  418. this.OnOpen(e.Message.Info);
  419. }
  420. }
  421. private void OnChannelOpenConfirmation(object sender, MessageEventArgs<ChannelOpenConfirmationMessage> e)
  422. {
  423. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  424. {
  425. this.OnOpenConfirmation(e.Message.RemoteChannelNumber, e.Message.InitialWindowSize, e.Message.MaximumPacketSize);
  426. }
  427. }
  428. private void OnChannelOpenFailure(object sender, MessageEventArgs<ChannelOpenFailureMessage> e)
  429. {
  430. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  431. {
  432. this.OnOpenFailure(e.Message.ReasonCode, e.Message.Description, e.Message.Language);
  433. }
  434. }
  435. private void OnChannelWindowAdjust(object sender, MessageEventArgs<ChannelWindowAdjustMessage> e)
  436. {
  437. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  438. {
  439. this.OnWindowAdjust(e.Message.BytesToAdd);
  440. this._channelWindowAdjustWaitHandle.Set();
  441. }
  442. }
  443. private void OnChannelData(object sender, MessageEventArgs<ChannelDataMessage> e)
  444. {
  445. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  446. {
  447. this.OnData(e.Message.Data);
  448. }
  449. }
  450. private void OnChannelExtendedData(object sender, MessageEventArgs<ChannelExtendedDataMessage> e)
  451. {
  452. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  453. {
  454. this.OnExtendedData(e.Message.Data, e.Message.DataTypeCode);
  455. }
  456. }
  457. private void OnChannelEof(object sender, MessageEventArgs<ChannelEofMessage> e)
  458. {
  459. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  460. {
  461. this.OnEof();
  462. }
  463. }
  464. private void OnChannelClose(object sender, MessageEventArgs<ChannelCloseMessage> e)
  465. {
  466. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  467. {
  468. this.OnClose();
  469. this._channelClosedWaitHandle.Set();
  470. }
  471. }
  472. private void OnChannelRequest(object sender, MessageEventArgs<ChannelRequestMessage> e)
  473. {
  474. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  475. {
  476. if (this._session.ConnectionInfo.ChannelRequests.ContainsKey(e.Message.RequestName))
  477. {
  478. // Get request specific class
  479. RequestInfo requestInfo = this._session.ConnectionInfo.ChannelRequests[e.Message.RequestName];
  480. // Load request specific data
  481. requestInfo.Load(e.Message.RequestData);
  482. // Raise request specific event
  483. this.OnRequest(requestInfo);
  484. }
  485. else
  486. {
  487. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Request '{0}' is not supported.", e.Message.RequestName));
  488. }
  489. }
  490. }
  491. private void OnChannelSuccess(object sender, MessageEventArgs<ChannelSuccessMessage> e)
  492. {
  493. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  494. {
  495. this.OnSuccess();
  496. }
  497. }
  498. private void OnChannelFailure(object sender, MessageEventArgs<ChannelFailureMessage> e)
  499. {
  500. if (e.Message.LocalChannelNumber == this.LocalChannelNumber)
  501. {
  502. this.OnFailure();
  503. }
  504. }
  505. #endregion
  506. private void AdjustDataWindow(byte[] messageData)
  507. {
  508. this.LocalWindowSize -= (uint)messageData.Length;
  509. // Adjust window if window size is too low
  510. if (this.LocalWindowSize < this.PacketSize)
  511. {
  512. this.SendMessage(new ChannelWindowAdjustMessage(this.RemoteChannelNumber, this._initialWindowSize - this.LocalWindowSize));
  513. this.LocalWindowSize = this._initialWindowSize;
  514. }
  515. }
  516. #region IDisposable Members
  517. private bool _isDisposed = false;
  518. /// <summary>
  519. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  520. /// </summary>
  521. public void Dispose()
  522. {
  523. this.Dispose(true);
  524. GC.SuppressFinalize(this);
  525. }
  526. /// <summary>
  527. /// Releases unmanaged and - optionally - managed resources
  528. /// </summary>
  529. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  530. protected virtual void Dispose(bool disposing)
  531. {
  532. // Check to see if Dispose has already been called.
  533. if (!this._isDisposed)
  534. {
  535. // If disposing equals true, dispose all managed
  536. // and unmanaged resources.
  537. if (disposing)
  538. {
  539. // Dispose managed resources.
  540. if (this._channelClosedWaitHandle != null)
  541. {
  542. this._channelClosedWaitHandle.Dispose();
  543. this._channelClosedWaitHandle = null;
  544. }
  545. if (this._channelWindowAdjustWaitHandle != null)
  546. {
  547. this._channelWindowAdjustWaitHandle.Dispose();
  548. this._channelWindowAdjustWaitHandle = null;
  549. }
  550. if (this._errorOccuredWaitHandle != null)
  551. {
  552. this._errorOccuredWaitHandle.Dispose();
  553. this._errorOccuredWaitHandle = null;
  554. }
  555. if (this._disconnectedWaitHandle != null)
  556. {
  557. this._disconnectedWaitHandle.Dispose();
  558. this._disconnectedWaitHandle = null;
  559. }
  560. }
  561. // Ensure that all events are detached from current instance
  562. this._session.ChannelOpenReceived -= OnChannelOpen;
  563. this._session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation;
  564. this._session.ChannelOpenFailureReceived -= OnChannelOpenFailure;
  565. this._session.ChannelWindowAdjustReceived -= OnChannelWindowAdjust;
  566. this._session.ChannelDataReceived -= OnChannelData;
  567. this._session.ChannelExtendedDataReceived -= OnChannelExtendedData;
  568. this._session.ChannelEofReceived -= OnChannelEof;
  569. this._session.ChannelCloseReceived -= OnChannelClose;
  570. this._session.ChannelRequestReceived -= OnChannelRequest;
  571. this._session.ChannelSuccessReceived -= OnChannelSuccess;
  572. this._session.ChannelFailureReceived -= OnChannelFailure;
  573. this._session.ErrorOccured -= Session_ErrorOccured;
  574. this._session.Disconnected -= Session_Disconnected;
  575. // Note disposing has been done.
  576. _isDisposed = true;
  577. }
  578. }
  579. /// <summary>
  580. /// Releases unmanaged resources and performs other cleanup operations before the
  581. /// <see cref="Channel"/> is reclaimed by garbage collection.
  582. /// </summary>
  583. ~Channel()
  584. {
  585. // Do not re-create Dispose clean-up code here.
  586. // Calling Dispose(false) is optimal in terms of
  587. // readability and maintainability.
  588. this.Dispose(false);
  589. }
  590. #endregion
  591. }
  592. }