| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Common; |
| | | 3 | | using Renci.SshNet.Messages.Connection; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Channels |
| | | 6 | | { |
| | | 7 | | internal abstract class ClientChannel : Channel |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="ClientChannel"/> class. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="session">The session.</param> |
| | | 13 | | /// <param name="localChannelNumber">The local channel number.</param> |
| | | 14 | | /// <param name="localWindowSize">Size of the window.</param> |
| | | 15 | | /// <param name="localPacketSize">Size of the packet.</param> |
| | | 16 | | protected ClientChannel(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize) |
| | 1977 | 17 | | : base(session, localChannelNumber, localWindowSize, localPacketSize) |
| | 1977 | 18 | | { |
| | 1977 | 19 | | session.ChannelOpenConfirmationReceived += OnChannelOpenConfirmation; |
| | 1977 | 20 | | session.ChannelOpenFailureReceived += OnChannelOpenFailure; |
| | 1977 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Occurs when <see cref="ChannelOpenConfirmationMessage"/> is received. |
| | | 25 | | /// </summary> |
| | | 26 | | public event EventHandler<ChannelOpenConfirmedEventArgs> OpenConfirmed; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Occurs when <see cref="ChannelOpenFailureMessage"/> is received. |
| | | 30 | | /// </summary> |
| | | 31 | | public event EventHandler<ChannelOpenFailedEventArgs> OpenFailed; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Called when channel is opened by the server. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="remoteChannelNumber">The remote channel number.</param> |
| | | 37 | | /// <param name="initialWindowSize">Initial size of the window.</param> |
| | | 38 | | /// <param name="maximumPacketSize">Maximum size of the packet.</param> |
| | | 39 | | protected virtual void OnOpenConfirmation(uint remoteChannelNumber, uint initialWindowSize, uint maximumPacketSi |
| | 1941 | 40 | | { |
| | 1941 | 41 | | InitializeRemoteInfo(remoteChannelNumber, initialWindowSize, maximumPacketSize); |
| | | 42 | | |
| | | 43 | | // Channel is consider to be open when confirmation message was received |
| | 1941 | 44 | | IsOpen = true; |
| | | 45 | | |
| | 1941 | 46 | | OpenConfirmed?.Invoke(this, new ChannelOpenConfirmedEventArgs(remoteChannelNumber, initialWindowSize, maximu |
| | 1941 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Send message to open a channel. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="message">Message to send.</param> |
| | | 53 | | /// <exception cref="SshConnectionException">The client is not connected.</exception> |
| | | 54 | | /// <exception cref="SshOperationTimeoutException">The operation timed out.</exception> |
| | | 55 | | /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the p |
| | | 56 | | protected void SendMessage(ChannelOpenMessage message) |
| | 2034 | 57 | | { |
| | 2034 | 58 | | Session.SendMessage(message); |
| | 2034 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Called when channel failed to open. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="reasonCode">The reason code.</param> |
| | | 65 | | /// <param name="description">The description.</param> |
| | | 66 | | /// <param name="language">The language.</param> |
| | | 67 | | protected virtual void OnOpenFailure(uint reasonCode, string description, string language) |
| | 6 | 68 | | { |
| | 6 | 69 | | OpenFailed?.Invoke(this, new ChannelOpenFailedEventArgs(LocalChannelNumber, reasonCode, description, languag |
| | 6 | 70 | | } |
| | | 71 | | |
| | | 72 | | private void OnChannelOpenConfirmation(object sender, MessageEventArgs<ChannelOpenConfirmationMessage> e) |
| | 2817 | 73 | | { |
| | 2817 | 74 | | if (e.Message.LocalChannelNumber == LocalChannelNumber) |
| | 1941 | 75 | | { |
| | | 76 | | try |
| | 1941 | 77 | | { |
| | 1941 | 78 | | OnOpenConfirmation(e.Message.RemoteChannelNumber, |
| | 1941 | 79 | | e.Message.InitialWindowSize, |
| | 1941 | 80 | | e.Message.MaximumPacketSize); |
| | 1935 | 81 | | } |
| | 6 | 82 | | catch (Exception ex) |
| | 6 | 83 | | { |
| | 6 | 84 | | OnChannelException(ex); |
| | 6 | 85 | | } |
| | 1941 | 86 | | } |
| | 2817 | 87 | | } |
| | | 88 | | |
| | | 89 | | private void OnChannelOpenFailure(object sender, MessageEventArgs<ChannelOpenFailureMessage> e) |
| | 600 | 90 | | { |
| | 600 | 91 | | if (e.Message.LocalChannelNumber == LocalChannelNumber) |
| | 90 | 92 | | { |
| | | 93 | | try |
| | 90 | 94 | | { |
| | 90 | 95 | | OnOpenFailure(e.Message.ReasonCode, e.Message.Description, e.Message.Language); |
| | 84 | 96 | | } |
| | 6 | 97 | | catch (Exception ex) |
| | 6 | 98 | | { |
| | 6 | 99 | | OnChannelException(ex); |
| | 6 | 100 | | } |
| | 90 | 101 | | } |
| | 600 | 102 | | } |
| | | 103 | | |
| | | 104 | | protected override void Dispose(bool disposing) |
| | 2011 | 105 | | { |
| | 2011 | 106 | | UnsubscribeFromSessionEvents(Session); |
| | | 107 | | |
| | 2011 | 108 | | base.Dispose(disposing); |
| | 2011 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Unsubscribes the current <see cref="ClientChannel"/> from session events. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="session">The session.</param> |
| | | 115 | | /// <remarks> |
| | | 116 | | /// Does nothing when <paramref name="session"/> is <see langword="null"/>. |
| | | 117 | | /// </remarks> |
| | | 118 | | private void UnsubscribeFromSessionEvents(ISession session) |
| | 2011 | 119 | | { |
| | 2011 | 120 | | if (session is null) |
| | 0 | 121 | | { |
| | 0 | 122 | | return; |
| | | 123 | | } |
| | | 124 | | |
| | 2011 | 125 | | session.ChannelOpenConfirmationReceived -= OnChannelOpenConfirmation; |
| | 2011 | 126 | | session.ChannelOpenFailureReceived -= OnChannelOpenFailure; |
| | 2011 | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |