Пример #1
0
 public static DeviceType Determine(SSDPResponse response)
 {
     try
     {
         if (response != null && response.Information != null)
         {
             if (response.Information.Manufacturer.ToLower().Contains("google"))
             {
                 return(DeviceType.Chromecast);
             }
             if (response.Information.Model.ToLower().Contains("eureka "))
             {
                 return(DeviceType.Chromecast);
             }
             if (response.Information.Manufacturer.ToLower().Contains("tv"))
             {
                 return(DeviceType.Television);
             }
             if (response.Information.Model.ToLower().Contains("tv"))
             {
                 return(DeviceType.Television);
             }
         }
     }
     catch (Exception)
     {
         return(DeviceType.Generic);
     }
     return(DeviceType.Generic);
 }
Пример #2
0
        private async Task <IEnumerable <SSDPResponse> > SSDPAsync(IPAddress bind_addr, CancellationToken cancel_token)
        {
            var msg = System.Text.Encoding.ASCII.GetBytes(
                "M-SEARCH * HTTP/1.1\r\n" +
                "HOST: 239.255.255.250:1900\r\n" +
                "MAN: \"ssdp:discover\"\r\n" +
                "MX: 3\r\n" +
                "ST: upnp:rootdevice\r\n\r\n"
                );
            var responses = new List <SSDPResponse>();

            try {
                using (var client = new UdpClient(new IPEndPoint(bind_addr, 0))) {
                    for (int i = 0; i < 3; i++)
                    {
                        await client.SendAsync(msg, msg.Length, SSDPEndpoint).ConfigureAwait(false);
                    }
                    for (int i = 0; i < 10 && client.Available == 0 && !cancel_token.IsCancellationRequested; i++)
                    {
                        await Task.Delay(100, cancel_token).ConfigureAwait(false);
                    }
                    while (client.Available > 0 && !cancel_token.IsCancellationRequested)
                    {
                        var result = await client.ReceiveAsync().ConfigureAwait(false);

                        var response = System.Text.Encoding.ASCII.GetString(result.Buffer).Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        if (response.Length < 0)
                        {
                            continue;
                        }
                        if (response[0].IndexOf("HTTP/1.1 200") != 0)
                        {
                            continue;
                        }
                        var header_pattern = new System.Text.RegularExpressions.Regex(@"(.*?):(.*)");
                        var headers        = new Dictionary <string, string>();
                        foreach (var line in response.Skip(1))
                        {
                            if (line.Length == 0)
                            {
                                break;
                            }
                            var md = header_pattern.Match(line);
                            if (!md.Success)
                            {
                                continue;
                            }
                            var key   = md.Groups[1].Value.Trim();
                            var value = md.Groups[2].Value.Trim();
                            headers.Add(key.ToUpperInvariant(), value);
                        }
                        var rsp = new SSDPResponse(result.RemoteEndPoint, headers);
                        logger.Debug("SSDP Found {0} at {1}", rsp.ST, rsp.Location);
                        responses.Add(rsp);
                        if (client.Available == 0 && !cancel_token.IsCancellationRequested)
                        {
                            await Task.Delay(100, cancel_token).ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (SocketException e) {
                logger.Debug("Search Error on {0}: {1}", bind_addr, e);
            }
            return(responses.Distinct());
        }
Пример #3
0
 private async Task<IEnumerable<SSDPResponse>> SSDPAsync(IPAddress bind_addr, CancellationToken cancel_token)
 {
   var msg = System.Text.Encoding.ASCII.GetBytes(
     "M-SEARCH * HTTP/1.1\r\n" +
     "HOST: 239.255.255.250:1900\r\n" +
     "MAN: \"ssdp:discover\"\r\n" +
     "MX: 3\r\n" +
     "ST: ssdp:all\r\n\r\n"
   );
   var responses = new List<SSDPResponse>();
   using (var client=new UdpClient(new IPEndPoint(bind_addr, 0))) {
     for (int i=0; i<3; i++) {
       await client.SendAsync(msg, msg.Length, SSDPEndpoint);
     }
     if (client.Available==0 && !cancel_token.IsCancellationRequested) {
       await Task.Delay(1000, cancel_token);
     }
     while (client.Available>0 && !cancel_token.IsCancellationRequested) {
       var result = await client.ReceiveAsync();
       var response = System.Text.Encoding.ASCII.GetString(result.Buffer).Split(new string[] { "\r\n" }, StringSplitOptions.None);
       if (response.Length<0) continue;
       if (response[0].IndexOf("HTTP/1.1 200")!=0) continue;
       var header_pattern = new System.Text.RegularExpressions.Regex(@"(.*?):(.*)");
       var headers = new Dictionary<string,string>();
       foreach (var line in response.Skip(1)) {
         if (line.Length==0) break;
         var md = header_pattern.Match(line);
         if (!md.Success) continue;
         var key   = md.Groups[1].Value.Trim();
         var value = md.Groups[2].Value.Trim();
         headers.Add(key.ToUpperInvariant(), value);
       }
       var rsp = new SSDPResponse(result.RemoteEndPoint, headers);
       logger.Debug("SSDP Found {0} at {1}", rsp.ST, rsp.Location);
       responses.Add(rsp);
       if (client.Available==0 && !cancel_token.IsCancellationRequested) {
         await Task.Delay(100, cancel_token);
       }
     }
   }
   return responses.Distinct();
 }