예제 #1
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;
            }
        }
예제 #2
0
        public void Go_Send(int currentPort)
        {
            try
            {
                SipUri from = new SipUri(this.AppServerSipUri);

                string AlarmEndPointSipUri = "sip:contact@" + this.LocalIpAddress + ":" + currentPort;
                SipUri to = new SipUri(AlarmEndPointSipUri);

                SipRequest request = MessageRequest.CreateRequest(SipMethod.Message, to, from);
                Random     random  = new Random();

                int cSeq = random.Next(0, 10000);
                request.CSeq.SequenceNumber = cSeq;
                request.CSeq.Method         = SipMethod.Message;

                //int FreePort = DoroCommon.GetAvailablePort(5060);
                //Console.WriteLine(FreePort);

                IPAddress localIP = NetworkInformation.IPv4Address;

                ViaHeaderField via = new ViaHeaderField(new IPDomainPort(new IPEndPoint(localIP, currentPort)), TransportProtocol.Udp);
                via.ResponsePort = currentPort;
                request.ViaHeaders.Clear();
                request.ViaHeaders.Add(via);

                AlarmReq ar = new AlarmReq();
                ar.Ref = random.Next(0, 16).ToString();
                ar.Aty = "PI";
                ar.Tty = "C9300";
                ar.Tid = "7942098";
                ar.Inf = "Heartbeat";

                Console.WriteLine(ar.ToString());

                string body = SerializationClass.SerializeObject(ar, false, true, true);

                byte[] content = Encoding.UTF8.GetBytes(body);

                Console.WriteLine(content.ToString());

                request.Body          = content;
                request.ContentType   = new ContentTypeHeaderField(MediaType.Text, "text");
                request.ContentLength = content.Length;

                //Console.WriteLine(request.Body.ToString());

                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(this.ServerIpAddress), this.ServerPort);
                IPEndPoint localEndpoint  = new IPEndPoint(localIP, currentPort);

                Protocol.DestinationTuple d = new Protocol.DestinationTuple(localEndpoint, remoteEndPoint);

                Console.WriteLine("Open Port: " + localIP + ":" + currentPort + " => Sent Message to " + this.ServerIpAddress + ":" + this.ServerPort);
                Set_TextBox("Open Port: " + localIP + ":" + currentPort + " => Sent Message to " + this.ServerIpAddress + ":" + this.ServerPort);

                //Protocol.DestinationTuple d = new Protocol.DestinationTuple(this.ServerIpAddress.ToString(), this.ServerPort);

                CipConnection cipConnection = new CipConnection(this);
                cipConnection.StartListening(currentPort);
                cipConnection.Send(request, d);

                Interlocked.Increment(ref CountConnection);
                Set_Status();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }