Click here to Skip to main content
15,881,882 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 322.5K   14.9K   85  
STUN client C# implementation with sample application
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace LumiSoft.Net.SIP.Proxy
{
    /// <summary>
    /// SIP registration collection.
    /// </summary>
    public class SIP_RegistrationCollection : IEnumerable
    {
        private List<SIP_Registration> m_pRegistrations = null;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SIP_RegistrationCollection()
        {
            m_pRegistrations = new List<SIP_Registration>();
        }

        
        #region method Add

        /// <summary>
        /// Adds specified registration to collection.
        /// </summary>
        /// <param name="registration">Registration to add.</param>
        public void Add(SIP_Registration registration)
        {
            lock(m_pRegistrations){
                if(Contains(registration.Address)){
                    throw new ArgumentException("Registration with specified registration name already exists !");
                }

                m_pRegistrations.Add(registration);
            }
        }

        #endregion

        #region method Remove

        /// <summary>
        /// Deletes specified registration and all it's contacts. 
        /// </summary>
        /// <param name="addressOfRecord">Registration address of record what to remove.</param>
        public void Remove(string addressOfRecord)
        {
            lock(m_pRegistrations){
                foreach(SIP_Registration registration in m_pRegistrations){
                    if(registration.Address.ToLower() == addressOfRecord.ToLower()){
                        m_pRegistrations.Remove(registration);
                        break;
                    }
                }
            }
        }

        #endregion

        #region method Contains

        /// <summary>
        /// Gets if registration with specified name already exists.
        /// </summary>
        /// <param name="addressOfRecord">Address of record of resgistration.</param>
        /// <returns></returns>
        public bool Contains(string addressOfRecord)
        {
            lock(m_pRegistrations){
                foreach(SIP_Registration registration in m_pRegistrations){
                    if(registration.Address.ToLower() == addressOfRecord.ToLower()){
                        return true;
                    }
                }
            }

            return false;
        }

        #endregion


        #region method RemoveExpired

        /// <summary>
        /// Removes all expires registration from the collection.
        /// </summary>
        public void RemoveExpired()
        {
            lock(m_pRegistrations){
                for(int i=0;i<m_pRegistrations.Count;i++){
                    SIP_Registration registration = m_pRegistrations[i];

                    // Force registration to remove all its expired contacts.
                    registration.RemoveExpiredContacts();
                    // No contacs left, so we need to remove that registration.
                    if(registration.Contacts.Length == 0){
                        m_pRegistrations.Remove(registration);
                        i--;
                    }
                }
            }
        }

        #endregion


        #region interface IEnumerator

		/// <summary>
		/// Gets enumerator.
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return m_pRegistrations.GetEnumerator();
		}

		#endregion

        #region Properties implementation

        /// <summary>
        /// Gets number of registrations in the collection.
        /// </summary>
        public int Count
        {
            get{ return m_pRegistrations.Count; }
        }

        /// <summary>
        /// Gets registration with specified registration name. Returns null if specified registration doesn't exist.
        /// </summary>
        /// <param name="addressOfRecord">Address of record of resgistration.</param>
        /// <returns></returns>
        public SIP_Registration this[string addressOfRecord]
        {
            get{ 
                lock(m_pRegistrations){
                    foreach(SIP_Registration registration in m_pRegistrations){
                        if(registration.Address.ToLower() == addressOfRecord.ToLower()){
                            return registration;
                        }
                    }
                    return null;
                }
            }
        }

        /// <summary>
        /// Gets SIP registrations what in the collection.
        /// </summary>
        public SIP_Registration[] Values
        {
            get{ return m_pRegistrations.ToArray(); }
        }
                
        #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