// Only called after connection middleware is complete which means the ConnectionClosed token has fired. public override async ValueTask DisposeAsync() { // Just in case we haven't started the connection, start it here so we can clean up properly. EnsureStarted(); _originalTransport.Input.Complete(); _originalTransport.Output.Complete(); try { // Now wait for both to complete if (_receivingTask != null) { await _receivingTask; } if (_sendingTask != null) { await _sendingTask; } } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}."); } finally { _receiver.Dispose(); _sender?.Dispose(); } _connectionClosedTokenSource.Dispose(); }
public void Start() { try { // Spawn send and receive logic _receivingTask = DoReceive(); _sendingTask = DoSend(); } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(Start)}."); } }
public async Task StartAsync(IConnectionHandler connectionHandler) { try { connectionHandler.OnConnection(this); // Spawn send and receive logic Task receiveTask = DoReceive(); Task sendTask = DoSend(); // If the sending task completes then close the receive // We don't need to do this in the other direction because the kestrel // will trigger the output closing once the input is complete. if (await Task.WhenAny(receiveTask, sendTask) == sendTask) { // Tell the reader it's being aborted _socket.Dispose(); } // Now wait for both to complete await receiveTask; await sendTask; // Dispose the socket(should noop if already called) _socket.Dispose(); } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}."); } }
private async Task StartAsync() { try { // Spawn send and receive logic var receiveTask = DoReceive(); var sendTask = DoSend(); // Now wait for both to complete await receiveTask; await sendTask; _receiver.Dispose(); _sender.Dispose(); } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}."); } }
public async Task StartAsync() { try { // Spawn send and receive logic var receiveTask = DoReceive(); var sendTask = DoSend(); // Now wait for both to complete await receiveTask; await sendTask; _receiver.Dispose(); _sender.Dispose(); ThreadPool.UnsafeQueueUserWorkItem(state => ((SocketConnection)state).CancelConnectionClosedToken(), this); } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}."); } }
public async Task StartAsync() { Exception sendError = null; try { // Spawn send and receive logic Task receiveTask = DoReceive(); Task <Exception> sendTask = DoSend(); // If the sending task completes then close the receive // We don't need to do this in the other direction because the kestrel // will trigger the output closing once the input is complete. if (await Task.WhenAny(receiveTask, sendTask) == sendTask) { // Tell the reader it's being aborted _socket.Dispose(); } // Now wait for both to complete await receiveTask; sendError = await sendTask; // Dispose the socket(should noop if already called) _socket.Dispose(); _receiver.Dispose(); _sender.Dispose(); ThreadPool.QueueUserWorkItem(state => ((SocketConnection)state).CancelConnectionClosedToken(), this); } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}."); } finally { // Complete the output after disposing the socket Output.Complete(sendError); } }
private async Task StartAsync() { try { // Spawn send and receive logic var receiveTask = DoReceive(); var sendTask = DoSend(); // Now wait for both to complete await receiveTask; await sendTask; _receiver.Dispose(); _sender.Dispose(); // Fire the connection closed token and wait for it to complete var waitForConnectionClosedTcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); ThreadPool.UnsafeQueueUserWorkItem(state => { (var connection, var tcs) = state; connection.CancelConnectionClosedToken(); tcs.TrySetResult(null); }, (this, waitForConnectionClosedTcs), preferLocal: false); await waitForConnectionClosedTcs.Task; } catch (Exception ex) { _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}."); } }