|
1 | 1 | using System;
|
2 | 2 | using System.Net.Sockets;
|
3 | 3 | using System.Threading;
|
| 4 | +#if FEATURE_TAP |
| 5 | +using System.Threading.Tasks; |
| 6 | +#endif |
4 | 7 | using Renci.SshNet.Abstractions;
|
5 | 8 | using Renci.SshNet.Common;
|
6 | 9 | using Renci.SshNet.Messages.Transport;
|
@@ -239,6 +242,63 @@ public void Connect()
|
239 | 242 | StartKeepAliveTimer();
|
240 | 243 | }
|
241 | 244 |
|
| 245 | +#if FEATURE_TAP |
| 246 | + /// <summary> |
| 247 | + /// Asynchronously connects client to the server. |
| 248 | + /// </summary> |
| 249 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param> |
| 250 | + /// <returns>A <see cref="Task"/> that represents the asynchronous connect operation. |
| 251 | + /// </returns> |
| 252 | + /// <exception cref="InvalidOperationException">The client is already connected.</exception> |
| 253 | + /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> |
| 254 | + /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.</exception> |
| 255 | + /// <exception cref="SshConnectionException">SSH session could not be established.</exception> |
| 256 | + /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception> |
| 257 | + /// <exception cref="ProxyException">Failed to establish proxy connection.</exception> |
| 258 | + public async Task ConnectAsync(CancellationToken cancellationToken) |
| 259 | + { |
| 260 | + CheckDisposed(); |
| 261 | + cancellationToken.ThrowIfCancellationRequested(); |
| 262 | + |
| 263 | + // TODO (see issue #1758): |
| 264 | + // we're not stopping the keep-alive timer and disposing the session here |
| 265 | + // |
| 266 | + // we could do this but there would still be side effects as concrete |
| 267 | + // implementations may still hang on to the original session |
| 268 | + // |
| 269 | + // therefore it would be better to actually invoke the Disconnect method |
| 270 | + // (and then the Dispose on the session) but even that would have side effects |
| 271 | + // eg. it would remove all forwarded ports from SshClient |
| 272 | + // |
| 273 | + // I think we should modify our concrete clients to better deal with a |
| 274 | + // disconnect. In case of SshClient this would mean not removing the |
| 275 | + // forwarded ports on disconnect (but only on dispose ?) and link a |
| 276 | + // forwarded port with a client instead of with a session |
| 277 | + // |
| 278 | + // To be discussed with Oleg (or whoever is interested) |
| 279 | + if (IsSessionConnected()) |
| 280 | + throw new InvalidOperationException("The client is already connected."); |
| 281 | + |
| 282 | + OnConnecting(); |
| 283 | + |
| 284 | + Session = await CreateAndConnectSessionAsync(cancellationToken).ConfigureAwait(false); |
| 285 | + try |
| 286 | + { |
| 287 | + // Even though the method we invoke makes you believe otherwise, at this point only |
| 288 | + // the SSH session itself is connected. |
| 289 | + OnConnected(); |
| 290 | + } |
| 291 | + catch |
| 292 | + { |
| 293 | + // Only dispose the session as Disconnect() would have side-effects (such as remove forwarded |
| 294 | + // ports in SshClient). |
| 295 | + DisposeSession(); |
| 296 | + throw; |
| 297 | + } |
| 298 | + StartKeepAliveTimer(); |
| 299 | + } |
| 300 | +#endif |
| 301 | + |
242 | 302 | /// <summary>
|
243 | 303 | /// Disconnects client from the server.
|
244 | 304 | /// </summary>
|
@@ -473,6 +533,26 @@ private ISession CreateAndConnectSession()
|
473 | 533 | }
|
474 | 534 | }
|
475 | 535 |
|
| 536 | +#if FEATURE_TAP |
| 537 | + private async Task<ISession> CreateAndConnectSessionAsync(CancellationToken cancellationToken) |
| 538 | + { |
| 539 | + var session = _serviceFactory.CreateSession(ConnectionInfo, _serviceFactory.CreateSocketFactory()); |
| 540 | + session.HostKeyReceived += Session_HostKeyReceived; |
| 541 | + session.ErrorOccured += Session_ErrorOccured; |
| 542 | + |
| 543 | + try |
| 544 | + { |
| 545 | + await session.ConnectAsync(cancellationToken).ConfigureAwait(false); |
| 546 | + return session; |
| 547 | + } |
| 548 | + catch |
| 549 | + { |
| 550 | + DisposeSession(session); |
| 551 | + throw; |
| 552 | + } |
| 553 | + } |
| 554 | +#endif |
| 555 | + |
476 | 556 | private void DisposeSession(ISession session)
|
477 | 557 | {
|
478 | 558 | session.ErrorOccured -= Session_ErrorOccured;
|
|
0 commit comments