private HubConnectionContext CreateHubConnectionContext(HubInvocationMessage message) { return(new CloudHubConnectionContext(_connection, CreateConnectionContext(message), Timeout.InfiniteTimeSpan, _loggerFactory) { ProtocolReaderWriter = _protocolReaderWriter }); }
private async Task DispatchAsync(HubInvocationMessage message) { _logger.LogInformation($"Received message: {message}"); var isMethodInvocation = message is HubMethodInvocationMessage; if (isMethodInvocation && OnConnectedAsyncMethod.IgnoreCaseEquals(((HubMethodInvocationMessage)message).Target)) { await OnClientConnectedAsync(message); return; } var connection = GetHubConnectionContext(message); if (connection == null) { await SendMessageAsync(CompletionMessage.WithError(message.InvocationId, "No connection found.")); return; } if (isMethodInvocation && OnDisconnectedAsyncMethod.IgnoreCaseEquals(((HubMethodInvocationMessage)message).Target)) { await OnClientDisconnectedAsync(connection); return; } await _hubDispatcher.DispatchMessageAsync(connection, message); }
public static void MessageSent(ILogger logger, HubInvocationMessage message) { if (logger.IsEnabled(LogLevel.Debug)) { _messageSent(logger, message.GetType().Name, message.InvocationId, null); } }
private static void WriteInvocationId(HubInvocationMessage message, Utf8JsonWriter writer) { if (!string.IsNullOrEmpty(message.InvocationId)) { writer.WriteString(InvocationIdPropertyNameBytes, message.InvocationId); } }
private Task SendLocal(HubConnectionContext connection, HubInvocationMessage hubMessage) { _logger.LogDebug( "Sending local message to connection {connectionId} on hub {hubName} (serverId: {serverId})", connection.ConnectionId, _hubName, _serverId); return(connection.WriteAsync(hubMessage).AsTask()); }
public static MockHubProtocol ReturnOnParse(HubInvocationMessage parsed) { return(new MockHubProtocol { _parsed = parsed }); }
public Task InvokeConnectionAsync(string connectionId, HubInvocationMessage message) { CheckNullConnectionId(connectionId); var connection = _connections[connectionId]; return(connection?.WriteAsync(message) ?? Task.CompletedTask); }
// This lets you add headers to a hub message and return it, in a single expression. public static HubMessage AddHeaders(IDictionary <string, string> headers, HubInvocationMessage hubMessage) { foreach (var header in headers) { hubMessage.Headers[header.Key] = header.Value; } return(hubMessage); }
private static void WriteInvocationId(HubInvocationMessage message, JsonTextWriter writer) { if (!string.IsNullOrEmpty(message.InvocationId)) { writer.WritePropertyName(InvocationIdPropertyName); writer.WriteValue(message.InvocationId); } }
private async Task OnClientConnectedAsync(HubInvocationMessage message) { var connection = CreateHubConnectionContext(message); _connections.Add(connection); await _lifetimeManager.OnConnectedAsync(connection); await _hubDispatcher.OnConnectedAsync(connection); }
private static void WriteHubInvocationMessageCommon(HubInvocationMessage message, JsonTextWriter writer, int type) { if (!string.IsNullOrEmpty(message.InvocationId)) { writer.WritePropertyName(InvocationIdPropertyName); writer.WriteValue(message.InvocationId); } WriteMessageType(writer, type); WriteMessageMetadata(writer, message.Metadata); }
public async Task WriteAsync(HubInvocationMessage message) { while (await Output.Writer.WaitToWriteAsync()) { if (Output.Writer.TryWrite(message)) { return; } } }
public static Task ReturnResultAsync(this HubConnectionContext connection, HubInvocationMessage message) { if (message == null) { return(Task.CompletedTask); } message.AddAction(nameof(HubLifetimeManager <Hub> .SendConnectionAsync)) .AddConnectionId(connection.ConnectionId); return(connection.WriteAsync(message)); }
private async Task WriteAsync(HubConnectionContext connection, HubInvocationMessage hubMessage) { while (await connection.Output.WaitToWriteAsync()) { if (connection.Output.TryWrite(hubMessage)) { break; } } }
private HubConnectionContext CreateHubConnectionContext(HubInvocationMessage message) { var context = CreateConnectionContext(message); // TODO: configurable KeepAliveInterval return(new HubConnectionContext(context, TimeSpan.FromSeconds(30), _loggerFactory) { Output = _output, ProtocolReaderWriter = _protocolReaderWriter }); }
private async Task OnConnectedAsync(HubInvocationMessage message) { var connection = CreateHubConnectionContext(message); _connections.Add(connection); await _lifetimeManager.OnConnectedAsync(connection); await _hubInvoker.OnConnectedAsync(connection); await SendMessageAsync(CompletionMessage.WithResult(message.InvocationId, "")); }
private static void WriteHeaders(Utf8JsonWriter writer, HubInvocationMessage message) { if (message.Headers != null && message.Headers.Count > 0) { writer.WriteStartObject(HeadersPropertyNameBytes); foreach (var value in message.Headers) { writer.WriteString(value.Key, value.Value); } writer.WriteEndObject(); } }
private async Task SendHubMessage(HubInvocationMessage hubMessage, CancellationToken cancellationToken = default) { AssertConnectionValid(); _protocol.WriteMessage(hubMessage, _connectionState.Connection.Transport.Output); Log.SendingMessage(_logger, hubMessage); // REVIEW: If a token is passed in and is canceled during FlushAsync it seems to break .Complete()... await _connectionState.Connection.Transport.Output.FlushAsync(); Log.MessageSent(_logger, hubMessage); }
private static void WriteHeaders(JsonTextWriter writer, HubInvocationMessage message) { if (message.Headers != null && message.Headers.Count > 0) { writer.WritePropertyName(HeadersPropertyName); writer.WriteStartObject(); foreach (var value in message.Headers) { writer.WritePropertyName(value.Key); writer.WriteValue(value.Value); } writer.WriteEndObject(); } }
private DefaultConnectionContext CreateConnectionContext(HubInvocationMessage message) { var connectionId = message.GetConnectionId(); // TODO: // No physical pipeline for logical ConnectionContext. These pipelines won't be used in current context. // So no exception or error will be thrown. // We should have a cleaner approach to reuse DefaultConnectionContext for Azure SignalR. var connectionContext = new DefaultConnectionContext(connectionId, null, null); if (message.TryGetClaims(out var claims)) { connectionContext.User = new ClaimsPrincipal(); connectionContext.User.AddIdentity(new ClaimsIdentity(claims, "Bearer")); } return(connectionContext); }
private async Task SendHubMessage(HubInvocationMessage hubMessage, InvocationRequest irq) { try { var payload = _protocol.WriteToArray(hubMessage); Log.SendInvocation(_logger, hubMessage.InvocationId); await _connection.SendAsync(payload, irq.CancellationToken); Log.SendInvocationCompleted(_logger, hubMessage.InvocationId); } catch (Exception ex) { Log.SendInvocationFailed(_logger, hubMessage.InvocationId, ex); irq.Fail(ex); TryRemoveInvocation(hubMessage.InvocationId, out _); } }
private async Task OnDisconnectedAsync(HubInvocationMessage message) { var connection = GetHubConnectionContext(message); if (connection == null) { await SendMessageAsync(CompletionMessage.WithError(message.InvocationId, "No connection found.")); return; } await _hubInvoker.OnDisconnectedAsync(connection, null); await _lifetimeManager.OnDisconnectedAsync(connection); await SendMessageAsync(CompletionMessage.WithResult(message.InvocationId, "")); _connections.Remove(connection); }
public AddressedMessage(string connectionId, HubInvocationMessage messagePayload) { ConnectionId = connectionId; Payload = messagePayload; }
public void ReceiveMessage(HubInvocationMessage message, MessageHandle handle) { messageCallback(new AddressedMessage(connectionId, message), handle).Ignore(); }
public AnonymousMessage(ISet <string> excluding, HubInvocationMessage messagePayload) { Excluding = excluding; Payload = messagePayload; }
public async Task AcceptMessageAsync(HubInvocationMessage message, GrainCancellationToken cancellationToken) { var handle = await rewoundMessagesGrain.PushMessageAsync(message); observers.Notify(x => x.ReceiveMessage(message, handle)); }
private Task SendLocal(HubConnectionContext connection, HubInvocationMessage hubMessage) { return(connection.WriteAsync(hubMessage).AsTask()); }
private HubConnectionContext GetHubConnectionContext(HubInvocationMessage message) { return(message.TryGetConnectionId(out var connectionId) ? _connections[connectionId] : null); }