Example #1
0
        private async Task <OperationResult <ChannelState> > WriteWithCancellationAsync(AsynchronousWritingOperation writingOperation, long length, CancellationToken cancellationToken, TimeSpan timeout)
        {
            if (!cancellationToken.CanBeCanceled)
            {
                return(await WriteWithTimeoutAsync(writingOperation, length, timeout));
            }

            ThrowIfDisposed();

            bool gainedExclusiveAccess = false;

            try
            {
                var status = await AsyncHelper.GetTaskForWaitHandle(_exclusiveAccessSemaphore, cancellationToken, timeout);

                if (status != OperationStatus.Completed)
                {
                    return(new OperationResult <ChannelState>(status, null));
                }

                gainedExclusiveAccess = true;

                return(await WriteAndSetEventsAsync(writingOperation, length));
            }
            finally
            {
                if (gainedExclusiveAccess)
                {
                    _exclusiveAccessSemaphore.Release();
                }
            }
        }
        private async Task <OperationResult <ChannelState> > ReadWithTimeoutAsync(AsynchronousReadingOperation readingOperation, TimeSpan timeout)
        {
            ThrowIfDisposed();

            bool gainedExclusiveAccess = false;

            try
            {
                var status = await AsyncHelper.GetTaskForWaitHandle(_exclusiveAccessSemaphore, timeout);

                if (status != OperationStatus.Completed)
                {
                    return(new OperationResult <ChannelState>(status, null));
                }

                gainedExclusiveAccess = true;

                return(await ReadAndSetEventsAsync(readingOperation));
            }
            finally
            {
                if (gainedExclusiveAccess)
                {
                    _exclusiveAccessSemaphore.Release();
                }
            }
        }
Example #3
0
        /// <summary>
        /// Retrieves information about current state of the channel. This method is thread-safe.
        /// </summary>
        /// <param name="timeout">Operation Timeout.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>
        /// OperationResult with OperationStatus.Completed, OperationStatus.Timeout or OperationStatus.Cancelled as well as ChannelState
        /// indicating current number of active messages in the queue
        /// </returns>
        public async Task <OperationResult <ChannelState> > GetChannelStateAsync(CancellationToken cancellationToken, TimeSpan timeout)
        {
            if (!cancellationToken.CanBeCanceled)
            {
                return(await GetChannelStateAsync(timeout));
            }

            ThrowIfDisposed();

            var gainedExclusiveAccess = false;

            try
            {
                var status = await AsyncHelper.GetTaskForWaitHandle(_exclusiveAccessSemaphore, cancellationToken, timeout);

                if (status != OperationStatus.Completed)
                {
                    return(new OperationResult <ChannelState>(status, null));
                }

                gainedExclusiveAccess = true;

                return(new OperationResult <ChannelState>(OperationStatus.Completed, _memoryManager.GetChannelState()));
            }
            finally
            {
                if (gainedExclusiveAccess)
                {
                    _exclusiveAccessSemaphore.Release();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Asynchronously waits until reader will process all messages and queue will be empty. This method is thread-safe.
        /// </summary>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <param name="timeout">Operation timeout.</param>
        /// <returns>
        /// OperationStatus.Completed, OperationStatus.Timeout or OperationStatus.Cancelled.
        /// </returns>
        public async Task <OperationStatus> WhenQueueIsEmptyAsync(CancellationToken cancellationToken, TimeSpan timeout)
        {
            if (!cancellationToken.CanBeCanceled)
            {
                return(await WhenQueueIsEmptyAsync(timeout));
            }

            ThrowIfDisposed();

            return(await AsyncHelper.GetTaskForWaitHandle(_noMessagesEvent, cancellationToken, timeout));
        }
Example #5
0
        /// <summary>
        /// Asynchronously waits when client will open this channel. This method is thread-safe.
        /// </summary>
        /// <param name="timeout">Operation timeout.</param>
        /// <returns>
        /// OpertionStatus.Completed or OperationStatus.Timeout.
        /// </returns>
        public async Task <OperationStatus> WhenClientConnectedAsync(TimeSpan timeout)
        {
            ThrowIfDisposed();

            if (_isClient)
            {
                return(OperationStatus.Completed);
            }

            return(await AsyncHelper.GetTaskForWaitHandle(_clientConnectedEvent, timeout));
        }
Example #6
0
        /// <summary>
        /// Asynchronously waits when client will open this channel. This method is thread-safe.
        /// </summary>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <param name="timeout">Operation timeout.</param>
        /// <returns>
        /// OpertionStatus.Completed, OperationStatus.Timeout or OperationStatus.Cancelled.
        /// </returns>
        public async Task <OperationStatus> WhenClientConnectedAsync(CancellationToken cancellationToken, TimeSpan timeout)
        {
            if (!cancellationToken.CanBeCanceled)
            {
                return(await WhenClientConnectedAsync(timeout));
            }

            ThrowIfDisposed();

            if (_isClient)
            {
                return(OperationStatus.Completed);
            }

            return(await AsyncHelper.GetTaskForWaitHandle(_clientConnectedEvent, cancellationToken, timeout));
        }
Example #7
0
        /// <summary>
        /// Asynchronously waits until reader will process all messages and queue will be empty. This method is thread-safe.
        /// </summary>
        /// <param name="timeout">Operation timeout.</param>
        /// <returns>
        /// OperationStatus.Completed or OperationStatus.Timeout.
        /// </returns>
        public async Task <OperationStatus> WhenQueueIsEmptyAsync(TimeSpan timeout)
        {
            ThrowIfDisposed();

            return(await AsyncHelper.GetTaskForWaitHandle(_noMessagesEvent, timeout));
        }