예제 #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 <List <SSDPResponse> > Find(IPAddress address)
        {
            var list = new List <SSDPResponse>();

            try
            {
                IPEndPoint localEndPoint     = new IPEndPoint(address, 1901);
                IPEndPoint multicastEndPoint = new IPEndPoint(IPAddress.Parse(DialConstants.GenericMulticastAddress), 1900);

                Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSocket.ReceiveTimeout = 1000;
                udpSocket.SendTimeout    = 1000;
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, (int)ushort.MaxValue);

                udpSocket.Bind(localEndPoint);

                var ssdpAddress = DialConstants.GenericMulticastAddress;
                var ssdpPort    = 1900;
                var ssdpMx      = 2;
                var ssdpSt      = DialConstants.DialMultiScreenUrn;

                var ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" +
                                  String.Format("HOST: {0}:{1}\r\n", ssdpAddress, ssdpPort) +
                                  "MAN: \"ssdp:discover\"\r\n" +
                                  String.Format("MX: {0}\r\n", ssdpMx) +
                                  String.Format("ST: {0}\r\n", ssdpSt) + "\r\n";
                var    bytes         = Encoding.UTF8.GetBytes(ssdpRequest);
                byte[] bytesReceived = new byte[(int)ushort.MaxValue];

                var endPoint = (EndPoint)localEndPoint;

                for (int index = 0; index < 3; ++index)
                {
                    int totalbytes;
                    if ((totalbytes = udpSocket.SendTo(bytes, (EndPoint)multicastEndPoint)) == 0)
                    {
                    }
                    else
                    {
                        while (totalbytes > 0)
                        {
                            try
                            {
                                totalbytes = udpSocket.ReceiveFrom(bytesReceived, ref endPoint);
                                if (totalbytes > 0)
                                {
                                    var response = Encoding.UTF8.GetString(bytesReceived, 0, totalbytes);

                                    var ssdpResponse = new SSDPResponse();
                                    ssdpResponse.Interface = localEndPoint.Address;

                                    ssdpResponse.Parse(response);

                                    list.Add(ssdpResponse);
                                }
                            }
                            catch (Exception ex)
                            {
                                break;
                            }
                        }
                    }
                }

                if (udpSocket != null)
                {
                    udpSocket.Close();
                }
            }
            catch (Exception)
            {
            }

            return(list);
        }
예제 #3
0
파일: SSDPDiscovery.cs 프로젝트: BO45/NCast
        private async Task<List<SSDPResponse>> Find(IPAddress address)
        {
            var list = new List<SSDPResponse>();

            try
            {
                IPEndPoint localEndPoint = new IPEndPoint(address, 1901);
                IPEndPoint multicastEndPoint = new IPEndPoint(IPAddress.Parse(DialConstants.GenericMulticastAddress), 1900);

                Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSocket.ReceiveTimeout = 1000;
                udpSocket.SendTimeout = 1000;
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, (int)ushort.MaxValue);

                udpSocket.Bind(localEndPoint);

                var ssdpAddress = DialConstants.GenericMulticastAddress;
                var ssdpPort = 1900;
                var ssdpMx = 2;
                var ssdpSt = DialConstants.DialMultiScreenUrn;

                var ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" +
                            String.Format("HOST: {0}:{1}\r\n", ssdpAddress, ssdpPort) +
                            "MAN: \"ssdp:discover\"\r\n" +
                            String.Format("MX: {0}\r\n", ssdpMx) +
                            String.Format("ST: {0}\r\n", ssdpSt) + "\r\n";
                var bytes = Encoding.UTF8.GetBytes(ssdpRequest);
                byte[] bytesReceived = new byte[(int)ushort.MaxValue];

                var endPoint = (EndPoint)localEndPoint;

                for (int index = 0; index < 3; ++index)
                {
                    int totalbytes;
                    if ((totalbytes = udpSocket.SendTo(bytes, (EndPoint)multicastEndPoint)) == 0)
                    {

                    }
                    else
                    {

                        while (totalbytes > 0)
                        {
                            try
                            {
                                totalbytes = udpSocket.ReceiveFrom(bytesReceived, ref endPoint);
                                if (totalbytes > 0)
                                {

                                    var response = Encoding.UTF8.GetString(bytesReceived, 0, totalbytes);

                                    var ssdpResponse = new SSDPResponse();
                                    ssdpResponse.Interface = localEndPoint.Address;

                                    ssdpResponse.Parse(response);

                                    list.Add(ssdpResponse);
                                }
                            }
                            catch (Exception ex)
                            {
                                break;
                            }
                        }
                    }
                }

                if (udpSocket != null)
                    udpSocket.Close();
            }
            catch (Exception)
            {
            }

            return list;
        }
예제 #4
0
 public SSDPDiscoveredDeviceEventArgs(SSDPResponse response)
 {
     Response = response;
 }
예제 #5
0
파일: SSDPDiscovery.cs 프로젝트: BO45/NCast
 public SSDPDiscoveredDeviceEventArgs(SSDPResponse response)
 {
     Response = response;
 }