C#- 메시지 대기열 시스템

6131 단어 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);
                    }
                }
            }
        }

    }

좋은 웹페이지 즐겨찾기