/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = contextOptions.KeepAliveInterval.Ticks; _clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _maxMessageSize = contextOptions.MaximumReceiveMessageSize; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; _closedRegistration = connectionContext.ConnectionClosed.Register((state) => ((HubConnectionContext)state !).Abort(), this); HubCallerContext = new DefaultHubCallerContext(this); _systemClock = contextOptions.SystemClock ?? new SystemClock(); _lastSendTimeStamp = _systemClock.UtcNowTicks; // We'll be avoiding using the semaphore when the limit is set to 1, so no need to allocate it var maxInvokeLimit = contextOptions.MaximumParallelInvocations; if (maxInvokeLimit != 1) { ActiveInvocationLimit = new SemaphoreSlim(maxInvokeLimit, maxInvokeLimit); } }
/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = contextOptions.KeepAliveInterval.Ticks; _clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; }
/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = (long)contextOptions.KeepAliveInterval.TotalMilliseconds; _clientTimeoutInterval = (long)contextOptions.ClientTimeoutInterval.TotalMilliseconds; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _maxMessageSize = contextOptions.MaximumReceiveMessageSize; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; _closedRegistration = connectionContext.ConnectionClosed.Register(static (state) => ((HubConnectionContext)state !).Abort(), this);
/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = contextOptions.KeepAliveInterval.Ticks; _clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _maxMessageSize = contextOptions.MaximumReceiveMessageSize; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; HubCallerContext = new DefaultHubCallerContext(this); }
/// <inheritdoc /> public override async Task OnConnectedAsync(ConnectionContext connection) { // We check to see if HubOptions<THub> are set because those take precedence over global hub options. // Then set the keepAlive and handshakeTimeout values to the defaults in HubOptionsSetup when they were explicitly set to null. var supportedProtocols = _hubOptions.SupportedProtocols ?? _globalHubOptions.SupportedProtocols; if (supportedProtocols == null || supportedProtocols.Count == 0) { throw new InvalidOperationException("There are no supported protocols"); } var handshakeTimeout = _hubOptions.HandshakeTimeout ?? _globalHubOptions.HandshakeTimeout ?? HubOptionsSetup.DefaultHandshakeTimeout; var contextOptions = new HubConnectionContextOptions() { KeepAliveInterval = _hubOptions.KeepAliveInterval ?? _globalHubOptions.KeepAliveInterval ?? HubOptionsSetup.DefaultKeepAliveInterval, ClientTimeoutInterval = _hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval, StreamBufferCapacity = _hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? HubOptionsSetup.DefaultStreamBufferCapacity, MaximumReceiveMessageSize = _maximumMessageSize, SystemClock = SystemClock, MaximumParallelInvocations = _maxParallelInvokes, }; Log.ConnectedStarting(_logger); var connectionContext = new HubConnectionContext(connection, contextOptions, _loggerFactory); var resolvedSupportedProtocols = (supportedProtocols as IReadOnlyList <string>) ?? supportedProtocols.ToList(); if (!await connectionContext.HandshakeAsync(handshakeTimeout, resolvedSupportedProtocols, _protocolResolver, _userIdProvider, _enableDetailedErrors)) { return; } // -- the connectionContext has been set up -- try { await _lifetimeManager.OnConnectedAsync(connectionContext); await RunHubAsync(connectionContext); } finally { connectionContext.Cleanup(); Log.ConnectedEnding(_logger); await _lifetimeManager.OnDisconnectedAsync(connectionContext); } }
/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = contextOptions.KeepAliveInterval.Ticks; _clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _maxMessageSize = contextOptions.MaximumReceiveMessageSize; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; _closedRegistration = connectionContext.ConnectionClosed.Register((state) => ((HubConnectionContext)state !).Abort(), this); HubCallerContext = new DefaultHubCallerContext(this); _systemClock = contextOptions.SystemClock ?? new SystemClock(); _lastSendTimeStamp = _systemClock.UtcNowTicks; }
/// <summary> /// Initializes a new instance of the <see cref="HubConnectionContext"/> class. /// </summary> /// <param name="connectionContext">The underlying <see cref="ConnectionContext"/>.</param> /// <param name="loggerFactory">The logger factory.</param> /// <param name="contextOptions">The options to configure the HubConnectionContext.</param> public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory) { _keepAliveInterval = contextOptions.KeepAliveInterval.Ticks; _clientTimeoutInterval = contextOptions.ClientTimeoutInterval.Ticks; _streamBufferCapacity = contextOptions.StreamBufferCapacity; _maxMessageSize = contextOptions.MaximumReceiveMessageSize; _connectionContext = connectionContext; _logger = loggerFactory.CreateLogger <HubConnectionContext>(); ConnectionAborted = _connectionAbortedTokenSource.Token; HubCallerContext = new DefaultHubCallerContext(this); if (AppContext.TryGetSwitch("Microsoft.AspNetCore.SignalR.UseAbsoluteClientTimeout", out var useAbsoluteClientTimeout)) { _useAbsoluteClientTimeout = useAbsoluteClientTimeout; } }