/// <summary> /// Equivalent to <see cref="CallInvoker.AsyncUnaryCall{TRequest, TResponse}(Method{TRequest, TResponse}, string, CallOptions, TRequest)"/>. /// </summary> internal AsyncUnaryCall <TResponse> AsyncUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { var restMethod = _serviceCollection.GetRestMethod(method); var cancellationTokenSource = new CancellationTokenSource(); if (options.Deadline.HasValue) { // TODO: [virost, 2021-12] Use IClock. var delayInterval = options.Deadline.Value - DateTime.UtcNow; if (delayInterval.TotalMilliseconds <= 0) { throw new RpcException(new Status(StatusCode.DeadlineExceeded, $"The timeout was reached when calling a method `{restMethod.FullName}`")); } cancellationTokenSource = new CancellationTokenSource(delayInterval); } var httpResponseTask = SendAsync(restMethod, host, options, request, cancellationTokenSource.Token); var responseTask = restMethod.ReadResponseAsync <TResponse>(httpResponseTask); var responseHeadersTask = ReadHeadersAsync(httpResponseTask); Func <Status> statusFunc = () => GetStatus(httpResponseTask); Func <Metadata> trailersFunc = () => GetTrailers(httpResponseTask); return(new AsyncUnaryCall <TResponse>(responseTask, responseHeadersTask, statusFunc, trailersFunc, cancellationTokenSource.Cancel)); }
/// <summary> /// Equivalent to <see cref="CallInvoker.AsyncUnaryCall{TRequest, TResponse}(Method{TRequest, TResponse}, string, CallOptions, TRequest)"/>. /// </summary> internal AsyncUnaryCall <TResponse> AsyncUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { var restMethod = _serviceCollection.GetRestMethod(method); var cancellationTokenSource = new CancellationTokenSource(); var httpResponseTask = SendAsync(restMethod, host, options, request, cancellationTokenSource.Token); // TODO: Cancellation? var responseTask = restMethod.ReadResponseAsync <TResponse>(httpResponseTask); var responseHeadersTask = ReadHeadersAsync(httpResponseTask); Func <Status> statusFunc = () => GetStatus(httpResponseTask); Func <Metadata> trailersFunc = () => GetTrailers(httpResponseTask); return(new AsyncUnaryCall <TResponse>(responseTask, responseHeadersTask, statusFunc, trailersFunc, cancellationTokenSource.Cancel)); }