示例#1
0
        /// <summary>
        /// Returned tasks completes once channel state has become different from
        /// given lastObservedState.
        /// If deadline is reached or and error occurs, returned task is cancelled.
        /// </summary>
        public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime?deadline = null)
        {
            GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Shutdown,
                                            "Shutdown is a terminal state. No further state changes can occur.");
            var tcs = new TaskCompletionSource <object>();
            var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture;

            // pass "tcs" as "state" for WatchConnectivityStateHandler.
            handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, WatchConnectivityStateHandler, tcs);
            return(tcs.Task);
        }
示例#2
0
        /// <summary>
        /// Returned tasks completes once channel state has become different from
        /// given lastObservedState.
        /// If deadline is reached or and error occurs, returned task is cancelled.
        /// </summary>
        public IObservable <bool> WaitForStateChangedAsync(ChannelState lastObservedState, DateTime?deadline = null)
        {
            GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Shutdown,
                                            "Shutdown is a terminal state. No further state changes can occur.");
            var subject          = new AsyncSubject <bool>();
            var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture;
            var handler          = new BatchCompletionDelegate((success, ctx) =>
            {
                subject.OnNext(success);
                subject.OnCompleted();
            });

            handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, handler);
            return(subject);
        }
示例#3
0
        /// <summary>
        /// Returned tasks completes once channel state has become different from
        /// given lastObservedState.
        /// If deadline is reached or and error occurs, returned task is cancelled.
        /// </summary>
        public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime?deadline = null)
        {
            Preconditions.CheckArgument(lastObservedState != ChannelState.FatalFailure,
                                        "FatalFailure is a terminal state. No further state changes can occur.");
            var tcs = new TaskCompletionSource <object>();
            var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture;
            var handler          = new BatchCompletionDelegate((success, ctx) =>
            {
                if (success)
                {
                    tcs.SetResult(null);
                }
                else
                {
                    tcs.SetCanceled();
                }
            });

            handle.WatchConnectivityState(lastObservedState, deadlineTimespec, environment.CompletionQueue, environment.CompletionRegistry, handler);
            return(tcs.Task);
        }
示例#4
0
        /// <summary>
        /// Returned tasks completes once channel state has become different from
        /// given lastObservedState (<c>true</c> is returned) or if the wait has timed out (<c>false</c> is returned).
        /// </summary>
        public Task <bool> TryWaitForStateChangedAsync(ChannelState lastObservedState, DateTime?deadline = null)
        {
            GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Shutdown,
                                            "Shutdown is a terminal state. No further state changes can occur.");
            var tcs = new TaskCompletionSource <bool>();
            var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture;

            lock (myLock)
            {
                if (handle.IsClosed)
                {
                    // If channel has been already shutdown and handle was disposed, we would end up with
                    // an abandoned completion added to the completion registry. Instead, we make sure we fail early.
                    throw new ObjectDisposedException(nameof(handle), "Channel handle has already been disposed.");
                }
                else
                {
                    // pass "tcs" as "state" for WatchConnectivityStateHandler.
                    handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, WatchConnectivityStateHandler, tcs);
                }
            }
            return(tcs.Task);
        }