Exemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            Console.WriteLine(context.Request.RawUrl);

            Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!");

            if (context.Request.Path.StartsWith("/igddesc.xml/"))
            {
                SsdpDevice device = ssdp.FindDevice(context.Request.Path.Substring(13));

                if (device == null)
                {
                    Console.WriteLine("Could not find UPnP device.");

                    context.Response.RaiseError("Could not find UPnP device.", HttpStatusCode.NotFound);
                }
                else
                {
                    Console.WriteLine("Sending XML...");

                    context.Response.ContentType = "text/xml";
                    context.Response.Write(device.ToSoapMessage());

                    Console.WriteLine(device.ToSoapMessage());
                }
            }
            else
            {
                context.Response.RaiseError(HttpStatusCode.NotFound);
            }
        }
Exemplo n.º 2
0
        internal void NotifyDevice(SsdpDevice device, bool isAlive)
        {
            /*
             * NOTIFY * HTTP/1.1
             * HOST: 239.255.255.250:1900
             * LOCATION: http://192.168.178.1:49000/igddesc.xml
             * SERVER: Interactive UPnP/1.0 AVM FRITZ!Box Fon WLAN 7270 54.04.70
             * CACHE-CONTROL: max-age=60
             * NT: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
             * NTS: ssdp:alive
             * USN: uuid:75802409-bccb-40e7-8e6b-001F3FF667FC::urn:schemas-upnp-org:service:WAN
             */


            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            s.ReceiveTimeout = 100;

            List <string> res = new List <string>();

            foreach (String address in _broadcastAddresses)
            {
                string req = string.Format(@"NOTIFY * HTTP/1.1
HOST: {0}:1900
LOCATION: http://{1}:{2}/igddesc.xml/{3}
SERVER: {5} UPnP/1.0 MFToolkit
CACHE-CONTROL: max-age=60
NT: upnp:rootdevice
NTS: ssdp:{6}
USN: {3}::upnp:rootdevice

",
                                           address,
                                           _http.Address.ToString(),
                                           _http.Port,
                                           device.UDN,
                                           DeviceTypeHelper.GetDeviceType(device.Type),
                                           device.Name,
                                           (isAlive ? "alive" : "byebye"));

                byte[] data   = Encoding.ASCII.GetBytes(req);
                byte[] buffer = new byte[1024];

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        Console.WriteLine("Sending ssdp:" + (isAlive ? "alive" : "byebye") + " for " + device.Name + " (" + device.UDN + ")...");

                        s.SendTo(data, new IPEndPoint(IPAddress.Broadcast, 1900));
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void RegisterDevice(SsdpDevice device)
        {
            if (_deviceList == null)
            {
                _deviceList = new List <SsdpDevice>();
            }

            _deviceList.Add(device);

            NotifyDevice(device, true);
        }
Exemplo n.º 4
0
        public void UnregisterDevice(string udn)
        {
            SsdpDevice device = FindDevice(udn);

            if (device == null)
            {
                return;
            }

            NotifyDevice(device, false);

            _deviceList.Remove(device);
        }