📄 cmppclient.cs
字号:
}
private void defaultQueryRespEventHandler()
{
}
private void defaultConnectRespEventHandler()
{
QueueItem q = new QueueItem(this.getNextSequence(), (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST, 0, (int)MSG_STATE.NEW);
MSG.CMPP_MSG_TEST test = new MSG.CMPP_MSG_TEST(q.Sequence); //立即发送包过去
q.setmsgObj(test);
this.addToOutQueue(q);
}
private void defaultSubmitRespEventHandler()
{
}
private void defaultClientStopEventHandler()
{ }
private void rePortError(string info)
{
}
private bool _init(string CMPPServer, int CMPPPort)
{
bool reVal = false;
CMPP_Server = CMPPServer;
CMPP_Port = CMPPPort;
try
{
tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ip = Dns.GetHostByName(CMPP_Server);
cmpp_ep = new IPEndPoint(ip.AddressList[0], CMPP_Port);
tcp.Connect(cmpp_ep); //连接
reVal = true;
}
catch (SocketException se)
{
ErrorInfo = "Socker Error:" + se.ToString();
}
return (reVal);
}
private uint getNextSequence()
{
lock (typeof(CMPPClient))
{
try
{
lastSequence++;
}
catch (OverflowException ofe)
{
this.ErrorInfo = this.ErrorInfo + "\r\n" + ofe.ToString();
lastSequence = uint.MinValue;
}
return (lastSequence);
}
}
private void RecvISMGMsgThread() //处理ISMG消息的线程
{
while (!this.isStop)
{
try
{
byte[] rbuf = new byte[10240]; //结果缓冲区
byte[] recv_temp = new Byte[1024]; //recv临时缓冲区
int index = 0;
int msglength = tcp.Receive(rbuf); //阻塞接收//分析收到的数据
MSG.CMPP_MSG_Header header; //=new MSG.CMPP_MSG_Header(rbuf,index); //取得一个消息
while (index < msglength) //逐个消息分析
{
header = new MSG.CMPP_MSG_Header(rbuf, index); //取得一个消息
byte[] the_pk = new byte[header.MSGLength]; //生成此消息的大小
for (int i = 0; i < header.MSGLength; i++)
{
the_pk[i] = rbuf[index++];
}
uint seq; //取得回复消息的下一个流水序列号
switch (header.Command_ID)
{
case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST: //服务器给客户的测试信号
this.ErrorInfo = this.ErrorInfo + "\r\n" + "收到:CMPP_ACTIVE_TEST";
MSG.CMPP_MSG_TEST test = new MSG.CMPP_MSG_TEST(the_pk);
seq = test.Sequence; //取得发送过来的流水号
MSG.CMPP_MSG_TEST_RESP test_reply = new MSG.CMPP_MSG_TEST_RESP(seq);
tcp.Send(test_reply.toBytes()); //马上送出回应包,不需要进入队列
if (this.onTestHandler != null)
{
TestEventArgs e = new TestEventArgs(test);
onTestHandler(this, e);
}
else
{
defaultTestEventHandler();
}
this.ErrorInfo = this.ErrorInfo + "\r\n" + "发送:CMPP_ACTIVE_TEST_RESP ";
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST_RESP: //服务器的回应消息,应当丢弃不管
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_ACTIVE_TEST_RESP ");
MSG.CMPP_MSG_TEST_RESP test_reply2 = new MSG.CMPP_MSG_TEST_RESP(the_pk); //构造消息
seq = test_reply2.Sequence; //寻找 曾经发送过去的消息
this.delFromWaitingQueue(seq); //删除等待队列中的消息 //清空等待回应队列
if (this.onTestRespHandler != null)
{
TestRespEventArgs e = new TestRespEventArgs(test_reply2);
onTestRespHandler(this, e);
}
else
{
defaultTestRespEventHandler();
}
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_CANCEL_RESP:
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_CANCEL_RESP ");
MSG.CMPP_MSG_CANCEL_RESP cancel_reply = new MSG.CMPP_MSG_CANCEL_RESP(the_pk);//构造消息
seq = cancel_reply.Sequence;
this.delFromWaitingQueue(seq);
if (this.onCancelRespHandler != null)
{
CancelRespEventArgs e = new CancelRespEventArgs(cancel_reply);
onCancelRespHandler(this, e);
}
else
{
defaultCancelRespEventHandler();
}
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_CONNECT_RESP: //检查下消息的正确性,清除等待队列 设定连接成功标志
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_CONNECT_RESP ");
MSG.CMPP_MSG_CONNECT_RESP cn_reply = new MSG.CMPP_MSG_CONNECT_RESP(the_pk);
seq = cn_reply.Sequence; //取得消息的seq
if (this.onConnectRespHandler != null)
{
ConnectRespEventArgs e = new ConnectRespEventArgs(cn_reply);
onConnectRespHandler(this, e);
}
else
{
defaultConnectRespEventHandler();
}
if (cn_reply.isOk)
{
this.isLogin = true;
}
else
{
this.isLogin = false;
}
this.delFromWaitingQueue(seq); //删除队列中的等待连接信息包
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_DELIVER: //检查消息正确定,立即返回 正确 或者 失败,正确则处理是否状态包,不是状态包则存到MO缓存,表示收到信息,时状态包则判断缓存消息进行消息送达处理
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_DELIVER ");
BIConvert.DumpBytes(the_pk, "c:\\CMPP_DELIVER.txt");//保留映像
MSG.CMPP_MSG_DELIVER deliver = new MSG.CMPP_MSG_DELIVER(the_pk);
seq = (uint)deliver.ISMGSequence; //发过来的流水号,需要立即发送一个deliver_resp //一条 ISMG--〉SP 的消息
MSG.CMPP_MSG_DELIVER_RESP deliver_resp = new MSG.CMPP_MSG_DELIVER_RESP(seq);
deliver_resp.MsgID = deliver.MsgID;
deliver_resp.Result = 0;
byte[] t = deliver_resp.toBytes();
tcp.Send(t);
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("发送:CMPP__DELIVER_RESP ");
if (deliver.isReport)
{ //删除等待队列的消息//报告消息已经正确发送到
//UInt64 ReportMsgID=deliver.ReportMsgID ; //取得消息ID ,更新 MsgID
string StateReport = deliver.StateReport; //取得关于此消息的状态
//_debugBs(the_pk);
ReportEventArgs arg = new ReportEventArgs(the_pk, MSG.CMPP_MSG_Header.HeaderLength + 8 + 21 + 10 + 1 + 1 + 1 + 21 + 1 + 1); //构造报告事件参数
//ReportEventArgs arg=new ReportEventArgs(ReportMsgID.ToString(),
if (this.onReportHandler != null) //ReportEventArgs传递的字节数组是 报告信息包的数据,在此不考虑多个报告的情况
{
onReportHandler(this, arg);
}
else
{
this.defaultReportHandler();
}
}
else
{//SMSEventArgs 传递的整个deliver包
SMSEventArgs smsarg = new SMSEventArgs(the_pk, MSG.CMPP_MSG_Header.HeaderLength);
if (this.onSMSHandler != null)
{
onSMSHandler(this, smsarg); //触发事件,应当很快结束处理,不要靠考虑存储之类的耗费资源事宜
}
else
{
defaultSMSHandler();
}
}
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_QUERY_RESP:
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_QUERY_RESP ");
//收到消息,处理后存入数据库
MSG.CMPP_MSG_QUERY_RESP query_resp = new MSG.CMPP_MSG_QUERY_RESP(the_pk);
this.delFromWaitingQueue(query_resp.Sequence); //将等待的队列中的元素删除
if (this.onQueryRespHandler != null)
{
QueryRespEventArgs e = new QueryRespEventArgs(query_resp);
}
else
{
defaultQueryRespEventHandler();
}
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT_RESP: //收到服务器送达的慧英消息
this.ErrorInfo = this.ErrorInfo + "\r\n" + ("收到:CMPP_SUBMIT_RESP ");
MSG.CMPP_MSG_SUBMIT_RESP submit_resp = new MSG.CMPP_MSG_SUBMIT_RESP(the_pk);
BIConvert.DumpBytes(the_pk, "c:\\CMPP_SUBMIT_RESP.txt");//保留映像
//BIConvert.DumpBytes(initValue,"c:\\CMPP_SUBMIT_RESP.txt");//保留映像
sub_resp++; //该变量仅供测试使用
delFromWaitingQueue(submit_resp.Sequence); //删除需要等待的消息
if (this.onSubmitRespHandler != null)
{
SubmitRespEventArgs e = new SubmitRespEventArgs(submit_resp);
//submit_resp.
onSubmitRespHandler(this, e);
}
else
{
defaultSubmitRespEventHandler();
}
break;
case (uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE:
this.ErrorInfo = this.ErrorInfo + "\r\n" + "收到:CMPP_TERMINATE";
MSG.CMPP_MSG_TERMINATE terminate = new MSG.CMPP_MSG_TERMINATE(the_pk);
seq = terminate.Sequence;
MSG.CMPP_MSG_TERMINATE_RESP terminate_resp = new MSG.CMPP_MSG_TERMINATE_RESP(seq);
this.ErrorInfo = this.ErrorInfo + "\r\n" + "收到:CMPP_TERMINATE_RESP";
tcp.Send(terminate_resp.toBytes());
if (this.onTerminateHandler != null)
{
TerminateEventArgs e = new TerminateEventArgs(terminate);
onTerminateHandler(this, e);
this.StopMe(); //准备自我停止?
}
else
{
defaultTerminateEventHandler();
}
this._StopMe(); //发出终止设定
return; //退出线程
case (uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE_RESP:
this.ErrorInfo = this.ErrorInfo + "\r\n" + "收到:CMPP_TERMINATE_RESP";
MSG.CMPP_MSG_TERMINATE_RESP ter_resp = new MSG.CMPP_MSG_TERMINATE_RESP(the_pk);
seq = ter_resp.Sequence; //取得流水信号
this.delFromOutQueue(seq); //删除输出表重点项目
if (this.onTerminateRespHandler != null)
{
TerminateRespEventArgs e = new TerminateRespEventArgs(ter_resp);
onTerminateRespHandler(this, e);
}
else
{
defaultTerminateRespEventHandler();
}
this._StopMe();
break;
}
}
LogLastOkTime(DateTime.Now); //记录当前最后一次消息soket正确时间
}
catch (SocketException se)
{
//超时
}
Thread.Sleep(50);
}
}
//debug
// private void _debugBs(byte[] the_pk) //存储byte字节
// {
//
// }
//debug
private void DeamonThread() //监视本系统连接是否正常
{//此线程是监视线程
int t_count = 0; //循环时间计数
_reStartRecvNSend(); //启动接收和发送
while (!this.isStop)
{
t_count++; //0.1秒
if (tcpIsCanUse())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -