Beispiel #1
0
        private void CompleteMessage(Messages.MessageBase msg, List <string> lines)
        {
            if (lines.Count == 0)
            {
                return;
            }

            bool isBoundaryFound = false;

            foreach (var item in lines)
            {
                if (isBoundaryFound)
                {
                    msg.Content += item + "\r\n";
                }
                else
                {
                    isBoundaryFound = item == "";
                    if (!isBoundaryFound)
                    {
                        msg.Headers.Add(item);
                    }
                }
            }
        }
Beispiel #2
0
        public Messages.MessageBase Parse(byte[] data)
        {
            List <string> lines = GetLines(data);

            if (lines == null || lines.Count == 0)
            {
                return(null);
            }

            Messages.MessageBase result = GetDiscoveryMessage(lines);
            if (result == null)
            {
                result = GetNotifyMessage(lines);
            }
            if (result == null)
            {
                result = GetHttpMessage(lines);
            }
            if (result == null)
            {
                return(null);
            }

            CompleteMessage(result, lines);
            return(result);
        }
Beispiel #3
0
        private bool GetServiceType(Messages.MessageBase message, string header, List <string> lines)
        {
            string tmp = GetValue(lines, header);

            if (string.IsNullOrEmpty(tmp))
            {
                return(false);
            }

            message.Service.ServiceType = tmp;
            return(true);
        }
Beispiel #4
0
        private bool GetServiceName(Messages.MessageBase message, List <string> lines)
        {
            string tmp = GetValue(lines, Headers.ServiceName);

            if (string.IsNullOrEmpty(tmp))
            {
                return(false);
            }

            message.Service.UniqueServiceName = tmp;
            return(true);
        }
Beispiel #5
0
        private void GetHost(List <string> lines, Messages.MessageBase message)
        {
            string tmp = GetValue(lines, "host");

            if (string.IsNullOrEmpty(tmp))
            {
                return;
            }

            int i = tmp.IndexOf(":");

            if (i >= 0)
            {
                message.Port = int.Parse(tmp.Substring(i + 1));
                message.Host = tmp.Substring(0, i);
            }
            else
            {
                message.Host = tmp;
            }
        }
Beispiel #6
0
        private Messages.MessageBase GetNotifyMessage(List <string> lines)
        {
            if (lines[0].ToUpper() != "NOTIFY * HTTP/1.1")
            {
                return(null);
            }

            lines.RemoveAt(0);

            Messages.MessageBase result = null;
            string tmp = GetValue(lines, Headers.NotificationType);

            if (tmp.Equals("ssdp:alive", StringComparison.InvariantCultureIgnoreCase))
            {
                result = GetAliveMessage(lines);
            }
            else if (tmp.Equals("ssdp:byebye", StringComparison.InvariantCultureIgnoreCase))
            {
                result = GetByeMessage(lines);
            }
            else
            {
                return(null);
            }

            if (!GetServiceType(result, Discovery.SSDP.Headers.NotifiedServiceType, lines))
            {
                return(null);
            }

            if (!GetServiceName(result, lines))
            {
                return(null);
            }
            GetHost(lines, result);

            return(result);
        }