public void ThrowIfError() { if (HeaderByte == ErrorPayload.Signature) { var error = ErrorPayload.Create(this); throw error.ToException(); } }
private ValueTask <PayloadData> TryAsync(Func <ProtocolErrorBehavior, IOBehavior, ValueTask <ArraySegment <byte> > > func, IOBehavior ioBehavior, CancellationToken cancellationToken) { VerifyConnected(); var task = func(ProtocolErrorBehavior.Throw, ioBehavior); if (task.IsCompletedSuccessfully) { var payload = new PayloadData(task.Result); if (payload.HeaderByte != ErrorPayload.Signature) { return(new ValueTask <PayloadData>(payload)); } var exception = ErrorPayload.Create(payload).ToException(); return(ValueTaskExtensions.FromException <PayloadData>(exception)); } return(new ValueTask <PayloadData>(task.AsTask() .ContinueWith(TryAsyncContinuation, cancellationToken, TaskContinuationOptions.LazyCancellation | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default))); }