Exemple #1
0
        public void SendResponse(SipResponse response)
        {
            Check.Require(response, "response");

            var result = new SipValidator().ValidateMessage(response);

            if (!result.IsValid)
            {
                ThrowSipException(result);
            }

            var sendToEndPoint = SipProvider.DetermineSendTo(response);

            var bytes = SipFormatter.FormatMessage(response);

            SendBytes(bytes, sendToEndPoint);

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Send response '{0}' --> {1}. # bytes:{2}.", response.StatusLine.StatusCode, sendToEndPoint, bytes.Length);
            }

            if (_responseSentObserver != null)
            {
                _responseSentObserver.OnNext(response);
            }
        }
Exemple #2
0
        public ISipProvider CreateSipProvider(SipListeningPoint listeningPoint)
        {
            Check.Require(listeningPoint, "listeningPoint");

            var contextSource = new SipContextSource(
                listeningPoint,
                CreateThreadPool(),
                CreateMessageFactory(),
                CreateHeaderFactory());

            var sipProvider = new SipProvider(this, contextSource);

            return(sipProvider);
        }
Exemple #3
0
        public void Start()
        {
            if(_configuration == null) throw new InvalidOperationException("The server is not configured.");

            _stack = new SipStack();
            _stack.MaxWorkerThreads = _configuration.MaxThreadPoolSize;
            _stack.MinWorkerThreads = _configuration.MinThreadPoolSize;
            _stack.EnableThreadPoolPerformanceCounters = _configuration.EnableThreadPoolPerformanceCounters;
             var listeningPoint = _stack.CreateUdpListeningPoint(_ipEndPoint);
            _provider = (SipProvider)_stack.CreateSipProvider(listeningPoint);
            _listener = new SipServerListener();
            _registrar = InitializeRegistrar();
            _listener.AddRequestHandler(_registrar);
            _provider.AddSipListener(_listener);
            //_stack.Start();
        }
Exemple #4
0
        private void SendErrorResponse(Exception exception, SipRequestEvent requestEvent)
        {
            Check.Require(exception, "exception");
            Check.Require(requestEvent, "requestEvent");
            Check.Require(requestEvent.Request, "requestEvent.Request");

            var request      = requestEvent.Request;
            var sipException = exception as SipException;

            string      responseCode = sipException != null ? sipException.ResponseCode : SipResponseCodes.x500_Server_Internal_Error;
            SipResponse response     = sipException != null?_stack.CreateMessageFactory().CreateResponse(request, responseCode) :
                                           _stack.CreateMessageFactory().CreateResponse(request, responseCode + ", " + exception.Message);

            if (requestEvent.ServerTransaction != null)
            {
                requestEvent.ServerTransaction.SendResponse(response);
            }
            else if (response.Vias.GetTopMost() != null)
            {
                IPEndPoint ipEndPoint = SipProvider.DetermineSendTo(response);

                try
                {
                    _contextSource.SendTo(SipFormatter.FormatMessage(response), ipEndPoint);
                    if (_responseSentObserver != null)
                    {
                        _responseSentObserver.OnNext(response);
                    }
                }
                catch (SocketException err)
                {
                    _logger.Error("Failed to send response to " + ipEndPoint.ToString(), err);
                }
            }
            else
            {
                _logger.Warn("Response can not be sent. Via TopMost header missing.");
            }
        }
Exemple #5
0
        public ISipProvider CreateSipProvider(SipListeningPoint listeningPoint)
        {
            Check.Require(listeningPoint, "listeningPoint");

            var contextSource = new SipContextSource(
                listeningPoint,
                CreateThreadPool(),
                CreateMessageFactory(),
                CreateHeaderFactory());

            var sipProvider = new SipProvider(this, contextSource);

            return sipProvider;
        }
