public PipeCompletionCallbacks?AddCallback(Action <Exception?, object?> callback, object?state)
        {
            if (_callbackCount == 0)
            {
                _firstCallback = new PipeCompletionCallback(callback, state);
                _callbackCount++;
            }
            else
            {
                EnsureSpace();

                // -1 to adjust for _firstCallback
                var callbackIndex = _callbackCount - 1;
                _callbackCount++;
                Debug.Assert(_callbacks != null);
                _callbacks[callbackIndex] = new PipeCompletionCallback(callback, state);
            }

            if (IsCompleted)
            {
                return(GetCallbacks());
            }

            return(null);
        }
Exemple #2
0
        public PipeCompletionCallbacks AddCallback(Action <Exception, object> callback, object state)
        {
            if (_callbacks == null)
            {
                _callbacks = CompletionCallbackPool.Rent(InitialCallbacksSize);
            }

            var newIndex = _callbackCount;

            _callbackCount++;

            if (newIndex == _callbacks.Length)
            {
                var newArray = new PipeCompletionCallback[_callbacks.Length * 2];
                Array.Copy(_callbacks, newArray, _callbacks.Length);
                CompletionCallbackPool.Return(_callbacks);
                _callbacks = newArray;
            }
            _callbacks[newIndex].Callback = callback;
            _callbacks[newIndex].State    = state;

            if (IsCompleted)
            {
                return(GetCallbacks());
            }

            return(null);
        }
 public PipeCompletionCallbacks(ArrayPool <PipeCompletionCallback> pool, int count, Exception?exception, PipeCompletionCallback firstCallback, PipeCompletionCallback[]?callbacks)
 {
     _pool          = pool;
     _count         = count;
     _exception     = exception;
     _firstCallback = firstCallback;
     _callbacks     = callbacks;
 }
Exemple #4
0
 private void Execute(PipeCompletionCallback callback, ref List <Exception>?exceptions)
 {
     try
     {
         callback.Callback(_exception, callback.State);
     }
     catch (Exception ex)
     {
         exceptions ??= new List <Exception>();
         exceptions.Add(ex);
     }
 }
Exemple #5
0
        private PipeCompletionCallbacks GetCallbacks()
        {
            Debug.Assert(IsCompleted);
            if (_callbackCount == 0)
            {
                return(null);
            }

            var callbacks = new PipeCompletionCallbacks(s_completionCallbackPool,
                                                        _callbackCount,
                                                        _exceptionInfo?.SourceException,
                                                        _firstCallback,
                                                        _callbacks);

            _firstCallback = default;
            _callbacks     = null;
            _callbackCount = 0;
            return(callbacks);
        }
        public void Execute()
        {
            if (_callbacks == null || _count == 0)
            {
                return;
            }

            try
            {
                List <Exception> exceptions = null;

                for (var i = 0; i < _count; i++)
                {
                    PipeCompletionCallback callback = _callbacks[i];
                    try
                    {
                        callback.Callback(_exception, callback.State);
                    }
                    catch (Exception ex)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(ex);
                    }
                }

                if (exceptions != null)
                {
                    throw new AggregateException(exceptions);
                }
            }
            finally
            {
                _pool.Return(_callbacks, clearArray: true);
            }
        }