本文件实现SP端的协议开发。

本文件实现SP端的协议开发。

//附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 SortedList  _waitingSeqQueue=new SortedList(); //消息队列存储 QueueItem
  private int   sub_resp=0;       //最后返回的包 Sequence
  private DateTime _lastOkTime;      //最后正确发送消息时间
  private bool  _bNre=false;      //空引用错误,套接字错误
 
  //private ManualResetEvent _connectionDone=new ManualResetEvent(false); //是否连接到套接字服务器,也就是CMPP服务器
  //private ManualResetEvent _lastsendDone=new ManualResetEvent(false);  //上一次发送是否完毕
  //private ManualResetEvent _lastrecvDone=new ManualResetEvent(false);  //上一次接收是否完毕
      
  private void ping()    //发送一次ping包 ,不经过_outSeqQueue 直接存储在 out queue中
  {
   uint seq=this.getNextSequence();
   MSG.CMPP_MSG_TEST test=new MSG.CMPP_MSG_TEST(seq);
   QueueItem q=new QueueItem(seq,(uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST,0,0);
   q.setmsgObj(test);
   this.addToOutQueue(q);
  }
  
  private string getValIdTime(DateTime d)        //返回短信存活时间
  {
   DateTime n=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+");           
  }
 
  private bool isPingTime( )  //是否到了ping一次的时间
  {
   System.TimeSpan   l=(DateTime.Now - this._current_time );
  
   if ( l.TotalSeconds >= (CMPPClient.CMPP_ACTIVE_TEST_C_TICKs))
   {   
    lock(this)
    {
     this._current_time =DateTime.Now;       
     return(true);
    }
   }
   else
   {
    return(false);
   }
  }
 
  private void checkReSend()    //是否需要再一次ping //查询 _waitingSeqQueue 是否存在 上一次 没有相应的消息
  {   //调查waiting queue 中的所有消息,如果入列时间超过60
   for(int i=0;i<this._waitingSeqQueue.Count;i++)
   {
    Thread.Sleep(20);
    QueueItem q=(QueueItem)this._waitingSeqQueue.GetByIndex(i);  
    if(q!=null)
    {
     DateTime this_time=DateTime.Now ; //去当前时间
     TimeSpan t=this_time-q.inQueueTime ;
     if(t.TotalSeconds >CMPPClient.CMPP_ACTIVE_TEST_T_TICKs ) //达到超时时间
     {//需要重新发送消息
      if(q.FailedCount>=CMPPClient.CMPP_ACTIVE_TEST_N_COUNT)
      {
       //报告消息发送失败
       if(this.onWaitingItemDeltedHandler!=null)
       {
        WaitingQueueItemEventArgs e=new WaitingQueueItemEventArgs(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);     
      }
     }     
    }
   }   
 
  }
 
  private void startThreads()
  {
   Deamo_Thread=new Thread(new ThreadStart(this.DeamonThread));
   Deamo_Thread.Start();
  }
 
  private QueueItem newQueueItem(int msgtype,int msgstate,object msg)  //生成一个消息队列成员对象实例
  {
   uint seq=this.getNextSequence();   //
   QueueItem q=new QueueItem(seq,(uint)msgtype,0,msgstate);
   q.setmsgObj(msg);       //设定消息为 object
   return(q);
  }
 
  private QueueItem getOutQueueItem(uint seq)  //获取MT 队列中的消息项目
  {
   lock(this)
   {
    return((QueueItem)this._outSeqQueue[seq]) ;
   }
  }
 
  private QueueItem getWaitingQueueItem(uint seq)  //获取等待队列中的消息
  {
   return((QueueItem) this._waitingSeqQueue[seq]);
  }
       
  private void addToOutQueue(QueueItem q)
  {
   lock(this)
   {
    this._outSeqQueue.Add(q.Sequence,q);    
   }
  }
 
  private void addToWaitingQueue(QueueItem q)
  {
   lock(this)
   {
    if(!this._waitingSeqQueue.ContainsKey(q.Sequence))
    {
     this._waitingSeqQueue.Add(q.Sequence,q);  
    }
   }
  }
 
  private QueueItem getTopOutQueue()     //需要在取之前进行判断
  {
   for(int i=0;i<this._outSeqQueue.Count;i++)
   {
    QueueItem q=(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);  
     }
     else
     {
      q=null;
     }
    }
   }   
   return(null);
  }
 
  private ArrayList getTop16Queue() //返回16条最顶的消息
  {
   int arrlength=0;
   ArrayList reArr=new ArrayList() ;
   QueueItem q=getTopOutQueue();
   while(q!=null || arrlength <= 16)
   {    
    if(q!=null)
    {
     reArr.Add(q);
     arrlength++;
    }
    else
    {
     break;
    }
    q=getTopOutQueue();
   }
   
   if(arrlength>0)
   {
    return(reArr);  
   }
   else
   {
    return(null);
   }
  }
 
  private void delFromOutQueue(QueueItem  q)
  {
   lock(this)
   {
    this._outSeqQueue.Remove(q.Sequence);  
   }
  }
 
  private void delFromOutQueue(uint seq)
  {
   lock(this)
   {
    this._outSeqQueue.Remove(seq); 
   }
  }
 
  private void delFromWaitingQueue(QueueItem q)
  {
   lock(this)
   {
    this._waitingSeqQueue.Remove(q.Sequence); 
   }
  }
 
  private void delFromWaitingQueue(uint seq)
  {
   this._waitingSeqQueue.Remove(seq); 
  }
 
  private void  SendLogin(string SystemID,string spNum,string Password)
  {//发送登录验证包   
   systemID=SystemID;
   userName=spNum;
   PassWord=Password;
   uint seq=this.getNextSequence(); //取得一个流水号
   MSG.CMPP_MSG_CONNECT cn=new MSG.CMPP_MSG_CONNECT(seq);
   cn.Password =Password.Trim();
   cn.SourceAdd =SystemID.Trim();
   tcp.Send(cn.ToBytes());
  }
 

  private byte[]  prepairPKs(QueueItem outitem)//将QueueItem发送出去
  {
   uint seq=outitem.Sequence ;
   uint msgtype=outitem.MsgType;  
   switch(msgtype)
   {
    case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST :
     MSG.CMPP_MSG_TEST test=(MSG.CMPP_MSG_TEST) outitem.getMsgObj(); //发送队列中取出
     lock(this)
     {
      outitem.MsgState =(int)MSG_STATE.SENDING;
      this.delFromOutQueue(seq);
      this.addToWaitingQueue(outitem);    //等待服务器的active_TEST_resp
     }
     outitem.MsgState =(int)MSG_STATE.SENDED_WAITTING ;
     return(test.toBytes());      
      
 
    case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST_RESP:
     MSG.CMPP_MSG_TEST_RESP test_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_CANCEL cancel=(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_RESP deliver_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_QUERY query = (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_SUBMIT submit =(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_TERMINATE terminate=(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_RESP terminate_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_resp
     }
     outitem.MsgState =(int)MSG_STATE.SENDED_WAITTING ;
     return(test.toBytes()); 
   }   
  }

  private void sendQueueItem(QueueItem outitem)//将QueueItem发送出去
  {
   uint seq=outitem.Sequence ;
   uint msgtype=outitem.MsgType;
   try
   {
    switch(msgtype)
    {
     case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST :
      MSG.CMPP_MSG_TEST test=(MSG.CMPP_MSG_TEST) outitem.getMsgObj(); //发送队列中取出
      lock(this)
      {
       outitem.MsgState =(int)MSG_STATE.SENDING;
       this.delFromOutQueue(seq);
       this.addToWaitingQueue(outitem);    //等待服务器的active_TEST_resp
      }
      tcp.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_RESP test_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_CANCEL cancel=(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_RESP deliver_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_QUERY query = (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_SUBMIT submit =(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_TERMINATE terminate=(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_RESP terminate_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(SocketException se)
   {
    //发生套接字错误
    this.ErrorInfo =this.ErrorInfo +" "+se.ToString ();
   }
   catch(NullReferenceException nre)
   {
    this._bNre =true;  //出现空引用错误
    this.ErrorInfo =this.ErrorInfo +" "+nre.ToString ();
   }
  }
 
  private bool tcpIsCanUse()  //测试当前tcp是否可用
  {
   bool reval=true;
   DateTime t=DateTime.Now ;
   TimeSpan ts=t- this._lastOkTime;
   if(ts.TotalSeconds > CMPPClient.CMPP_ACTIVE_TEST_T_TICKs ) //60秒
   {
    reval=false;  //不可用
   }
   if(this._bNre )
   {
    reval=false;
   }
   return(reval);
  }
 
  private void _reStartRecvNSend()
  {
   Send_Thread=new Thread(new ThreadStart(this.SendSPMsgThread));
   Send_Thread.Start();
   Recv_Thread=new Thread(new ThreadStart(this.RecvISMGMsgThread));
   Recv_Thread.Start();
  }
 
  private void LogLastOkTime(DateTime lastoktime)
  {
   lock(this)
   {
    this._lastOkTime=lastoktime;  //设定最后成功消息交互时间
   }
  }
 
  private void defaultReportHandler() //却省的报告事件处理函数
  {
 
  }
 
  private void defaultSMSHandler()
  {
 
  }
 
  private void defaultTeminateHandler()
  {
 
  }
 
  private void defaultTestEventHandler()
  {
 
  }
  private void defaultTestRespEventHandler()
  {
 
  }
  private void defaultTerminateEventHandler()
  {
  }
  private void defaultTerminateRespEventHandler()
  {
  }
  private void defaultCancelRespEventHandler()
  {
  }
  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 +" "+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 +" "+"收到: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 +" "+"发送:CMPP_ACTIVE_TEST_RESP ";
        break;
 
       case (uint)MSG.CMPP_COMMAND_ID.CMPP_ACTIVE_TEST_RESP : //服务器的回应消息,应当丢弃不管
        this.ErrorInfo =this.ErrorInfo +" "+("收到: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 +" "+("收到: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 +" "+("收到: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 +" "+("收到: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 +" "+("发送: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 +" "+("收到: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 +" "+("收到: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 +" "+"收到: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 +" "+"收到: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 +" "+"收到: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()) 
    {
     if(this.isPingTime())
     {
      this.ping();  //发送一个ping包
     }
     if(t_count>50)  // 500*100=50000=50秒
     {
      t_count=0;
      checkReSend() ; //检查需要重新发送的消息
      //触发一个事件,让系统自动检查消息队列,存储消息队列中的消息状态
     }
    }
    else
    {
     EventArgs e=new EventArgs();
     if(this.onSocketClosedHandler!=null)
     {
      onSocketClosedHandler(this,e);
     }
     else
     {     
     }
     this.isStop =true;  //通知其他线程退出
    }
    Thread.Sleep(1000);
   }
  }  
 
  private void SendSPMsgThread()
  {
   while (!this.isStop )
   {
    Thread.Sleep(10);
    if(this.isLogin)
    {
     ArrayList lists=this.getTop16Queue();  //取出16条最顶的消息    
     if(lists!=null  && lists.Count >0)
     {
      int count=lists.Count;            
      ArrayList pks=new ArrayList( count); //定义容量
      for (int i=0;i<lists.Count; i++)
      {
       QueueItem outitem=(QueueItem)lists[i]; //取出每一个消息对象
       if(outitem!=null)
       {
        try
        {
         sendQueueItem(outitem);    //发送每一个消息
        }
        catch(SocketException se)
        {
         //发送失败
         outitem.FailedCount ++;
        }
       }      
      }
     }
    }
    Thread.Sleep(100);
   }  
  } 

  private void _StopMe()
  {
   lock(this)
   {
    this.isStop =true;
   }
  }
 
  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 +" "+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 + " "+" 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_

你可能感兴趣的:(本文件实现SP端的协议开发。)