C# 事件系统

   public class CallBack
    {
        public double _time;
        public bool _dirty;
        public Action<JsonObject> _action;

        public CallBack()
        {
            _time = 0f;
            _dirty = false;
            _action = null;
        }

        public CallBack(Action<JsonObject> action_)
        {
            _time = Clock.GetCurrenSeconds;
            _dirty = false;
            _action = action_;
        }

        public bool CheckExpire(Int64 now)
        {
            if (_time + 15 < now)
            {
                _action = null;
                _dirty = true;
            }
            return _dirty;
        }

        public bool Expire()
        {
            return _dirty;
        }
    }

    public class EventManager : IDisposable
    {
        private object locker = new object();
        private Dictionary<uint, CallBack> callBackMap;           //ConcurrentDictionary
        private Dictionary<string, List<Action<JsonObject>>> eventMap;
        private int num = 0;
        private Dictionary<uint, double> callBackTimes;
        private Timer callBackTimer;

        public EventManager()
        {
            this.callBackMap = new Dictionary<uint, CallBack>();
            this.eventMap = new Dictionary<string, List<Action<JsonObject>>>();
            callBackTimer = new Timer();
            callBackTimer.Interval = 2000f;
            callBackTimer.Elapsed += new ElapsedEventHandler(checkCallBackTimeOut);
            callBackTimer.Enabled = true;
        }

        public void checkCallBackTimeOut(object source, ElapsedEventArgs e)
        {
            Int64 now = Clock.GetCurrenSeconds;
            lock (locker)
            {
                List<uint> expires = new List<uint>();
                foreach (KeyValuePair<uint, CallBack> kv in callBackMap)
                {
                    if (kv.Value.CheckExpire(now))
                    {
                        expires.Add(kv.Key);
                    }
                }
                for (int i = 0; i < expires.Count; ++i)
                {
                    CallBack cb = callBackMap[expires[i]];
                    callBackMap.Remove(expires[i]);
                    cb = null;
                }
            }
        }

        //Adds callback to callBackMap by id.
        public void AddCallBack(uint id, Action<JsonObject> callback)
        {
            lock (locker)
            {
                if (id > 0 && callback != null && !this.callBackMap.ContainsKey(id))
                {
                    this.callBackMap[id] = new CallBack(callback);
                }
            }
        }

        public CallBack DelCallBack(uint id)
        {
            lock (locker)
            {
                if (!callBackMap.ContainsKey(id))
                    return null;
                CallBack cb = callBackMap[id];
                callBackMap.Remove(id);
                return cb;
            }
        }

        /// <summary>
        /// Invoke the callback when the server return messge .
        /// </summary>
        /// <param name='pomeloMessage'>
        /// Pomelo message.
        /// </param>
        public void InvokeCallBack(uint id, JsonObject data)
        {
            CallBack cb = DelCallBack(id);
            if (cb != null)
            {
                cb._action.Invoke(data);
                cb = null;
            }
        }

        //Adds the event to eventMap by name.
        public void AddOnEvent(string eventName, Action<JsonObject> callback)
        {
            List<Action<JsonObject>> list = null;
            if (this.eventMap.TryGetValue(eventName, out list))
            {
                //Debuger.Log("RegisterEventName:" + eventName + "  contains:" + list.Contains(callback));
                if (!list.Contains(callback))
                    list.Add(callback);
            }
            else
            {
                //Debuger.Log("RegisterEventName:" + eventName + "  New Event");
                list = new List<Action<JsonObject>>();
                list.Add(callback);
                this.eventMap.Add(eventName, list);
            }
        }

        /// <summary>
        /// If the event exists,invoke the event when server return messge.
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        ///
        public void InvokeOnEvent(string route, JsonObject msg)
        {
            if (!this.eventMap.ContainsKey(route)) return;
            if ((string.Equals(route, NotifyPro.ON_ADDFISH) || string.Equals(route, NotifyPro.ON_OTHERSHOOT) || string.Equals(route, NotifyPro.ON_ClearFISH)) && Main.Instance().IsPause)
            {
                return;
            }
            List<Action<JsonObject>> list = eventMap[route];
            foreach (Action<JsonObject> action in list) action.Invoke(msg);
        }

        // Dispose() calls Dispose(true)
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected void Dispose(bool disposing)
        {
            this.callBackMap.Clear();
            this.eventMap.Clear();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值