private void TimeTick() { List <string> timeoutNeedRemove = new List <string>(); while (true) { { foreach (var item in m_RpcMap) { RPC rpc = item.Value; if (rpc.isTimeout()) { rpc.state = RpcState.Timeout; if (m_RpcUIMap.ContainsKey(rpc.uniqueName)) { RpcRequestUIData uiData = m_RpcUIMap[rpc.uniqueName]; if (uiData.needRetry) { App.Manager.Thread.RunOnMainThread(() => { Common.UI.OpenRetry(); }); } } if (rpc.autoRetry) { Resend(rpc); } } if (rpc.state == RpcState.Timeout) { timeoutNeedRemove.Add(item.Key); } } //清除无效的超时Rpc for (int i = 0; i < timeoutNeedRemove.Count; i++) { lock (m_RpcMap){ m_RpcMap.Remove(timeoutNeedRemove[i]); } if (m_RpcUIMap.ContainsKey(timeoutNeedRemove[i])) { lock (m_RpcUIMap){ m_RpcUIMap.Remove(timeoutNeedRemove[i]); } } } timeoutNeedRemove.Clear(); } Thread.Sleep(1000); } }
private void DealSocketData(byte[] receiveData) { SocketData socketData = ParseSocketBinaryData(receiveData); _Response response = _Response.Parser.ParseFrom(socketData.data); Assembly assem = response.GetType().Assembly; RPC currentRpc = GetRpcData(response.Rpc, response.Unique); if (currentRpc != null) { if (currentRpc.isTimeout()) //已经超时的不处理 { lock (m_RpcMap) { m_RpcMap.Remove(currentRpc.uniqueName); } return; } if (response.Token != "") { m_Token = response.Token; //统一身份标识 } if (response.Code != 0) //处理ErrorCode { if (m_RpcUIMap.ContainsKey(currentRpc.uniqueName)) { RpcRequestUIData uiData = m_RpcUIMap[currentRpc.uniqueName]; if (uiData.needErrorCodeAlert) { App.Manager.Thread.RunOnMainThread(() => { Debugger.Log("Receive Rpc:" + response.Code); }); if (App.Manager.Network.HasRegistedErrorCode(response.Code)) { App.Manager.Thread.RunOnMainThread(() => { Common.UI.CloseWaiting(); App.Manager.Network.DoErrorCode(response.Code); }); } else { App.Manager.Thread.RunOnMainThread(() => { Common.UI.CloseWaiting(); Common.UI.OpenAlert("错误", "ErrorCode:" + response.Code, "确认", null); }); } } if (uiData.needWaitingUI) { m_waitUICount--; } if (m_waitUICount <= 0) { App.Manager.Thread.RunOnMainThread(() => { Common.UI.CloseWaiting(); }); } lock (m_RpcUIMap) { m_RpcUIMap.Remove(currentRpc.uniqueName); } } lock (m_RpcMap) { m_RpcMap.Remove(currentRpc.uniqueName); } } else { Type type = assem.GetType(Config.RpcNamespace + "." + response.Rpc + "Response");//这里要按规则来!!!! if (type != null) { IMessage resRpc = Activator.CreateInstance(type) as IMessage; resRpc.MergeFrom(response.Data); App.Manager.Thread.RunOnMainThread(() => { currentRpc.callback(resRpc); }); if (m_RpcUIMap.ContainsKey(currentRpc.uniqueName)) { RpcRequestUIData uiData = m_RpcUIMap[currentRpc.uniqueName]; if (uiData.needWaitingUI) { m_waitUICount--; } if (m_waitUICount <= 0) { App.Manager.Thread.RunOnMainThread(() => { Common.UI.CloseWaiting(); }); } lock (m_RpcUIMap) { m_RpcUIMap.Remove(currentRpc.uniqueName); } } lock (m_RpcMap) { m_RpcMap.Remove(currentRpc.uniqueName); } } } } else { Type type = assem.GetType(Config.RpcNamespace + "." + response.Rpc + "Push");//这里要按Push规则来!!!! if (type != null) { IMessage resRpc = Activator.CreateInstance(type) as IMessage; resRpc.MergeFrom(response.Data); App.Manager.Thread.RunOnMainThread(() => { Debugger.Warn("RecievePush:" + response.Rpc); m_Listener.Invoke(response.Rpc, resRpc);//分发给注册的Listener }); } } }