private async void StartReading() { try { while (true) { var result = await _lowerConnection.Input.ReadAsync(); var buffer = result.Buffer; try { ReadableBuffer messageBuffer; while (RecordProcessor.TryGetFrame(ref buffer, out messageBuffer)) { var recordType = RecordProcessor.ReadRecord(ref messageBuffer, _state); if (_state == null) { if (recordType != RecordType.Handshake) { Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.unexpected_message, "Requre a handshake for first message"); } _state = VersionStateFactory.GetNewStateMachine(messageBuffer, _listener); HandshakeWriting(); } Console.WriteLine($"Received TLS frame {recordType}"); if (recordType == RecordType.Handshake) { var writer = _handshakePipe.Alloc(); writer.Append(messageBuffer); await writer.FlushAsync(); await HandshakeReading(); if (_state.State == StateType.HandshakeComplete && !_startedApplicationWrite) { ApplicationWriting(); _startedApplicationWrite = true; } continue; } if (recordType == RecordType.Alert) { _state.HandleAlertMessage(messageBuffer); continue; } if (recordType == RecordType.Application) { Console.WriteLine("Writing Application Data"); var writer = _outputPipe.Alloc(); writer.Append(messageBuffer); await writer.FlushAsync(); continue; } Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.unexpected_message, $"Unknown message type {recordType}"); } if (result.IsCompleted) { return; } } finally { _lowerConnection.Input.Advance(buffer.Start, buffer.End); } } } catch { //nom nom Dispose(); } }
private async void StartReading() { try { while (true) { var result = await _lowerConnection.Input.ReadAsync(); var buffer = result.Buffer; if (result.IsCompleted && buffer.IsEmpty) { return; } try { while (RecordProcessor.TryGetFrame(ref buffer, out ReadableBuffer messageBuffer)) { var recordType = RecordProcessor.ReadRecord(ref messageBuffer, _state); _logger?.LogTrace($"Received TLS frame {recordType}"); if (recordType == RecordType.Handshake) { var writer = _handshakePipe.Writer.Alloc(); writer.Append(messageBuffer); await writer.FlushAsync(); await HandshakeReading(); if (_state.State == StateType.HandshakeComplete && !_startedApplicationWrite) { _logger?.LogInformation("Handshake complete starting application writing"); _startedApplicationWrite = true; ApplicationWriting(); _handshakeDone.SetResult(true); } continue; } if (recordType == RecordType.Alert) { _state.HandleAlertMessage(messageBuffer); continue; } if (recordType == RecordType.Application) { _logger?.LogTrace("Writing Application Data"); var writer = _outputPipe.Writer.Alloc(); writer.Append(messageBuffer); await writer.FlushAsync(); continue; } if (recordType == RecordType.ChangeCipherSpec) { _state.HandleChangeCipherSpec(messageBuffer); continue; } Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.unexpected_message, $"Unknown message type {recordType}"); } if (result.IsCompleted) { return; } } finally { _lowerConnection.Input.Advance(buffer.Start, buffer.End); } } } catch (Exception ex) { _logger?.LogWarning(new EventId(1), ex, "There was an unhandled exception in the reading loop"); } finally { Dispose(); } }