Beispiel #1
0
        public static SessionDescription Load(TextReader reader, LoadOptions loadOptions)
        {
            SessionDescription sd = new SessionDescription(false);

            Media  media = null;
            string line  = reader.ReadLine();

            while (line != null)
            {
                if ((line.Length == 0) && ((loadOptions & LoadOptions.IgnoreEmptyLines) != 0))
                {
                    goto nextline;
                }
                if (line.Length < 3)
                {
                    goto invalidline;
                }
                if (line[1] != '=')
                {
                    goto invalidline;
                }
                string   value = line.Substring(2);
                string[] parts = null;
                int      sep   = -1;
                try
                {
                    switch (line[0])
                    {
                    case 'v':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        int version = -1;
                        Grammar.ValidateDigits(value, false);
                        if (!int.TryParse(value, out version))
                        {
                            goto invalidline;
                        }
                        if ((loadOptions & LoadOptions.IgnoreUnsupportedVersion) == 0)
                        {
                            if (version != 0)
                            {
                                goto notsupported;
                            }
                        }
                        sd.Version = version;
                        break;

                    case 'o':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        sd.Origin = Origin.Parse(value);
                        break;

                    case 's':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        sd.Name = value;
                        break;

                    case 'i':
                        if (media == null)
                        {
                            sd.Information = value;
                        }
                        else
                        {
                            media.Information = value;
                        }
                        break;

                    case 'u':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        sd.Uri = new Uri(value);
                        break;

                    case 'e':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        sd.EMails.Add(value);
                        break;

                    case 'p':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        sd.PhoneNumbers.Add(value);
                        break;

                    case 'c':
                        if (media == null)
                        {
                            if (sd.HasConnection)
                            {
                                goto invalidline;
                            }
                        }
                        Connection conn = new Connection();
                        parts = value.Split(' ');
                        if (parts.Length != 3)
                        {
                            goto invalidline;
                        }
                        string networkType = parts[0];
                        string addressType = parts[1];
                        parts = parts[2].Split('/');
                        if (parts.Length > 3)
                        {
                            goto invalidline;
                        }
                        conn.SetAddress(networkType, addressType, parts[0]);
                        conn.Address      = parts[0];
                        conn.AddressCount = 1;
                        if (parts.Length >= 2)
                        {
                            uint ttl = 0;
                            if (!uint.TryParse(parts[1], out ttl))
                            {
                                goto invalidline;
                            }
                            conn.Ttl = ttl;
                            if (parts.Length == 3)
                            {
                                uint addressCount = 0;
                                if (!uint.TryParse(parts[2], out addressCount))
                                {
                                    goto invalidline;
                                }
                                conn.AddressCount = addressCount;
                            }
                            if (conn.AddressType == "IP6")
                            {
                                if (parts.Length == 3)
                                {
                                    goto invalidline;
                                }
                                conn.AddressCount = conn.Ttl;
                                conn.Ttl          = 0;
                            }
                        }

                        if (media != null)
                        {
                            media.Connections.Add(conn);
                        }
                        else
                        {
                            sd.Connection = conn;
                        }
                        break;

                    case 'b':
                        sep = value.IndexOf(':');
                        if (sep == -1)
                        {
                            goto invalidline;
                        }
                        string type = value.Substring(0, sep);
                        string bw   = value.Substring(sep + 1);
                        Grammar.ValidateDigits(bw, false);
                        uint bwValue = 0;
                        if (!uint.TryParse(bw, out bwValue))
                        {
                            goto invalidline;
                        }
                        if (media != null)
                        {
                            media.Bandwidths.Add(new Bandwidth(type, bwValue));
                        }
                        else
                        {
                            sd.Bandwidths.Add(new Bandwidth(type, bwValue));
                        }
                        break;

                    case 'a':
                        sep = value.IndexOf(':');
                        string attrName;
                        string attrValue;
                        if (sep != -1)
                        {
                            attrName  = value.Substring(0, sep);
                            attrValue = value.Substring(sep + 1);
                        }
                        else
                        {
                            attrName  = value;
                            attrValue = null;
                        }
                        if (media != null)
                        {
                            media.Attributes.Add(attrName, attrValue);
                        }
                        else
                        {
                            sd.Attributes.Add(attrName, attrValue);
                        }
                        break;

                    case 't':
                        if (media != null)
                        {
                            goto invalidline;
                        }
                        parts = value.Split(' ');
                        if (parts.Length != 2)
                        {
                            goto invalidline;
                        }
                        Grammar.ValidateTime(parts[0]);
                        double startTime = 0;
                        if (!double.TryParse(parts[0], out startTime))
                        {
                            goto invalidline;
                        }
                        Grammar.ValidateTime(parts[1]);
                        double stopTime = 0;
                        if (!double.TryParse(parts[1], out stopTime))
                        {
                            goto invalidline;
                        }
                        DateTime startDateTime = Time.Zero;
                        DateTime stopDateTime  = Time.Zero;
                        if (startTime != 0)
                        {
                            startDateTime = Time.Zero + TimeSpan.FromSeconds(startTime);
                        }
                        if (stopTime != 0)
                        {
                            stopDateTime = Time.Zero + TimeSpan.FromSeconds(stopTime);
                        }
                        sd.Times.Add(new Time(startDateTime, stopDateTime));
                        break;

                    case 'm':
                        parts = value.Split(' ');
                        if (parts.Length < 4)
                        {
                            goto invalidline;
                        }
                        string mediaType = parts[0];
                        string protocol  = parts[2];
                        var    formats   = parts.Skip(3).ToList();
                        parts = parts[1].Split('/');
                        if (parts.Length > 2)
                        {
                            goto invalidline;
                        }
                        uint port      = 0;
                        uint portCount = 1;
                        Grammar.ValidateDigits(parts[0], false);
                        if (!uint.TryParse(parts[0], out port))
                        {
                            goto invalidline;
                        }
                        if (parts.Length == 2)
                        {
                            Grammar.ValidateDigits(parts[1], true);
                            if (!uint.TryParse(parts[1], out portCount))
                            {
                                goto invalidline;
                            }
                        }
                        media = new Media(mediaType, port, portCount, protocol, formats);
                        sd.Medias.Add(media);
                        break;

                    case 'z':
                    case 'k':
                    case 'r':
                        if ((loadOptions & LoadOptions.IgnoreUnsupportedLines) == 0)
                        {
                            goto notsupported;
                        }
                        break;

                    default:
                        if ((loadOptions & LoadOptions.IgnoreUnknownLines) == 0)
                        {
                            goto invalidline;
                        }
                        break;
                    }
                }
                catch
                {
                    goto invalidline;
                }
nextline:
                line = reader.ReadLine();
                continue;
invalidline:
                throw new SdpException(string.Format("Invalid Line {0}", line));
notsupported:
                throw new SdpException(string.Format("Unsupported line {0}", line));
            }

            if (sd.Version == -1)
            {
                throw new SdpException("Version ('v=') is required");
            }
            if (!sd.HasOrigin)
            {
                throw new SdpException("Origin ('o=') is required");
            }
            if (sd.Name == null)
            {
                throw new SdpException("Name ('s=') is required");
            }
            if (!sd.HasTimes)
            {
                throw new SdpException("Time ('t=') is required");
            }
            return(sd);
        }