Esempio n. 1
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            if (targetMethod == null)
            {
                throw new ArgumentException(nameof(targetMethod));
            }

            string targetMethodName = targetMethod.Name;

            if (targetMethodName == "Dispose")
            {
                Dispose();
                return(targetMethod.Invoke(_decorated, args));
            }

            if (targetMethod.ReturnType != typeof(void) ||
                targetMethodName.StartsWith("add_", StringComparison.Ordinal) ||
                targetMethodName.StartsWith("remove_", StringComparison.Ordinal))
            {
                return(targetMethod.Invoke(_decorated, args));
            }

            _fiber.Enqueue(() => (Task)targetMethod.Invoke(_decorated, args));
            return(null);
        }
Esempio n. 2
0
            public void Signal()
            {
                if (Interlocked.CompareExchange(ref _flushPending, 1, 0) == 1)
                {
                    return;
                }

                _target.Enqueue(_cache);
            }
Esempio n. 3
0
        public void Run(IAsyncFiber fiber)
        {
            using AutoResetEvent wait = new AutoResetEvent(false);
            using (fiber)
            {
                int i = 0;

                Task AsyncHandler()
                {
                    i++;
                    if (i == OperationsPerInvoke)
                    {
                        wait.Set();
                    }

                    return(Task.CompletedTask);
                }

                for (int j = 0; j < OperationsPerInvoke; j++)
                {
                    fiber.Enqueue(AsyncHandler);
                }

                WaitHandle.WaitAny(new WaitHandle[] { wait });
            }
        }
Esempio n. 4
0
        public IDisposable Schedule(IAsyncFiber fiber, Func <Task> action, TimeSpan dueTime)
        {
            if (dueTime.TotalMilliseconds <= 0)
            {
                AsyncPendingAction pending = new AsyncPendingAction(action);
                fiber.Enqueue(pending.ExecuteAsync);
                return(pending);
            }

            return(new AsyncTimerAction(fiber, action, dueTime));
        }
Esempio n. 5
0
        public IDisposable Subscribe(IAsyncFiber fiber, Func <T, Task> receive)
        {
            void Receive(T msg)
            {
                fiber.Enqueue(() => receive(msg));
            }

            IDisposable disposable = _internalEvent.Subscribe(Receive);

            return(new Unsubscriber(disposable, fiber));
        }
Esempio n. 6
0
        public IDisposable Subscribe(IAsyncFiber fiber, Func <Task> receive)
        {
            void Action()
            {
                fiber.Enqueue(receive);
            }

            IDisposable disposable = _internalEvent.Subscribe(Action);

            return(new Unsubscriber(disposable, fiber));
        }
Esempio n. 7
0
            public void Publish(TSnapshot msg)
            {
                if (_disposed)
                {
                    return;
                }

                _fiber.Enqueue(() => _receiveSnapshot(msg));
                //publishing the snapshot subscribes the updates...
                _sub = _updatesPort.Subscribe(_fiber, _receive);
            }
Esempio n. 8
0
        private void ExecuteOnTimerThread(IAsyncFiber fiber)
        {
            if (_interval.Ticks == TimeSpan.FromMilliseconds(-1).Ticks || _cancelled)
            {
                fiber.Remove(this);
                DisposeTimer();
            }

            if (!_cancelled)
            {
                fiber.Enqueue(ExecuteAsync);
            }
        }
Esempio n. 9
0
        public IDisposable Subscribe(IAsyncFiber fiber, Func <T, Task> receive)
        {
            lock (_lock)
            {
                IDisposable disposable = _updateChannel.Subscribe(fiber, receive);
                if (_hasValue)
                {
                    T item = _last;
                    fiber.Enqueue(() => receive(item));
                }

                return(disposable);
            }
        }
Esempio n. 10
0
        /// <summary>
        ///     Subscribe an AsyncFiber to an Action based event
        /// </summary>
        /// <param name="fiber"></param>
        /// <param name="obj"></param>
        /// <param name="eventName"></param>
        /// <param name="receive"></param>
        /// <returns></returns>
        public static IDisposable SubscribeToEvent(this IAsyncFiber fiber, object obj, string eventName,
                                                   Func <Task> receive)
        {
            EventInfo  evt    = obj.GetType().GetEvent(eventName);
            MethodInfo add    = evt.GetAddMethod();
            MethodInfo remove = evt.GetRemoveMethod();

            void Action()
            {
                fiber.Enqueue(receive);
            }

            object[] addHandlerArgs = { (Action)Action };
            add.Invoke(obj, addHandlerArgs);

            return(new Unsubscriber(new DisposeAction(() => remove.Invoke(obj, addHandlerArgs)), fiber));
        }
Esempio n. 11
0
        /// <summary>
        ///     Subscribe with a message predicate to filter messages
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="fiber"></param>
        /// <param name="receive"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static IDisposable Subscribe <T>(this ISubscriberPort <T> port,
                                                IAsyncFiber fiber,
                                                Func <T, Task> receive,
                                                Predicate <T> filter)
        {
            void FilteredReceiver(T x)
            {
                if (filter(x))
                {
                    fiber.Enqueue(() => receive(x));
                }
            }

            IDisposable sub = port.Subscribe(FilteredReceiver);

            return(new Unsubscriber(sub, fiber));
        }
Esempio n. 12
0
        /// <summary>
        ///     Subscribe with a message predicate to filter messages
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="fiber"></param>
        /// <param name="receive"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static IDisposable Subscribe <T>(this IAsyncFiber fiber,
                                                ISubscriberPort <T> port,
                                                Func <T, Task> receive,
                                                Predicate <T> filter)
        {
            void FilteredReceiver(T x)
            {
                if (filter(x))
                {
                    fiber.Enqueue(() => receive(x));
                }
            }

            //we use a stub fiber to force the filtering onto the publisher thread.
            IDisposable sub = port.Subscribe(FilteredReceiver);

            return(new Unsubscriber(sub, fiber));
        }
Esempio n. 13
0
 public void Signal() => _target.Enqueue(_cache);
Esempio n. 14
0
 public void Signal(TMsg msg) => _target.Enqueue(() => _callback(msg));
Esempio n. 15
0
 public void Publish(T msg) => Fiber.Enqueue(() => _handler(msg));