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

📄 messagepack.cs

📁 这个是基于SMPP的开发源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using SMPP.Variable;

namespace SMPP
{
	public interface IMessagePack: IEnumerator, ISMPPData
	{
		ISMPPData this[int Index]
		{
			get;
		}

		int DataCount
		{
			get;
		}
	}

	public class MessagePack : IMessagePack
	{
		private List<SMPP.Variable.ISMPPData> Variables = new List<SMPP.Variable.ISMPPData>();

		#region IMessagePack 成员

		public int DataCount
		{
			get
			{
				return Variables.Count;
			}
		}

		public ISMPPData this[int Index]
		{
			get
			{
				return Variables[Index];
			}
		}

		public virtual byte[] GetBytes()
		{
			int Index = 0;
			byte[] ret;

			ret = new byte[Length];

			foreach (SMPP.Variable.ISMPPData variable in Variables)
			{
				int len = variable.Length;
				Array.Copy(variable.GetBytes(), 0, ret, Index, len);
				Index += len;
			}

			return ret;
		}

		public int Length
		{
			get
			{
				int len = 0;
				foreach (SMPP.Variable.ISMPPData variable in Variables)
				{
					len += variable.Length;
				}
				return len;
			}
		}

		public virtual void Parse(byte[] Datas, ref int Index)
		{
			foreach (SMPP.Variable.ISMPPData variable in Variables)
			{
				variable.Parse(Datas, ref Index);
			}
		}

		#endregion

		protected void Add(SMPP.Variable.ISMPPData variable)
		{
			if (variable.GetType().IsSubclassOf(typeof(SMPP.Variable.Variable)))
			{
				SMPP.Variable.Variable v = variable as SMPP.Variable.Variable;
				if (v.Owner == null)
					v.SetOwner(this);
			}
			Variables.Add(variable);
		}

		protected void Clear()
		{
			Variables.Clear();
		}

		#region IEnumerator 成员

		private int Index = 0;

		public object Current
		{
			get
			{
				return Variables[Index];
			}
		}

		public bool MoveNext()
		{
			bool ret = true;
			Index++;
			if (Index > Variables.Count)
				ret = false;

			return ret;
		}

		public void Reset()
		{
			Index = 0;
		}

		#endregion
	}

	public class TLVPack : MessagePack
	{
		private Type Default = typeof(TLVVARBinary);

		private static Hashtable TLVDeclare = new Hashtable();

		public static Type GetDeclare(UInt16 TagID)
		{
			return (Type)TLVDeclare[TagID];
		}

		public static void AddDeclare(UInt16 TagID, Type TLVType)
		{
			if (TLVType.IsSubclassOf(typeof(SMPP.Variable.TLV)))
			{
				TLVDeclare.Add(TagID, TLVType);
			}
			else
			{
				throw new Exception("只允许添加从TLV类型继承的类类型");
			}
		}

		public SMPP.Variable.TLV[] GetArray()
		{
			SMPP.Variable.TLV[] TLVFields = new SMPP.Variable.TLV[this.DataCount];

			for (int i = 0; i < TLVFields.Length; i++)
			{
				TLVFields[i] = this[i] as SMPP.Variable.TLV;
			}
			return TLVFields;
		}

		public void Add(SMPP.Variable.TLV Field)
		{
			base.Add(Field);
		}

		public override void Parse(byte[] Datas, ref int Index)
		{
			base.Clear();
			Integer16 TagID = new Integer16();

			while (Index < Datas.Length)
			{
				int ind = Index;
				TagID.Parse(Datas, ref ind);
				Integer16 Length = new Integer16();
				Length.Parse(Datas, ref ind);

				Type TLVType = GetDeclare((UInt16)TagID.Value);

				if (TLVType == null)
					TLVType = Default;

				TLV TlvField = (TLV)TLVType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, null, new object[] { TagID.Value });

				TlvField.SetNewLength(Length.Value);

				TlvField.Parse(Datas, ref Index);

			}
			//base.Parse(Datas, ref Index);
		}
	}
}

⌨️ 快捷键说明

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