Esempio n. 1
0
        public Trigger(
            string identifier,
            Action <ITriggerMsg> call,
            TriggerOption opt,
            TriggerThreading threading)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException(nameof(identifier), nullEx);
            }
            if (call == null)
            {
                throw new ArgumentNullException(nameof(call));
            }
            if (identifier == string.Empty)
            {
                throw new ArgumentException(emptyEx, nameof(identifier));
            }
            if (Tools.ContainsWhitespace(identifier))
            {
                throw new ArgumentException(whiteEx, nameof(identifier));
            }

            Identifiers = new ReadOnlyCollection <string>(new[] { identifier });
            Call        = call;
            Option      = opt;
            Threading   = threading;
        }
Esempio n. 2
0
        // Dispatch the correct delegate to the correct queue (or Threadpool) according to `threading`.
        void ThreadingDispatch <T>(
            TriggerThreading threading,
            Action queueDelegate,
            WaitCallback threadpoolDelegate,
            T id,
            Dictionary <T, Queue <Action> > map)
        {
            switch (threading)
            {
            case TriggerThreading.Queue:
                Queue <Action> queue;
                if (map.TryGetValue(id, out queue))
                {
                    Push(queueDelegate, queue);
                    break;
                }
                goto default;

            case TriggerThreading.Threadpool:
                ThreadPool.QueueUserWorkItem(threadpoolDelegate);
                break;

            default:
                Push(queueDelegate, Standard);
                break;
            }
        }
Esempio n. 3
0
        public Trigger(
            Action <ITriggerMsg> call,
            TriggerOption opt,
            TriggerThreading threading,
            params string[] identifiers)
        {
            if (call == null)
            {
                throw new ArgumentNullException(nameof(call));
            }
            if (identifiers == null)
            {
                throw new ArgumentNullException(nameof(identifiers));
            }
            if (identifiers.Length == 0)
            {
                throw new ArgumentException("Cannot be an empty collection.", nameof(identifiers));
            }

            // Create our own copy.
            var triggerIdents = new string[identifiers.Length];

            for (int i = 0; i < identifiers.Length; i++)
            {
                var id = identifiers[i];

                if (id == null)
                {
                    throw new ArgumentException(nullEx, nameof(identifiers));
                }
                if (id == string.Empty)
                {
                    throw new ArgumentException(emptyEx, nameof(identifiers));
                }
                if (Tools.ContainsWhitespace(id))
                {
                    throw new ArgumentException(whiteEx, nameof(identifiers));
                }

                // We need to check each element anyway, so use the loop the create our own copy
                // instead of using Array.Copy afterwards.
                triggerIdents[i] = id;
            }

            Identifiers = new ReadOnlyCollection <string>(triggerIdents);
            Call        = call;
            Option      = opt;
            Threading   = threading;
        }
Esempio n. 4
0
        // Map `id` to `queue` via a Dictionary in a way determined by ThreadingModel.
        static void RegisterThreading <T>(
            TriggerThreading threading,
            T id,
            Dictionary <T, Queue <Action> > map,
            ref Queue <Action> queue)
        {
            if (threading == TriggerThreading.Queue)
            {
                if (queue == null)
                {
                    queue = new Queue <Action>();
                }

                map[id] = queue;
            }
            // We don't need to do anything for other types of threading.
        }
Esempio n. 5
0
 public Trigger(string identifier, Action <ITriggerMsg> call, TriggerThreading threading) :
     this(identifier, call, TriggerOption.None, threading)
 {
 }
Esempio n. 6
0
 public Trigger(Action <ITriggerMsg> call, TriggerThreading threading, params string[] identifiers) :
     this(call, TriggerOption.None, threading, identifiers)
 {
 }
Esempio n. 7
0
 public IrcHandler(Action <T> handler, TriggerThreading threading)
 {
     IrcEventType = typeof(T);
     this.handler = handler;
     Threading    = threading;
 }