// Adds callback to callBackMap by id. public void AddCallBack(uint id, Action <StatusCode, JsonObject> callback) { if (id > 0 && callback != null) { PackageCallBack packageCallBack = new PackageCallBack(); packageCallBack.CallBack = callback; packageCallBack.SendTime = DateTime.Now; this.callBackMap.Add(id, packageCallBack); } }
// Invoke the callback when the server return messge . public void InvokeCallBack(uint id, StatusCode code, JsonObject data) { if (!this.callBackMap.ContainsKey(id)) { return; } PackageCallBack packageCallback = this.callBackMap[id]; this.callBackMap.Remove(id); packageCallback.CallBack(code, data); }
private void ProcessTimeOut(object source, ElapsedEventArgs e) { if (this.callBackMap.Count <= 0) { return; } List <uint> listKey = new List <uint>(this.callBackMap.Keys); for (int i = 0; i < listKey.Count; i++) { uint reqId = listKey[i]; PackageCallBack packageCallback = this.callBackMap[reqId]; if (DateTime.Now.Subtract(packageCallback.SendTime).TotalSeconds > TimeoutSec) { this.InvokeCallBack(reqId, StatusCode.TIMEOUT, null); } } }