Click here to Skip to main content
13,048,766 members (68,626 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

199.5K views
10.7K downloads
68 bookmarked
Posted 20 Apr 2007

STUN Client

, 20 Apr 2007
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.Net;

namespace LumiSoft.Net
{
	/// <summary>
	/// This is base class for SocketServer sessions.
	/// </summary>
	public abstract class SocketServerSession
	{
        private string       m_SessionID = "";
        private SocketEx     m_pSocket   = null;
        private SocketServer m_pServer   = null;
        private BindInfo     m_pBindInfo = null;        
        private DateTime     m_SessionStartTime;
        private string       m_UserName  = "";
        private object       m_Tag       = null;
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="sessionID">Session ID.</param>
        /// <param name="socket">Server connected socket.</param>
        /// <param name="bindInfo">BindInfo what accepted socket.</param>
        /// <param name="server">Reference to server.</param>
        public SocketServerSession(string sessionID,SocketEx socket,BindInfo bindInfo,SocketServer server)
        {
            m_SessionID = sessionID;
            m_pSocket   = socket;
            m_pBindInfo = bindInfo;
            m_pServer   = server;
                        
            m_SessionStartTime = DateTime.Now;
        }


		#region method Kill

        /// <summary>
        /// Kills session.
        /// </summary>
        public virtual void Kill()
        {
        }

        #endregion


        #region method OnSessionTimeout

        /// <summary>
		/// Times session out.
		/// </summary>
		internal protected virtual void OnSessionTimeout()
        {           
        }

        #endregion

        #region method SetUserName

        /// <summary>
        /// Sets property UserName value.
        /// </summary>
        /// <param name="userName">User name.</param>
        protected void SetUserName(string userName)
        {
            m_UserName = userName;

            if(m_pSocket.Logger != null){
                m_pSocket.Logger.UserName = m_UserName;
            }
        }

        #endregion


        #region Properties Implemtation

        /// <summary>
		/// Gets session ID.
		/// </summary>
		public string SessionID
		{
			get{ return m_SessionID; }
		}

        /// <summary>
		/// Gets session start time.
		/// </summary>
		public DateTime SessionStartTime
		{
			get{ return m_SessionStartTime; }
		}

        /// <summary>
        /// Gets if session is authenticated.
        /// </summary>
        public bool Authenticated
        {
            get{
                if(m_UserName.Length > 0){
                    return true;
                }
                else{
                    return false;
                }
            }
        }

        /// <summary>
		/// Gets authenticated user name.
		/// </summary>
		public string UserName
		{
			get{ return m_UserName; }
		}

        /// <summary>
        /// Gets how many seconds has left before timout is triggered.
        /// </summary>
        public int ExpectedTimeout
        {
            get{
                return (int)((m_pServer.SessionIdleTimeOut - ((DateTime.Now.Ticks - SessionLastDataTime.Ticks) / 10000)) / 1000);
            }
        }

        /// <summary>
		/// Gets last data activity time.
		/// </summary>
		public DateTime SessionLastDataTime
		{
			get{
                if(m_pSocket == null){
                    return DateTime.MinValue;
                }
                else{
                    return m_pSocket.LastActivity;
                }
            }
        }
                
        /// <summary>
		/// Gets EndPoint which accepted conection.
		/// </summary>
		public IPEndPoint LocalEndPoint
		{
			get{ return (IPEndPoint)m_pSocket.LocalEndPoint; }
		}

		/// <summary>
		/// Gets connected Host(client) EndPoint.
		/// </summary>
		public IPEndPoint RemoteEndPoint
		{
			get{ 
                try{
                    return (IPEndPoint)m_pSocket.RemoteEndPoint; 
                }
                catch{ // Socket closed/disposed already
                    return null;
                }
            }
		}
		
		/// <summary>
		/// Gets or sets custom user data.
		/// </summary>
		public object Tag
		{
			get{ return m_Tag; }

			set{ m_Tag = value; }
		}

        /// <summary>
        /// Gets log entries that are currently in log buffer.
        /// </summary>
        public SocketLogger SessionActiveLog
        {
            get{ return m_pSocket.Logger; }
        }

        /// <summary>
        /// Gets how many bytes are readed through this session.
        /// </summary>
        public long ReadedCount
        {
            get{ return m_pSocket.ReadedCount; }
        }
        
        /// <summary>
        /// Gets how many bytes are written through this session.
        /// </summary>
        public long WrittenCount
        {
            get{ return m_pSocket.WrittenCount; }
        }
        
        /// <summary>
        /// Gets if the connection is an SSL connection.
        /// </summary>
        public bool IsSecureConnection
        {
            get{ return m_pSocket.SSL; }
        }



        /// <summary>
        /// Gets access to SocketEx.
        /// </summary>
        protected SocketEx Socket
        {
            get{ return m_pSocket; }
        }

        /// <summary>
        /// Gets access to BindInfo what accepted socket.
        /// </summary>
        protected BindInfo BindInfo
        {
            get{ return m_pBindInfo; }
        }

        #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...

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