已阅读5页,还剩83页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
CMPP SP端 C# 实例 (转)/附CMPPClient.cs:/*作者:TNT时间:2003年12月文件说明:本文件实现SP端的协议开发。*/usingSystem;usingSystem.Security.Cryptography;usingSystem.Net.Sockets;usingSystem.Net;usingSystem.Text;usingSystem.Threading;usingSystem.Collections;namespaceCMPP.YOURCOMPANYpublicdelegatevoidReportEventHandler(objectsender,ReportEventArgse);/声明一个事件的指代(指针)publicdelegatevoidSMSEventHandler(objectsender,SMSEventArgse);/声明一个事件的指代(指针)publicdelegatevoidTerminateEventHandler(objectsender,TerminateEventArgse);/声明收到终止信号publicdelegatevoidTerminateRespEventHandler(objectsender,TerminateRespEventArgse);/回应事件发生publicdelegatevoidTestEventHandler(objectsender,TestEventArgse);publicdelegatevoidTestRespEventHandler(objectsender,TestRespEventArgse);publicdelegatevoidConnectRespEventHandler(objectsender,ConnectRespEventArgse);publicdelegatevoidCancelRespEventHandler(objectsender,CancelRespEventArgse);publicdelegatevoidSubmitRespEventHandler(objectsender,SubmitRespEventArgse);publicdelegatevoidQueryRespEventHandler(objectsender,QueryRespEventArgse);publicdelegatevoidLogonSuccEventHandler(objectsender,EventArgse);/当成功登录系统publicdelegatevoidSocketClosedEventHandler(objectsender,EventArgse);/当套接字被检测到关闭publicdelegatevoidFailedItemDeletedEventHandler(objectsender,WaitingQueueItemEventArgse);/当一条等待队列的消息超过60秒没有回应publicdelegatevoidCMPPClientSvcStopEventHandler(objectsender,ClientQueueStateArgse);/当CMPP服务停止时候触发事件/作为CMPP协议的客户端,具有的登陆、发送、接受功能/会开3个线程处理:1、处理需要发送MO(下行)的消息/2、处理从移动服务器发送过来CMPP的消息/3、处理连接断等信息,检查需要重发的消息,检查收到的报告、短信,并调用OnReport事件OnSMS事件/publicclassCMPPClientpublicstaticlongCMPP_ACTIVE_TEST_C_TICKs=30;/*3;/长连接的active_test测试时间publicstaticlongCMPP_ACTIVE_TEST_T_TICKs=60;/消息失败时间60秒publicstaticintCMPP_ACTIVE_TEST_N_COUNT=3;/3次/publicstaticintCMPP_MSG_MAX=100;/一次取得的最大消息数量publicstaticintCMPP_Port=7890;publiceventReportEventHandleronReportHandler;/指向事件处理代码的指针publiceventSMSEventHandleronSMSHandler;/短信到来处理publiceventTestEventHandleronTestHandler;publiceventTestRespEventHandleronTestRespHandler;publiceventConnectRespEventHandleronConnectRespHandler;publiceventCancelRespEventHandleronCancelRespHandler;publiceventTerminateEventHandleronTerminateHandler;publiceventTerminateRespEventHandleronTerminateRespHandler;publiceventSubmitRespEventHandleronSubmitRespHandler;publiceventQueryRespEventHandleronQueryRespHandler;publiceventLogonSuccEventHandleronLogonSuccEventHandler;publiceventSocketClosedEventHandleronSocketClosedHandler;publiceventFailedItemDeletedEventHandleronWaitingItemDeltedHandler;/当等待队列消息超时publiceventCMPPClientSvcStopEventHandleronClientSvcStopedHandler;/当服务停止时候的事件/private函数区域/privateSockettcp=null;privateIPHostEntryip=null;privateIPEndPointcmpp_ep=null;privateintRecvTimeOut=1000;/2000ms的接受超时privateintSendTimeout=2000;/2000ms的发送超时privatestringCMPP_Server=;/移动的服务器IP或者DNS名privatestringsystemID=;/企业编号privatestringuserName=;/sp的号码/企业编号privatestringPassWord=;/口令privateboolisStop=false;/本服务是否终止运行privateboolisLogin=false;/是否已经登录privateThreadSend_Thread;/发送线程,专门处理对移动的数据包privateThreadRecv_Thread;/专门处理接收包privateThreadDeamo_Thread;/监控线程privatestringErrorInfo=;/存放最后一次发生的错误信息或者参考信息privateDateTime_current_time=DateTime.Now;/上一次ping的时间privateuintlastSequence;/流水号,每一次重新启动都需要重新设定lastSequenceprivateSortedList_outSeqQueue=newSortedList();/消息队列存储QueueItem,存储发送队列中的状态privateSortedList_waitingSeqQueue=newSortedList();/消息队列存储QueueItemprivateintsub_resp=0;/最后返回的包SequenceprivateDateTime_lastOkTime;/最后正确发送消息时间privatebool_bNre=false;/空引用错误,套接字错误/privateManualResetEvent_connectionDone=newManualResetEvent(false);/是否连接到套接字服务器,也就是CMPP服务器/privateManualResetEvent_lastsendDone=newManualResetEvent(false);/上一次发送是否完毕/privateManualResetEvent_lastrecvDone=newManualResetEvent(false);/上一次接收是否完毕privatevoidping()/发送一次ping包,不经过_outSeqQueue直接存储在outqueue中uintseq=this.getNextSequence();MSG.CMPP_MSG_TESTtest=newMSG.CMPP_MSG_TEST(seq);QueueItemq=newQueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST,0,0);q.setmsgObj(test);this.addToOutQueue(q);privatestringgetValIdTime(DateTimed)/返回短信存活时间DateTimen=d.AddHours(2);/2小时return(n.Year.ToString().Substring(2)+n.Month.ToString().PadLeft(2,0)+n.Day.ToString().PadLeft(2,0)+n.Hour.ToString().PadLeft(2,0)+n.Minute.ToString().PadLeft(2,0)+n.Second.ToString().PadLeft(2,0)+032+);privateboolisPingTime()/是否到了ping一次的时间System.TimeSpanl=(DateTime.Now-this._current_time);if(l.TotalSeconds=(CMPPClient.CMPP_ACTIVE_TEST_C_TICKs)lock(this)this._current_time=DateTime.Now;return(true);elsereturn(false);privatevoidcheckReSend()/是否需要再一次ping/查询_waitingSeqQueue是否存在上一次没有相应的消息/调查waitingqueue中的所有消息,如果入列时间超过60for(inti=0;iCMPPClient.CMPP_ACTIVE_TEST_T_TICKs)/达到超时时间/需要重新发送消息if(q.FailedCount=CMPPClient.CMPP_ACTIVE_TEST_N_COUNT)/报告消息发送失败if(this.onWaitingItemDeltedHandler!=null)WaitingQueueItemEventArgse=newWaitingQueueItemEventArgs(q);this.onWaitingItemDeltedHandler(this,e);this.delFromWaitingQueue(q);/从等待队列中删除/q.MsgState=(int)MSG_STATE.SENDED_WAITTING;else/可以尝试继续发送q.inQueueTime=this_time;q.FailedCount+;q.MsgState=(int)MSG_STATE.SENDED_WAITTING;this.sendQueueItem(q);privatevoidstartThreads()Deamo_Thread=newThread(newThreadStart(this.DeamonThread);Deamo_Thread.Start();privateQueueItemnewQueueItem(intmsgtype,intmsgstate,objectmsg)/生成一个消息队列成员对象实例uintseq=this.getNextSequence();/QueueItemq=newQueueItem(seq,(uint)msgtype,0,msgstate);q.setmsgObj(msg);/设定消息为objectreturn(q);privateQueueItemgetOutQueueItem(uintseq)/获取MT队列中的消息项目lock(this)return(QueueItem)this._outSeqQueueseq);privateQueueItemgetWaitingQueueItem(uintseq)/获取等待队列中的消息return(QueueItem)this._waitingSeqQueueseq);privatevoidaddToOutQueue(QueueItemq)lock(this)this._outSeqQueue.Add(q.Sequence,q);privatevoidaddToWaitingQueue(QueueItemq)lock(this)if(!this._waitingSeqQueue.ContainsKey(q.Sequence)this._waitingSeqQueue.Add(q.Sequence,q);privateQueueItemgetTopOutQueue()/需要在取之前进行判断for(inti=0;ithis._outSeqQueue.Count;i+)QueueItemq=(QueueItem)this._outSeqQueue.GetByIndex(i);if(q!=null)if(q.MsgState=(int)MSG_STATE.NEW)/新消息,立即返回lock(this)q.MsgState=(int)MSG_STATE.SENDING;/发送状态return(q);elseq=null;return(null);privateArrayListgetTop16Queue()/返回16条最顶的消息intarrlength=0;ArrayListreArr=newArrayList();QueueItemq=getTopOutQueue();while(q!=null|arrlength0)return(reArr);elsereturn(null);privatevoiddelFromOutQueue(QueueItemq)lock(this)this._outSeqQueue.Remove(q.Sequence);privatevoiddelFromOutQueue(uintseq)lock(this)this._outSeqQueue.Remove(seq);privatevoiddelFromWaitingQueue(QueueItemq)lock(this)this._waitingSeqQueue.Remove(q.Sequence);privatevoiddelFromWaitingQueue(uintseq)this._waitingSeqQueue.Remove(seq);privatevoidSendLogin(stringSystemID,stringspNum,stringPassword)/发送登录验证包systemID=SystemID;userName=spNum;PassWord=Password;uintseq=this.getNextSequence();/取得一个流水号MSG.CMPP_MSG_CONNECTcn=newMSG.CMPP_MSG_CONNECT(seq);cn.Password=Password.Trim();cn.SourceAdd=SystemID.Trim();tcp.Send(cn.ToBytes();privatebyteprepairPKs(QueueItemoutitem)/将QueueItem发送出去uintseq=outitem.Sequence;uintmsgtype=outitem.MsgType;switch(msgtype)case(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST:MSG.CMPP_MSG_TESTtest=(MSG.CMPP_MSG_TEST)outitem.getMsgObj();/发送队列中取出lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);/等待服务器的active_TEST_respoutitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;return(test.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST_RESP:MSG.CMPP_MSG_TEST_RESPtest_reply=(MSG.CMPP_MSG_TEST_RESP)outitem.getMsgObj();/发送队列中取出/取出需要发送的具体消息lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;/完成return(test_reply.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_CANCEL:MSG.CMPP_MSG_CANCELcancel=(MSG.CMPP_MSG_CANCEL)outitem.getMsgObj();/还原成消息类lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);/等待回应outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;return(cancel.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_DELIVER_RESP:MSG.CMPP_MSG_DELIVER_RESPdeliver_resp=(MSG.CMPP_MSG_DELIVER_RESP)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;/完成return(deliver_resp.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_QUERY:MSG.CMPP_MSG_QUERYquery=(MSG.CMPP_MSG_QUERY)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;/等待回应return(query.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT:MSG.CMPP_MSG_SUBMITsubmit=(MSG.CMPP_MSG_SUBMIT)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;return(submit.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE:MSG.CMPP_MSG_TERMINATEterminate=(MSG.CMPP_MSG_TERMINATE)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;return(terminate.toBytes();case(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE_RESP:MSG.CMPP_MSG_TERMINATE_RESPterminate_resp=(MSG.CMPP_MSG_TERMINATE_RESP)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;return(terminate_resp.toBytes();default:test=(MSG.CMPP_MSG_TEST)outitem.getMsgObj();/发送队列中取出lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);/等待服务器的active_TEST_respoutitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;return(test.toBytes();privatevoidsendQueueItem(QueueItemoutitem)/将QueueItem发送出去uintseq=outitem.Sequence;uintmsgtype=outitem.MsgType;tryswitch(msgtype)case(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST:MSG.CMPP_MSG_TESTtest=(MSG.CMPP_MSG_TEST)outitem.getMsgObj();/发送队列中取出lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);/等待服务器的active_TEST_resptcp.Send(test.toBytes();outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST_RESP:MSG.CMPP_MSG_TEST_RESPtest_reply=(MSG.CMPP_MSG_TEST_RESP)outitem.getMsgObj();/发送队列中取出/取出需要发送的具体消息lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);tcp.Send(test_reply.toBytes();outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;/完成break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_CANCEL:MSG.CMPP_MSG_CANCELcancel=(MSG.CMPP_MSG_CANCEL)outitem.getMsgObj();/还原成消息类lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);/等待回应tcp.Send(cancel.toBytes();outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_DELIVER_RESP:MSG.CMPP_MSG_DELIVER_RESPdeliver_resp=(MSG.CMPP_MSG_DELIVER_RESP)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);tcp.Send(deliver_resp.toBytes();outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;/完成break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_QUERY:MSG.CMPP_MSG_QUERYquery=(MSG.CMPP_MSG_QUERY)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);tcp.Send(query.toBytes();outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;/等待回应break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_SUBMIT:MSG.CMPP_MSG_SUBMITsubmit=(MSG.CMPP_MSG_SUBMIT)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);tcp.Send(submit.toBytes();outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE:MSG.CMPP_MSG_TERMINATEterminate=(MSG.CMPP_MSG_TERMINATE)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);this.addToWaitingQueue(outitem);if(this.tcpIsCanUse()tcp.Send(terminate.toBytes();outitem.MsgState=(int)MSG_STATE.SENDED_WAITTING;this.isStop=true;/通知其他线程可以退出了break;case(uint)MSG.CMPP_COMMAND_ID.CMPP_TERMINATE_RESP:MSG.CMPP_MSG_TERMINATE_RESPterminate_resp=(MSG.CMPP_MSG_TERMINATE_RESP)outitem.getMsgObj();/发送队列中取出;lock(this)outitem.MsgState=(int)MSG_STATE.SENDING;this.delFromOutQueue(seq);tcp.Send(terminate_resp.toBytes();outitem.MsgState=(int)MSG_STATE.SENDING_FINISHED;break;LogLastOkTime(DateTime.Now);/记录当前最后一次消息soket正确时间catch(SocketExceptionse)/发生套接字错误this.ErrorInfo=this.ErrorInfo+rn+se.ToString();catch(NullReferenceExceptionnre)this._bNre=true;/出现空引用错误this.ErrorInfo=this.ErrorInfo+rn+nre.ToString();privatebooltcpIsCanUse()/测试当前tcp是否可用boolreval=true;DateTimet=DateTime.Now;TimeSpants=t-this._lastOkTime;if(ts.TotalSecondsCMPPClient.CMPP_ACTIVE_TEST_T_TICKs)/60秒reval=false;/不可用if(this._bNre)reval=false;return(reval);privatevoid_reStartRecvNSend()Send_Thread=newThread(newThreadStart(this.SendSPMsgThread);Send_Thread.Start();Recv_Thread=newThread(newThreadStart(this.RecvISMGMsgThread);Recv_Thread.Start();privatevoidLogLastOkTime(DateTimelastoktime)lock(this)this._lastOkTime=lastoktime;/设定最后成功消息交互时间private
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版肺癌早期症状识别及护理建议
- 2025版结肠癌常见症状及护理护士操作规范
- 3分钟科普演讲词
- 中班室内安全教育
- 2025版疼痛管理疾病症状诊疗与护理经验交流
- 高校毕业生实习评语书写指导手册
- 市政工程项目质量监控方案
- 肾绞痛监测与护理方案
- 石油测井解析与生产应用案例
- 用药与急救第2课时
- 高压清洗安全培训内容课件
- 2025年新疆阿克苏地区温宿县招聘警务辅助人员公共基础知识+写作综合练习题及答案
- DB32T 5177-2025预拌混凝土绿色生产管理技术规程
- 质检的管理制度
- 职业院校学生公寓管理服务实施指南
- 双杠2课件教学课件
- 保安岗位安全意识培训课件
- 2025-2026冀人版三年级科学上册教学设计(附目录)
- 2025年度山坪塘生态保护与承包管理合同
- 2025年综合执法局公务员招录考试法律法规知识解析与模拟题集
- 新能源充电行业知识培训课件
评论
0/150
提交评论