Click here to Skip to main content
12,351,423 members (34,977 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

170.6K views
9.5K downloads
65 bookmarked
Posted

STUN Client

, 20 Apr 2007 CPOL
STUN client C# implementation with sample application
Stun
Net
docs
dns
dns_records.jpg
dns_records.vsd
Net
_junk
_Obsolete
_Stuff
AUTH
bin
Release
LumiSoft.Net.dll
Data
Dns
Client
FTP
Client
Server
HTTP
Server
ICMP
IMAP
Client
Server
IO
Log
LumiSoft.Net
Mime
vCard
Net.csproj.user
Net.suo
NNTP
Client
POP3
Client
Server
SDP
ServersCore
SIP
Client
Message
Proxy
Stack
SMTP
Client
Server
STUN
Client
Message
STUN_Client_app
STUN_Client_app.suo
STUN_Client_app
bin
Debug
LumiSoft.Net.dll
STUN_Client_app.exe
STUN_Client_app.vshost.exe
dep
LumiSoft.Net.dll
Properties
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)

Share

About the Author

Ivar Lumi
Estonia Estonia
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Ivar Lumi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid