Пример #1
0
        /// <summary>
        /// Implements support for the UPnP protocol, as described in:
        /// http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0.pdf
        /// </summary>
        public UPnPClient()
        {
            UdpClient Outgoing;
            UdpClient Incoming;

            foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (Interface.OperationalStatus != OperationalStatus.Up)
                {
                    continue;
                }

                IPInterfaceProperties Properties = Interface.GetIPProperties();
                IPAddress             MulticastAddress;

                foreach (UnicastIPAddressInformation UnicastAddress in Properties.UnicastAddresses)
                {
                    if (UnicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4)
                    {
                        try
                        {
                            Outgoing                   = new UdpClient(AddressFamily.InterNetwork);
                            MulticastAddress           = IPAddress.Parse("239.255.255.250");
                            Outgoing.DontFragment      = true;
                            Outgoing.MulticastLoopback = false;
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    else if (UnicastAddress.Address.AddressFamily == AddressFamily.InterNetworkV6 && Socket.OSSupportsIPv6)
                    {
                        try
                        {
                            Outgoing = new UdpClient(AddressFamily.InterNetworkV6);
                            Outgoing.MulticastLoopback = false;
                            MulticastAddress           = IPAddress.Parse("[FF02::C]");
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    Outgoing.EnableBroadcast   = true;
                    Outgoing.MulticastLoopback = false;
                    Outgoing.Ttl = 30;
                    Outgoing.Client.Bind(new IPEndPoint(UnicastAddress.Address, 0));
                    Outgoing.JoinMulticastGroup(MulticastAddress);

                    IPEndPoint EP = new IPEndPoint(MulticastAddress, ssdpPort);
                    this.ssdpOutgoing.AddLast(new KeyValuePair <UdpClient, IPEndPoint>(Outgoing, EP));

                    Outgoing.BeginReceive(this.EndReceiveOutgoing, Outgoing);

                    try
                    {
                        Incoming = new UdpClient(Outgoing.Client.AddressFamily);
                        Incoming.ExclusiveAddressUse = false;
                        Incoming.Client.Bind(new IPEndPoint(UnicastAddress.Address, ssdpPort));

                        Incoming.BeginReceive(this.EndReceiveIncoming, Incoming);

                        this.ssdpIncoming.AddLast(Incoming);
                    }
                    catch (Exception)
                    {
                        Incoming = null;
                    }

                    try
                    {
                        Incoming = new UdpClient(ssdpPort, Outgoing.Client.AddressFamily);
                        Incoming.MulticastLoopback = false;
                        Incoming.JoinMulticastGroup(MulticastAddress);

                        Incoming.BeginReceive(this.EndReceiveIncoming, Incoming);

                        this.ssdpIncoming.AddLast(Incoming);
                    }
                    catch (Exception)
                    {
                        Incoming = null;
                    }
                }
            }
        }