Beispiel #1
0
        /// <summary>
        /// Starts the client transaction.
        /// </summary>
        public void Start()
        {
            SipCSeqValue vCSeq;

            // Add a CSeq header to the request if necessary and intialize the
            // transaction's local sequence number.

            vCSeq = request.GetHeader <SipCSeqValue>(SipHeader.CSeq);
            if (vCSeq == null)
            {
                request.AddHeader(SipHeader.CSeq, new SipCSeqValue(SipHelper.GenCSeq(), request.MethodText));
            }

            // Add Max-Forwards if necessary.

            if (request.GetHeaderText(SipHeader.MaxForwards) == null)
            {
                request.AddHeader(SipHeader.MaxForwards, SipHelper.MaxForwards);
            }

            // Start the transaction

            using (TimedLock.Lock(agent))
            {
                SetState(request.Method == SipMethod.Invite ? SipTransactionState.InviteCalling : SipTransactionState.Trying);
                transport.Send(remoteEP, request);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates the proper ACK request based on the original INVITE request sent
        /// to the server and the 2xx response received.
        /// </summary>
        /// <param name="inviteRequest">The INVITE <see cref="SipRequest" /> sent to the server.</param>
        /// <param name="response">The 2xx <see cref="SipResponse" /> received from the server.</param>
        /// <returns>The created ACK <see cref="SipRequest" />.</returns>
        private SipRequest CreateAckRequest(SipRequest inviteRequest, SipResponse response)
        {
            SipRequest   ackRequest;
            SipHeader    callID;
            SipHeader    to;
            SipHeader    from;
            SipHeader    via;
            SipHeader    contact;
            SipHeader    route;
            SipCSeqValue vCSeq;

            if (inviteRequest.Method != SipMethod.Invite)
            {
                throw new ArgumentException("INVITE request expected.", "inviteRequest");
            }

            if (response.IsProvisional)
            {
                throw new ArgumentException("Non-provisional response expected.", "response");
            }

            ackRequest = new SipRequest(SipMethod.Ack, inviteRequest.Uri, null);

            callID  = inviteRequest[SipHeader.CallID];
            to      = response[SipHeader.To];
            from    = inviteRequest[SipHeader.From];
            via     = inviteRequest[SipHeader.Via];
            contact = inviteRequest[SipHeader.Contact];
            route   = inviteRequest[SipHeader.Route];

            if (callID == null)
            {
                throw new SipException("INVITE request is missing header: [Call-ID]");
            }

            if (to == null)
            {
                throw new SipException("INVITE response is missing header: [To]");
            }

            if (from == null)
            {
                throw new SipException("INVITE request is missing header: [From]");
            }

            if (contact == null)
            {
                throw new SipException("INVITE request is missing header: [Contact]");
            }

            if (via == null)
            {
                throw new SipException("INVITE request is missing header: [Via]");
            }

            vCSeq = inviteRequest.GetHeader <SipCSeqValue>(SipHeader.CSeq);
            if (vCSeq == null)
            {
                throw new SipException("INVITE request is missing header: [CSeq]");
            }

            ackRequest.AddHeader(SipHeader.Via, via.Text);
            ackRequest.AddHeader(SipHeader.To, to.Text);
            ackRequest.AddHeader(SipHeader.From, from.Text);
            ackRequest.AddHeader(SipHeader.Contact, contact.Text);
            ackRequest.AddHeader(SipHeader.CallID, callID.Text);
            ackRequest.AddHeader(SipHeader.CSeq, new SipCSeqValue(vCSeq.Number, "ACK"));
            ackRequest.AddHeader(SipHeader.MaxForwards, SipHelper.MaxForwards);
            ackRequest.AddHeader(SipHeader.UserAgent, agent.Core.Settings.UserAgent);

            if (route != null)
            {
                ackRequest.Headers.Add(SipHeader.Route, route.Clone());
            }

            return(ackRequest);
        }
Beispiel #3
0
        /// <summary>
        /// Initiates an asynchronous SIP request transaction.
        /// </summary>
        /// <param name="request">The <see cref="SipRequest" /> to be submitted.</param>
        /// <param name="dialog">The <see cref="SipDialog" /> for requests that initiate a dialog (or <c>null</c>).</param>
        /// <param name="callback">The delegate to be called when the operation completes (or <c>null</c>).</param>
        /// <param name="state">Application defined state (or <c>null</c>).</param>
        /// <returns>The <see cref="IAsyncResult" /> to be used to track the operation's progress.</returns>
        /// <remarks>
        /// <para>
        /// All requests to <see cref="BeginRequest(SipRequest,SipDialog,AsyncCallback,object)" /> must be matched with a
        /// call to <see cref="EndRequest" />.
        /// </para>
        /// <note>
        /// This method adds reasonable <b>Call-ID</b> and <b>CSeq</b> headers to the request if these
        /// headers are not already present.
        /// </note>
        /// </remarks>
        public IAsyncResult BeginRequest(SipRequest request, SipDialog dialog, AsyncCallback callback, object state)
        {
            ClientAsyncResult    arClient = new ClientAsyncResult(request, dialog, callback, state);
            SipValue             viaValue;
            SipCSeqValue         vCSeq;
            string               transactionID;
            SipClientTransaction transaction;
            ISipTransport        transport;
            NetworkBinding       remoteEP;

            if (dialog != null && request.Method != SipMethod.Invite)
            {
                throw new InvalidOperationException("Dialogs may be created only for INVITE requests.");
            }

            arClient.Dialog = dialog;

            transport = router.SelectTransport(this, request, out remoteEP);
            if (transport == null)
            {
                throw new SipException("No approriate transport is available.");
            }

            // Initialize the request's Via header and transaction ID as necessary.

            transactionID      = SipHelper.GenerateBranchID();
            viaValue           = new SipValue(string.Format("SIP/2.0/{0} {1}", transport.Name, transport.Settings.ExternalBinding.Address));
            viaValue["branch"] = transactionID;
            viaValue["rport"]  = string.Empty;

            request.PrependHeader(SipHeader.Via, viaValue);

            // Initialize common headers as necessary

            if (!request.ContainsHeader(SipHeader.CallID))
            {
                request.AddHeader(SipHeader.CallID, SipHelper.GenerateCallID());
            }

            vCSeq = request.GetHeader <SipCSeqValue>(SipHeader.CSeq);
            if (vCSeq == null)
            {
                vCSeq = new SipCSeqValue(SipHelper.GenCSeq(), request.MethodText);
                request.AddHeader(SipHeader.CSeq, vCSeq);
            }

            // Initialize the transaction

            transaction            = new SipClientTransaction(this, request, transactionID, transport, remoteEP);
            transaction.AgentState = arClient;

            // Handle initial dialog INVITE specific initialization

            if (dialog != null && request.Method == SipMethod.Invite && dialog.State == SipDialogState.Waiting)
            {
                // Client-side dialogs need to know the transaction so
                // they'll be able to send the confirming ACK.

                dialog.InitiatingTransaction = transaction;

                // Dialogs need to know about the sequence number used in INVITE requests so
                // that the ACK can be generated with the same sequence number.

                dialog.AckCSeq = vCSeq.Number;

                // The dialog has been intialized enough to be added to the core's
                // early dialog table.

                core.AddEarlyDialog(dialog);
            }

            // Start the transaction

            using (TimedLock.Lock(this))
            {
                transactions.Add(transactionID, transaction);
            }

            transaction.Start();
            arClient.Started();

            return(arClient);
        }