Пример #1
0
            public override void run()
            {
                Console.WriteLine(string.Format("Starting AutoDetectJpcsp ListenerThread"));
                sbyte[] response = new sbyte[256];

                while (!exit_Renamed)
                {
                    try
                    {
                        InetAddress     listenAddress = InetAddress.getByName(multicastIp);
                        MulticastSocket socket        = new MulticastSocket(discoveryPort);
                        socket.joinGroup(listenAddress);
                        while (!exit_Renamed)
                        {
                            DatagramPacket packet = new DatagramPacket(response, response.Length);
                            socket.receive(packet);
                            processRequest(socket, new string(packet.Data, packet.Offset, packet.Length), packet.Address, packet.Port);
                        }
                        socket.close();
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine("ListenerThread", e);
                        exit();
                    }
                }
            }
Пример #2
0
        public virtual void discover()
        {
            try
            {
                igd = new IGD();
                ListenerThread listener = new ListenerThread(this, igd);
                listener.Daemon = true;
                listener.Name   = "UPnP Discovery Listener";
                listener.Start();
                while (!listener.Ready)
                {
                    Utilities.sleep(100);
                }

                foreach (string device in deviceList)
                {
                    string discoveryRequest = string.Format("M-SEARCH * HTTP/1.1\r\nHOST: {0}:{1:D}\r\nST: {2}\r\nMAN: \"ssdp:discover\"\r\nMX: {3:D}\r\n\r\n", multicastIp, discoveryPort, device, discoveryTimeoutMillis / 1000);
                    IEnumerator <NetworkInterface> networkInterfaces = NetworkInterface.NetworkInterfaces;
                    while (networkInterfaces.MoveNext())
                    {
                        NetworkInterface networkInterface = networkInterfaces.Current;
                        if (networkInterface.Up && networkInterface.supportsMulticast())
                        {
                            for (IEnumerator <InetAddress> addresses = networkInterface.InetAddresses; addresses.MoveNext();)
                            {
                                InetAddress address = addresses.Current;
                                if (address is Inet4Address && !address.LoopbackAddress)
                                {
                                    MulticastSocket   socket        = new MulticastSocket(new InetSocketAddress(address, discoverySearchPort));
                                    InetSocketAddress socketAddress = new InetSocketAddress(multicastIp, discoveryPort);
                                    DatagramPacket    packet        = new DatagramPacket(discoveryRequest.GetBytes(), discoveryRequest.Length, socketAddress);
                                    socket.send(packet);
                                    socket.disconnect();
                                    socket.close();
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < discoveryTimeoutMillis / 10; i++)
                {
                    if (listener.Done)
                    {
                        break;
                    }
                    Utilities.sleep(10, 0);
                }

                listener.Done = true;
                while (!listener.Ready)
                {
                    Utilities.sleep(100);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("discover", e);
            }
        }