Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

STUN Client

, 20 Apr 2007 CPOL
STUN client C# implementation with sample application
Stun.zip
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
Settings.settings
using System;

namespace LumiSoft.Data.lsDB
{
	/// <summary>
	/// 
	/// </summary>
	public class LDB_DataColumn
	{
		private LDB_DataType m_DataType   = LDB_DataType.String;
		private string       m_ColumnName = "";
		private int          m_ColumSize  = -1;

        /// <summary>
		/// Default constructor.
		/// </summary>
		/// <param name="columnName">Column name.</param>
		/// <param name="dataType">Column data type.</param>
        public LDB_DataColumn(string columnName,LDB_DataType dataType) : this(columnName,dataType,-1)
        {
        }

		/// <summary>
		/// Default constructor.
		/// </summary>
		/// <param name="columnName">Column name.</param>
		/// <param name="dataType">Column data type.</param>  
        /// <param name="columnSize">Specifies column data size. This is available for String datatype only.</param>  
		public LDB_DataColumn(string columnName,LDB_DataType dataType,int columnSize)
		{
			m_ColumnName = columnName;
			m_DataType = dataType;

			// TODO: check that column name won't exceed 50 bytes

            if(dataType == LDB_DataType.Bool){
                m_ColumSize = 1;
            }
            else if(dataType == LDB_DataType.DateTime){
                m_ColumSize = 13;
            }
            else if(dataType == LDB_DataType.Int){
                m_ColumSize = 4;
            }
            else if(dataType == LDB_DataType.Long){
                m_ColumSize = 8;
            }
            else{
                m_ColumSize = columnSize;
            }
		}

        /// <summary>
        /// Internal constructor.
        /// </summary>
		internal LDB_DataColumn()
		{
		}


		#region method Parse

		/// <summary>
		/// Parses column from byte[] data.
		/// </summary>
		/// <param name="columnData">Column data.</param>
		internal void Parse(byte[] columnData)
		{
			if(columnData.Length != 102){
				throw new Exception("Invalid column data length '" + columnData.Length.ToString() + "' !");
			}

			//-- total 102 bytes
			// 1 byte   - column type
			// 4 bytes  - column size
			// 45 bytes - reserved
			// 50 bytes - column name
            // 2  bytes - CRLF

			// column type
			m_DataType = (LDB_DataType)columnData[0];
			// column size
			m_ColumSize = (columnData[1] << 24) | (columnData[2] << 16) | (columnData[3] << 8) | (columnData[4] << 0);
			// reserved
			// 45 bytes
			// column name
			byte[] columnName = new byte[50];
			Array.Copy(columnData,50,columnName,0,columnName.Length);
			m_ColumnName = GetChar0TerminatedString(System.Text.Encoding.UTF8.GetString(columnName));

            if(m_DataType == LDB_DataType.Bool){
                m_ColumSize = 1;
            }
            else if(m_DataType == LDB_DataType.DateTime){
                m_ColumSize = 13;
            }
            else if(m_DataType == LDB_DataType.Int){
                m_ColumSize = 4;
            }
            else if(m_DataType == LDB_DataType.Long){
                m_ColumSize = 8;
            }
		}

		#endregion

		#region method ToColumnInfo

		/// <summary>
		/// Convert column to byte[] data.
		/// </summary>
		/// <returns></returns>
		internal byte[] ToColumnInfo()
		{
			//-- total 100 + CRLF bytes
			// 1 byte   - column type
			// 4 bytes  - column size
			// 45 bytes - reserved
			// 50 bytes - column name
			// CRLF

			byte[] columnData = new byte[102];
			// column type
			columnData[0] = (byte)m_DataType;
			// column size
			columnData[1] = (byte)((m_ColumSize & (1 << 24)) >> 24);
			columnData[2] = (byte)((m_ColumSize & (1 << 16)) >> 16);
			columnData[3] = (byte)((m_ColumSize & (1 << 8)) >> 8);
			columnData[4] = (byte)((m_ColumSize & (1 << 0)) >> 0);
			// reserved
			// 45 bytes
			// column name
			byte[] columnName = System.Text.Encoding.UTF8.GetBytes(m_ColumnName);
			Array.Copy(columnName,0,columnData,50,columnName.Length);
			// CRLF
			columnData[100] = (byte)'\r';
			columnData[101] = (byte)'\n';

			return columnData;
		}

		#endregion


		#region method GetChar0TerminatedString

		/// <summary>
		/// Gets string from char(0) terminated text.
		/// </summary>
		/// <param name="text">Text.</param>
		/// <returns></returns>
		internal static string GetChar0TerminatedString(string text)
		{
			if(text.IndexOf('\0') > -1){
				return text.Substring(0,text.IndexOf('\0'));
			}
			else{
				return text;
			}
		}

		#endregion


		#region Properties Implementation

		/// <summary>
		/// Gets LDB data type.
		/// </summary>
		public LDB_DataType DataType
		{
			get{ return m_DataType; }
		}

		/// <summary>
		/// Gets column name.
		/// </summary>
		public string ColumnName
		{
			get{ return m_ColumnName; }
		}

        /// <summary>
        /// Gets column size. Returns -1 if column is with variable length.
        /// </summary>
        public int ColumnSize
        {
            get{ return m_ColumSize; }
        }

		#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

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