예제 #1
0
 public void ThrowIfError()
 {
     if (HeaderByte == ErrorPayload.Signature)
     {
         var error = ErrorPayload.Create(this);
         throw error.ToException();
     }
 }
예제 #2
0
        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)));
        }