public byte[] GetBytes(DataFrameHeader _header, byte[] _extend, byte[] _mask, byte[] _content) { byte[] buffer = new byte[2 + _extend.Length + _mask.Length + _content.Length]; Buffer.BlockCopy(_header.GetBytes(), 0, buffer, 0, 2); Buffer.BlockCopy(_extend, 0, buffer, 2, _extend.Length); Buffer.BlockCopy(_mask, 0, buffer, 2 + _extend.Length, _mask.Length); Buffer.BlockCopy(_content, 0, buffer, 2 + _extend.Length + _mask.Length, _content.Length); return(buffer); }
/// <summary> /// 打包服务器数据 /// </summary> /// <param name="message">数据</param> /// <returns>数据包</returns> private byte[] PackData(byte[] buffer) { byte[] _extend = new byte[0]; byte[] _mask = new byte[0]; byte[] _content = buffer; //帧头 DataFrameHeader _header = new DataFrameHeader(buffer); int length = _content.Length; if (length < 126) { _extend = new byte[0]; _header = new DataFrameHeader(true, false, false, false, 2, false, length); } else if (length < 65536) { _extend = new byte[2]; _header = new DataFrameHeader(true, false, false, false, 2, false, 126); _extend[0] = (byte)(length / 256); _extend[1] = (byte)(length % 256); } else { _extend = new byte[8]; _header = new DataFrameHeader(true, false, false, false, 2, false, 127); int left = length; int unit = 256; for (int i = 7; i > 1; i--) { _extend[i] = (byte)(left % unit); left = left / unit; if (left == 0) { break; } } } return(GetBytes(_header, _extend, _mask, _content)); }
public byte[] GetBytes(DataFrameHeader _header,byte[] _extend, byte[] _mask, byte[] _content) { byte[] buffer = new byte[2 + _extend.Length + _mask.Length + _content.Length]; Buffer.BlockCopy(_header.GetBytes(), 0, buffer, 0, 2); Buffer.BlockCopy(_extend, 0, buffer, 2, _extend.Length); Buffer.BlockCopy(_mask, 0, buffer, 2 + _extend.Length, _mask.Length); Buffer.BlockCopy(_content, 0, buffer, 2 + _extend.Length + _mask.Length, _content.Length); return buffer; }
/// <summary> /// 打包服务器数据 /// </summary> /// <param name="message">数据</param> /// <returns>数据包</returns> private byte[] PackData(byte[] buffer) { byte[] _extend = new byte[0]; byte[] _mask = new byte[0]; byte[] _content = buffer; //帧头 DataFrameHeader _header = new DataFrameHeader(buffer); int length = _content.Length; if (length < 126) { _extend = new byte[0]; _header = new DataFrameHeader(true, false, false, false, 2, false, length); } else if (length < 65536) { _extend = new byte[2]; _header = new DataFrameHeader(true, false, false, false, 2, false, 126); _extend[0] = (byte)(length / 256); _extend[1] = (byte)(length % 256); } else { _extend = new byte[8]; _header = new DataFrameHeader(true, false, false, false, 2, false, 127); int left = length; int unit = 256; for (int i = 7; i > 1; i--) { _extend[i] = (byte)(left % unit); left = left / unit; if (left == 0) break; } } return GetBytes(_header, _extend, _mask, _content); }
private void packageData(object obj) { NETcollection netc = obj as NETcollection; 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; } labe881: if (tempbtye.Length > 0) { #region MyRegion String temp = ""; int a = tempbtye[1]; if (bytesRead > 2 + a) { int len = 0; if (DT == DataType.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 == DataType.json) { temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); } } else { len = tempbtye.Length - 2 - a; if (DT == DataType.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 == DataType.json) { modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Soc = netc.Soc; me.Masks = masks; //System.Threading.Thread t = new Thread(new ParameterizedThreadStart(receiveeventto)); //t.Start(me); //receiveeventto(me); if (receiveevent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); } //if (receiveevent != null) // receiveevent(me.Command, me.Data, me.Soc); } else if (DT == DataType.bytes) { byte[] bs = new byte[len - (2 + a)]; Array.Copy(tempbtye, 2 + a, bs, 0, bs.Length); modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = ""; me.Databit = bs; me.Soc = netc.Soc; if (receiveeventbit != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventtobit), me); } } netc.Ispage = false; return; } catch (Exception e) { netc.Ispage = false; return; } } #endregion } } // } } catch (Exception ex) { if (netc.Datalist.Count > 0) { netc.Datalist.RemoveAt(0); } netc.Ispage = false; return; } }
String combine(string temp, byte[] tempbtyes, List <byte[]> ListData) { DataFrameHeader dfh = null; // Array.Copy(ListData[0], tempbtyes, tempbtyes.Length); byte[] masks = new byte[4]; int lens = 0; int paylen = 0; byte[] tempbtye = null; DataFrame df = new DataFrame(); // AnalyticData(tempbtyes, bytesRead, ref masks, ref lens, ref paylen); try { tempbtye = df.GetData(tempbtyes, ref masks, ref lens, ref paylen, ref dfh); } catch { if (paylen > tempbtyes.Length) { ListData.RemoveAt(0); byte[] temps = new byte[tempbtyes.Length]; Array.Copy(tempbtyes, temps, temps.Length); tempbtyes = new byte[temps.Length + ListData[0].Length]; Array.Copy(temps, tempbtyes, temps.Length); Array.Copy(ListData[0], 0, tempbtyes, temps.Length, ListData[0].Length); ListData[0] = tempbtyes; temp = combine(temp, ListData[0], ListData); return(temp); } } try { temp += System.Text.Encoding.UTF8.GetString(tempbtye); if (ListData[0].Length > tempbtye.Length + lens) { int aa = ListData[0].Length - (tempbtye.Length + lens); byte[] temptt = new byte[aa]; Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length); ListData[0] = temptt; } else { ListData.RemoveAt(0); } if (!dfh.FIN) { while (!(ListData.Count > 0)) { System.Threading.Thread.Sleep(100); } temp = combine(temp, ListData[0], ListData); } } catch (Exception e) { } return(temp); }