Exemple #6
0
        private void OnIncomingRequestContext(SipContext context)
        {
            if (_requestReceivedObserver != null)
            {
                _requestReceivedObserver.OnNext(context.Request);
            }

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Validating the request...");
            }

            var result = new SipValidator().ValidateMessage(context.Request);

            if (!result.IsValid)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("The received request is not valid. Throwing a sip exception.");
                }

                ThrowSipException(result);
            }

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Request valid.");
            }

            /*firewall traversal not supported. SupportFirewalTraversal(context);*/

            var requestEvent = new SipRequestEvent(context);

            ISipRequestProcessor sipRequestProcessor = _sipListener;

            //8.2.2.2: merged requests, not implemented

            SipAbstractServerTransaction stx;

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Searching the table for a matching tx..");
            }

            if (_stxTable.TryGetValue(GetServerTransactionId(context.Request), out stx))
            {
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace("Found a matching tx. Setting it as the requestprocessor.");
                }

                sipRequestProcessor = stx;

                //if (IsDialogInitiatingRequest(stx.Request))
                //{
                //if (_logger.IsDebugEnabled) _logger.Debug("The received request is an 'INVITE' request. Try setting the dialog on tx...");

                //set the dialog, so it can initiate
                SipAbstractDialog found;
                if (TryGetDialogOnTx(context.Request, out found))
                {
                    requestEvent.Dialog = found;
                }
                //}
            }
            else
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Could not find a matching tx..");
                }

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Searching the table for a matching dialog...");
                }

                SipAbstractDialog dialog;
                if (_dialogTable.TryGetValue(GetDialogId(context.Request, true), out dialog))
                {
                    if (_logger.IsTraceEnabled)
                    {
                        _logger.Trace("Found a matching dialog. Setting it as the requestprocessor.");
                    }

                    sipRequestProcessor = dialog;
                    requestEvent.Dialog = dialog;
                }
                else
                {
                    if (_logger.IsTraceEnabled)
                    {
                        _logger.Trace("Could not find a matching dialog. Using the SipProvider's SipListener as the requestprocessor.");
                    }
                }
            }

            try
            {
                sipRequestProcessor.ProcessRequest(requestEvent);

                if (requestEvent.IsSent)
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug("Processed '{0}' request. The response has already been sent. Skipping automatic response sending.", requestEvent.Request.RequestLine.Method);
                    }
                    return;
                }

                if (!ShouldHaveResponse(requestEvent.Request))
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug("Processed '{0}' request. The request does not require a response. Skipping automatic response sending.", requestEvent.Request.RequestLine.Method);
                    }
                    return;
                }

                if (requestEvent.Response == null)
                {
                    throw new SipCoreException("Response to send can not be null. The ProcessRequest method is supposed to create a response message that is to be sent.");
                }

                if (!requestEvent.IsSent)
                {
                    var response = requestEvent.Response;

                    var remoteEndPoint = SipProvider.DetermineSendTo(response);

                    _contextSource.SendTo(SipFormatter.FormatMessage(response), remoteEndPoint);

                    if (_responseSentObserver != null)
                    {
                        _responseSentObserver.OnNext(response);
                    }
                }
            }
            catch (SipCoreException coreException)
            {
                throw coreException;
            }
            catch (SipException sipException)
            {
                if (ShouldHaveResponse(requestEvent.Request))
                {
                    SendErrorResponse(sipException, requestEvent);
                }
            }
            catch (Exception err)
            {
                _logger.ErrorException("Request failed.", err);

                try
                {
                    if (ShouldHaveResponse(requestEvent.Request))
                    {
                        SendErrorResponse(err, requestEvent);
                    }
                }
                catch (Exception errr)
                {
                    _logger.Debug("Failed to send the response.", errr);
                }
            }
        }
 protected override void Given()
 {
     _contextSource = new FakeSipContextSource();
     _sipStack = new SipStack();
     _provider = new SipProvider(_sipStack, _contextSource);
     _inviteRequest = CreateInviteRequest();
     BeforeCreateInviteTransaction();
     _inviteTransaction = _provider.CreateClientTransaction(_inviteRequest).As<SipInviteClientTransaction>();
     _dialog = _provider.CreateClientDialog(_inviteTransaction);
     _inviteTransaction.SendRequest();
     GivenOverride();
 }
Exemple #8
0
 void DiagnosticsForm_Load(object sender, EventArgs e)
 {
     _sipProvider = MainForm.SipProvider;
     _tmrRefresh.Start();
     _usageThreadsInPool.Maximum = MainForm.SipStack.MaxWorkerThreads;
 }
Exemple #9
0
 public DialogListener(SipProvider provider)
 {
     _provider = provider;
 }
        protected override void Given()
        {
            //create invite that is addresses to the phone's sipuri
            _invite = CreateInviteRequest(_testClientUaUri, _phoneUaUri);
            //create phone that is located at IpEndPoint2
            var phoneCs = new FakeSipContextSource(_phoneUaEndPoint);

            _network = new FakeNetwork();
            _sipProvider1 = new SipProvider(new SipStack(), phoneCs);

            _phone = new SoftPhone(_sipProvider1, new SipMessageFactory(), new SipHeaderFactory(), new SipAddressFactory(), _stateProvider, _timerFactory, new SipListeningPoint(_phoneUaEndPoint));
            phoneCs.AddToNetwork(_network);
            _network.AddReceiver(_testClientUaEndPoint, OnTestClientUaReceive);
            _phone.InternalStateChanged += new EventHandler<EventArgs>(_calleePhone_InternalStateChanged);
            _phone.IncomingCall += new EventHandler<VoipEventArgs<IPhoneCall>>(_calleePhone_IncomingCall);
            _phone.Start();

            GivenOverride();
        }