📄 smgppacket.cs
字号:
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace n_smgppackage
{
public enum SMGP_COMMAND_ID:uint
{
SMGP_LOGIN=0x00000001,
SMGP_LOGIN_RESP=0x80000001,
SMGP_SUMBIT=0x00000002,
SMGP_SUBMIT_RESP=0x80000002,
SMGP_DELIVER=0x00000003,
SMGP_DELIVER_RESP=0x80000003,
SMGP_ACTIVE_TEST=0x00000004,
SMGP_ACTIVE_TEST_RESP=0x80000004,
SMGP_FORWARD=0x00000005,
SMGP_FORWARD_RESP=0x80000005,
SMGP_EXIT=0x00000006,
SMGP_EXIT_RESP=0x80000006
}
public class SMGP_Header //包头类
{
private const int Length = 4 + 4 + 4;
private int PacketLength;
private SMGP_COMMAND_ID RequestID;
private uint SequenceID;
public byte[] SMGP_Header_pack() //封装包头
{
byte[] bytes = new byte[SMGP_Header.Length];
int i=0;
byte[] b_pkLength=BitConverter.GetBytes(this.PacketLength);
Array.Reverse(b_pkLength);
Buffer.BlockCopy(b_pkLength,0,bytes,i,b_pkLength.Length);
i+=4;
byte[] b_ReqID=BitConverter.GetBytes((uint)this.RequestID);
Array.Reverse(b_ReqID);
Buffer.BlockCopy(b_ReqID,0,bytes,i,b_ReqID.Length);
i+=4;
byte[] b_SeqID=BitConverter.GetBytes(this.SequenceID);
Array.Reverse(b_SeqID);
Buffer.BlockCopy(b_SeqID,0,bytes,i,b_SeqID.Length);
return bytes;
}
public void SMGP_Header_unpack(byte[] bytes) //拆解包头
{
int i=0;
byte[] b_pkLength=new byte[4];
Buffer.BlockCopy(bytes,i,b_pkLength,0,b_pkLength.Length);
Array.Reverse(b_pkLength);
this.PacketLength = BitConverter.ToInt32(b_pkLength, 0);
i+=4;
byte[] b_ReqID=new byte[4];
Buffer.BlockCopy(bytes, i,b_ReqID, 0,b_ReqID.Length);
Array.Reverse(b_ReqID);
this.RequestID= (SMGP_COMMAND_ID)(BitConverter.ToUInt32(b_ReqID, 0));
i+=4;
byte[] b_SeqID=new byte[4];
Buffer.BlockCopy(bytes, i,b_SeqID, 0,b_SeqID.Length);
Array.Reverse(b_SeqID);
this.SequenceID= BitConverter.ToUInt32(b_SeqID, 0);
}
public void setPacketLength(int length)
{
this.PacketLength=length;
}
public void setRequestID(SMGP_COMMAND_ID reqid)
{
this.RequestID=reqid;
}
public void setSequenceID(uint seqid)
{
this.SequenceID=seqid;
}
public int getPacketLength()
{
return this.PacketLength;
}
public SMGP_COMMAND_ID getCommandID()
{
return this.RequestID;
}
public uint getSequenceID()
{
return this.SequenceID;
}
}
public class SMGP_Login //登录包类
{
public SMGP_Header header;
private byte[] ClientID=new byte[8];
private byte[] AuthenticatorClient=new byte[16];
private byte LoginMode;
private uint TimeStamp;
private string strTimestamp;
private byte Version;
public SMGP_Login()
{
header=new SMGP_Header();
this.header.setRequestID(SMGP_COMMAND_ID.SMGP_LOGIN);
this.header.setPacketLength(12+8+16+1+4+1);
}
public byte[] SMGP_Login_pack()
{
byte[] bytes = new byte[12+8+16+1+4+1];
int i=0;
Buffer.BlockCopy(header.SMGP_Header_pack(),0,bytes,0,12);
i+=12;
Buffer.BlockCopy(this.ClientID, 0, bytes, i,8);
i+=8;
Buffer.BlockCopy(this.AuthenticatorClient, 0, bytes, i,16);
i+=16;
bytes[i]=this.LoginMode;
i+=1;
byte[] buffer=BitConverter.GetBytes(this.TimeStamp);
Array.Reverse(buffer);
Buffer.BlockCopy(buffer,0,bytes,i,4);
i+=4;
bytes[i]=this.Version;
return bytes;
}
public void setClientID(string clientID)
{
this.ClientID=Encoding.ASCII.GetBytes(clientID);
}
public void setAuthenticatorClient(string password)
{
byte[] buffer = new byte[8 + 7 + password.Length + 10];
int i=0;
Buffer.BlockCopy(this.ClientID,0,buffer,i,8);
i+=8;
i+=7;
Buffer.BlockCopy(Encoding.ASCII.GetBytes(password),0,buffer,i,password.Length);
i+=password.Length;
Buffer.BlockCopy(Encoding.ASCII.GetBytes(this.strTimestamp),0,buffer,i,10);
i+=10;
this.AuthenticatorClient=new MD5CryptoServiceProvider().ComputeHash(buffer);
}
public string Get_MMDDHHMMSS_String(DateTime dt)
{
//将时间转换成MMDDHHMMSS的格式
string s = dt.Month.ToString().PadLeft(2, '0');
s += dt.Day.ToString().PadLeft(2, '0');
s += dt.Hour.ToString().PadLeft(2, '0');
s += dt.Minute.ToString().PadLeft(2, '0');
s+=dt.Second.ToString().PadLeft(2,'0');
return (s);
}
public void setLoginMode(int loginmode)
{
this.LoginMode=(byte)loginmode;
}
public void setTimestamp(DateTime dt)
{
this.TimeStamp=UInt32.Parse(Get_MMDDHHMMSS_String(dt));
this.strTimestamp=Get_MMDDHHMMSS_String(dt);
}
public void setVersion(int version)
{
this.Version=(byte)version;
}
}
public class SMGP_Login_resp //登录包回应类
{
public SMGP_Header header;
private int Status;
private byte[] AuthenicatorServer=new byte[16];
private byte Version;
public SMGP_Login_resp()
{
header=new SMGP_Header();
}
public void SMGP_Login_resp_unpack(byte[] bytes) //拆包
{
int i=0;
this.header.SMGP_Header_unpack(bytes);
i+=12;
byte[] b_status= new byte[4];
Buffer.BlockCopy(bytes, i, b_status, 0, b_status.Length);
Array.Reverse(b_status);
this.Status= BitConverter.ToInt32(b_status, 0);
i+=4;
Buffer.BlockCopy(bytes,i,this.AuthenicatorServer,0,this.AuthenicatorServer.Length);
i+=16;
this.Version=bytes[i];
}
public string getVsersion()
{
return System.Convert.ToString(this.Version,16);
}
public int getStatus()
{
return this.Status;
}
}
public class SMGP_Submit //SUBMIT包类
{
public SMGP_Header header=new SMGP_Header();
private byte SubType;
private byte NeedReport;
private byte Priority;
private byte[] ServiceID = new byte[10];
private byte[] FeeType = new byte[2];
private byte[] FeeCode = new byte[6];
private byte[] FixCode = new byte[6];
private byte MsgFormat;
private byte[] ValidTime = new byte[17];
private byte[] AtTime = new byte[17];
private byte[] SrcTermID = new byte[21];
private byte[] ChargeTermID = new byte[21];
private byte DestTermIDCount;
private byte[] DestTermID=new byte[21]; //21*count,这里只存一个号码
private byte MsgLength;
private byte[] MsgContent;
private byte[] Reserve = new byte[8];
public void setSubType(byte b_SubType)
{
this.SubType=b_SubType;
}
public void setNeedReport(byte b_NeedReport)
{
this.NeedReport=b_NeedReport;
}
public void setPriority(byte b_Priority)
{
this.Priority=b_Priority;
}
public void setServiceID(string strServiceID)
{
byte[] b_ServiceID=Encoding.ASCII.GetBytes(strServiceID);
Buffer.BlockCopy(b_ServiceID,0,this.ServiceID,0,b_ServiceID.Length);
}
public void setFeeType(string strFeeType)
{
byte[] b_FeeType=Encoding.ASCII.GetBytes(strFeeType);
Buffer.BlockCopy(b_FeeType,0,this.FeeType,0,b_FeeType.Length);
}
public void setFeeCode(string strFeeCode)
{
byte[] b_FeeCode=Encoding.ASCII.GetBytes(strFeeCode);
Buffer.BlockCopy(b_FeeCode,0,this.FeeCode,0,b_FeeCode.Length);
}
public void setFixCode(string strFixCode)
{
byte[] b_FixCode=Encoding.ASCII.GetBytes(strFixCode);
Buffer.BlockCopy(b_FixCode,0,this.FixCode,0,b_FixCode.Length);
}
public void setMsgFormat(byte b_MsgFormat)
{
this.MsgFormat=b_MsgFormat;
}
public void setValidTime(string strValidTime)
{
byte[] b_ValidTime=Encoding.ASCII.GetBytes(strValidTime);
Buffer.BlockCopy(b_ValidTime,0,this.ValidTime,0,b_ValidTime.Length);
}
public void setAtTime(string strAtTime)
{
byte[] b_AtTime=Encoding.ASCII.GetBytes(strAtTime);
Buffer.BlockCopy(b_AtTime,0,this.AtTime,0,b_AtTime.Length);
}
public void setSrcTermID(string strSrcTermID)
{
byte[] b_SrcTermID=Encoding.ASCII.GetBytes(strSrcTermID);
Buffer.BlockCopy(b_SrcTermID,0,this.SrcTermID,0,b_SrcTermID.Length);
}
public void setChargeTermID(string strChargeTermID)
{
byte[] b_ChargeTermID=Encoding.ASCII.GetBytes(strChargeTermID);
Buffer.BlockCopy(b_ChargeTermID,0,this.ChargeTermID,0,b_ChargeTermID.Length);
}
public void setDestTermIDCount(byte b_DestTermIDCount)
{
this.DestTermIDCount=b_DestTermIDCount;
}
public void setDestTermID(string strDestTermID)
{
byte[] b_DestTermID=Encoding.ASCII.GetBytes(strDestTermID);
Buffer.BlockCopy(b_DestTermID,0,this.DestTermID,0,b_DestTermID.Length);
}
public void setMsgContent(string strMsgContent)
{
if (this.MsgFormat==0)
{
byte[] b_MsgContent=Encoding.ASCII.GetBytes(strMsgContent);
this.MsgContent=new byte[b_MsgContent.Length];
Buffer.BlockCopy(b_MsgContent,0,this.MsgContent,0,b_MsgContent.Length);
this.MsgLength=(byte)b_MsgContent.Length;
}
if (this.MsgFormat==8)
{
byte[] b_MsgContent=Encoding.BigEndianUnicode.GetBytes(strMsgContent);
this.MsgContent=new byte[b_MsgContent.Length];
Buffer.BlockCopy(b_MsgContent,0,this.MsgContent,0,b_MsgContent.Length);
this.MsgLength=(byte)b_MsgContent.Length;
}
if (this.MsgFormat==15)
{
byte[] b_MsgContent=Encoding.Default.GetBytes(strMsgContent);
this.MsgContent=new byte[b_MsgContent.Length];
Buffer.BlockCopy(b_MsgContent,0,this.MsgContent,0,b_MsgContent.Length);
this.MsgLength=(byte)b_MsgContent.Length;
}
}
public void setReserve(string strReserve)
{
byte[] b_Reserve=Encoding.ASCII.GetBytes(strReserve);
Buffer.BlockCopy(b_Reserve,0,this.Reserve,0,b_Reserve.Length);
}
public SMGP_Submit()
{
header=new SMGP_Header();
this.header.setRequestID(SMGP_COMMAND_ID.SMGP_SUMBIT);
}
public byte[] SMGP_Submit_pack() // 打包
{
this.header.setPacketLength(12+114+21*this.DestTermIDCount+this.MsgContent.Length);
byte[] bytes= new byte[this.header.getPacketLength()];
int i=0;
Buffer.BlockCopy(header.SMGP_Header_pack(),0,bytes,i,12);
i+=12;
bytes[i]=this.SubType;
i+=1;
bytes[i]=this.NeedReport;
i+=1;
bytes[i]=this.Priority;
i+=1;
Buffer.BlockCopy(this.ServiceID,0,bytes,i,10);
i+=10;
Buffer.BlockCopy(this.FeeType,0,bytes,i,2);
i+=2;
Buffer.BlockCopy(this.FeeCode,0,bytes,i,6);
i+=6;
Buffer.BlockCopy(this.FixCode,0,bytes,i,6);
i+=6;
bytes[i]=this.MsgFormat;
i+=1;
Buffer.BlockCopy(this.ValidTime,0,bytes,i,17);
i+=17;
Buffer.BlockCopy(this.AtTime,0,bytes,i,17);
i+=17;
Buffer.BlockCopy(this.SrcTermID,0,bytes,i,21);
i+=21;
Buffer.BlockCopy(this.ChargeTermID,0,bytes,i,21);
i+=21;
bytes[i]=this.DestTermIDCount;
i+=1;
Buffer.BlockCopy(this.DestTermID,0,bytes,i,21*this.DestTermIDCount);
i+=21*this.DestTermIDCount;
bytes[i]=this.MsgLength;
i+=1;
Buffer.BlockCopy(this.MsgContent,0,bytes,i,this.MsgContent.Length);
i+=this.MsgContent.Length;
Buffer.BlockCopy(this.Reserve,0,bytes,i,8);
i+=8;
return bytes;
}
}
public class SMGP_Submit_resp //SUBMIT回应包类
{
public SMGP_Header header;
private byte[] MsgID=new byte[10];
private int Status;
private const int Length =12+10+4;
public SMGP_Submit_resp()
{
header=new SMGP_Header();
}
public void SMGP_Submit_resp_unpack(byte[] bytes) //拆解
{
int i=0;
byte[] buffer_result = new byte[4];
this.header.SMGP_Header_unpack(bytes);
i+=12;
Buffer.BlockCopy(bytes, i, this.MsgID, 0,this.MsgID.Length);
i+=10;
Buffer.BlockCopy(bytes, i, buffer_result, 0, buffer_result.Length);
Array.Reverse(buffer_result);
this.Status= BitConverter.ToInt32(buffer_result, 0);
}
public string getMsgID()
{
return bcdtostring(this.MsgID);
}
public int getStatus()
{
return this.Status;
}
// bcd编码转换为字符串输出
// 描述:bcd编码转换为字符串输出
// 参数:BCD编码的数组
// 返回:string 转换好的字符串
// 创建:李文达 2005-08-24
public string bcdtostring(byte[] b_bcd)
{
string[] strbcd=new string[b_bcd.Length];
string output="";
for(int i=0;i<b_bcd.Length;i++)
{
strbcd[i]=System.Convert.ToString(b_bcd[i],16);
if(strbcd[i].Length==1)
{
strbcd[i]="0" + strbcd[i];
}
output=output + strbcd[i];
}
return output;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -