Ejemplo n.º 1
0
 public bool SelectDevice(string key)
 {
     SelectedDevice = Devices.Select(key);
     return(SelectedDevice.Name.Length > 0);
 }
Ejemplo n.º 2
0
        public void SearchRenderers(bool output2console = false)
        {
            IPEndPoint localEndPoint     = new IPEndPoint(IPAddress.Any, 0);
            IPEndPoint multicastEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);

            Socket        udpSocket = null;
            StringBuilder sb        = new StringBuilder();

            try {
                udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                udpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastEndPoint.Address, IPAddress.Any));
                udpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
                udpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);

                udpSocket.Bind(localEndPoint);

                string SearchString = "M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nMAN:\"ssdp:discover\"\r\nST:upnp:rootdevice\r\nMX:4\r\nUSER-AGENT: UDAP/2.0 UPnP/1.1 testdlna/1.0\r\n\r\n";

                udpSocket.SendTo(Encoding.UTF8.GetBytes(SearchString), SocketFlags.None, multicastEndPoint);

                if (output2console)
                {
                    Console.WriteLine("M-Search sent...\r\n");
                }
                System.Threading.Thread.Sleep(4000);

                byte[] ReceiveBuffer = new byte[64000];
                int    ReceivedBytes = 0;

                while (udpSocket.Available > 0)
                {
                    ReceivedBytes = udpSocket.Receive(ReceiveBuffer, SocketFlags.None);
                    if (ReceivedBytes > 0)
                    {
                        sb.Append(Encoding.UTF8.GetString(ReceiveBuffer, 0, ReceivedBytes));
                    }
                }
            } finally {
                if (udpSocket != null)
                {
                    udpSocket.Close();
                }
            }
            string answer = sb.ToString();

            if (output2console)
            {
                Console.WriteLine(answer);
            }

            MatchCollection mc = Regex.Matches(answer, @"HTTP/.*?\r\n\r\n", RegexOptions.Singleline);

            foreach (Match m in mc)
            {
                string usn = GetAnswerValue(m.Value, "USN");
                if (!Devices.Exists(usn))
                {
                    UPnPDevice device = new UPnPDevice(usn);
                    device.Type        = GetAnswerValue(m.Value, "ST");
                    device.Location    = GetAnswerValue(m.Value, "LOCATION");
                    device.Server      = GetAnswerValue(m.Value, "SERVER");
                    device.HeadersInfo = m.Value;
                    device.DeviceAddr  = Regex.Match(device.Location, @"^.*?//[^/]+").Value;

                    if (device.Location.Length > 0)
                    {
                        string info = GetResponse(device.Location);
                        device.Name         = GetXmlValue(info, "friendlyName");
                        device.Manufacturer = GetXmlValue(info, "manufacturer");
                        device.ModelName    = GetXmlValue(info, "modelName");
                        device.UDN          = GetXmlValue(info, "UDN");
                        device.Type         = GetXmlValue(info, "deviceType");
                        MatchCollection mcServices = Regex.Matches(info, @"<service>.*?</service>", RegexOptions.Singleline);
                        foreach (Match s in mcServices)
                        {
                            UPnPService service = new UPnPService();
                            service.ServiceType = GetXmlValue(s.Value, "serviceType");
                            service.ServiceID   = GetXmlValue(s.Value, "serviceId");
                            service.SCPDURL     = device.DeviceAddr + GetXmlValue(s.Value, "SCPDURL");
                            service.ControlUrl  = device.DeviceAddr + GetXmlValue(s.Value, "controlURL");
                            service.EventSubURL = device.DeviceAddr + GetXmlValue(s.Value, "eventSubURL");
                            device.Services.Add(service);
                            if (service.ServiceType.IndexOf("AVTransport") > 0)
                            {
                                device.Ready = true;
                            }
                        }
                    }

                    Devices.Add(device);
                }
            }
        }