📄 smgppack.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace SMGP3_0
{
public class SMGPPack : SMPP.MessagePack
{
private static Hashtable SMGPDeclare = new Hashtable();
public static Type GetDeclare(UInt32 RequestID)
{
return (Type)SMGPDeclare[RequestID];
}
public static void AddDeclare(UInt32 RequestID, Type SMGPType)
{
if (SMGPType.IsSubclassOf(typeof(SMGPBody)))
{
SMGPDeclare.Add(RequestID, SMGPType);
}
else
{
throw new Exception("只允许添加从SMGPBody类型继承的类类型");
}
}
/// <summary>
/// 该数据包实例的唯一标识
/// </summary>
public Guid ID;
public object Tag = null;
public DateTime SendTime = DateTime.MinValue;
private SMGPPack()
{
Add(new SMPP.Variable.Integer32());
Add(new SMGPHeader());
}
public SMGPPack(SMGPBody Body)
{
Add(new SMPP.Variable.Integer32());
Add(new SMGPHeader());
Header.RequestID = (int)Body.PackID;
Add(Body);
}
//隐藏基类的Parse方法
private new void Parse(byte[] Data, ref int Index)
{
base.Parse(Data, ref Index);
}
public static SMGPPack CreateInstance(byte[] Data, ref int Index)
{
int OldIndex = Index;
SMGPPack pack = new SMGPPack();
if (Data.Length - Index < pack.Length) //如果长度不足以解析SMGP包头,则返回空
{
return null;
}
pack.Parse(Data, ref Index);
Type BodyType = GetDeclare((UInt32)pack.Header.RequestID);
if (BodyType != null)
{
object Body = BodyType.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, null, null);
pack.Add((SMGPBody)Body);
}
else
{
pack.Add(new SMGPBody_Unknow(pack.PacketLength - pack.Header.Length));
}
#region OLD
/*
switch ((UInt32)pack.Header.RequestID)
{
case 0x00000001:
{
pack.Add(new SMGPBody_Login());
break;
}
case 0x80000001:
{
pack.Add(new SMGPBody_Login_Resp());
break;
}
case 0x00000002:
{
pack.Add(new SMGPBody_Submit());
break;
}
case 0x80000002:
{
pack.Add(new SMGPBody_Submit_Resp());
break;
}
case 0x00000003:
{
break;
}
case 0x80000003:
{
break;
}
case 0x00000004:
{
pack.Add(new SMGPBody_Active());
break;
}
case 0x80000004:
{
pack.Add(new SMGPBody_Active_Resp());
break;
}
case 0x00000006:
{
break;
}
case 0x80000006:
{
break;
}
default:
{
break;
}
}
*/
#endregion
if (Data.Length - OldIndex < pack.PacketLength) //如果长度不足以解析SMGP包体,则返回空
{
Index = OldIndex;
return null;
}
if (pack.PacketLength < pack.Header.Length + pack[0].Length) //如果数据包头发生明显错误,则引发异常
throw new Exception("数据是错误的");
if (pack.Body != null)
pack.Body.Parse(Data, ref Index);
if (pack != null)//进行索引修复(如果遇到不可识别的Body,用此可以修复索引)
Index = pack.PacketLength;//由于加入了SMGPBody_Unknow包,此修复已不重要
return pack;
}
public override byte[] GetBytes()
{
PacketLength = Length;
return base.GetBytes();
}
private Int32 PacketLength
{
get
{
return (this[0] as SMPP.Variable.Integer32).Value;
}
set
{
(this[0] as SMPP.Variable.Integer32).Value = value;
}
}
public SMGPHeader Header
{
get
{
return this[1] as SMGPHeader;
}
}
public SMPP.MessagePack Body
{
get
{
return this.DataCount < 3 ? null : this[2] as SMPP.MessagePack;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -