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();
}
}