📄 cmpp with c#.txt
字号:
}
private void _forcedSubThread(Thread t) //强制停止线程
{
try
{
t.Abort();
t.Join();
}
catch(Exception )
{}
}
//private 函数区域//////////////////////////////////////////////////////////////////
//公用函数 属性区域////////////////////////////////////////
public bool Init(string CMPPServer,int CMPPPort)
{
return(this._init(CMPPServer,CMPPPort));
}
public bool Init(string CMPPServer,int CMPPPort,int recvtimeout,int sendtimeout)
{
this.RecvTimeOut =recvtimeout;
this.SendTimeout =sendtimeout;
return(this._init(CMPPServer,CMPPPort));
}
public bool Init(string CMPPServer,int CMPPPort,int recvtimeout)
{
this.RecvTimeOut =recvtimeout;
this.SendTimeout =recvtimeout;
return(this._init(CMPPServer,CMPPPort));
}
public bool Login(string SystemID,string UserName,string Password)
{
try
{
SendLogin(SystemID, UserName, Password);
this.LogLastOkTime(DateTime.Now); //最后一次正确的发送
}
catch(SocketException se)
{
//发送出错
this.ErrorInfo = this.ErrorInfo +"\r\n"+se.ToString();
return(false);
}
DateTime t1=DateTime.Now;
while(!this.isLogin)
{
byte[] rbuf=new Byte[400];
int l;
try
{
l=tcp.Receive(rbuf) ;
if(l>16)
{
if(BIConvert.Bytes2UInt(rbuf,4)==(uint)MSG.CMPP_COMMAND_ID.CMPP_CONNECT_RESP)
{
MSG.CMPP_MSG_CONNECT_RESP resp=new MSG.CMPP_MSG_CONNECT_RESP(rbuf);
if(resp.isOk)
{
EventArgs e=new EventArgs();
if(onLogonSuccEventHandler!=null)
{
onLogonSuccEventHandler(this,e);
}
else
{
this.defaultConnectRespEventHandler();
}
this.isLogin =true;
}
else
{
}
break;
}
}
this._lastOkTime =DateTime.Now ; //更新当前最后成功收发套接字的时间
}
catch(SocketException)
{
}
System.TimeSpan t=DateTime.Now - t1;
if(t.TotalSeconds > 10)
{
break;
}
}
if(this.isLogin)
{ //登录ok,就立即发送active_test包
this.ErrorInfo =this.ErrorInfo + "\r\n"+" Logon succ! ";
startThreads(); // 启动 主监视程序de线程
return(true);
}
else
{
return(false);
}
}
public uint SubmitSMS(string to_user,string fee_code,string svc_code, string fee_user,string spnum,string content,int fee_usertype)
{
MSG.CMPP_MSG_SUBMIT sndmsg;
uint seq=this.getNextSequence(); //取得下一个sequence
sndmsg=new MSG.CMPP_MSG_SUBMIT(seq);
sndmsg.FeeCode =fee_code;
sndmsg.FeeTerminalId =to_user;
sndmsg.FeeType=MSG.FeeType.FEE_TERMINAL_PERITEM; //按条收取
sndmsg.FeeUserType = fee_usertype ;
sndmsg.Msg_Level=0;
sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
sndmsg.SMS_Content=content;
sndmsg.SrcID=spnum; //长号码
sndmsg.SPID=this.systemID ;
sndmsg.Svc_Code= svc_code;
sndmsg.UDHI=0;
sndmsg.ValIdTime=getValIdTime(DateTime.Now); //存活时间
sndmsg.addTerminalID(to_user);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT ,0,0);
q.setmsgObj(sndmsg);
this.addToOutQueue(q);
return(seq);
}
public uint SendMsg(string to_user,string fee_user,string fee,string svccode,string content,string spnum)
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_SUBMIT sndmsg=new MSG.CMPP_MSG_SUBMIT(seq) ;
sndmsg.FeeCode=fee;
sndmsg.FeeType=MSG.FeeType.FEE_TERMINAL_PERITEM ;
sndmsg.FeeTerminalId =fee_user;
sndmsg.FeeUserType =(int)MSG.FeeUserType.FEE_NULL ; //计费 按照计费号码计费
sndmsg.SPID =this.systemID ; //企业代码
sndmsg.UDHI=0; //
sndmsg.MSGFormat=(uint)MSG.Msg_Format.GB2312 ;
sndmsg.SMS_Content =content;
sndmsg.SrcID=spnum;
sndmsg.Svc_Code=svccode;
sndmsg.addTerminalID(to_user);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT ,0,0);
q.setmsgObj(sndmsg);
this.addToOutQueue(q);
return(seq);
}
public uint SendSMC(string fee_user,string feecode,string svccode) //向计费用户发送一条包月计费信息
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_SUBMIT sndmsg=new MSG.CMPP_MSG_SUBMIT(seq) ;
sndmsg.SMS_Delivery_Type=2; //产生包月SMC
sndmsg.FeeCode=feecode;
sndmsg.FeeType=MSG.FeeType.FEE_TERMINAL_MONTH ; //包月计费
sndmsg.FeeTerminalId =fee_user;
sndmsg.FeeUserType =(int)MSG.FeeUserType.FEE_TERMINAL_ID ; //计费 按照计费号码计费
sndmsg.SPID =this.systemID ; //企业代码
sndmsg.UDHI=0; //
sndmsg.MSGFormat=(uint)MSG.Msg_Format.UCS2 ;
sndmsg.SMS_Content ="SMC";
sndmsg.SrcID =this.userName; //sp的特符号码
sndmsg.Svc_Code=svccode;
sndmsg.addTerminalID(fee_user);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT ,0,0);
q.setmsgObj(sndmsg);
this.addToOutQueue(q);
return(seq);
}
public uint SendSMT(string to_user,string feecode,string svccode,string spnum,string content)
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_SUBMIT sndmsg=new MSG.CMPP_MSG_SUBMIT(seq) ;
sndmsg.SMS_Delivery_Type=1; //产生包月SMC
sndmsg.FeeCode=feecode; //包月计费代码
sndmsg.FeeType=MSG.FeeType.FEE_TERMINAL_MONTH ; //包月计费
sndmsg.FeeTerminalId =to_user;
sndmsg.FeeUserType =(int)MSG.FeeUserType.FEE_TERMINAL_ID ; //计费 按照计费号码计费
sndmsg.SPID =this.systemID ; //企业代码
sndmsg.UDHI=0; //
sndmsg.MSGFormat=(uint)MSG.Msg_Format.UCS2 ;
sndmsg.SMS_Content =content;
sndmsg.SrcID =spnum; //sp的特符号码
sndmsg.Svc_Code=svccode;
sndmsg.addTerminalID(to_user);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT ,0,0);
q.setmsgObj(sndmsg);
this.addToOutQueue(q);
return(seq);
}
public uint SendQuery(string svccode,string whichday) //查询某个业务的总计数
{
string wd=whichday.Trim();
int query_type=0;
if(svccode==null || svccode.CompareTo("")==0)
{//查询全部页数量
}
else
{//查询某项业务
query_type=1;
}
if(wd==null|| wd.CompareTo ("")==0)
{
DateTime d=DateTime.Now;
wd=d.Year.ToString()+d.Month.ToString().PadLeft(2,'0')+d.Day.ToString().PadLeft(2,'0');
}
uint seq=this.getNextSequence();
MSG.CMPP_MSG_QUERY query=new MSG.CMPP_MSG_QUERY(seq);
query.Query_Type =query_type;
query.Query_Code =svccode;
query.Time =wd; //设定那一天
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_QUERY ,0,0);
q.setmsgObj(query);
this.addToOutQueue(q);
return(seq); //返回消息的内部编号
}
public uint StopCMPPConnection() //停止CMPP协议的socket连接
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_TERMINATE t=new MSG.CMPP_MSG_TERMINATE(seq);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE,0,0);
q.setmsgObj(t);
this.addToOutQueue(q);
return(seq); //返回终止消息,便于等待
}
public uint CancelMsg( string msgid)
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_CANCEL cancel=new MSG.CMPP_MSG_CANCEL(seq);
cancel.MsgID =msgid;
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_CANCEL ,0,0);
q.setmsgObj(cancel);
this.addToOutQueue(q);
return(seq); //返回消息的内部编号
}
public void StopMe()
{
if(!this.isStop)
{
if(this.tcpIsCanUse())//发送一条对服务器的通告
{
uint seq=this.getNextSequence();
MSG.CMPP_MSG_TERMINATE t=new MSG.CMPP_MSG_TERMINATE(seq);
QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE,0,0);
q.setmsgObj(t);
this.addToOutQueue(q);
}
Thread.Sleep(500); //等待1000ms,告知服务器
this._StopMe();
tcp.Close();
if(this.onClientSvcStopedHandler!=null)
{
ClientQueueStateArgs arg=new ClientQueueStateArgs(this._outSeqQueue ,this._waitingSeqQueue);
onClientSvcStopedHandler(this,arg);
}
else
{
this.defaultClientStopEventHandler();
}
Thread.Sleep(500); //再次主动等待线程结束
//此处报告 2个队列中的信息
}
//准备强行结束
_forcedSubThread(this.Send_Thread);
Thread.Sleep(500); //等待1000ms,告知服务器
_forcedSubThread(this.Recv_Thread );
Thread.Sleep(500); //等待1000ms,告知服务器
_forcedSubThread(this.Deamo_Thread );
Thread.Sleep(500); //等待1000ms,告知服务器
}
public string getLogInfo()
{
string t= this.ErrorInfo;
this.ErrorInfo="";
return(t);
}
public int getQueueItemState(uint seq) //根据seq寻找发送内部队列的消息对象的状态
{
int status=0; //状态未知
if(this._outSeqQueue.ContainsKey(seq)) //存在于outSeqQueue中
{
if(this._waitingSeqQueue.Contains(seq))
{
//正在发送等待返回,状态未定
}
else
{
//还没有发送
}
}
else
{
if(this._waitingSeqQueue.ContainsKey(seq))
{
//正等待回应
}
else
{
//已经发送结束了
}
}
return(status);
}
public void TestSubmit( string[] nums ,int topI,int topJ) //发送测试包
{
int count=0;
int total=0;
ArrayList pks=new ArrayList( );
//准备100个包
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
uint seq=this.getNextSequence(); //准备流水号
MSG.CMPP_MSG_SUBMIT sndmsg=new MSG.CMPP_MSG_SUBMIT(seq);
sndmsg.FeeCode ="000001";
sndmsg.FeeTerminalId =nums[i];
sndmsg.FeeType=MSG.FeeType.FEE_TERMINAL_PERITEM; //按条收取
sndmsg.FeeUserType = 0 ; //终端用户计费
sndmsg.Msg_Level=0;
sndmsg.MSGFormat = (uint)MSG.Msg_Format.UCS2;
sndmsg.SMS_Content="test";
sndmsg.SrcID="09880"; //长号码
sndmsg.SPID=this.systemID ;
sndmsg.Svc_Code= "cmcctest";
sndmsg.UDHI=0;
sndmsg.ValIdTime=getValIdTime(DateTime.Now); //存活时间
sndmsg.addTerminalID(nums[i]);
pks.Add( sndmsg.toBytes()) ; //存入数组
}
}
DateTime t1=DateTime.Now ;
for(int i=0;i< topI;i++)
{
for(int j=0;j<topJ;j++)
{
try
{
tcp.Send((byte[])pks[i*10+j]);
count++;
total++;
}
catch (SocketException se)
{
this.ErrorInfo =this.ErrorInfo +"\r\n"+"发送错误: "+se.ToString() ;
}
if( count>=16)
{
count=0; //复位
Thread.Sleep(50); //暂停20ms
}
}
}
DateTime t2=DateTime.Now ;
TimeSpan t= t2-t1;
this.ErrorInfo =this.ErrorInfo +"\r\n"+"发送: "+total +" 条消息, 总计花费时间:"+ t.TotalMilliseconds +"毫秒" ;
} //测试函数////////////////////////////////////////////////供测试移动网络测试
//测试翰数区域///////////////////////////////////////////
//公用函数 属性区域////////////////////////////////////////
}
//*************工具类结束***********************************
enum MSG_STATE //CMPP消息在队列中的状态枚举值
{
NEW=0, //加入到队列等待发送出去
SENDING=1, //正被某个线程锁定
SENDED_WAITTING=2, //发送出去,现在等待resp消息返回
SENDING_FINISHED=3 //得到回应,一般等待被清理出队列
}
public class QueueItem //代表一个存储在缓存队列中的消息,序列号由CMPPClient产生
{
uint _sequence; //消息索引 就是流水号
uint _msgType; //消息的类别就是COMMAND_ID,根据此值决定 Object _msgObj的原类型
int _failedCount=0; //失败计数,如果失败次数超过3此需要进行清理
int _msgState; //当前消息状态,具体为 MSG_STATE的枚举类型
object _msgObj; //存放消息对象,具体类型参考 _msgType
DateTime _inQueueTime; //消息进入队列的时间,上次消息的发送时间
public int FailedCount
{
set
{
this._failedCount =value;
}
get
{
return(this._failedCount);
}
}
public object getMsgObj()
{
return(this._msgObj);
}
public void setmsgObj(object inmsg)
{
this._msgObj=inmsg;
}
public DateTime inQueueTime
{
set
{
this._inQueueTime =value;
}
get
{
return(this._inQueueTime);
}
}
public uint MsgType
{
get
{
return(this._msgType);
}
}
public int MsgState
{
get
{
return(this._msgState);
}
set
{
this._msgState =value;
}
}
public uint Sequence
{
get
{
return(this._sequence);
}
set
{
this._sequence =value;
}
}
public QueueItem(uint sequence,uint msgtype ,int faildedcount,int msgstate)
{
this._failedCount =faildedcount;
this._msgState =msgstate;
this._msgType =msgtype;
this._sequence=sequence;
}
public QueueItem(uint sequence,uint msgtype,int faildedcount,int msgstate,object msgobj)
{
this._failedCount =faildedcount;
this._msgState =msgstate;
this._msgType =msgtype;
this._sequence=sequence;
this.setmsgObj(msgobj);
}
}
public class BIConvert //字节 整形 转换类 网络格式转换为内存格式
{
public static byte[] Int2Bytes(uint i) //转换整形数据网络次序的字节数组
{
byte[] t=BitConverter.GetBytes(i) ;
byte b=t[0];
t[0]=t[3];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -