private async Task WaitOnApplicationTask(ServiceConnectionContext connection) { Exception exception = null; try { // Wait for the application task to complete await connection.ApplicationTask; } catch (Exception ex) { // Capture the exception to communicate it to the transport (this isn't strictly required) exception = ex; } finally { // Close the transport side since the application is no longer running connection.Transport.Output.Complete(exception); connection.Transport.Input.Complete(); } // If we aren't already aborted, we send the abort message to the service if (connection.AbortOnClose) { // Inform the Service that we will remove the client because SignalR told us it is disconnected. var serviceMessage = new CloseConnectionMessage(connection.ConnectionId, errorMessage: ""); await WriteAsync(serviceMessage); Log.CloseConnection(_logger, connection.ConnectionId); } }
private async Task ProcessOutgoingMessagesAsync(ServiceConnectionContext connection) { try { while (true) { var result = await connection.Application.Input.ReadAsync(); if (result.IsCanceled) { break; } var buffer = result.Buffer; if (!buffer.IsEmpty) { try { // Forward the message to the service await WriteAsync(new ConnectionDataMessage(connection.ConnectionId, buffer)); } catch (Exception ex) { Log.ErrorSendingMessage(Logger, ex); } } if (result.IsCompleted) { // This connection ended (the application itself shut down) we should remove it from the list of connections break; } connection.Application.Input.AdvanceTo(buffer.End); } } catch (Exception ex) { // The exception means application fail to process input anymore // Cancel any pending flush so that we can quit and perform disconnect // Here is abort close and WaitOnApplicationTask will send close message to notify client to disconnect Log.SendLoopStopped(Logger, connection.ConnectionId, ex); connection.Application.Output.CancelPendingFlush(); } finally { connection.Application.Input.Complete(); await PerformDisconnectAsyncCore(connection.ConnectionId, true); connection.OnCompleted(); } }
private async Task ProcessOutgoingMessagesAsync(ServiceConnectionContext connection) { try { while (true) { var result = await connection.Application.Input.ReadAsync(); var buffer = result.Buffer; if (!buffer.IsEmpty) { try { // Forward the message to the service await WriteAsync(new ConnectionDataMessage(connection.ConnectionId, buffer)); } catch (Exception ex) { Log.ErrorSendingMessage(_logger, ex); } } if (result.IsCompleted) { // This connection ended (the application itself shut down) we should remove it from the list of connections break; } connection.Application.Input.AdvanceTo(buffer.End); } } catch (Exception ex) { Log.SendLoopStopped(_logger, connection.ConnectionId, ex); } finally { connection.Application.Input.Complete(); } }
private void AddClientConnection(ServiceConnectionContext connection) { _clientConnectionManager.AddClientConnection(connection); _connectionIds.TryAdd(connection.ConnectionId, connection.ConnectionId); }
private void AddClientConnection(ServiceConnectionContext connection, string instanceId) { _clientConnectionManager.AddClientConnection(connection); _connectionIds.TryAdd(connection.ConnectionId, instanceId); }
public void AddClientConnection(ServiceConnectionContext clientConnection) { ClientConnections[clientConnection.ConnectionId] = clientConnection; }