// Continues a conversation with the server by receiving a response to a packet sent with 'Send' or 'SendReply'. public ValueTask <PayloadData> ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) { ValueTask <ArraySegment <byte> > task; try { VerifyConnected(); task = m_payloadHandler.ReadPayloadAsync(m_payloadCache, ProtocolErrorBehavior.Throw, ioBehavior); } catch (Exception ex) { if ((ex as MySqlException)?.Number == (int)MySqlErrorCode.CommandTimeoutExpired) { HandleTimeout(); } task = ValueTaskExtensions.FromException <ArraySegment <byte> >(ex); } 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))); }
private ValueTask <PayloadData> TryAsync(Func <CancellationToken, ValueTask <PayloadData> > func, CancellationToken cancellationToken) { VerifyConnected(); var task = func(cancellationToken); if (task.IsCompletedSuccessfully) { if (task.Result.HeaderByte != ErrorPayload.Signature) { return(task); } var exception = ErrorPayload.Create(task.Result).ToException(); #if NETSTANDARD1_3 return(new ValueTask <PayloadData>(Task.FromException <PayloadData>(exception))); #else var tcs = new TaskCompletionSource <PayloadData>(); tcs.SetException(exception); return(new ValueTask <PayloadData>(tcs.Task)); #endif } return(new ValueTask <PayloadData>(task.AsTask() .ContinueWith(TryAsyncContinuation, cancellationToken, TaskContinuationOptions.LazyCancellation | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default))); }
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))); }