private async Task EnsureOrleansClusterConnection() { if (_clusterClient == null) { throw new NullReferenceException(nameof(_clusterClient)); } if (_clusterClient.IsInitialized) { OrleansHubLifetimeManagerLog.Connected(_logger); return; } _clusterClientConnectionRetryCount = 0; OrleansHubLifetimeManagerLog.NotConnected(_logger); await _clusterClient.Connect(async ex => { if (_clusterClientConnectionRetryCount >= MaxClusterClientConnectionRetries) { throw ex; } OrleansHubLifetimeManagerLog.ConnectionFailed(_logger, ex); await Task.Delay(ClusterClientConnectionRetryInterval); _clusterClientConnectionRetryCount++; return(true); }); OrleansHubLifetimeManagerLog.ConnectionRestored(_logger); }
private async Task OnReceivedAsync(SendToClientInvocationMessage message) { OrleansHubLifetimeManagerLog.ReceivedFromStream(_logger, _clientStreamId); if (_connectionsById.TryGetValue(message.ConnectionId, out var connection)) { var invocationMessage = new InvocationMessage(message.MethodName, message.Args); try { await connection.WriteAsync(invocationMessage).AsTask(); } catch (Exception e) { OrleansHubLifetimeManagerLog.FailedWritingMessage(_logger, e); } } }
private async Task OnReceivedAsync(SendToAllInvocationMessage message) { OrleansHubLifetimeManagerLog.ReceivedFromStream(_logger, _clientStreamId); var invocationMessage = new InvocationMessage(message.MethodName, message.Args); var tasks = _connectionsById .Where(pair => !pair.Value.ConnectionAborted.IsCancellationRequested && !message.ExcludedConnectionIds.Contains(pair.Key)) .Select(pair => pair.Value.WriteAsync(invocationMessage).AsTask()); try { await Task.WhenAll(tasks); } catch (Exception e) { OrleansHubLifetimeManagerLog.FailedWritingMessage(_logger, e); } }
private async Task InitializeAsync() { await EnsureOrleansClusterConnection(); OrleansHubLifetimeManagerLog.Subscribing(_logger, _clientStreamId); try { var streamProvider = _clusterClient.GetStreamProvider(OrleansSignalRConstants.StreamProviderName); _clientMessageStream = streamProvider .GetStream <SendToClientInvocationMessage>(_clientStreamId, OrleansSignalRConstants.ClientMessageSendingStreamNamespace); _clientMessageHandle = await _clientMessageStream.SubscribeAsync(async (message, token) => await OnReceivedAsync(message)); _allMessageHandle = await HubProxy.AllMessageStream.SubscribeAsync(async (message, token) => await OnReceivedAsync(message)); } catch (Exception e) { OrleansHubLifetimeManagerLog.InternalMessageFailed(_logger, e); } }
public void Dispose() { if (!_initialized) { return; } OrleansHubLifetimeManagerLog.Unsubscribe(_logger, _clientStreamId); var streamProvider = _clusterClient.GetStreamProvider(OrleansSignalRConstants.StreamProviderName); var tasks = _connectionsById.Keys.Select(id => streamProvider .GetStream <EventArgs>(OrleansSignalRConstants.DisconnectionStreamId, id) .OnNextAsync(EventArgs.Empty)); Task.WaitAll(tasks.ToArray()); Task.WaitAll( _allMessageHandle.UnsubscribeAsync(), _clientMessageHandle.UnsubscribeAsync()); }