Ejemplo n.º 1
0
        /// <summary>
        /// Parses SIP_Request from stream.
        /// </summary>
        /// <param name="stream">Stream what contains valid SIP request.</param>
        /// <returns>Returns parsed SIP_Request obeject.</returns>
        /// <exception cref="ArgumentNullException">Raised when <b>stream</b> is null.</exception>
        /// <exception cref="SIP_ParseException">Raised when invalid SIP message.</exception>
        public static SIP_Request Parse(Stream stream)
        {
            /* Syntax:
             *  SIP-Method SIP-URI SIP-Version
             *  SIP-Message
             */

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Parse Response-line
            StreamLineReader r = new StreamLineReader(stream);

            r.Encoding = "utf-8";
            string[] method_uri_version = r.ReadLineString().Split(' ');
            if (method_uri_version.Length != 3)
            {
                throw new Exception(
                          "Invalid SIP request data ! Method line doesn't contain: SIP-Method SIP-URI SIP-Version.");
            }
            SIP_Request retVal = new SIP_Request(method_uri_version[0]);

            retVal.RequestLine.Uri     = AbsoluteUri.Parse(method_uri_version[1]);
            retVal.RequestLine.Version = method_uri_version[2];

            // Parse SIP message
            retVal.InternalParse(stream);

            return(retVal);
        }
        /// <summary>
        /// Parses "name-addr" or "addr-spec" from specified reader.
        /// </summary>
        /// <param name="reader">Reader from where to parse.</param>
        /// <exception cref="ArgumentNullException">Raised when <b>reader</b> is null.</exception>
        /// <exception cref="SIP_ParseException">Raised when invalid SIP message.</exception>
        public void Parse(StringReader reader)
        {
            /* RFC 3261.
             *  name-addr =  [ display-name ] LAQUOT addr-spec RAQUOT
             *  addr-spec =  SIP-URI / SIPS-URI / absoluteURI
             */

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            reader.ReadToFirstChar();

            // LAQUOT addr-spec RAQUOT
            if (reader.StartsWith("<"))
            {
                m_pUri = AbsoluteUri.Parse(reader.ReadParenthesized());
            }
            else
            {
                // Read while we get "<","," or EOF.
                StringBuilder buf = new StringBuilder();
                while (true)
                {
                    buf.Append(reader.ReadToFirstChar());

                    string word = reader.ReadWord();
                    if (string.IsNullOrEmpty(word))
                    {
                        break;
                    }
                    else
                    {
                        buf.Append(word);
                    }
                }

                reader.ReadToFirstChar();

                // name-addr
                if (reader.StartsWith("<"))
                {
                    m_DisplayName = buf.ToString().Trim();
                    m_pUri        = AbsoluteUri.Parse(reader.ReadParenthesized());
                }
                // addr-spec
                else
                {
                    m_pUri = AbsoluteUri.Parse(buf.ToString());
                }
            }
        }
Ejemplo n.º 3
0
        public void BeginRegister()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name, "不能执行注册操作。");
            }
            if (_regi != null)
            {
                _regi.Dispose();
            }

            string localSn   = _gateway.SipNumber;
            int    localPort = _gateway.Port;

            string remoteSn   = _platform.SipNumber;
            int    remotePort = _platform.Port;
            string remoteIP   = _platform.Ip;


            string user  = _platform.UserName;
            string pwd   = _platform.Password;
            string realm = _platform.Realm;

            SIP_Stack         stack = SipProxyWrapper.Instance.Stack;
            SIP_t_NameAddress from  = new SIP_t_NameAddress($"sip:{localSn}@{_localIp}:{localPort}");
            SIP_t_NameAddress to    = new SIP_t_NameAddress($"sip:{localSn}@{_localIp}:{localPort}");

            SIP_Uri server = new SIP_Uri()
            {
                Host = remoteIP,
                Port = remotePort,
                User = remoteSn
            };
            string              aor     = $"{localSn}@{_localIp}:{localPort}";
            AbsoluteUri         contact = AbsoluteUri.Parse($"sip:{localSn}@" + stack.BindInfo[0].EndPoint.ToString());
            SIP_UA_Registration regi    = stack.CreateRegistration(server, aor, contact, 7200);

            _regi = regi; //记录

            regi.Credential    = new NetworkCredential(user, pwd, realm);
            regi.Registered   += Regi_Registered;
            regi.StateChanged += Regi_StateChanged;

            if (_timerRegi == null)
            {
                _timerRegi = new Timer(timerRegi_Callback, null, 21000, Timeout.Infinite);
            }
            else
            {
                _timerRegi.Change(21000, Timeout.Infinite);
            }
            regi.BeginRegister(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses "name-addr" or "addr-spec" from specified reader.
        /// </summary>
        /// <param name="reader">Reader from where to parse.</param>
        /// <exception cref="ArgumentNullException">Raised when <b>reader</b> is null.</exception>
        /// <exception cref="SIP_ParseException">Raised when invalid SIP message.</exception>
        public void Parse(StringReader reader)
        {
            /* RFC 3261.
             *  name-addr =  [ display-name ] LAQUOT addr-spec RAQUOT
             *  addr-spec =  SIP-URI / SIPS-URI / absoluteURI
             */

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            reader.ReadToFirstChar();

            // LAQUOT addr-spec RAQUOT
            if (reader.StartsWith("<"))
            {
                m_pUri = AbsoluteUri.Parse(reader.ReadParenthesized());
            }
            else
            {
                string word = reader.ReadWord();
                if (word == null)
                {
                    throw new SIP_ParseException("Invalid 'name-addr' or 'addr-spec' value !");
                }

                reader.ReadToFirstChar();

                // name-addr
                if (reader.StartsWith("<"))
                {
                    m_DisplayName = word;
                    m_pUri        = AbsoluteUri.Parse(reader.ReadParenthesized());
                }
                // addr-spec
                else
                {
                    m_pUri = AbsoluteUri.Parse(word);
                }
            }
        }