Example #1
0
 /// <summary>
 /// Initializes a new instance of the Service class.
 /// </summary>
 /// <param name="uri">Must specify device base uri, Because the description file does not contain uri.</param>
 /// <param name="root">Root device.</param>
 /// <param name="parent">Parent device.</param>
 /// <param name="serviceNode">Service XmlNode.</param>
 /// <param name="nm">XmlNamespaceManager.</param>
 /// <exception cref="Exception"/>
 public UPnPService(Uri uri, UPnPRootDevice root, UPnPDevice parent, XmlNode serviceNode, XmlNamespaceManager nm)
 {
     if (uri is null)
     {
         throw new ArgumentNullException(nameof(uri));
     }
     if (serviceNode is null)
     {
         throw new ArgumentNullException(nameof(serviceNode));
     }
     this.Root        = root;
     this.Parent      = parent;
     this.ServiceType = serviceNode.SelectSingleNode("ns:serviceType", nm).InnerText.Trim();
     this.ServiceID   = serviceNode.SelectSingleNode("ns:serviceId", nm).InnerText.Trim();
     this.ScpdUrl     = string.Format(CultureInfo.InvariantCulture, "{0}{1}", uri.AbsoluteUri, serviceNode.SelectSingleNode("ns:SCPDURL", nm).InnerText.Trim().Trim('/'));
     this.ControlUrl  = string.Format(CultureInfo.InvariantCulture, "{0}{1}", uri.AbsoluteUri, serviceNode.SelectSingleNode("ns:controlURL", nm).InnerText.Trim().Trim('/'));
     this.EventSubUrl = string.Format(CultureInfo.InvariantCulture, "{0}{1}", uri.AbsoluteUri, serviceNode.SelectSingleNode("ns:eventSubURL", nm).InnerText.Trim().Trim('/'));
 }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the Device class.
        /// </summary>
        /// <param name="uri">Must specify device base uri, Because the description file does not contain uri.</param>
        /// <param name="root">Root device info.</param>
        /// <param name="parent">Parent device.</param>
        /// <param name="deviceNode">Device XmlNode.</param>
        /// <param name="nm">XmlNamespaceManager.</param>
        /// <exception cref="Exception"/>
        public UPnPDevice(Uri uri, UPnPRootDevice root, UPnPDevice parent, XmlNode deviceNode, XmlNamespaceManager nm)
        {
            if (deviceNode is null)
            {
                throw new ArgumentNullException(nameof(deviceNode));
            }
            List <UPnPDevice>  childDevices = new List <UPnPDevice>();
            List <UPnPService> services     = new List <UPnPService>();

            //
            this.Root             = root;
            this.Parent           = parent;
            this.DeviceType       = deviceNode.SelectSingleNode("ns:deviceType", nm).InnerText.Trim();
            this.FriendlyName     = deviceNode.SelectSingleNode("ns:friendlyName", nm).InnerText.Trim();
            this.Manufacturer     = deviceNode.SelectSingleNode("ns:manufacturer", nm).InnerText.Trim();
            this.ManufacturerUrl  = deviceNode.SelectSingleNode("ns:manufacturerURL", nm).InnerText.Trim();
            this.ModelDescription = deviceNode.SelectSingleNode("ns:modelDescription", nm).InnerText.Trim();
            this.ModelName        = deviceNode.SelectSingleNode("ns:modelName", nm).InnerText.Trim();
            this.ModelNumber      = deviceNode.SelectSingleNode("ns:modelNumber", nm).InnerText.Trim();
            this.ModelUrl         = deviceNode.SelectSingleNode("ns:modelURL", nm).InnerText.Trim();
            this.SerialNumber     = deviceNode.SelectSingleNode("ns:serialNumber", nm).InnerText.Trim();
            this.Udn = deviceNode.SelectSingleNode("ns:UDN", nm).InnerText.Replace("uuid:", string.Empty).Trim();
            XmlNodeList childDeviceNodes = deviceNode.SelectNodes("ns:deviceList/ns:device", nm);

            //
            foreach (XmlNode childDeviceNode in childDeviceNodes)
            {
                childDevices.Add(new UPnPDevice(uri, root, this, childDeviceNode, nm));
            }
            //
            XmlNodeList serviceNodes = deviceNode.SelectNodes("ns:serviceList/ns:service", nm);

            foreach (XmlNode serviceNode in serviceNodes)
            {
                services.Add(new UPnPService(uri, root, this, serviceNode, nm));
            }
            //
            this.Devices  = childDevices.ToArray();
            this.Services = services.ToArray();
        }
Example #3
0
        public static UPnPRootDevice[] Discover(AddressFamily addressFamilyFilter, int mx)
        {
            List <UPnPRootDevice> dis       = new List <UPnPRootDevice>();
            List <string>         responses = new List <string>();

            using (UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 0)))
            {
                client.Client.ReceiveTimeout    = mx * 1000;
                client.Client.ReceiveBufferSize = 8 * 1024;
                client.Client.EnableBroadcast   = true;
                client.Client.MulticastLoopback = true;
                client.Client.Ttl = 1;
                client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                StringBuilder request = new StringBuilder();
                request.Append("M-SEARCH * HTTP/1.1\r\n");
                request.Append("HOST: 239.255.255.250:1900\r\n");
                request.Append("MAN: \"ssdp:discover\"\r\n");
                request.Append("MX: " + mx + "\r\n");
                request.Append("ST: upnp:rootdevice\r\n");
                request.Append("\r\n");
                byte[]     data = Encoding.UTF8.GetBytes(request.ToString());
                IPEndPoint ep   = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
                client.Send(data, data.Length, ep);

                while (true)
                {
                    try
                    {
                        byte[] received = client.Receive(ref ep);
                        responses.Add(Encoding.UTF8.GetString(received));
                    }
                    catch (SocketException)
                    {
                        break;
                    }
                }
            }
            if (responses.Count > 0)
            {
                using (WebClient wc = new WebClient())
                {
                    foreach (string response in responses)
                    {
                        if (response.Contains("200 OK"))
                        {
                            string descriptionUrl;
                            string find;
                            int    index;
                            int    count;
                            find  = "LOCATION:";
                            index = response.IndexOf(find, StringComparison.InvariantCulture);
                            if (index >= 0)
                            {
                                index += find.Length;
                                count  = response.IndexOf("\r\n", index, StringComparison.InvariantCulture) - index;
                                if (count > 0)
                                {
                                    descriptionUrl = response.Substring(index, count).Trim();
                                    try
                                    {
                                        Uri       uri       = new Uri(descriptionUrl);
                                        IPAddress ipAddress = IPAddress.Parse(uri.Host);
                                        if ((addressFamilyFilter & ipAddress.AddressFamily) != ipAddress.AddressFamily)
                                        {
                                            continue;
                                        }
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                continue;
                            }
                            bool exist = false;
                            foreach (UPnPRootDevice di in dis)
                            {
                                if (descriptionUrl == di.DescriptionUrl)
                                {
                                    exist = true;
                                    break;
                                }
                            }
                            if (!exist)
                            {
                                try
                                {
                                    byte[]         down = wc.DownloadData(descriptionUrl);
                                    UPnPRootDevice di   = new UPnPRootDevice(descriptionUrl, Encoding.UTF8.GetString(down));
                                    if (di is null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        dis.Add(di);
                                    }
                                }
                                catch
                                {
                                    continue;
                                }
                            }
                        }
                    }
                }
            }

            return(dis.ToArray());
        }