Пример #1
0
 private SessionDescription(bool initialize)
 {
     if (initialize)
     {
         Version = 0;
         Origin = new Origin();
         Name = " ";
         Times.Add(new Time(Time.Zero, Time.Zero));
     }
     else
     {
         _version = -1;
         _origin = null;
         _name = null;
     }
 }
Пример #2
0
        internal void OnSessionDelete(NetworkInterfaceHandler interfaceHandler, Origin origin)
        {
            lock (_sessionData)
            {
                SdpSession session = new SdpSession(origin, interfaceHandler.NetworkInterface);
                SessionData sessionData = null;
                SessionAnnouncement sessionAnnouncement = null;
                if (_sessionData.TryGetValue(session, out sessionData))
                {
                    sessionAnnouncement = sessionData.Session;
                }

                if (sessionData != null)
                {
                    if (origin.IsUpdateOrEqual(sessionAnnouncement.SessionDescription.Origin))
                    {
                        sessionData.Timer.Dispose();
                        _sessionData.Remove(session);
                        _announcements.Remove(sessionData.Id);
                        SynchronizationContextPost(o =>
                        {
                            lock (Sessions)
                            {
                                Sessions.Remove(sessionData.Session);
                            }
                        });
                    }
                }
            }
        }
Пример #3
0
 public SdpSession(Origin o, NetworkInterface inter)
 {
     SessionDescription = new SessionDescription();
     SessionDescription.Origin = o;
     NetworkInterface = inter;
 }
Пример #4
0
 internal static Origin Parse(string value)
 {
     Origin origin = new Origin();
     string[] parts = value.Split(' ');
     if (parts.Length != 6)
     {
         throw new SdpException("origin");
     }
     origin.UserName = parts[0];
     ulong sessionID = 0;
     Grammar.ValidateDigits(parts[1], false);
     if (!ulong.TryParse(parts[1], out sessionID))
     {
         throw new SdpException("origin");
     }
     origin.SessionID = sessionID;
     ulong sessionVersion = 0;
     Grammar.ValidateDigits(parts[2], false);
     if (!ulong.TryParse(parts[2], out sessionVersion))
     {
         throw new SdpException("origin");
     }
     origin.SessionVersion = sessionVersion;
     origin.SetAddress(parts[3], parts[4], parts[5]);
     return origin;
 }
Пример #5
0
 public bool IsUpdateOrEqual(Origin o)
 {
     if (IsSameSession(o))
     {
         return SessionVersion >= o.SessionVersion;
     }
     return false;
 }
Пример #6
0
 public bool IsSameSession(Origin o)
 {
     return ((SessionID.Equals(o.SessionID))
         && (UserName.Equals(o.UserName))
         && (NetworkType.Equals(o.NetworkType))
         && (AddressType.Equals(o.AddressType))
         && (Address.Equals(o.Address)));
 }
Пример #7
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);
        }
Пример #8
0
 public override int GetHashCode()
 {
     return(Origin.GetHashCode());
 }