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);
            }
        }
Beispiel #2
0
 void udp_receiveevent(byte command, string data, NETcollectionUdp NETc)
 {
     if (NATthroughevent != null)
     {
         NATthroughevent(command, data, NETc.Iep);
     }
 }
 void udp_receiveevent(byte command, string data, NETcollectionUdp NETc)
 {
      
         
         if (NATthroughevent != null)
             NATthroughevent(data, NETc.Iep);
   
 }
 public NETcollectionUdp[] getUdpList()
 {
     try
     {
         NETcollectionUdp[] netlist = new NETcollectionUdp[listconn.Count];
         listconn.CopyTo(netlist);
         return(netlist);
     }
     catch { return(null); }
 }
        private void udpup(object state)
        {
            NETcollectionUdp nudp = (NETcollectionUdp)state;

            try
            {
                foreach (NETcollectionUdp temnudp in listconn)
                {
                    if (temnudp != null)
                    {
                        if (temnudp.Iep.ToString() == nudp.Iep.ToString())
                        {
                            temnudp.Timeout = DateTime.Now;
                        }
                    }
                }
            }
            catch { }
        }
        private void udpadd(object state)
        {
            NETcollectionUdp nudp = (NETcollectionUdp)state;

            try
            {
                NETcollectionUdp[] netlist = getUdpList();
                foreach (NETcollectionUdp temnudp in netlist)
                {
                    if (temnudp != null)
                    {
                        if (temnudp.Iep == nudp.Iep)
                        {
                            return;
                        }
                    }
                }
                listconn.Add(nudp);
            }
            catch { }
        }
 public bool mapping(int port, NETcollectionUdp netc)
 {
     try
     {
         NETcollectionUdp[] netclist = getUdpList();
         foreach (NETcollectionUdp tempnetc in netclist)
         {
             if (tempnetc.Port == port)
             {
                 return(false);
             }
         }
         Socket listeners = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         listeners.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);
         listeners.Blocking = false;
         listeners.Bind(localEndPoint);
         netc.Soc = listeners;
         return(true);
     }
     catch  { return(false); }
 }
Beispiel #8
0
 public bool mapping(int port,NETcollectionUdp  netc)
 {
     try
        {   
             NETcollectionUdp[] netclist = getUdpList();
             foreach (NETcollectionUdp tempnetc in netclist)
             {
                 if (tempnetc.Port == port)
                 {
                     return false;
                 }
             }
         Socket listeners = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         listeners.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);
         listeners.Blocking = false;
         listeners.Bind(localEndPoint);
         netc.Soc= listeners;
         return true;
     }
     catch  { return false; }
      
 }
Beispiel #9
0
       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);
           }
       }
Beispiel #10
0
 public NETcollectionUdp[] getUdpList()
 {
     try
     {
         NETcollectionUdp[] netlist = new NETcollectionUdp[listconn.Count];
         listconn.CopyTo(netlist);
         return netlist;
     }
     catch { return null; }
 }