public static void Main(string[] argv)
    {
        SSDPDiscovery dis = new SSDPDiscovery("ssdp:all");

        while (true)
        {
            List <Dictionary <string, string> > devices = dis.discover();
            foreach (var dev in devices)
            {
                Console.WriteLine("Device");
                foreach (KeyValuePair <string, string> kvp in dev)
                {
                    Console.WriteLine("{0}:{1}", kvp.Key, kvp.Value);
                }
            }
        }
    }
    public static List <Dictionary <string, string> > ssdpDiscover(string servicetype, int timeout)
    {
        SSDPDiscovery dis  = new SSDPDiscovery(servicetype);
        var           tend = getAbsTime_ms() + timeout;
        int           ttg  = timeout;

        while (ttg > 0)
        {
            var devices = dis.discover(ttg);
            if (devices.Count > 0)
            {
                // TODO: accumulate the devices lists
                return(devices);
            }
            ttg = (int)(tend - getAbsTime_ms());
        }
        return(null);
    }
示例#3
0
        // TODO: make timeout work correctly
        public bool connect(string host, int port = -1, int timeout_ms = 5000)
        {
            if (port < 0)
            {
                port = Constants.DEFAULTPORT;
            }
            if (this.clientSocket != null && this.clientSocket.Connected)
            {
                this.clientSocket.Close();
                this.clientSocket = null;
            }
            if (host == null || host.Length == 0 || host == "-")
            {
                // auto-search for host port
                Console.WriteLine("Trying SSDP discovery");
                if (ssdpDiscovery == null)     // create if needed
                {
                    ssdpDiscovery = new SSDPDiscovery("utopia/1.1");
                }
                var devices = ssdpDiscovery.discover(timeout_ms);
                Console.WriteLine("Discovered " + devices.Count() + " devices");
                if (devices.Any())
                {
                    var dev = devices[0];
                    if (dev.ContainsKey("location"))
                    {
                        host = dev["location"];
                        port = Constants.DEFAULTPORT;
                        if (host.StartsWith("http://"))
                        {
                            host = host.Substring("http://".Length);
                        }
                        if (host.Contains("/"))
                        {
                            host = host.Substring(0, host.IndexOf("/"));
                        }
                        string[] hostport = host.Split(':');
                        host = hostport[0];
                        if (hostport.Length > 1)
                        {
                            Int32.TryParse(hostport[1], out port);
                        }
                    }
                    Console.WriteLine("SSDP discovered: " + host + ":" + port);
                }
            }

            if (host == null || host.Length == 0 || host == "-")
            {
                return(false);
            }
            //if ( this.clientSocket == null )
            this.clientSocket = new TcpClient();
            try
            {
                // connect with a timeout... csharp style
                var result  = this.clientSocket.BeginConnect(host, port, null, null);
                var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout_ms));
                this.clientSocket.EndConnect(result);
                // we have connected
                //this.clientSocket.Connect(host, port);
            } catch (SocketException ex)
            {
            }
            if (this.clientSocket.Connected)
            {
                this.networkStream = this.clientSocket.GetStream();
                this.clientSocket.ReceiveTimeout = 500;
                this.clientSocket.NoDelay        = true; // disable Nagle?

                // udp client bound to same local port number to make matching udp/tcp messages easier
                this.udpClient = new UdpClient(((System.Net.IPEndPoint) this.clientSocket.Client.LocalEndPoint).Port);
                this.udpClient.Connect(host, port);
            }
            return(this.clientSocket.Connected);
        }