Ejemplo n.º 1
0
        public static SsdpMessage Parse(string text)
        {
            SsdpMessage message = new SsdpMessage();

            Regex msgCatReg = new Regex("^(.*?)\\ .*?$", RegexOptions.Multiline | RegexOptions.Compiled);
            Regex headerReg = new Regex("^(.*?)\\:\\ ?(.*?)$", RegexOptions.Multiline | RegexOptions.Compiled);

            string msgCategory = msgCatReg.Match(text).Groups[1].Value;

            if (msgCategory == "NOTIFY")
            {
                message.Category = SsdpMessageCategory.Advertisement;
            }
            else if (msgCategory == "M-SEARCH")
            {
                message.Category = SsdpMessageCategory.DiscoveryRequest;
            }
            else if (msgCategory == "HTTP/1.1")
            {
                message.Category = SsdpMessageCategory.DiscoveryResponse;
            }

            MatchCollection headerMatches = headerReg.Matches(text);

            foreach (Match m in headerMatches)
            {
                message.Headers.Add(m.Groups[1].Value, m.Groups[2].Value);
            }

            return(message);
        }
Ejemplo n.º 2
0
        public void DiscoverAll()
        {
            SsdpMessage message = new SsdpMessage(SsdpMessageCategory.DiscoveryRequest);

            message.Headers.Add(SsdpHeaders.Host, $"{_mcaddr.ToString()}:{_ssdpPort}");
            message.Headers.Add(SsdpHeaders.Man, SsdpMessageType.Discover);
            message.Headers.Add(SsdpHeaders.Mx, "1");
            message.Headers.Add(SsdpHeaders.St, "\"ssdp:all\"");

            byte[] buffer = Encoding.UTF8.GetBytes(message.Serialize());
            _ucastSocket.SendTo(buffer, new IPEndPoint(_mcaddr, _ssdpPort));
        }
Ejemplo n.º 3
0
        private void OnReceive(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            int len = 0;

            try
            {
                len = socket.EndReceiveFrom(ar, ref _recvEp);
            }
            catch (SystemException)
            {
                return;
            }

            SsdpMessage message = SsdpMessage.Parse(_recvBuffer, 0, len);

            MessageReceived?.Invoke(this, new SsdpMessageEventArgs(((IPEndPoint)_recvEp), message));

            if (message.Category == SsdpMessageCategory.Advertisement || message.Category == SsdpMessageCategory.DiscoveryResponse)
            {
                string usnString;

                if (message.Headers.TryGetValue(SsdpHeaders.Usn, out usnString))
                {
                    Usn usn = new Usn(usnString);

                    if (Devices.Where(x => x.Uuid.Equals(usn.Uuid)).Count() == 0)
                    {
                        SsdpDevice d = new SsdpDevice(((IPEndPoint)_recvEp).Address, usn.Uuid);

                        Devices.Add(d);
                        Console.WriteLine("Added {0}", usn);
                    }
                    else
                    {
                        Console.WriteLine("Already in {0}", usn);
                    }
                }
            }

            socket.BeginReceiveFrom(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None, ref _recvEp, OnReceive, socket);
        }
Ejemplo n.º 4
0
 public SsdpMessageEventArgs(IPEndPoint remoteEndpoint, SsdpMessage message)
 {
     RemoteEndpoint = remoteEndpoint;
     Message        = message;
 }