Beispiel #1
0
        /// <summary>
        /// @see http://www.johnthom.com/implementing-ssdp-search-in-windows-phone-8-using-winrt/
        /// </summary>
        public async Task DiscoverDevices()
        {
            int timeout = 3000;
            var remoteIp = "239.255.255.250";
            var remoteIpHostName = new Windows.Networking.HostName(remoteIp);
            var port = "1900";

            string query = "M-SEARCH * HTTP/1.1\r\n" +
                         "HOST: 239.255.255.250:1900\r\n" +
                         "ST: upnp:rootdevice\r\n" +
                         "MAN: \"ssdp:discover\"\r\n" +
                         "MX: " + timeout.ToString() + "\r\n\r\n";

            var buffer = Encoding.UTF8.GetBytes(query);

            using (var socket = new DatagramSocket())
            {
                socket.MessageReceived += (sender, args) => {
                    Task.Run(() => {
                        using (var reader = args.GetDataReader())
                        {
                            byte[] respBuff = new byte[reader.UnconsumedBufferLength];
                            reader.ReadBytes(respBuff);
                            string response = Encoding.UTF8.GetString(respBuff, 0, respBuff.Length).ToLower();
                            response.Trim('\0');

                            ProcessSSDPResponse(response);
                        }
                    });
                };

                await socket.BindEndpointAsync(null, "");
                socket.JoinMulticastGroup(remoteIpHostName);
                
                using (var stream = await socket.GetOutputStreamAsync(remoteIpHostName, port))
                {
                    await stream.WriteAsync(buffer.AsBuffer());
                }
                
                // Execute within timeout
                await Task.Delay(timeout);
            }
        }
Beispiel #2
0
        internal async Task<IList<Camera>> probeTask()
        { 
            var cameras = new List<Camera>();
            var cameraAddressMap = new Dictionary<string, Camera>();

            listener.MessageReceived +=  (DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args) =>
            {
                try
                {
                    var reader = args.GetDataReader();
                    var bytesRemaining = reader.UnconsumedBufferLength;
                    var receivedString = reader.ReadString(bytesRemaining);

                    var soapEnvelope = new XmlDocument();
                    soapEnvelope.LoadXml(receivedString);

                    //var probeMatches = soapEnvelope.GetElementsByTagName("wsdd:ProbeMatch");
                    var probeMatches = soapEnvelope.SelectNodesNS("//wsdd:ProbeMatch", "xmlns:wsdd='http://schemas.xmlsoap.org/ws/2005/04/discovery'");
                    foreach (var probeMatch in probeMatches)
                    {
                        Debug.WriteLine(probeMatch.GetXml());
                        var endpointAddressNode = probeMatch.SelectSingleNodeNS("wsa:EndpointReference/wsa:Address", "xmlns:wsdd='http://schemas.xmlsoap.org/ws/2005/04/discovery' xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/08/addressing'");

                        var xAddressNode = probeMatch.SelectSingleNodeNS("wsdd:XAddrs", "xmlns:wsdd='http://schemas.xmlsoap.org/ws/2005/04/discovery' xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/08/addressing'");

                        var cameraAddress = endpointAddressNode.InnerText;
                        var cameraUri = xAddressNode.InnerText;

                        if (!cameraAddressMap.ContainsKey(cameraAddress))
                        {
                            var uri = new Uri(cameraUri);
                            var cameraInstance = new Camera(cameraAddress, uri);
                            cameras.Add(cameraInstance);
                            cameraAddressMap.Add(cameraAddress, cameraInstance);
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                }
            };

            await listener.BindEndpointAsync(null, string.Empty);
            var ssdp = new Windows.Networking.HostName("239.255.255.250");
            listener.JoinMulticastGroup(ssdp);


            using (var stream = await listener.GetOutputStreamAsync(ssdp, "3702"))
            {
                using (var writer = new DataWriter(stream))
                {
                    string req = GetRequestString();

                    writer.WriteString(req);
                    await writer.StoreAsync();

                    await Task.Delay(5000);
                }
            }

            return cameras;
        }