예제 #1
0
        private static ToHeaderField ParseToHeaderField(string p)
        {
            try
            {
                string name;
                if (p.StartsWith(ToHeaderField.LongName))
                {
                    name = ToHeaderField.LongName + ":";
                }
                else
                {
                    name = ToHeaderField.ShortName + ":";
                }
                name = name.ToLower();
                p    = p.ToLower();

                int           s = p.IndexOf(name);
                int           e = p.IndexOf("<");
                string        u = p.Substring(e + 1, p.Length - e - 2);
                ToHeaderField t = new ToHeaderField();
                t.Uri = new SipUri(u);
                return(t);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
예제 #2
0
        public static SipResponse GetSipReponse(string message, byte[] networkData, int readSize)
        {
            try
            {
                string[] parts = message.Split('\n');
                if (parts.Length == 0)
                {
                    return(null);
                }

                StatusLine sl = ParseStatusLine(parts[0]);
                if (sl == null)
                {
                    return(null);
                }

                SipResponse response = new SipResponse();
                response.StatusLine = sl;

                foreach (string pt in parts)
                {
                    if (pt.StartsWith(ViaHeaderField.LongName))
                    {
                        string         p   = pt.Replace('\r', ' ').TrimEnd();
                        ViaHeaderField via = new ViaHeaderField();
                        via.Parse(p);
                        response.ViaHeaders.Add(via);
                    }
                    else if (pt.StartsWith(ToHeaderField.LongName))
                    {
                        string        p   = pt.Replace('\r', ' ').TrimEnd();
                        ToHeaderField thf = ParseToHeaderField(p);
                        response.To = thf;
                    }
                    else if (pt.StartsWith(FromHeaderField.LongName))
                    {
                        string          p   = pt.Replace('\r', ' ').TrimEnd();
                        FromHeaderField fhf = ParseFromHeaderField(p);
                        response.From = fhf;
                    }
                    else if (pt.StartsWith(CallIdHeaderField.LongName))
                    {
                        string            p   = pt.Replace('\r', ' ').TrimEnd();
                        CallIdHeaderField chf = new CallIdHeaderField();
                        chf.Parse(p);
                        response.CallId = chf;
                    }
                    else if (pt.StartsWith(CSeqHeaderField.LongName))
                    {
                        string          p    = pt.Replace('\r', ' ').TrimEnd();
                        CSeqHeaderField cshf = new CSeqHeaderField();
                        cshf.Parse(p);
                        response.CSeq = cshf;
                    }
                    else if (pt.StartsWith(ContentLengthHeaderField.LongName))
                    {
                        string p = pt.Replace('\r', ' ').TrimEnd();
                        ContentLengthHeaderField clhf = new ContentLengthHeaderField();
                        clhf.Parse(p);
                        response.ContentLength = clhf;
                        int len = clhf.Length;
                        if (len > 0)
                        {
                            response.Body = new byte[len];
                            Array.Copy(networkData, (readSize - len), response.Body, 0, len);
                        }
                    }
                    else if (pt.StartsWith(ContentTypeHeaderField.LongName))
                    {
                        string p = pt.Replace('\r', ' ').TrimEnd();
                        ContentTypeHeaderField cthf = new ContentTypeHeaderField();
                        cthf.Parse(p);
                        response.ContentType = cthf;
                    }
                }
                return(response);
            }
            catch (Exception exception)
            {
                string error_text = String.Concat(Assembly.GetExecutingAssembly().GetName().Name, Dns.GetHostName()
                                                  , "[CipSipParser][GetSipReponse]: " + message + " | exception: " + exception.Message + " | innerException: " + (exception.InnerException != null && !string.IsNullOrEmpty(exception.InnerException.Message) ? exception.InnerException.Message : "")
                                                  , exception.StackTrace);
                Console.WriteLine(error_text);
                throw exception;
            }
        }