| | | 1 | | using System; |
| | | 2 | | using System.Net.Sockets; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | |
| | | 6 | | using Renci.SshNet.Abstractions; |
| | | 7 | | using Renci.SshNet.Common; |
| | | 8 | | using Renci.SshNet.Messages.Transport; |
| | | 9 | | |
| | | 10 | | namespace Renci.SshNet |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Serves as base class for client implementations, provides common client functionality. |
| | | 14 | | /// </summary> |
| | | 15 | | public abstract class BaseClient : IBaseClient |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Holds value indicating whether the connection info is owned by this client. |
| | | 19 | | /// </summary> |
| | | 20 | | private readonly bool _ownsConnectionInfo; |
| | | 21 | | |
| | | 22 | | private readonly IServiceFactory _serviceFactory; |
| | 1556 | 23 | | private readonly object _keepAliveLock = new object(); |
| | | 24 | | private TimeSpan _keepAliveInterval; |
| | | 25 | | private Timer _keepAliveTimer; |
| | | 26 | | private ConnectionInfo _connectionInfo; |
| | | 27 | | private bool _isDisposed; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the current session. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <value> |
| | | 33 | | /// The current session. |
| | | 34 | | /// </value> |
| | 11872 | 35 | | internal ISession Session { get; private set; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the factory for creating new services. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <value> |
| | | 41 | | /// The factory for creating new services. |
| | | 42 | | /// </value> |
| | | 43 | | internal IServiceFactory ServiceFactory |
| | | 44 | | { |
| | 6285 | 45 | | get { return _serviceFactory; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the connection info. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <value> |
| | | 52 | | /// The connection info. |
| | | 53 | | /// </value> |
| | | 54 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 55 | | public ConnectionInfo ConnectionInfo |
| | | 56 | | { |
| | | 57 | | get |
| | 4452 | 58 | | { |
| | 4452 | 59 | | CheckDisposed(); |
| | 4452 | 60 | | return _connectionInfo; |
| | 4452 | 61 | | } |
| | | 62 | | private set |
| | 1553 | 63 | | { |
| | 1553 | 64 | | _connectionInfo = value; |
| | 1553 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets a value indicating whether this client is connected to the server. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <value> |
| | | 72 | | /// <see langword="true"/> if this client is connected; otherwise, <see langword="false"/>. |
| | | 73 | | /// </value> |
| | | 74 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 75 | | public bool IsConnected |
| | | 76 | | { |
| | | 77 | | get |
| | 27 | 78 | | { |
| | 27 | 79 | | CheckDisposed(); |
| | | 80 | | |
| | 24 | 81 | | return IsSessionConnected(); |
| | 24 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets or sets the keep-alive interval. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <value> |
| | | 89 | | /// The keep-alive interval. Specify negative one (-1) milliseconds to disable the |
| | | 90 | | /// keep-alive. This is the default value. |
| | | 91 | | /// </value> |
| | | 92 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 93 | | public TimeSpan KeepAliveInterval |
| | | 94 | | { |
| | | 95 | | get |
| | 27 | 96 | | { |
| | 27 | 97 | | CheckDisposed(); |
| | 27 | 98 | | return _keepAliveInterval; |
| | 27 | 99 | | } |
| | | 100 | | set |
| | 86 | 101 | | { |
| | 86 | 102 | | CheckDisposed(); |
| | | 103 | | |
| | 86 | 104 | | if (value == _keepAliveInterval) |
| | 0 | 105 | | { |
| | 0 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | 86 | 109 | | if (value == SshNet.Session.InfiniteTimeSpan) |
| | 21 | 110 | | { |
| | | 111 | | // stop the timer when the value is -1 milliseconds |
| | 21 | 112 | | StopKeepAliveTimer(); |
| | 21 | 113 | | } |
| | | 114 | | else |
| | 65 | 115 | | { |
| | 65 | 116 | | if (_keepAliveTimer != null) |
| | 0 | 117 | | { |
| | | 118 | | // change the due time and interval of the timer if has already |
| | | 119 | | // been created (which means the client is connected) |
| | 0 | 120 | | _ = _keepAliveTimer.Change(value, value); |
| | 0 | 121 | | } |
| | 65 | 122 | | else if (IsSessionConnected()) |
| | 36 | 123 | | { |
| | | 124 | | // if timer has not yet been created and the client is already connected, |
| | | 125 | | // then we need to create the timer now |
| | | 126 | | // |
| | | 127 | | // this means that - before connecting - the keep-alive interval was set to |
| | | 128 | | // negative one (-1) and as such we did not create the timer |
| | 36 | 129 | | _keepAliveTimer = CreateKeepAliveTimer(value, value); |
| | 36 | 130 | | } |
| | | 131 | | |
| | | 132 | | // note that if the client is not yet connected, then the timer will be created with the |
| | | 133 | | // new interval when Connect() is invoked |
| | 65 | 134 | | } |
| | | 135 | | |
| | 86 | 136 | | _keepAliveInterval = value; |
| | 86 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Occurs when an error occurred. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <example> |
| | | 144 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect Err |
| | | 145 | | /// </example> |
| | | 146 | | public event EventHandler<ExceptionEventArgs> ErrorOccurred; |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Occurs when host key received. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <example> |
| | | 152 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect Hos |
| | | 153 | | /// </example> |
| | | 154 | | public event EventHandler<HostKeyEventArgs> HostKeyReceived; |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Occurs when server identification received. |
| | | 158 | | /// </summary> |
| | | 159 | | public event EventHandler<SshIdentificationEventArgs> ServerIdentificationReceived; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Initializes a new instance of the <see cref="BaseClient"/> class. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="connectionInfo">The connection info.</param> |
| | | 165 | | /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param> |
| | | 166 | | /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except |
| | | 167 | | /// <remarks> |
| | | 168 | | /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the |
| | | 169 | | /// connection info will be disposed when this instance is disposed. |
| | | 170 | | /// </remarks> |
| | | 171 | | protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo) |
| | 0 | 172 | | : this(connectionInfo, ownsConnectionInfo, new ServiceFactory()) |
| | 0 | 173 | | { |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Initializes a new instance of the <see cref="BaseClient"/> class. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="connectionInfo">The connection info.</param> |
| | | 180 | | /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param> |
| | | 181 | | /// <param name="serviceFactory">The factory to use for creating new services.</param> |
| | | 182 | | /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except |
| | | 183 | | /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</except |
| | | 184 | | /// <remarks> |
| | | 185 | | /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the |
| | | 186 | | /// connection info will be disposed when this instance is disposed. |
| | | 187 | | /// </remarks> |
| | 1556 | 188 | | private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFact |
| | 1556 | 189 | | { |
| | 1556 | 190 | | if (connectionInfo is null) |
| | 3 | 191 | | { |
| | 3 | 192 | | throw new ArgumentNullException(nameof(connectionInfo)); |
| | | 193 | | } |
| | | 194 | | |
| | 1553 | 195 | | if (serviceFactory is null) |
| | 0 | 196 | | { |
| | 0 | 197 | | throw new ArgumentNullException(nameof(serviceFactory)); |
| | | 198 | | } |
| | | 199 | | |
| | 1553 | 200 | | ConnectionInfo = connectionInfo; |
| | 1553 | 201 | | _ownsConnectionInfo = ownsConnectionInfo; |
| | 1553 | 202 | | _serviceFactory = serviceFactory; |
| | 1553 | 203 | | _keepAliveInterval = SshNet.Session.InfiniteTimeSpan; |
| | 1553 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Connects client to the server. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <exception cref="InvalidOperationException">The client is already connected.</exception> |
| | | 210 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 211 | | /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be establish |
| | | 212 | | /// <exception cref="SshConnectionException">SSH session could not be established.</exception> |
| | | 213 | | /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception> |
| | | 214 | | /// <exception cref="ProxyException">Failed to establish proxy connection.</exception> |
| | | 215 | | public void Connect() |
| | 1719 | 216 | | { |
| | 1719 | 217 | | CheckDisposed(); |
| | | 218 | | |
| | | 219 | | // TODO (see issue #1758): |
| | | 220 | | // we're not stopping the keep-alive timer and disposing the session here |
| | | 221 | | // |
| | | 222 | | // we could do this but there would still be side effects as concrete |
| | | 223 | | // implementations may still hang on to the original session |
| | | 224 | | // |
| | | 225 | | // therefore it would be better to actually invoke the Disconnect method |
| | | 226 | | // (and then the Dispose on the session) but even that would have side effects |
| | | 227 | | // eg. it would remove all forwarded ports from SshClient |
| | | 228 | | // |
| | | 229 | | // I think we should modify our concrete clients to better deal with a |
| | | 230 | | // disconnect. In case of SshClient this would mean not removing the |
| | | 231 | | // forwarded ports on disconnect (but only on dispose ?) and link a |
| | | 232 | | // forwarded port with a client instead of with a session |
| | | 233 | | // |
| | | 234 | | // To be discussed with Oleg (or whoever is interested) |
| | 1719 | 235 | | if (IsSessionConnected()) |
| | 0 | 236 | | { |
| | 0 | 237 | | throw new InvalidOperationException("The client is already connected."); |
| | | 238 | | } |
| | | 239 | | |
| | 1719 | 240 | | OnConnecting(); |
| | | 241 | | |
| | 1719 | 242 | | Session = CreateAndConnectSession(); |
| | | 243 | | |
| | | 244 | | try |
| | 1708 | 245 | | { |
| | | 246 | | // Even though the method we invoke makes you believe otherwise, at this point only |
| | | 247 | | // the SSH session itself is connected. |
| | 1708 | 248 | | OnConnected(); |
| | 1650 | 249 | | } |
| | 58 | 250 | | catch |
| | 58 | 251 | | { |
| | | 252 | | // Only dispose the session as Disconnect() would have side-effects (such as remove forwarded |
| | | 253 | | // ports in SshClient). |
| | 58 | 254 | | DisposeSession(); |
| | 58 | 255 | | throw; |
| | | 256 | | } |
| | | 257 | | |
| | 1650 | 258 | | StartKeepAliveTimer(); |
| | 1650 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Asynchronously connects client to the server. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| | | 265 | | /// <returns>A <see cref="Task"/> that represents the asynchronous connect operation. |
| | | 266 | | /// </returns> |
| | | 267 | | /// <exception cref="InvalidOperationException">The client is already connected.</exception> |
| | | 268 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 269 | | /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be establish |
| | | 270 | | /// <exception cref="SshConnectionException">SSH session could not be established.</exception> |
| | | 271 | | /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception> |
| | | 272 | | /// <exception cref="ProxyException">Failed to establish proxy connection.</exception> |
| | | 273 | | public async Task ConnectAsync(CancellationToken cancellationToken) |
| | 8 | 274 | | { |
| | 8 | 275 | | CheckDisposed(); |
| | 8 | 276 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 277 | | |
| | | 278 | | // TODO (see issue #1758): |
| | | 279 | | // we're not stopping the keep-alive timer and disposing the session here |
| | | 280 | | // |
| | | 281 | | // we could do this but there would still be side effects as concrete |
| | | 282 | | // implementations may still hang on to the original session |
| | | 283 | | // |
| | | 284 | | // therefore it would be better to actually invoke the Disconnect method |
| | | 285 | | // (and then the Dispose on the session) but even that would have side effects |
| | | 286 | | // eg. it would remove all forwarded ports from SshClient |
| | | 287 | | // |
| | | 288 | | // I think we should modify our concrete clients to better deal with a |
| | | 289 | | // disconnect. In case of SshClient this would mean not removing the |
| | | 290 | | // forwarded ports on disconnect (but only on dispose ?) and link a |
| | | 291 | | // forwarded port with a client instead of with a session |
| | | 292 | | // |
| | | 293 | | // To be discussed with Oleg (or whoever is interested) |
| | 8 | 294 | | if (IsSessionConnected()) |
| | 0 | 295 | | { |
| | 0 | 296 | | throw new InvalidOperationException("The client is already connected."); |
| | | 297 | | } |
| | | 298 | | |
| | 8 | 299 | | OnConnecting(); |
| | | 300 | | |
| | 8 | 301 | | Session = await CreateAndConnectSessionAsync(cancellationToken).ConfigureAwait(false); |
| | | 302 | | |
| | | 303 | | try |
| | 2 | 304 | | { |
| | | 305 | | // Even though the method we invoke makes you believe otherwise, at this point only |
| | | 306 | | // the SSH session itself is connected. |
| | 2 | 307 | | OnConnected(); |
| | 2 | 308 | | } |
| | 0 | 309 | | catch |
| | 0 | 310 | | { |
| | | 311 | | // Only dispose the session as Disconnect() would have side-effects (such as remove forwarded |
| | | 312 | | // ports in SshClient). |
| | 0 | 313 | | DisposeSession(); |
| | 0 | 314 | | throw; |
| | | 315 | | } |
| | | 316 | | |
| | 2 | 317 | | StartKeepAliveTimer(); |
| | 2 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Disconnects client from the server. |
| | | 322 | | /// </summary> |
| | | 323 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 324 | | public void Disconnect() |
| | 1835 | 325 | | { |
| | 1835 | 326 | | DiagnosticAbstraction.Log("Disconnecting client."); |
| | | 327 | | |
| | 1835 | 328 | | CheckDisposed(); |
| | | 329 | | |
| | 1835 | 330 | | OnDisconnecting(); |
| | | 331 | | |
| | | 332 | | // stop sending keep-alive messages before we close the session |
| | 1835 | 333 | | StopKeepAliveTimer(); |
| | | 334 | | |
| | | 335 | | // dispose the SSH session |
| | 1835 | 336 | | DisposeSession(); |
| | | 337 | | |
| | 1835 | 338 | | OnDisconnected(); |
| | 1835 | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Sends a keep-alive message to the server. |
| | | 343 | | /// </summary> |
| | | 344 | | /// <remarks> |
| | | 345 | | /// Use <see cref="KeepAliveInterval"/> to configure the client to send a keep-alive at regular |
| | | 346 | | /// intervals. |
| | | 347 | | /// </remarks> |
| | | 348 | | /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| | | 349 | | #pragma warning disable S1133 // Deprecated code should be removed |
| | | 350 | | [Obsolete("Use KeepAliveInterval to send a keep-alive message at regular intervals.")] |
| | | 351 | | #pragma warning restore S1133 // Deprecated code should be removed |
| | | 352 | | public void SendKeepAlive() |
| | 0 | 353 | | { |
| | 0 | 354 | | CheckDisposed(); |
| | | 355 | | |
| | 0 | 356 | | SendKeepAliveMessage(); |
| | 0 | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Called when client is connecting to the server. |
| | | 361 | | /// </summary> |
| | | 362 | | protected virtual void OnConnecting() |
| | 1727 | 363 | | { |
| | 1727 | 364 | | } |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Called when client is connected to the server. |
| | | 368 | | /// </summary> |
| | | 369 | | protected virtual void OnConnected() |
| | 1710 | 370 | | { |
| | 1710 | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Called when client is disconnecting from the server. |
| | | 375 | | /// </summary> |
| | | 376 | | protected virtual void OnDisconnecting() |
| | 1835 | 377 | | { |
| | 1835 | 378 | | Session?.OnDisconnecting(); |
| | 1835 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Called when client is disconnected from the server. |
| | | 383 | | /// </summary> |
| | | 384 | | protected virtual void OnDisconnected() |
| | 1835 | 385 | | { |
| | 1835 | 386 | | } |
| | | 387 | | |
| | | 388 | | private void Session_ErrorOccured(object sender, ExceptionEventArgs e) |
| | 7 | 389 | | { |
| | 7 | 390 | | ErrorOccurred?.Invoke(this, e); |
| | 7 | 391 | | } |
| | | 392 | | |
| | | 393 | | private void Session_HostKeyReceived(object sender, HostKeyEventArgs e) |
| | 1199 | 394 | | { |
| | 1199 | 395 | | HostKeyReceived?.Invoke(this, e); |
| | 1199 | 396 | | } |
| | | 397 | | |
| | | 398 | | private void Session_ServerIdentificationReceived(object sender, SshIdentificationEventArgs e) |
| | 1199 | 399 | | { |
| | 1199 | 400 | | ServerIdentificationReceived?.Invoke(this, e); |
| | 1199 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 405 | | /// </summary> |
| | | 406 | | public void Dispose() |
| | 1377 | 407 | | { |
| | 1377 | 408 | | Dispose(disposing: true); |
| | 1377 | 409 | | GC.SuppressFinalize(this); |
| | 1377 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 414 | | /// </summary> |
| | | 415 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 416 | | protected virtual void Dispose(bool disposing) |
| | 1623 | 417 | | { |
| | 1623 | 418 | | if (_isDisposed) |
| | 67 | 419 | | { |
| | 67 | 420 | | return; |
| | | 421 | | } |
| | | 422 | | |
| | 1556 | 423 | | if (disposing) |
| | 1310 | 424 | | { |
| | 1310 | 425 | | DiagnosticAbstraction.Log("Disposing client."); |
| | | 426 | | |
| | 1310 | 427 | | Disconnect(); |
| | | 428 | | |
| | 1310 | 429 | | if (_ownsConnectionInfo && _connectionInfo is not null) |
| | 174 | 430 | | { |
| | 174 | 431 | | if (_connectionInfo is IDisposable connectionInfoDisposable) |
| | 174 | 432 | | { |
| | 174 | 433 | | connectionInfoDisposable.Dispose(); |
| | 174 | 434 | | } |
| | | 435 | | |
| | 174 | 436 | | _connectionInfo = null; |
| | 174 | 437 | | } |
| | | 438 | | |
| | 1310 | 439 | | _isDisposed = true; |
| | 1310 | 440 | | } |
| | 1623 | 441 | | } |
| | | 442 | | |
| | | 443 | | /// <summary> |
| | | 444 | | /// Check if the current instance is disposed. |
| | | 445 | | /// </summary> |
| | | 446 | | /// <exception cref="ObjectDisposedException">THe current instance is disposed.</exception> |
| | | 447 | | protected void CheckDisposed() |
| | 19631 | 448 | | { |
| | | 449 | | #if NET7_0_OR_GREATER |
| | 18966 | 450 | | ObjectDisposedException.ThrowIf(_isDisposed, this); |
| | | 451 | | #else |
| | 665 | 452 | | if (_isDisposed) |
| | 3 | 453 | | { |
| | 3 | 454 | | throw new ObjectDisposedException(GetType().FullName); |
| | | 455 | | } |
| | | 456 | | #endif // NET7_0_OR_GREATER |
| | 19622 | 457 | | } |
| | | 458 | | |
| | | 459 | | /// <summary> |
| | | 460 | | /// Finalizes an instance of the <see cref="BaseClient"/> class. |
| | | 461 | | /// </summary> |
| | | 462 | | ~BaseClient() |
| | 492 | 463 | | { |
| | 246 | 464 | | Dispose(disposing: false); |
| | 492 | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Stops the keep-alive timer, and waits until all timer callbacks have been |
| | | 469 | | /// executed. |
| | | 470 | | /// </summary> |
| | | 471 | | private void StopKeepAliveTimer() |
| | 1856 | 472 | | { |
| | 1856 | 473 | | if (_keepAliveTimer is null) |
| | 1812 | 474 | | { |
| | 1812 | 475 | | return; |
| | | 476 | | } |
| | | 477 | | |
| | 44 | 478 | | _keepAliveTimer.Dispose(); |
| | 44 | 479 | | _keepAliveTimer = null; |
| | 1856 | 480 | | } |
| | | 481 | | |
| | | 482 | | private void SendKeepAliveMessage() |
| | 88 | 483 | | { |
| | 88 | 484 | | var session = Session; |
| | | 485 | | |
| | | 486 | | // do nothing if we have disposed or disconnected |
| | 88 | 487 | | if (session is null) |
| | 0 | 488 | | { |
| | 0 | 489 | | return; |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | // do not send multiple keep-alive messages concurrently |
| | 88 | 493 | | if (Monitor.TryEnter(_keepAliveLock)) |
| | 88 | 494 | | { |
| | | 495 | | try |
| | 88 | 496 | | { |
| | 88 | 497 | | _ = session.TrySendMessage(new IgnoreMessage()); |
| | 88 | 498 | | } |
| | | 499 | | finally |
| | 88 | 500 | | { |
| | 88 | 501 | | Monitor.Exit(_keepAliveLock); |
| | 88 | 502 | | } |
| | 88 | 503 | | } |
| | 88 | 504 | | } |
| | | 505 | | |
| | | 506 | | /// <summary> |
| | | 507 | | /// Starts the keep-alive timer. |
| | | 508 | | /// </summary> |
| | | 509 | | /// <remarks> |
| | | 510 | | /// When <see cref="KeepAliveInterval"/> is negative one (-1) milliseconds, then |
| | | 511 | | /// the timer will not be started. |
| | | 512 | | /// </remarks> |
| | | 513 | | private void StartKeepAliveTimer() |
| | 1652 | 514 | | { |
| | 1652 | 515 | | if (_keepAliveInterval == SshNet.Session.InfiniteTimeSpan) |
| | 1644 | 516 | | { |
| | 1644 | 517 | | return; |
| | | 518 | | } |
| | | 519 | | |
| | 8 | 520 | | if (_keepAliveTimer != null) |
| | 0 | 521 | | { |
| | | 522 | | // timer is already started |
| | 0 | 523 | | return; |
| | | 524 | | } |
| | | 525 | | |
| | 8 | 526 | | _keepAliveTimer = CreateKeepAliveTimer(_keepAliveInterval, _keepAliveInterval); |
| | 1652 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Creates a <see cref="Timer"/> with the specified due time and interval. |
| | | 531 | | /// </summary> |
| | | 532 | | /// <param name="dueTime">The amount of time to delay before the keep-alive message is first sent. Specify negat |
| | | 533 | | /// <param name="period">The time interval between attempts to send a keep-alive message. Specify negative one ( |
| | | 534 | | /// <returns> |
| | | 535 | | /// A <see cref="Timer"/> with the specified due time and interval. |
| | | 536 | | /// </returns> |
| | | 537 | | private Timer CreateKeepAliveTimer(TimeSpan dueTime, TimeSpan period) |
| | 44 | 538 | | { |
| | 132 | 539 | | return new Timer(state => SendKeepAliveMessage(), Session, dueTime, period); |
| | 44 | 540 | | } |
| | | 541 | | |
| | | 542 | | private ISession CreateAndConnectSession() |
| | 1719 | 543 | | { |
| | 1719 | 544 | | var session = _serviceFactory.CreateSession(ConnectionInfo, _serviceFactory.CreateSocketFactory()); |
| | 1719 | 545 | | session.ServerIdentificationReceived += Session_ServerIdentificationReceived; |
| | 1719 | 546 | | session.HostKeyReceived += Session_HostKeyReceived; |
| | 1719 | 547 | | session.ErrorOccured += Session_ErrorOccured; |
| | | 548 | | |
| | | 549 | | try |
| | 1719 | 550 | | { |
| | 1719 | 551 | | session.Connect(); |
| | 1708 | 552 | | return session; |
| | | 553 | | } |
| | 11 | 554 | | catch |
| | 11 | 555 | | { |
| | 11 | 556 | | DisposeSession(session); |
| | 11 | 557 | | throw; |
| | | 558 | | } |
| | 1708 | 559 | | } |
| | | 560 | | |
| | | 561 | | private async Task<ISession> CreateAndConnectSessionAsync(CancellationToken cancellationToken) |
| | 8 | 562 | | { |
| | 8 | 563 | | var session = _serviceFactory.CreateSession(ConnectionInfo, _serviceFactory.CreateSocketFactory()); |
| | 8 | 564 | | session.ServerIdentificationReceived += Session_ServerIdentificationReceived; |
| | 8 | 565 | | session.HostKeyReceived += Session_HostKeyReceived; |
| | 8 | 566 | | session.ErrorOccured += Session_ErrorOccured; |
| | | 567 | | |
| | | 568 | | try |
| | 8 | 569 | | { |
| | 8 | 570 | | await session.ConnectAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 571 | | return session; |
| | | 572 | | } |
| | 6 | 573 | | catch |
| | 6 | 574 | | { |
| | 6 | 575 | | DisposeSession(session); |
| | 6 | 576 | | throw; |
| | | 577 | | } |
| | 2 | 578 | | } |
| | | 579 | | |
| | | 580 | | private void DisposeSession(ISession session) |
| | 1595 | 581 | | { |
| | 1595 | 582 | | session.ErrorOccured -= Session_ErrorOccured; |
| | 1595 | 583 | | session.HostKeyReceived -= Session_HostKeyReceived; |
| | 1595 | 584 | | session.ServerIdentificationReceived -= Session_ServerIdentificationReceived; |
| | 1595 | 585 | | session.Dispose(); |
| | 1595 | 586 | | } |
| | | 587 | | |
| | | 588 | | /// <summary> |
| | | 589 | | /// Disposes the SSH session, and assigns <see langword="null"/> to <see cref="Session"/>. |
| | | 590 | | /// </summary> |
| | | 591 | | private void DisposeSession() |
| | 1893 | 592 | | { |
| | 1893 | 593 | | var session = Session; |
| | 1893 | 594 | | if (session != null) |
| | 1578 | 595 | | { |
| | 1578 | 596 | | Session = null; |
| | 1578 | 597 | | DisposeSession(session); |
| | 1578 | 598 | | } |
| | 1893 | 599 | | } |
| | | 600 | | |
| | | 601 | | /// <summary> |
| | | 602 | | /// Returns a value indicating whether the SSH session is established. |
| | | 603 | | /// </summary> |
| | | 604 | | /// <returns> |
| | | 605 | | /// <see langword="true"/> if the SSH session is established; otherwise, <see langword="false"/>. |
| | | 606 | | /// </returns> |
| | | 607 | | private bool IsSessionConnected() |
| | 1816 | 608 | | { |
| | 1816 | 609 | | var session = Session; |
| | 1816 | 610 | | return session != null && session.IsConnected; |
| | 1816 | 611 | | } |
| | | 612 | | } |
| | | 613 | | } |