void receiveeventto(object obj) { modelevent me = (modelevent)obj; int command = me.Command; if (command == 0x9c) { if (minport > max) { minport = 33333; } lable999: int temp = minport++; if (mapping(temp, me.Iep)) { me.Iep.Port = temp; me.Iep.Localiep = new IPEndPoint(IPAddress.Parse(loaclip), temp); send(0x98, loaclip + ":" + temp, (IPEndPoint)me.Iep.Iep); } else { goto lable999; } } if (receiveevent != null) { receiveevent(me.Command, me.Data, me.Iep); } }
void receive(object ias) { while (true) { try { IPEndPoint anyEndPoint = new IPEndPoint(IPAddress.Any, 0); EndPoint remoteEndPoint = anyEndPoint; byte[] data = new byte[1024]; int recv = listener.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref remoteEndPoint); int bytesRead = data.Length; byte[] tempbtye = new byte[data.Length]; data.CopyTo(tempbtye, 0); if (tempbtye[0] == 0x99) { NETcollectionUdp nudpa = new NETcollectionUdp(); nudpa.Timeout = DateTime.Now; nudpa.Iep = (IPEndPoint)remoteEndPoint; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(udpup), nudpa); continue; } NETcollectionUdp nudp = new NETcollectionUdp(); nudp.Timeout = DateTime.Now; nudp.Iep = (IPEndPoint)remoteEndPoint; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(udpadd), nudp); labe881: int a = tempbtye[1]; if (a == 0) { continue; } String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); int len = int.Parse(temp); //int b = netc.Buffer[2 + a+1]; //temp = System.Text.ASCIIEncoding.ASCII.GetString(netc.Buffer, 2 + a + 1, b); //len = int.Parse(temp); temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Iep = nudp; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); if (bytesRead > (2 + a + len)) { byte[] b = new byte[bytesRead - (2 + a + len)]; byte[] t = tempbtye; Array.Copy(t, (2 + a + len), b, 0, b.Length); tempbtye = b; bytesRead = bytesRead - (2 + a + len); goto labe881; } } catch { } System.Threading.Thread.Sleep(100); } }
void receiveeventto(object obj) { modelevent me = (modelevent)obj; int command = me.Command; if (command == 0x9c) { // if (minport > max) // minport = 33333; //lable999: // int temp = minport++; // if (mapping(temp, me.Iep)) // { // me.Iep.Port = temp; // me.Iep.Localiep = new IPEndPoint(IPAddress.Parse(loaclip), temp); send(0x98, me.Iep.Iep.ToString() + me.Iep.Iep.Port + "---" + me.Iep.Iep.Address.ToString(), (IPEndPoint)me.Iep.Iep); // } // else goto lable999; } if (receiveevent != null) { receiveevent(me.Command, me.Data, me.Iep); } }
void receiveeventto(object obj) { modelevent me = (modelevent)obj; if (receiveevent != null) { receiveevent(me.Command, me.Data, me.Soc); } }
void receiveeventto(object obj) { modelevent me = (modelevent)obj; //if (me.Command == 0x98) //{ // IPEndPoint iep = new IPEndPoint(IPAddress.Parse(me.Data.Split(':')[0]), int.Parse(me.Data.Split(':')[1])); // System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(xintiao2), iep); //} if (receiveevent != null) { receiveevent(me.Command, me.Data, me.Iep); } }
private void ReadCallback(IAsyncResult ar) { NETcollection netc = (NETcollection)ar.AsyncState; Socket handler = netc.Soc; //if (!netc.Soc.Poll(100, SelectMode.SelectRead)) //{ // listconn.Remove(netc); // return; //} // Read data from the client socket. try { int bytesRead = 0; try { bytesRead = handler.EndReceive(ar); } catch { netc.Soc.Close(); listconn.Remove(netc); } if (bytesRead > 0) { // There might be more data, so store the data received so far. byte[] tempbtye = new byte[bytesRead]; //netc.Buffer.CopyTo(tempbtye, 0); Array.Copy(netc.Buffer, 0, tempbtye, 0, bytesRead); labe881: int a = tempbtye[1]; String temp2 = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); int len = int.Parse(temp2); //int b = netc.Buffer[2 + a+1]; //temp = System.Text.ASCIIEncoding.ASCII.GetString(netc.Buffer, 2 + a + 1, b); //len = int.Parse(temp); String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Soc = netc.Soc; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); if (bytesRead > (2 + a + len)) { byte[] b = new byte[bytesRead - (2 + a + len)]; byte[] t = tempbtye; Array.Copy(t, (2 + a + len), b, 0, b.Length); tempbtye = b; bytesRead = bytesRead - (2 + a + len); goto labe881; } } } catch { } //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc); }
private void packageData(object obj) { NETcollection netc = obj as NETcollection; 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) { int a = tempbtye[1]; if (bytesRead > 2 + a) { String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); int len = int.Parse(temp); if ((len + 2 + a) > tempbtye.Length) { try { 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; } } 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 { temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); } catch { } try { modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Soc = netc.Soc; if (receiveevent != null) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); } //receiveeventto(me); //if (receiveevent != null) // receiveevent.BeginInvoke(tempbtye[0], temp, netc.Soc, null, null); //if (ListData.Count > 0) ListData.RemoveAt(i); netc.Ispage = false; return; } catch (Exception e) { 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; } } } } catch (Exception e) { if (netc.Datalist.Count > 0) { netc.Datalist.RemoveAt(0); } netc.Ispage = false; return; } finally { netc.Ispage = false; } }
private void ReadCallback(IAsyncResult ar) { NETcollection netc = (NETcollection)ar.AsyncState; Socket handler = netc.Soc; //if (!netc.Soc.Poll(100, SelectMode.SelectRead)) //{ // listconn.Remove(netc); // return; //} // Read data from the client socket. try { int bytesRead = 0; try { bytesRead = handler.EndReceive(ar); } catch { netc.Soc.Close(); listconn.Remove(netc); } if (bytesRead > 0) { // There might be more data, so store the data received so far. byte[] tempbtye = new byte[bytesRead]; //netc.Buffer.CopyTo(tempbtye, 0); Array.Copy(netc.Buffer, 0, tempbtye, 0, bytesRead); tempbtye = AnalyticData(tempbtye, bytesRead); bytesRead = tempbtye.Length; labe881: int a = tempbtye[1]; String temp2 = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); int len = int.Parse(temp2); //int b = netc.Buffer[2 + a+1]; //temp = System.Text.ASCIIEncoding.ASCII.GetString(netc.Buffer, 2 + a + 1, b); //len = int.Parse(temp); String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Soc = netc.Soc; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); if (bytesRead > (2 + a + len)) { byte[] b = new byte[bytesRead - (2 + a + len)]; byte[] t = tempbtye; Array.Copy(t, (2 + a + len), b, 0, b.Length); tempbtye = b; bytesRead = bytesRead - (2 + a + len); goto labe881; } } } catch { } //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc); }
void receive(object ias) { while (true) { try { IPEndPoint anyEndPoint = new IPEndPoint(IPAddress.Any, 0); EndPoint remoteEndPoint = anyEndPoint; byte[] data = new byte[1024]; int recv = listener.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref remoteEndPoint); int bytesRead = data.Length; byte[] tempbtye = new byte[data.Length]; data.CopyTo(tempbtye, 0); if (tempbtye[0] == 0x99) { NETcollectionUdp nudpa = new NETcollectionUdp(); nudpa.Timeout = DateTime.Now; nudpa.Iep = (IPEndPoint)remoteEndPoint; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(udpup), nudpa); continue; } NETcollectionUdp nudp = new NETcollectionUdp(); nudp.Timeout = DateTime.Now; nudp.Iep = (IPEndPoint)remoteEndPoint; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(udpadd), nudp); labe881: int a = tempbtye[1]; if (a == 0) continue; String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a); int len = int.Parse(temp); //int b = netc.Buffer[2 + a+1]; //temp = System.Text.ASCIIEncoding.ASCII.GetString(netc.Buffer, 2 + a + 1, b); //len = int.Parse(temp); temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len); modelevent me = new modelevent(); me.Command = tempbtye[0]; me.Data = temp; me.Iep = nudp; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me); if (bytesRead > (2 + a + len)) { byte[] b = new byte[bytesRead - (2 + a + len)]; byte[] t = tempbtye; Array.Copy(t, (2 + a + len), b, 0, b.Length); tempbtye = b; bytesRead = bytesRead - (2 + a + len); goto labe881; } } catch { } System.Threading.Thread.Sleep(100); } }
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) { WebSocketServer.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; } }