private byte[] HandleReadBuffer(byte[] data, string MsgID, T token) { //base.LogTool.Log("接受组合数据包:" + MsgID + " " + data[0].ToString() + " " + data.Length); if (data[0] > 0 && !string.IsNullOrEmpty(MsgID)) { if (redContactDataBufferKeys.Contains(MsgID)) { UpdateDataSortModel(data, MsgID, token); } else { try { DataSortModel model = new DataSortModel(); model.MsgID = MsgID; model.AllCount = data[0]; model.UpdateTime = DateTime.Now.ToString(CommonFlag.F_DateTimeFormat); model.DataWithServers = new Dictionary <byte, byte[]>(); model.DataWithServersKeys = new List <byte>(); model.AsyncFlag = new Stack <bool>(); model.AsyncFlag.Push(true); model.DataWithServersKeys.Add(data[1]); model.DataWithServers.Add(data[1], data.Skip(2).ToArray()); //双保险 redContactDataBufferconfic1.Add(MsgID, false); redContactDataBufferconfic2.Add(MsgID, false); if (!redContactDataBufferKeys.Contains(MsgID)) { redContactDataBuffer.Add(model); redContactDataBufferKeys.Add(MsgID); redContactDataBufferconfic1.Remove(MsgID); redContactDataBufferconfic2.Remove(MsgID); } else { UpdateDataSortModel(data, MsgID, token); } } catch (ArgumentException ex) { UpdateDataSortModel(data, MsgID, token); } } } return(null); }
private void HandlerReadBuffer() { while (IsRunning) { if (redContactDataBufferKeys.Count > 0) { int i = redContactDataBuffer.Count - 1; while (i >= 0) { DataSortModel item = redContactDataBuffer[i]; if (item.IsDelete) { redContactDataBuffer.Remove(item); i--; continue; } string now = DateTime.Now.AddSeconds(-reciveWait).ToString(CommonFlag.F_DateTimeFormat); if (item.UpdateTime.CompareTo(now) < 0) { string tmpstr = ""; for (int j = 0; j < item.DataWithServersKeys.Count; j++) { tmpstr = tmpstr + item.DataWithServersKeys[j].ToString() + ","; } //base.LogTool.Log("未接受成功的数据 item.MsgID:" + item.MsgID + " item.AllCount:" + item.AllCount + " item.DataWithServersKeys.Count:" + item.DataWithServersKeys.Count // + " item.DataWithServers.Count:" + item.DataWithServers.Count + " tmpstr:" + tmpstr); redContactDataBuffer.Remove(item); redContactDataBufferKeys.Remove(item.MsgID); } i--; } } Thread.Sleep(sendContactDataDelay); } }
private void UpdateDataSortModel(byte[] data, string MsgID, T token) { int retrycount = 0; int index = redContactDataBufferKeys.IndexOf(MsgID); while (index < 0 && retrycount <= 100) { index = redContactDataBufferKeys.IndexOf(MsgID); retrycount++; } retrycount = 0; DataSortModel model = null; DataSortModel tempModel = null; while (retrycount <= 100 && model == null) { try { tempModel = redContactDataBuffer[index]; if (MsgID == tempModel.MsgID) { model = tempModel; } else { index = 0; while (index < redContactDataBuffer.Count) { tempModel = redContactDataBuffer[index]; if (MsgID == tempModel.MsgID) { model = tempModel; break; } } } break; } catch (Exception ex) { //index--; //已被其它进程删除 往下找 retrycount++; } } if (model != null) { model.UpdateTime = DateTime.Now.ToString(CommonFlag.F_DateTimeFormat); if (!model.DataWithServersKeys.Contains(data[1])) { try { model.DataWithServers.Add(data[1], data.Skip(2).ToArray()); model.DataWithServersKeys.Add(data[1]); //base.LogTool.Log("当前数量:" + MsgID + " " + model.DataWithServers.Count); if (model.DataWithServers.Count == model.AllCount) { if (model.AsyncFlag.Pop()) { byte[] tempData = null; for (byte i = 1; i < model.AllCount; i++) { tempData = tempData.Combine(model.DataWithServers[i]); } redContactDataBufferKeys.Remove(model.MsgID); model.IsDelete = true; HandleRecivedMessage(tempData, token); } } } catch (ArgumentException ex) { } } } //else //{ // base.LogTool.Log("我擦,还是没有!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //} }