Path: redb-route/redb.Route.SignalR/RedbBridgeHub.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.SignalR/RedbBridgeHub.cs
----
using System.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Telemetry;
namespace redb.Route.SignalR;
///
/// Internal bridge hub that converts SignalR invocations into redb.Route exchanges.
/// Not subclassed by users — acts as a generic bridge (like gRPC RedbServiceImpl).
///
/// Clients call Invoke("MethodName", arg1, arg2, ...) — the hub creates an exchange
/// with the method name and arguments, processes it through the pipeline, and returns
/// the Out body as the method result (when InOut is enabled).
///
///
internal sealed class RedbBridgeHub(SignalREndpoint endpoint) : Hub
{
private const string HandshakeTraceItem = "__redb_handshake_trace";
///
/// Called by SignalR clients. All client method invocations are routed here.
///
/// Hub method name the client wants to invoke.
/// Arguments passed by the client.
/// Result from the pipeline (when InOut), otherwise null.
public async Task Invoke(string method, object?[]? args)
{
var consumer = ResolveConsumer()
?? throw new HubException("SignalRConsumer not available for this hub path.");
var options = consumer.EndpointOptions;
// If a method filter is set, reject non-matching invocations
if (options.Method is not null
&& !options.Method.Equals(method, StringComparison.OrdinalIgnoreCase))
{
throw new HubException($"Method '{method}' is not supported on this hub.");
}
// Normalize args: deserialize any JsonElement values to basic .NET types
var normalizedArgs = NormalizeArgs(args);
var body = normalizedArgs switch
{
null or { Length: 0 } => (object?)null,
{ Length: 1 } => normalizedArgs[0],
_ => normalizedArgs
};
var message = new Message(body);
message.Headers[SignalRHeaders.Method] = method;
message.Headers[SignalRHeaders.ConnectionId] = Context.ConnectionId;
message.Headers[SignalRHeaders.HubPath] = consumer.HubPath;
message.Headers[SignalRHeaders.Ssl] = options.Ssl.ToString();
message.Headers[SignalRHeaders.Protocol] = options.MessagePack ? "messagepack" : "json";
if (Context.UserIdentifier is not null)
message.Headers[SignalRHeaders.UserId] = Context.UserIdentifier;
var exchange = Exchange.Create(message, consumer.ScopeFactory);
ExchangePrincipal.Set(exchange, CallerPrincipal());
exchange.Pattern = options.InOut ? ExchangePattern.InOut : ExchangePattern.InOnly;
try
{
await consumer.ProcessExchange(exchange, Context.ConnectionAborted, HandshakeTrace()).ConfigureAwait(false);
// Post-process: group management commands
await HandleGroupCommands(exchange).ConfigureAwait(false);
if (exchange.Exception is not null && !exchange.ExceptionHandled)
throw new HubException(exchange.Exception.Message);
// InOut: return Out body
if (options.InOut && exchange.HasOut && exchange.Out!.Body is not null)
return exchange.Out.Body;
return null;
}
finally
{
await exchange.DisposeAsync().ConfigureAwait(false);
}
}
///
public override async Task OnConnectedAsync()
{
var consumer = ResolveConsumer();
if (consumer is null) { await base.OnConnectedAsync(); return; }
// Admission: a connection over maxConnections is aborted here —
// no group membership, no Connected event, nothing reaches the pipeline. The marker keeps
// OnDisconnected from releasing a slot this connection never held.
if (!consumer.TryAcquireConnection())
{
Context.Abort();
return;
}
Context.Items["__redb_conn_slot"] = true;
// The connection request is the only place a client can put trace headers; every message span links it.
if (Context.GetHttpContext() is { } http)
{
var handshake = RouteTelemetryExtensions.ExtractTraceContext(http.Request.Headers, ReadHeader);
if (handshake != default)
Context.Items[HandshakeTraceItem] = handshake;
}
// Auto-add to default group
if (consumer.EndpointOptions.DefaultGroup is not null)
{
await Groups.AddToGroupAsync(Context.ConnectionId, consumer.EndpointOptions.DefaultGroup)
.ConfigureAwait(false);
}
// Fire Connected event through pipeline
var message = new Message(null);
message.Headers[SignalRHeaders.Event] = "Connected";
message.Headers[SignalRHeaders.ConnectionId] = Context.ConnectionId;
message.Headers[SignalRHeaders.HubPath] = consumer.HubPath;
if (Context.UserIdentifier is not null)
message.Headers[SignalRHeaders.UserId] = Context.UserIdentifier;
var exchange = Exchange.Create(message, consumer.ScopeFactory);
ExchangePrincipal.Set(exchange, CallerPrincipal());
try
{
await consumer.ProcessExchange(exchange, Context.ConnectionAborted, HandshakeTrace()).ConfigureAwait(false);
await HandleGroupCommands(exchange).ConfigureAwait(false);
}
finally
{
await exchange.DisposeAsync().ConfigureAwait(false);
}
await base.OnConnectedAsync();
}
///
public override async Task OnDisconnectedAsync(Exception? exception)
{
var consumer = ResolveConsumer();
if (consumer is null) { await base.OnDisconnectedAsync(exception); return; }
// Release the admission slot only if OnConnected actually claimed one (see the marker).
// A refused connection (no marker under an active limit) also gets NO Disconnected event:
// the pipeline never saw it connect, so it must not see it disconnect.
if (Context.Items.ContainsKey("__redb_conn_slot"))
consumer.ReleaseConnection();
else if (consumer.EndpointOptions.MaxConnections > 0)
{
await base.OnDisconnectedAsync(exception).ConfigureAwait(false);
return;
}
var message = new Message(null);
message.Headers[SignalRHeaders.Event] = "Disconnected";
message.Headers[SignalRHeaders.ConnectionId] = Context.ConnectionId;
message.Headers[SignalRHeaders.HubPath] = consumer.HubPath;
if (exception is not null)
message.Headers[SignalRHeaders.DisconnectError] = exception.Message;
if (Context.UserIdentifier is not null)
message.Headers[SignalRHeaders.UserId] = Context.UserIdentifier;
var exchange = Exchange.Create(message, consumer.ScopeFactory);
ExchangePrincipal.Set(exchange, CallerPrincipal());
try
{
await consumer.ProcessExchange(exchange, default, HandshakeTrace()).ConfigureAwait(false);
}
finally
{
await exchange.DisposeAsync().ConfigureAwait(false);
}
await base.OnDisconnectedAsync(exception);
}
/// The trace context of the connection request, or default when the client sent none.
private ActivityContext HandshakeTrace()
=> Context.Items.TryGetValue(HandshakeTraceItem, out var value) && value is ActivityContext handshake
? handshake
: default;
private static string? ReadHeader(IHeaderDictionary headers, string name)
=> headers.TryGetValue(name, out var values) ? values.ToString() : null;
///
/// The caller's principal as the hub's identity gate handed it to SignalR, or null for an anonymous
/// connection. SignalR always has a User ; for an anonymous connection it is an empty principal
/// (no claims, no authenticated identity), which identifies nobody and is not put on the exchange.
///
private System.Security.Claims.ClaimsPrincipal? CallerPrincipal()
{
var user = Context.User;
return user is not null && user.Identities.Any(i => i.IsAuthenticated || i.Claims.Any()) ? user : null;
}
///
/// The consumer behind this hub. Each hub has a container of its own ( )
/// holding its endpoint, so the hub knows its consumer without looking at the request path.
///
private SignalRConsumer? ResolveConsumer() => endpoint.ServingConsumer;
private async Task HandleGroupCommands(IExchange exchange)
{
var outMsg = exchange.HasOut ? exchange.Out! : exchange.In;
if (outMsg.Headers.TryGetValue(SignalRHeaders.AddToGroup, out var addGroup)
&& addGroup is string groupToAdd && !string.IsNullOrEmpty(groupToAdd))
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupToAdd).ConfigureAwait(false);
}
if (outMsg.Headers.TryGetValue(SignalRHeaders.RemoveFromGroup, out var removeGroup)
&& removeGroup is string groupToRemove && !string.IsNullOrEmpty(groupToRemove))
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupToRemove).ConfigureAwait(false);
}
}
private static object?[]? NormalizeArgs(object?[]? args)
{
if (args is null) return null;
for (var i = 0; i < args.Length; i++)
{
if (args[i] is JsonElement je)
args[i] = DeserializeJsonElement(je);
}
return args;
}
private static object? DeserializeJsonElement(JsonElement element)
{
return element.ValueKind switch
{
JsonValueKind.String => element.GetString(),
JsonValueKind.Number when element.TryGetInt64(out var l) => l,
JsonValueKind.Number => element.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null or JsonValueKind.Undefined => null,
// For arrays and objects, keep as JsonElement — pipeline can deserialize as needed
_ => element
};
}
}