日期:2014-05-17  浏览次数:21144 次

CMPP SP端 C# 实例
//附CMPPClient.cs:
/*
作者:TNT 时间:2003年12月
文件说明:本文件实现SP端的协议开发。
*/
using System;
using System.Security.Cryptography;  
using System.Net.Sockets; 
using System.Net;
using System.Text; 
using System.Threading; 
using System.Collections; 

namespace CMPP.YOURCOMPANY
{
 public delegate void ReportEventHandler(object sender, ReportEventArgs e);  //声明一个事件的指代(指针)
 public delegate void SMSEventHandler(object sender, SMSEventArgs e);   //声明一个事件的指代(指针)
 public delegate void TerminateEventHandler(object sender,TerminateEventArgs e);   //声明收到终止信号
 public delegate void TerminateRespEventHandler(object sender,TerminateRespEventArgs e);  //回应事件发生
 public delegate void TestEventHandler(object sender,TestEventArgs e);
 public delegate void TestRespEventHandler(object sender,TestRespEventArgs e);
 public delegate void ConnectRespEventHandler(object sender,ConnectRespEventArgs e);
 public delegate void CancelRespEventHandler(object sender,CancelRespEventArgs e);
 public delegate void SubmitRespEventHandler(object sender,SubmitRespEventArgs e);
 public delegate void QueryRespEventHandler(object sender,QueryRespEventArgs e);
 public delegate void LogonSuccEventHandler(object sender,EventArgs e); //当成功登录系统
 public delegate void SocketClosedEventHandler(object sender,EventArgs e); //当套接字被检测到关闭
 public delegate void FailedItemDeletedEventHandler(object sender,WaitingQueueItemEventArgs e); //当一条等待队列的消息超过60秒没有回应
 

 public delegate void CMPPClientSvcStopEventHandler(object sender, ClientQueueStateArgs e); //当CMPP服务停止时候触发事件

 /// <summary>
 /// 作为CMPP协议的客户端,具有的登陆、发送、接受功能
 /// 会开3 个线程处理: 1、处理需要发送 MO(下行)的消息 
 ///       2、处理从移动服务器发送过来CMPP的消息
 ///       3、处理连接断等信息,检查需要重发的消息,检查收到的报告、短信,并调用 OnReport 事件 OnSMS事件
 /// </summary>
 public class CMPPClient
 { 
  public static long CMPP_ACTIVE_TEST_C_TICKs= 30  ;  // *3 ;  //长连接的active_test测试时间
  public static long CMPP_ACTIVE_TEST_T_TICKs= 60 ;    // 消息失败时间 60秒
  public static int CMPP_ACTIVE_TEST_N_COUNT=3;  //3次  
  //public static int CMPP_MSG_MAX=100;   //一次取得的最大消息数量
  public static int CMPP_Port=7890;
  
  public event ReportEventHandler onReportHandler;   //指向事件处理代码的指针
  public event SMSEventHandler onSMSHandler;     //短信到来处理
  public event TestEventHandler onTestHandler;
  public event TestRespEventHandler onTestRespHandler;
  public event ConnectRespEventHandler onConnectRespHandler;
  public event CancelRespEventHandler onCancelRespHandler;
  public event TerminateEventHandler onTerminateHandler;
  public event TerminateRespEventHandler onTerminateRespHandler;
  public event SubmitRespEventHandler onSubmitRespHandler;
  public event QueryRespEventHandler onQueryRespHandler;
  public event LogonSuccEventHandler onLogonSuccEventHandler;
  public event SocketClosedEventHandler onSocketClosedHandler;
  public event FailedItemDeletedEventHandler onWaitingItemDeltedHandler; //当等待队列消息超时
  
  public event CMPPClientSvcStopEventHandler onClientSvcStopedHandler;  //当服务停止时候的事件

  //private 函数区域//////////////////////////////////////////////////////////////////////
  private Socket  tcp=null;     
  private IPHostEntry ip=null;   
  private IPEndPoint  cmpp_ep=null;   
  private int   RecvTimeOut =1000;       //2000ms的接受超时
  private int   SendTimeout =2000;       //2000ms的发送超时 
  private string  CMPP_Server="";   //移动的服务器IP或者DNS名
  private string  systemID="";   //企业编号
  private string  userName="";   //sp的号码 /企业编号
  private string  PassWord="";   //口令 
  private bool  isStop=false;   //本服务是否终止运行
  private bool  isLogin=false;   //是否已经登录    
  private Thread  Send_Thread;   //发送线程,专门处理对移动的数据包
  private Thread  Recv_Thread;   //专门处理接收包
  private Thread  Deamo_Thread;   //监控线程
  private string  ErrorInfo="";   //存放最后一次发生的错误信息 或者参考信息      
  private DateTime _current_time=DateTime.Now;     //上一次 ping的时间  
  private uint  lastSequence;   //流水号,每一次重新启动都需要重新设定 lastSequence
  private SortedList _outSeqQueue=new SortedList();   //消息队列存储 QueueItem,存储发送队列中的状态
  private