Channel.cs 26 KB

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