⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlv.cs

📁 短信网关的应用开发
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace SMPP.Variable
{
	/// <summary>
	/// TLV类型基类
	/// </summary>
	public abstract class TLV : Variable
	{
		private Integer16 _Tag;
		public Int16 Tag
		{
			get
			{
				return _Tag.Value;
			}
			set
			{
				_Tag.Value = value;
			}
		}

		private Integer16 _Length = new Integer16();

		private Variable _ValueField;
		protected Variable ValueField
		{
			get
			{
				return _ValueField;
			}
			set
			{
				if (value.Length > Int16.MaxValue)
					throw new Exception("数据太长,超过TLV字段限制");
				_ValueField = value;
				UpdateLength();
			}
		}

		public byte[] GetValueBytes()
		{
			return _ValueField.GetBytes();
		}

		protected void UpdateLength()
		{
			_Length.Value = (Int16)_ValueField.Length;
		}

		public abstract void SetNewLength(int Length);

		public TLV(Int16 Tag)
		{
			_Tag = new Integer16();
			_Tag.Value = Tag;
		}

		public sealed override int Length
		{
			get
			{
				return 4 + _ValueField.Length;
			}
		}

		public sealed override void Parse(byte[] Datas, ref int Index)
		{
			_Tag.Parse(Datas, ref Index);

			_Length.Parse(Datas, ref Index);

			_ValueField.Parse(Datas, ref Index);
		}

		public sealed override byte[] GetBytes()
		{
			UpdateLength();

			byte[] Data = new byte[Length];

			byte[] tmp = _Tag.GetBytes();

			Array.Copy(tmp, Data, tmp.Length);

			tmp = _Length.GetBytes();

			Array.Copy(tmp, 0, Data, _Tag.Length, _Length.Length);

			tmp = _ValueField.GetBytes();

			Array.Copy(tmp, 0, Data, _Tag.Length + _Length.Length, tmp.Length);

			return Data;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -