Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add main logic for Client Invocation #1687

Merged
merged 42 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
79a0567
main logic for client invocation
xingsy97 Sep 23, 2022
f3281af
add ClientInvocation main logic
xingsy97 Sep 23, 2022
790b6c0
bug fix
xingsy97 Sep 23, 2022
f594136
bug fix
xingsy97 Sep 23, 2022
9dccae7
bug fix
xingsy97 Sep 23, 2022
a7c8c8a
update
xingsy97 Sep 23, 2022
f1b96c1
update
xingsy97 Sep 23, 2022
b129204
update
xingsy97 Sep 26, 2022
f4e143c
sync with PR 1684 and some updates
xingsy97 Sep 27, 2022
ee60530
minor fix
xingsy97 Sep 27, 2022
d2a3178
update about nameProvider
xingsy97 Sep 27, 2022
8299c2d
Merge branch 'dev' into ci-main
xingsy97 Sep 28, 2022
d18743a
tmp
xingsy97 Sep 28, 2022
ecf28b4
update
xingsy97 Oct 10, 2022
789427d
Merge branch 'dev' into ci-main
xingsy97 Oct 10, 2022
a64dec1
solve conflict
xingsy97 Oct 14, 2022
9a57478
Merge branch 'dev' into ci-main
xingsy97 Oct 14, 2022
c8198d5
update
xingsy97 Oct 14, 2022
1ad6573
Merge branch 'dev' into ci-main
xingsy97 Oct 14, 2022
9265187
update
xingsy97 Oct 14, 2022
b93a8c1
minior fix
xingsy97 Oct 14, 2022
ec252ad
new-line character fix
xingsy97 Oct 14, 2022
886b985
minor fix
xingsy97 Oct 14, 2022
0c40d03
update
xingsy97 Oct 17, 2022
c337cd3
add new UTs and move existing UTs
xingsy97 Oct 20, 2022
3ba27a2
add & update UTs
xingsy97 Oct 20, 2022
4b1dc1e
Merge branch 'dev' into ci-main
xingsy97 Oct 20, 2022
fcb849e
migrate to ServiceConnection and ServiceLifetimeManager from their ba…
xingsy97 Oct 21, 2022
995cadb
Merge branch 'ci-main' of https://github.com/xingsy97/azure-signalr i…
xingsy97 Oct 21, 2022
d0a36d0
minor bug fix
xingsy97 Oct 21, 2022
fb5a38e
format clean
xingsy97 Oct 21, 2022
2d98f5a
bug fix for PingMessage
xingsy97 Oct 21, 2022
2c58f3b
bug fix & add log
xingsy97 Oct 21, 2022
e6dcc32
code clean
xingsy97 Oct 21, 2022
2168535
add RemoveInvocation for caller, improve exception handling
xingsy97 Oct 24, 2022
8a5356f
remove instanceId for caller AddInvocation
xingsy97 Oct 24, 2022
85f4b9f
make RemoveInvocation return void
xingsy97 Oct 24, 2022
67ff88c
update
xingsy97 Oct 24, 2022
51b1271
Merge branch 'dev' into ci-main
xingsy97 Oct 24, 2022
0e6631f
update
xingsy97 Oct 24, 2022
a80e79f
Merge branch 'ci-main' of https://github.com/xingsy97/azure-signalr i…
xingsy97 Oct 24, 2022
a1a38e2
minor fix
xingsy97 Oct 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<MicrosoftAspNetCoreHttpConnectionsClientPackage3_0Version>3.0.0</MicrosoftAspNetCoreHttpConnectionsClientPackage3_0Version>
<MicrosoftAspNetCoreHttpConnectionsClientPackage3_1Version>3.1.9</MicrosoftAspNetCoreHttpConnectionsClientPackage3_1Version>
<MicrosoftAspNetCoreHttpConnectionsClientPackage5_0Version>5.0.1</MicrosoftAspNetCoreHttpConnectionsClientPackage5_0Version>
<MicrosoftAspNetCoreHttpConnectionsClientPackage7_0Version>7.0.0-preview.7.22376.6</MicrosoftAspNetCoreHttpConnectionsClientPackage7_0Version>
<MicrosoftAspNetCoreHttpConnectionsCommonPackageVersion>1.0.4</MicrosoftAspNetCoreHttpConnectionsCommonPackageVersion>
<MicrosoftAspNetCoreHttpConnectionsCommonPackage3_0Version>3.0.0</MicrosoftAspNetCoreHttpConnectionsCommonPackage3_0Version>
<MicrosoftAspNetCoreHttpConnectionsCommonPackage3_1Version>3.1.9</MicrosoftAspNetCoreHttpConnectionsCommonPackage3_1Version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public ServiceConnection(
ILoggerFactory loggerFactory,
IServiceMessageHandler serviceMessageHandler,
IServiceEventHandler serviceEventHandler,
IClientInvocationManager clientInvocationManager = null,
ServiceConnectionType connectionType = ServiceConnectionType.Default)
: base(
serviceProtocol,
Expand All @@ -51,6 +52,7 @@ public ServiceConnection(
serviceMessageHandler,
serviceEventHandler,
connectionType,
clientInvocationManager,
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
loggerFactory?.CreateLogger<ServiceConnection>())
{
_connectionFactory = connectionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public IServiceConnection Create(HubServiceEndpoint endpoint, IServiceMessageHan
_logger,
serviceMessageHandler,
_serviceEventHandler,
null,
type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Connections;
using Microsoft.Azure.SignalR.Common;
using Microsoft.Azure.SignalR.Protocol;
Expand Down Expand Up @@ -74,6 +73,8 @@ internal abstract class ServiceConnectionBase : IServiceConnection

protected IServiceProtocol ServiceProtocol { get; }

protected IClientInvocationManager ClientInvocationManager { get; }

private ConnectionContext _connectionContext;

public event Action<StatusChange> ConnectionStatusChanged;
Expand Down Expand Up @@ -111,6 +112,7 @@ protected ServiceConnectionBase(
IServiceMessageHandler serviceMessageHandler,
IServiceEventHandler serviceEventHandler,
ServiceConnectionType connectionType,
IClientInvocationManager clientInvocationManager,
ILogger logger,
GracefulShutdownMode mode = GracefulShutdownMode.Off)
{
Expand All @@ -132,6 +134,7 @@ protected ServiceConnectionBase(
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
_serviceMessageHandler = serviceMessageHandler;
_serviceEventHandler = serviceEventHandler;
ClientInvocationManager = clientInvocationManager ?? throw new ArgumentNullException(nameof(clientInvocationManager));
}

/// <summary>
Expand Down Expand Up @@ -300,6 +303,9 @@ protected Task OnPingMessageAsync(PingMessage pingMessage)
if (RuntimeServicePingMessage.TryGetOffline(pingMessage, out var instanceId))
{
Log.ReceivedInstanceOfflinePing(Logger, instanceId);
#if NET7_0_OR_GREATER
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
ClientInvocationManager.Caller.CleanupInvocationsByInstance(instanceId);
#endif
return CleanupClientConnections(instanceId);
}
if (RuntimeServicePingMessage.IsFinAck(pingMessage))
Expand All @@ -310,6 +316,30 @@ protected Task OnPingMessageAsync(PingMessage pingMessage)
return _serviceMessageHandler.HandlePingAsync(pingMessage);
}

private Task OnClientInvocationAsync(ClientInvocationMessage message)
{
ClientInvocationManager.Router.AddInvocation(message.ConnectionId, message.InvocationId, message.CallerServerId, default);
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
return Task.CompletedTask;
}

private Task OnServiceMappingAsync(ServiceMappingMessage message)
{
ClientInvocationManager.Caller.AddServiceMapping(message);
return Task.CompletedTask;
}

private Task OnClientCompletionAsync(ClientCompletionMessage clientCompletionMessage)
{
ClientInvocationManager.Caller.TryCompleteResult(clientCompletionMessage.ConnectionId, clientCompletionMessage);
return Task.CompletedTask;
}

private Task OnErrorCompletionAsync(ErrorCompletionMessage errorCompletionMessage)
{
ClientInvocationManager.Caller.TryCompleteResult(errorCompletionMessage.ConnectionId, errorCompletionMessage);
return Task.CompletedTask;
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
}

protected Task OnAckMessageAsync(AckMessage ackMessage)
{
_serviceMessageHandler.HandleAck(ackMessage);
Expand Down Expand Up @@ -562,6 +592,10 @@ private Task DispatchMessageAsync(ServiceMessage message)
AckMessage ackMessage => OnAckMessageAsync(ackMessage),
ServiceEventMessage eventMessage => OnEventMessageAsync(eventMessage),
AccessKeyResponseMessage keyMessage => OnAccessKeyMessageAsync(keyMessage),
ClientInvocationMessage clientInvocationMessage => OnClientInvocationAsync(clientInvocationMessage),
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
ServiceMappingMessage serviceMappingMessage => OnServiceMappingAsync(serviceMappingMessage),
ClientCompletionMessage clientCompletionMessage => OnClientCompletionAsync(clientCompletionMessage),
ErrorCompletionMessage errorCompletionMessage => OnErrorCompletionAsync(errorCompletionMessage),
_ => Task.CompletedTask,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static IServiceCollection AddSignalRServiceCore(this IServiceCollection
// Remove the JsonHubProtocol and add new one.
// On .NET Standard 2.0, registering multiple hub protocols with the same name is forbidden.
services.Replace(ServiceDescriptor.Singleton<IHubProtocol>(sp =>
{
{
xingsy97 marked this conversation as resolved.
Show resolved Hide resolved
var objectSerializer = sp.GetRequiredService<IOptions<ServiceManagerOptions>>().Value.ObjectSerializer;
return objectSerializer != null ? new JsonObjectSerializerHubProtocol(objectSerializer) : new JsonHubProtocol();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class WebSocketsHubLifetimeManager<THub> : ServiceLifetimeManagerBase<T
private readonly IOptions<ServiceManagerOptions> _serviceManagerOptions;

public WebSocketsHubLifetimeManager(IServiceConnectionManager<THub> serviceConnectionManager, IHubProtocolResolver protocolResolver,
IOptions<HubOptions> globalHubOptions, IOptions<HubOptions<THub>> hubOptions, ILoggerFactory loggerFactory, IOptions<ServiceManagerOptions> serviceManagerOptions) :
base(serviceConnectionManager, protocolResolver, globalHubOptions, hubOptions, loggerFactory?.CreateLogger(nameof(WebSocketsHubLifetimeManager<Hub>)))
IOptions<HubOptions> globalHubOptions, IOptions<HubOptions<THub>> hubOptions, IClientInvocationManager clientInvocationManager, IClientConnectionManager clientConnectionManager, ILoggerFactory loggerFactory, IOptions<ServiceManagerOptions> serviceManagerOptions) :
base(serviceConnectionManager, protocolResolver, globalHubOptions, hubOptions, new DefaultServerNameProvider(), clientInvocationManager, clientConnectionManager, loggerFactory?.CreateLogger(nameof(WebSocketsHubLifetimeManager<Hub>)))
{
_serviceManagerOptions = serviceManagerOptions ?? throw new ArgumentNullException(nameof(serviceManagerOptions));
}
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Azure.SignalR/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ private static ISignalRServerBuilder AddAzureSignalRCore(this ISignalRServerBuil
.AddSingleton<IClientConnectionFactory, ClientConnectionFactory>()
.AddSingleton<IHostedService, HeartBeat>()
.AddSingleton<IAccessKeySynchronizer, AccessKeySynchronizer>()
#if NET7_0_OR_GREATER
.AddSingleton<IClientInvocationManager, ClientInvocationManager>()
#else
.AddSingleton<IClientInvocationManager, DummyClientInvocationManager>()
#endif
.AddSingleton(typeof(NegotiateHandler<>));

// If a custom router is added, do not add the default router
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class ServiceHubDispatcher<THub> where THub : Hub
private readonly IEndpointRouter _router;
private readonly string _hubName;
private readonly IServiceEventHandler _serviceEventHandler;
private readonly IClientInvocationManager _clientInvocationManager;

protected readonly IServerNameProvider _nameProvider;

Expand All @@ -45,6 +46,7 @@ public ServiceHubDispatcher(
IServerNameProvider nameProvider,
ServerLifetimeManager serverLifetimeManager,
IClientConnectionFactory clientConnectionFactory,
IClientInvocationManager clientInvocationManager,
IServiceEventHandler serviceEventHandler)
{
_serviceProtocol = serviceProtocol;
Expand All @@ -62,6 +64,7 @@ public ServiceHubDispatcher(
_nameProvider = nameProvider;
_hubName = typeof(THub).Name;
_serviceEventHandler = serviceEventHandler;
_clientInvocationManager = clientInvocationManager;

serverLifetimeManager?.Register(ShutdownAsync);
}
Expand Down Expand Up @@ -150,7 +153,8 @@ internal virtual ServiceConnectionFactory GetServiceConnectionFactory(
connectionDelegate,
_clientConnectionFactory,
_nameProvider,
_serviceEventHandler)
_serviceEventHandler,
_clientInvocationManager)
{
ConfigureContext = contextConfig,
ShutdownMode = _options.GracefulShutdown.Mode
Expand Down
16 changes: 9 additions & 7 deletions src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ internal class ServiceLifetimeManager<THub> : ServiceLifetimeManagerBase<THub> w
private const string MarkerNotConfiguredError =
"'AddAzureSignalR(...)' was called without a matching call to 'IApplicationBuilder.UseAzureSignalR(...)'.";

private readonly IClientConnectionManager _clientConnectionManager;

public ServiceLifetimeManager(
IServiceConnectionManager<THub> serviceConnectionManager,
IClientConnectionManager clientConnectionManager,
Expand All @@ -29,12 +27,18 @@ public ServiceLifetimeManager(
AzureSignalRMarkerService marker,
IOptions<HubOptions> globalHubOptions,
IOptions<HubOptions<THub>> hubOptions,
IBlazorDetector blazorDetector)
IBlazorDetector blazorDetector,
IServerNameProvider nameProvider,
IClientInvocationManager clientInvocationManager)
: base(
serviceConnectionManager,
protocolResolver,
globalHubOptions,
hubOptions, logger)
hubOptions,
nameProvider,
clientInvocationManager,
clientConnectionManager,
logger)
{
// after core 3.0 UseAzureSignalR() is not required.
#if NETSTANDARD2_0
Expand All @@ -43,8 +47,6 @@ public ServiceLifetimeManager(
throw new InvalidOperationException(MarkerNotConfiguredError);
}
#endif
_clientConnectionManager = clientConnectionManager;

if (hubOptions.Value.SupportedProtocols != null && hubOptions.Value.SupportedProtocols.Any(x => x.Equals(Constants.Protocol.BlazorPack, StringComparison.OrdinalIgnoreCase)))
{
blazorDetector?.TrySetBlazor(typeof(THub).Name, true);
Expand Down Expand Up @@ -74,7 +76,7 @@ public override async Task SendConnectionAsync(string connectionId, string metho
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
}

if (_clientConnectionManager.ClientConnections.TryGetValue(connectionId, out var serviceConnectionContext))
if (ClientConnectionManager.ClientConnections.TryGetValue(connectionId, out var serviceConnectionContext))
{
var message = CreateMessage(connectionId, methodName, args, serviceConnectionContext);
var messageWithTracingId = (IMessageWithTracingId)message;
Expand Down
Loading