Skip to content

Commit 507a98f

Browse files
authored
add event for ssh keep alive (#544)
1 parent 06ba857 commit 507a98f

7 files changed

Lines changed: 81 additions & 35 deletions

File tree

cs/build/build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<ReportGeneratorVersion>5.3.9</ReportGeneratorVersion>
4242
<SystemTextEncodingsWebPackageVersion>4.7.2</SystemTextEncodingsWebPackageVersion>
4343
<VisualStudioValidationVersion>15.5.31</VisualStudioValidationVersion>
44-
<DevTunnelsSshPackageVersion>3.12.8</DevTunnelsSshPackageVersion>
44+
<DevTunnelsSshPackageVersion>3.12.11</DevTunnelsSshPackageVersion>
4545
<XunitRunnerVisualStudioVersion>2.4.0</XunitRunnerVisualStudioVersion>
4646
<XunitVersion>2.4.0</XunitVersion>
4747
</PropertyGroup>

cs/src/Connections/ITunnelClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ Task ConnectAsync(
202202
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
203203
/// if the keep-alive interval is greater than 0.
204204
/// </remarks>
205-
public event EventHandler<SshKeepAliveFailureEventArgs>? KeepAliveFailed;
205+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveFailed;
206+
207+
/// <summary>
208+
/// Event raised when a keep-alive message response is received.
209+
/// </summary>
210+
/// <remarks>
211+
/// The event args provide the count of keep-alive messages that got a response within the
212+
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
213+
/// if the keep-alive interval is greater than 0.
214+
/// </remarks>
215+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveSucceeded;
206216
}
207217

cs/src/Connections/ITunnelHost.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Task ConnectAsync(
127127
/// ForwardedPortConnecting event will be raised.
128128
/// </remarks>
129129
event EventHandler<ForwardedPortConnectingEventArgs>? ForwardedPortConnecting;
130-
130+
131131
/// <summary>
132132
/// Event raised when a keep-alive message response is not received.
133133
/// </summary>
@@ -136,5 +136,15 @@ Task ConnectAsync(
136136
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
137137
/// if the keep-alive interval is greater than 0.
138138
/// </remarks>
139-
public event EventHandler<SshKeepAliveFailureEventArgs>? KeepAliveFailed;
139+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveFailed;
140+
141+
/// <summary>
142+
/// Event raised when a keep-alive message response is received.
143+
/// </summary>
144+
/// <remarks>
145+
/// The event args provide the count of keep-alive messages that got a response within the
146+
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
147+
/// if the keep-alive interval is greater than 0.
148+
/// </remarks>
149+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveSucceeded;
140150
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
// <copyright file="SshKeepAliveFailureEventArgs.cs" company="Microsoft">
2-
// Copyright (c) Microsoft. All rights reserved.
3-
// Licensed under the MIT license.
4-
// </copyright>
5-
6-
using System;
7-
8-
namespace Microsoft.DevTunnels.Connections;
9-
10-
/// <summary>
11-
/// Event raised when a keep-alive message respose is not received.
12-
/// </summary>
13-
public class SshKeepAliveFailureEventArgs : EventArgs
14-
{
15-
/// <summary>
16-
/// Create a new instance of <see cref="SshKeepAliveFailureEventArgs"/>.
17-
/// </summary>
18-
public SshKeepAliveFailureEventArgs(int count)
19-
{
20-
Count = count;
21-
}
22-
23-
/// <summary>
24-
/// The number of keep-alive messages that have been sent without a response.
25-
/// </summary>
26-
public int Count { get; }
27-
}
1+
// <copyright file="SshKeepAliveFailureEventArgs.cs" company="Microsoft">
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license.
4+
// </copyright>
5+
6+
using System;
7+
8+
namespace Microsoft.DevTunnels.Connections;
9+
10+
/// <summary>
11+
/// Event raised when a keep-alive message respose is or is not received.
12+
/// </summary>
13+
public class SshKeepAliveEventArgs : EventArgs
14+
{
15+
/// <summary>
16+
/// Create a new instance of <see cref="SshKeepAliveEventArgs"/>.
17+
/// </summary>
18+
public SshKeepAliveEventArgs(int count)
19+
{
20+
Count = count;
21+
}
22+
23+
/// <summary>
24+
/// The number of keep-alive messages that have been sent with the same state.
25+
/// </summary>
26+
public int Count { get; }
27+
}

cs/src/Connections/TunnelClient.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,14 @@ protected async Task StartSshSessionAsync(Stream stream, TunnelConnectionOptions
206206

207207
session = new SshClientSession(clientConfig, Trace.WithName("SSH"));
208208
SshSession = session;
209-
session.KeepAliveRequestFailed += (_, e) =>
209+
session.KeepAliveFailed += (_, e) =>
210210
{
211211
OnKeepAliveFailed(e.Count);
212212
};
213+
session.KeepAliveSucceeded += (_, e) =>
214+
{
215+
OnKeepAliveSucceeded(e.Count);
216+
};
213217
SshPortForwardingService = session.ActivateService<PortForwardingService>();
214218
ConfigurePortForwardingService();
215219
SubscribeSessionEvents(session);

cs/src/Connections/TunnelConnection.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,17 @@ protected virtual void ValidateAccessToken()
265265
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
266266
/// if the keep-alive interval is greater than 0.
267267
/// </remarks>
268-
public event EventHandler<SshKeepAliveFailureEventArgs>? KeepAliveFailed;
268+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveFailed;
269+
270+
/// <summary>
271+
/// Event raised when a keep-alive message response is received.
272+
/// </summary>
273+
/// <remarks>
274+
/// The event args provide the count of keep-alive messages that got a response within the
275+
/// configured <see cref="TunnelConnectionOptions.KeepAliveIntervalInSeconds"/>. This callback is only invoked
276+
/// if the keep-alive interval is greater than 0.
277+
/// </remarks>
278+
public event EventHandler<SshKeepAliveEventArgs>? KeepAliveSucceeded;
269279

270280
/// <summary>
271281
/// Fetch the tunnel from the service if <see cref="ManagementClient"/> and <see cref="Tunnel"/> are not null.
@@ -360,7 +370,15 @@ protected virtual async Task<bool> OnRefreshingTunnelAccessTokenAsync(Cancellati
360370
/// </summary>
361371
protected virtual void OnKeepAliveFailed(int count)
362372
{
363-
KeepAliveFailed?.Invoke(this, new SshKeepAliveFailureEventArgs(count));
373+
KeepAliveFailed?.Invoke(this, new SshKeepAliveEventArgs(count));
374+
}
375+
376+
/// <summary>
377+
/// Event raised when a keep-alive message response is not received.
378+
/// </summary>
379+
protected virtual void OnKeepAliveSucceeded(int count)
380+
{
381+
KeepAliveSucceeded?.Invoke(this, new SshKeepAliveEventArgs(count));
364382
}
365383

366384
/// <inheritdoc />

cs/src/Connections/TunnelRelayTunnelHost.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,14 @@ protected override async Task ConfigureSessionAsync(Stream stream, bool isReconn
207207
hostPfs.MessageFactory = this;
208208
}
209209

210-
session.KeepAliveRequestFailed += (_, e) =>
210+
session.KeepAliveFailed += (_, e) =>
211211
{
212212
OnKeepAliveFailed(e.Count);
213213
};
214+
session.KeepAliveSucceeded += (_, e) =>
215+
{
216+
OnKeepAliveSucceeded(e.Count);
217+
};
214218

215219
SshSession = session;
216220
SubscribeSessionEvents(session);

0 commit comments

Comments
 (0)