private void packageData(object obj) { WeaveNetWorkItems netc = obj as WeaveNetWorkItems; List <byte[]> ListData = netc.DataList; try { int i = 0; int count = ListData.Count; if (count > 0) { int bytesRead = ListData[i] != null ? ListData[i].Length : 0; if (bytesRead == 0) { if (ListData.Count > 0) { ListData.RemoveAt(0); } netc.IsPage = false; return; } ; byte[] tempbtye = new byte[bytesRead]; Array.Copy(ListData[i], tempbtye, tempbtye.Length); if (bytesRead > 2) { netc.Lasttime = DateTime.Now; int a = tempbtye[1]; if (bytesRead > 2 + a) { int len = 0; if (weaveDataType == WeaveDataTypeEnum.Bytes) { byte[] bb = new byte[a]; Array.Copy(tempbtye, 2, bb, 0, a); len = ConvertToInt(bb); } else { String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); len = int.Parse(temp); } if ((len + 2 + a) > tempbtye.Length) { try { if (ListData.Count > 1) { ListData.RemoveAt(i); byte[] temps = new byte[tempbtye.Length]; Array.Copy(tempbtye, temps, temps.Length); byte[] tempbtyes = new byte[temps.Length + ListData[i].Length]; Array.Copy(temps, tempbtyes, temps.Length); Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length); ListData[i] = tempbtyes; } } catch { } netc.IsPage = false; return; } else if (tempbtye.Length > (len + 2 + a)) { try { byte[] temps = new byte[tempbtye.Length - (len + 2 + a)]; Array.Copy(tempbtye, (len + 2 + a), temps, 0, temps.Length); ListData[i] = temps; } catch { } // netc.Ispage = false; return; } else if (tempbtye.Length == (len + 2 + a)) { if (ListData.Count > 0) { ListData.RemoveAt(i); } } try { if (weaveDataType == WeaveDataTypeEnum.Json) { String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = temp; me.Ep = netc.Ep; if (waveReceiveEvent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveEventHander), me); } //receiveeventto(me); //if (receiveevent != null) // receiveevent.BeginInvoke(tempbtye[0], temp, netc.Soc, null, null); //if (ListData.Count > 0) ListData.RemoveAt(i); } else if (weaveDataType == WeaveDataTypeEnum.Bytes) { // temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); byte[] bs = new byte[len]; Array.Copy(tempbtye, (2 + a), bs, 0, bs.Length); WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = ""; me.Databit = bs; me.Ep = netc.Ep; if (weaveReceiveBitEvent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveBitEventHander), me); } } netc.IsPage = false; return; } catch { netc.IsPage = false; return; } } else { if (ListData.Count > 0) { ListData.RemoveAt(i); byte[] temps = new byte[tempbtye.Length]; Array.Copy(tempbtye, temps, temps.Length); byte[] tempbtyes = new byte[temps.Length + ListData[i].Length]; Array.Copy(temps, tempbtyes, temps.Length); Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length); ListData[i] = tempbtyes; } netc.IsPage = false; return; } } else { try { if (ListData.Count > 1) { ListData.RemoveAt(i); byte[] temps = new byte[tempbtye.Length]; Array.Copy(tempbtye, temps, temps.Length); byte[] tempbtyes = new byte[temps.Length + ListData[i].Length]; Array.Copy(temps, tempbtyes, temps.Length); Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length); ListData[i] = tempbtyes; } } catch { } netc.IsPage = false; return; } } } catch { if (netc.DataList.Count > 0) { netc.DataList.RemoveAt(0); } netc.IsPage = false; return; } finally { netc.IsPage = false; } }
void ReceiveBitEventHander(object obj) { WeaveEvent me = (WeaveEvent)obj; weaveReceiveBitEvent(me.Command, me.Databit, me.Ep); }
public byte[] packageDatabtye(byte[] alldata, Socket soc, WaitCallback ReceiveBitEventHandercback, SslStream ssl, WaitCallback ReceiveBitEventHandercbackssl) { try { //int i = 0; //int count = ListData.Count; // if (count > 0) { int bytesRead = alldata.Length; if (bytesRead == 0) { // if (ListData.Count > 0) ListData.RemoveAt(0); return(alldata); } ; byte[] tempbtye = new byte[bytesRead]; Buffer.BlockCopy(alldata, 0, tempbtye, 0, tempbtye.Length); if (bytesRead > 2) { int a = tempbtye[1]; if (a == 0) { byte[] temps = new byte[tempbtye.Length - 2]; Buffer.BlockCopy(tempbtye, 2, temps, 0, temps.Length); alldata = temps; Console.WriteLine("a == 0"); return(alldata); } else if (bytesRead > 4 + a) { int len = 0; byte[] bbcrc = new byte[4 + a]; Buffer.BlockCopy(tempbtye, 0, bbcrc, 0, 4 + a); if (CRC.DataCRC(ref bbcrc, 4 + a)) { byte[] bb = new byte[a]; Buffer.BlockCopy(tempbtye, 2, bb, 0, a); len = ConvertToInt(bb); } else { byte[] temps = new byte[tempbtye.Length - 1]; Buffer.BlockCopy(tempbtye, 1, temps, 0, temps.Length); alldata = temps; Console.WriteLine("DataCRC == false"); return(alldata); } if ((len + 4 + a) > tempbtye.Length) { return(alldata); } else if (tempbtye.Length > (len + 4 + a)) { try { byte[] temps = new byte[tempbtye.Length - (len + 4 + a)]; Buffer.BlockCopy(tempbtye, (len + 4 + a), temps, 0, temps.Length); alldata = temps; } catch (Exception e) { Console.WriteLine("tempbtye.Length > (len + 4 + a):" + e.Message); return(alldata); } //netc.IsPage = false; return; } else if (tempbtye.Length == (len + 4 + a)) { alldata = new byte[0]; } try { // temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); byte[] bs = new byte[len]; Buffer.BlockCopy(tempbtye, (4 + a), bs, 0, bs.Length); WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = ""; me.Databit = bs; me.Soc = soc; me.Ssl = ssl; if (ssl == null) { // Task.Run(() => { ReceiveBitEventHandercback(me); }); System.Threading.ThreadPool.UnsafeQueueUserWorkItem(ReceiveBitEventHandercback, me); } else { System.Threading.ThreadPool.UnsafeQueueUserWorkItem(ReceiveBitEventHandercbackssl, me); } // if (weaveReceiveBitEvent != null) // System.Threading.ThreadPool.QueueUserWorkItem(ReceiveBitEventHandercback, me); //weaveReceiveBitEvent?.Invoke(tempbtye[0], bs, soc); // weaveReceiveBitEvent?.BeginInvoke(tempbtye[0], bs, soc,null,null); return(alldata); } catch (Exception e) { Console.WriteLine("ee:" + e.Message); // netc.IsPage = false; return(new byte[0]); } } else { return(alldata); } } else { return(alldata); } } } catch (Exception ee) { Console.WriteLine("all:" + ee.Message); return(new byte[0]); } }
/// <summary> /// 对粘包,分包的处理方法 /// </summary> /// <param name="alldata"></param> /// <param name="soc"></param> /// <param name="ReceiveBitEventHandercback"></param> /// <param name="ssl"></param> /// <param name="ReceiveBitEventHandercbackssl"></param> public byte[] packageDatajson(byte[] alldata, Socket soc, WaitCallback ReceiveBitEventHandercback, SslStream ssl, WaitCallback ReceiveBitEventHandercbackssl) { try { { lb1122: int bytesRead = alldata.Length; if (bytesRead == 0) { return(alldata); } ; byte[] tempbtye = new byte[bytesRead]; Buffer.BlockCopy(alldata, 0, tempbtye, 0, tempbtye.Length); if (bytesRead > 2) { int a = tempbtye[1]; if (a == 0) { byte[] temps = new byte[tempbtye.Length - 2]; Buffer.BlockCopy(tempbtye, 2, temps, 0, temps.Length); alldata = temps; goto lb1122; } else if (bytesRead > 2 + a) { int len = 0; String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); len = int.Parse(temp); if ((len + 2 + a) > tempbtye.Length) { return(alldata); } else if (tempbtye.Length > (len + 2 + a)) { try { byte[] temps = new byte[tempbtye.Length - (len + 2 + a)]; Buffer.BlockCopy(tempbtye, (len + 2 + a), temps, 0, temps.Length); alldata = temps; } catch { return(alldata); } //netc.IsPage = false; return; } else if (tempbtye.Length == (len + 2 + a)) { alldata = new byte[0]; } try { String temp2 = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = temp2; me.Soc = soc; me.Ssl = ssl; //if (waveReceiveEvent != null) // waveReceiveEvent?.Invoke(tempbtye[0], temp, soc); if (ssl == null) { System.Threading.ThreadPool.QueueUserWorkItem(ReceiveBitEventHandercback, me); } else { System.Threading.ThreadPool.QueueUserWorkItem(ReceiveBitEventHandercbackssl, me); } //receiveeventto(me); //if (receiveevent != null) // waveReceiveEvent.BeginInvoke(tempbtye[0], temp, soc, null, null); //if (ListData.Count > 0) ListData.RemoveAt(i); return(alldata); } catch //(Exception e) { // netc.IsPage = false; return(new byte[0]); } } else { return(alldata); } } else { return(alldata); } } } catch { return(new byte[0]); } }
private void packageData(object obj) { WeaveNetWorkItems netc = obj as WeaveNetWorkItems; try { // Array.Copy(netc.Datalist, ListData, count); //while (true) //{ int count = netc.DataList.Count; List <Byte[]> ListData = netc.DataList; int i = 0; if (netc.DataList.Count > 0) { DataFrameHeader dfh = null; int bytesRead = ListData[i] != null ? ListData[i].Length : 0; if (bytesRead == 0) { if (ListData.Count > 0) { ListData.RemoveAt(0); } netc.IsPage = false; return; } ; byte[] tempbtyes = new byte[bytesRead]; Array.Copy(ListData[i], tempbtyes, tempbtyes.Length); byte[] masks = new byte[4]; int lens = 0; int paylen = 0; byte[] tempbtye = null; try { DataFrame df = new DataFrame(); // AnalyticData(tempbtyes, bytesRead, ref masks, ref lens, ref paylen); tempbtye = df.GetData(tempbtyes, ref masks, ref lens, ref paylen, ref dfh); if (dfh.OpCode != 2) { ListData.RemoveAt(i); netc.IsPage = false; return; } } catch { if (paylen > bytesRead) { ListData.RemoveAt(i); byte[] temps = new byte[tempbtyes.Length]; Array.Copy(tempbtyes, temps, temps.Length); tempbtyes = new byte[temps.Length + ListData[i].Length]; Array.Copy(temps, tempbtyes, temps.Length); Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length); ListData[i] = tempbtyes; } else { ListData.RemoveAt(i); } netc.IsPage = false; return; } if (tempbtye == null) { netc.IsPage = false; return; } if (tempbtye.Length > 0) { #region MyRegion String temp = ""; int a = tempbtye[1]; if (bytesRead > 2 + a) { int len = 0; if (DT == WeaveDataTypeEnum.Bytes) { byte[] bb = new byte[a]; Array.Copy(tempbtye, 2, bb, 0, a); len = ConvertToInt(bb); } else { temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); try { len = int.Parse(temp); } catch { if (bytesRead > tempbtye.Length + lens) { int aa = bytesRead - (tempbtye.Length + lens); byte[] temptt = new byte[aa]; Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length); ListData[i] = temptt; netc.IsPage = false; return; } } } if (tempbtye.Length == (len + 2 + a)) { if (bytesRead > tempbtye.Length + lens) { int aa = bytesRead - (tempbtye.Length + lens); byte[] temptt = new byte[aa]; Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length); ListData[i] = temptt; } else if (bytesRead < tempbtye.Length + lens) { } else { ListData.RemoveAt(i); } if (DT == WeaveDataTypeEnum.Json) { temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); } } else { len = tempbtye.Length - 2 - a; if (DT == WeaveDataTypeEnum.Json) { temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); } if (bytesRead > tempbtye.Length + lens) { int aa = bytesRead - (tempbtye.Length + lens); byte[] temptt = new byte[aa]; Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length); ListData[i] = temptt; temp = combine(temp, temptt, ListData); } else { ListData.RemoveAt(i); while (!(ListData.Count > 0)) { System.Threading.Thread.Sleep(100); } temp = combine(temp, ListData[i], ListData); } // netc.Ispage = false; return; } try { if (DT == WeaveDataTypeEnum.Json) { WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = temp; me.Soc = netc.SocketSession; me.Masks = masks; //System.Threading.Thread t = new Thread(new ParameterizedThreadStart(receiveeventto)); //t.Start(me); //receiveeventto(me); if (waveReceiveEvent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveToEventHander), me); } //if (receiveevent != null) // receiveevent(me.Command, me.Data, me.Soc); } else if (DT == WeaveDataTypeEnum.Bytes) { byte[] bs = new byte[len - (2 + a)]; Array.Copy(tempbtye, 2 + a, bs, 0, bs.Length); WeaveEvent me = new WeaveEvent(); me.Command = tempbtye[0]; me.Data = ""; me.Databit = bs; me.Soc = netc.SocketSession; if (weaveReceiveBitEvent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveToBitEventHander), me); } } netc.IsPage = false; return; } catch (Exception ex) { string ems = ex.Message; netc.IsPage = false; return; } } #endregion } } // } } catch (Exception ex) { string ems = ex.Message; if (netc.DataList.Count > 0) { netc.DataList.RemoveAt(0); } netc.IsPage = false; return; } }
void ReceiveToBitEventHander(object obj) { WeaveEvent me = (WeaveEvent)obj; weaveReceiveBitEvent?.Invoke(me.Command, me.Databit, me.Soc); }
void ReceiveEventHander(object obj) { WeaveEvent me = (WeaveEvent)obj; WaveReceiveEvent(me.Command, me.Data, me.Ep); }