SIPSorcery.SIP.SIPTransport.SendRequest C# (CSharp) Method

SendRequest() private method

private SendRequest ( SIPChannel sipChannel, SIPEndPoint dstEndPoint, SIPRequest sipRequest ) : void
sipChannel SIPChannel
dstEndPoint SIPEndPoint
sipRequest SIPRequest
return void
        private void SendRequest(SIPChannel sipChannel, SIPEndPoint dstEndPoint, SIPRequest sipRequest)
        {
            try
            {
                if (dstEndPoint != null && dstEndPoint.Address.Equals(BlackholeAddress))
                {
                    // Ignore packet, it's destined for the blackhole.
                    return;
                }

                if (m_sipChannels.Count == 0)
                {
                    throw new ApplicationException("No channels are configured in the SIP transport layer. The request could not be sent.");
                }

                sipRequest.Header.ContentLength = (sipRequest.Body.NotNullOrBlank()) ? Encoding.UTF8.GetByteCount(sipRequest.Body) : 0;

                if (sipChannel.IsTLS)
                {
                    sipChannel.Send(dstEndPoint.GetIPEndPoint(), Encoding.UTF8.GetBytes(sipRequest.ToString()), sipRequest.URI.Host);
                }
                else
                {
                    sipChannel.Send(dstEndPoint.GetIPEndPoint(), Encoding.UTF8.GetBytes(sipRequest.ToString()));
                }

                if (SIPRequestOutTraceEvent != null)
                {
                    FireSIPRequestOutTraceEvent(sipChannel.SIPChannelEndPoint, dstEndPoint, sipRequest);
                }
            }
            catch (ApplicationException appExcp)
            {
                logger.Warn("ApplicationException SIPTransport SendRequest. " + appExcp.Message);

                SIPResponse errorResponse = GetResponse(sipRequest, SIPResponseStatusCodesEnum.InternalServerError, appExcp.Message);

                // Remove any Via headers, other than the last one, that are for sockets hosted by this process.
                while (errorResponse.Header.Vias.Length > 0)
                {
                    if (IsLocalSIPEndPoint(SIPEndPoint.ParseSIPEndPoint(errorResponse.Header.Vias.TopViaHeader.ReceivedFromAddress)))
                    {
                        errorResponse.Header.Vias.PopTopViaHeader();
                    }
                    else
                    {
                        break;
                    }
                }

                if (errorResponse.Header.Vias.Length == 0)
                {
                    logger.Warn("Could not send error response for " + appExcp.Message + " as no non-local Via headers were available.");
                }
                else
                {
                    SendResponse(errorResponse);
                }
            }
        }

Same methods

SIPTransport::SendRequest ( SIPEndPoint dstEndPoint, SIPRequest sipRequest ) : void
SIPTransport::SendRequest ( SIPRequest sipRequest ) : void

Usage Example

            public void B2BOptionsStatefulProxyTest()
            {
                SIPTransactionEngine transactionEngine1 = new SIPTransactionEngine();
                SIPTransport sipTransport1 = new SIPTransport(SIPDNSManager.Resolve, transactionEngine1, true, false);
                sipTransport1.AddSIPChannel(new SIPUDPChannel(new IPEndPoint(IPAddress.Loopback, 3000)));
                SIPAppServerCore appServerCore1 = new SIPAppServerCore(sipTransport1, null, statefulProxyCore1_StatefulProxyLogEvent, null, null, null);

                SIPTransactionEngine transactionEngine2 = new SIPTransactionEngine();
                SIPTransport sipTransport2 = new SIPTransport(SIPDNSManager.Resolve, transactionEngine2, true, false);
                sipTransport2.AddSIPChannel(new SIPUDPChannel(new IPEndPoint(IPAddress.Loopback, 3001)));
                SIPAppServerCore appServerCore2 = new SIPAppServerCore(sipTransport2, null, statefulProxyCore2_StatefulProxyLogEvent, null, null, null);

                sipTransport1.SIPRequestOutTraceEvent += sipTransport1_SIPRequestOutTraceEvent;
                sipTransport1.SIPResponseInTraceEvent += sipTransport1_SIPResponseInTraceEvent;
                sipTransport2.SIPRequestInTraceEvent += sipTransport2_SIPRequestInTraceEvent;

                SIPRequest optionsRequest = GetOptionsRequest(SIPURI.ParseSIPURI("sip:127.0.0.1:3001"), 1, sipTransport1.GetDefaultTransportContact(SIPProtocolsEnum.udp).SocketEndPoint);
                sipTransport1.SendRequest(optionsRequest);

                Thread.Sleep(200);

                // Check the NUnit Console.Out to make sure there are SIP requests and responses being displayed.

                sipTransport1.Shutdown();
                sipTransport2.Shutdown();
            }
All Usage Examples Of SIPSorcery.SIP.SIPTransport::SendRequest