C#-消息队列系统

C#消息队列系统简单实现

通过AddEvent()添加监听事件
通过调用DoEvent()触发事件
消息系统内置一个消息队列,通过不断轮询调用

    public abstract class IEventManager
    {
        public delegate void EventDelegate(byte[] stream);

        Dictionary<int, Dictionary<int, List>> mainDict = new Dictionary<int, Dictionary<int, List>>();

        private Queue callDataList = new Queue();

        private int _delayTime = 0;
        public int DelayTime
        {
            get
            {
                return _delayTime;
            }
        }

        struct CallData
        {
            public int _mainId;
            public int _assistId;
            public byte[] _stream;
        }

        public IEventManager(int delayTime = 0)
        {
            _delayTime = delayTime;
        }

        public virtual void AddEvent(int mainId, int assistId, EventDelegate callback)
        {
            Dictionary<int, List> temp;
            List eventList;
            if (!mainDict.ContainsKey(mainId))
            {
                temp = new Dictionary<int, List>();
                eventList = new List();
                eventList.Add(callback);
                temp.Add(assistId, eventList);
                mainDict.Add(mainId, temp);
            }
            else
            {
                temp = mainDict[mainId];
                if (!temp.ContainsKey(assistId))
                {
                    eventList = new List();
                    eventList.Add(callback);
                    temp.Add(assistId, eventList);
                }
                else
                {
                    eventList = temp[assistId];
                    eventList.Add(callback);
                    temp[assistId] = eventList;
                }
                mainDict[mainId] = temp;
            }

        }

        public void RemoveEvent(int mainId, int assistId)
        {
            if (mainDict.ContainsKey(mainId))
            {
                if (mainDict[mainId].ContainsKey(assistId))
                    mainDict[mainId].Remove(assistId);
            }
        }

        public virtual void DoEvent(int mainId, int assistId, byte[] stream)
        {
            CallData data = new CallData
            {
                _mainId = mainId,
                _assistId = assistId,
                _stream = stream
            };
            callDataList.Enqueue(data);
        }

        public void Update()
        {
            if (callDataList!=null && callDataList.Count > 0)
            {
                CallData data = callDataList.Dequeue();
                Do(data);
            }
        }

        private void Do(CallData data)
        {
            if (mainDict.ContainsKey(data._mainId))
            {
                Dictionary<int, List> temp = mainDict[data._mainId];
                if (temp.ContainsKey(data._assistId))
                {
                    List eventList = temp[data._assistId];
                    foreach (EventDelegate ed in eventList)
                    {
                        ed(data._stream);
                    }
                }
            }
        }

    }

你可能感兴趣的:(C#)