Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

STUN Client

Rate me:
Please Sign up or sign in to vote.
4.83/5 (36 votes)
20 Apr 2007CPOL 323.4K   14.9K   85  
STUN client C# implementation with sample application
using System;
using System.Collections.Generic;
using System.Text;

using LumiSoft.Net.SIP.Message;
using LumiSoft.Net.SIP.Stack;

namespace LumiSoft.Net.SIP.Proxy
{
    /// <summary>
    /// Implements SIP registration contact.
    /// </summary>
    public class SIP_RegistrationContact : IComparable
    {
        private SIP_t_ContactParam m_pContact        = null;
        private int                m_Expire          = 0;
        private DateTime           m_LastUpdate;
        private SIP_EndPointInfo   m_pRemoteEndPoint = null; 

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="contact">SIP contact.</param>
        /// <param name="expires">Time in seconds when contact expires.</param>
        /// <param name="remoteEP">SIP remote end point info what did registration.</param>
        public SIP_RegistrationContact(SIP_t_ContactParam contact,int expires/*,SIP_EndPointInfo remoteEP*/)
        {
             UpdateContact(contact,expires/*,remoteEP*/);
        }


        #region method UpdateContact

        /// <summary>
        /// Updates contact info.
        /// </summary>
        /// <param name="contact">SIP contact.</param>
        /// <param name="expires">Time in seconds when contact expires.</param>
        /// <param name="remoteEP">SIP remote end point info what did registration.</param>
        public void UpdateContact(SIP_t_ContactParam contact,int expires/*,SIP_EndPointInfo remoteEP*/)
        {
            m_pContact        = contact;
            m_Expire          = expires;
            //m_pRemoteEndPoint = remoteEP;
            m_LastUpdate      = DateTime.Now;
        }

        #endregion

        #region method ToStringValue

        /// <summary>
        /// Returns this.Contact as string value, but expires parameter is replaced with remaining time value.
        /// </summary>
        /// <returns></returns>
        public string ToStringValue()
        {
            SIP_t_ContactParam retVal = new SIP_t_ContactParam();
            retVal.Parse(new StringReader(m_pContact.ToStringValue()));
            retVal.Expires = this.Expires;

            return retVal.ToStringValue();
        }

        #endregion


        #region IComparable interface Implementation

        /// <summary>
        /// Compares the current instance with another object of the same type. 
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>Returns 0 if two objects equal, -1 if this instance is less or 1 this instance is greater.</returns>
        public int CompareTo(object obj)
        {
            if(obj == null){
                return -1;
            }
            if(!(obj is SIP_RegistrationContact)){
                return -1;
            }

            // We must reverse values, because greater value mean higer priority.

            SIP_RegistrationContact compareValue = (SIP_RegistrationContact)obj;
            if(compareValue.QValue == this.QValue){
                return 0;
            }
            else if(compareValue.QValue > this.QValue){
                return 1;
            }            
            else if(compareValue.QValue < this.QValue){
                return -1;
            }

            return -1;
        }

        #endregion

        #region Properties Implementation

        /// <summary>
        /// Gets SIP contact.
        /// </summary>
        public SIP_t_ContactParam Contact
        {
            get{ return m_pContact; }
        }

        /// <summary>
        /// Gets if this contact is expired.
        /// </summary>
        public bool IsExpired
        {
            get{ return this.Expires == 0; }
        }

        /// <summary>
        /// Gets after how many seconds this contact expires. This is live calulated value, so it decreses every second.
        /// </summary>
        public int Expires
        {
            get{ 
                if(DateTime.Now > m_LastUpdate.AddSeconds(m_Expire)){
                    return 0;
                }
                else{
                    return (int)((TimeSpan)(m_LastUpdate.AddSeconds(m_Expire) - DateTime.Now)).TotalSeconds;
                }
            }
        }

        /// <summary>
        /// Gets qvalue. QValue is used for contacts sorting. Higer value means higher priority.
        /// </summary>
        public double QValue
        {
            get{
                // No qvalue value = 1.0.  Not checked from RFC, but found some where in net. 
                // If some one know exact RFC topic, let me know.
                if(m_pContact.QValue == -1){
                    return 1.0;
                }
                return m_pContact.QValue; 
            }
        }

        /// <summary>
        /// Gets remote end point info what did registration.
        /// </summary>
        public SIP_EndPointInfo RemoteEndPoint
        {
            get{ return m_pRemoteEndPoint; }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions