中国移动短信网关CMPP3.0 C#源代码:CMPP30类(2)

转载 2006年06月07日 09:07:00
protected CMPP_HEAD ReadHead()
  {
   CMPP_HEAD head=new CMPP_HEAD();
   head.CommandID=0;
   Byte[] buffer=new Byte[12];
   try
   {
    if(m_NetworkStream.DataAvailable)
    {
     m_NetworkStream.Read(buffer,0,buffer.Length);
     head.TotalLength=(UInt32)Utility.NetBytesToInt(buffer,0,4);
     head.CommandID=(UInt32)Utility.NetBytesToInt(buffer,4,4);
     head.SequenceID=(UInt32)Utility.NetBytesToInt(buffer,8,4);
    }

   }
   catch
   {
    head.CommandID=CMD_ERROR;
   }
   return head;
  }

  protected CMPP_CONNECT_RESP ReadConnectResp(CMPP_HEAD Head)
  {
   CMPP_CONNECT_RESP resp=new CMPP_CONNECT_RESP();
   resp.Head=Head;
   try
   {
    if(m_NetworkStream.DataAvailable)
    {
     Byte[] buffer=new Byte[resp.Head.TotalLength-Marshal.SizeOf(resp.Head)];
     m_NetworkStream.Read(buffer,0,buffer.Length);
     resp.Status=(UInt32)Utility.NetBytesToInt(buffer,0,4);
     resp.AuthenticatorISMG=new Byte[16];
     Array.Copy(buffer,4,resp.AuthenticatorISMG,0,16);
     resp.Version=buffer[buffer.Length-1];
    }
   }
   catch
   {
    resp.Head.CommandID=CMD_ERROR;
   }

   return resp;

   
  }
  protected CMPP_SUBMIT_RESP ReadSubmitResp(CMPP_HEAD Head)
  {
   CMPP_SUBMIT_RESP resp=new CMPP_SUBMIT_RESP();
   resp.Head=Head;
   string strError=string.Empty;
   bool bOK=true;
   try
   {
    if(m_NetworkStream.DataAvailable)
    {
     Byte[] buffer=new Byte[resp.Head.TotalLength-Marshal.SizeOf(resp.Head)];
     m_NetworkStream.Read(buffer,0,buffer.Length);
     //resp.MsgID=(UInt64)Utility.NetBytesToInt(buffer,0,8);
     resp.Msg_ID=(UInt64)BitConverter.ToUInt64(buffer,0);
     resp.Result=(UInt32)Utility.NetBytesToInt(buffer,8,4);
     Monitor.Enter(SlidingWindow);
     for(int i=0;i<m_iSlidingWindowSize;i++)
     {
      if((SlidingWindow[i].Status==1)&&//已发送,等待回应
       (SlidingWindow[i].SequenceID==resp.Head.SequenceID)&&//序列号相同
       (SlidingWindow[i].Command==CMD_SUBMIT))//是Submit
      {
       SlidingWindow[i].Status=0;//清空窗口
       break;
      }
     }
     Monitor.Exit(SlidingWindow);
    }
    
   }
   catch(Exception ex)
   {
    resp.Head.CommandID=CMD_ERROR;
    strError=ex.Message;
    bOK=false;
   }
   if(bOK)
    RaiseEvent(SMS_STATE.SUBMIT_RESPONSE,resp);
   else
    RaiseEvent(SMS_STATE.SUBMIT_RESPONSE_ERROR,strError);
   return resp;

  }

  protected CMPP_DELIVER ReadDeliver(CMPP_HEAD Head)
  {
   CMPP_DELIVER deliver=new CMPP_DELIVER();
   deliver.Head=Head;
   string strError=string.Empty;
   try
   {
    if(m_NetworkStream.DataAvailable)
    {
     Byte[] buffer=new Byte[deliver.Head.TotalLength-Marshal.SizeOf(deliver.Head)];
     m_NetworkStream.Read(buffer,0,buffer.Length);
     deliver.Init(buffer);
     DeliverResponse(deliver.Head.SequenceID,deliver.Msg_ID,0);
    }
   }
   catch(Exception ex)
   {
    deliver.Head.CommandID=CMD_ERROR;
    strError=ex.Message;
   }
   if((deliver.Head.CommandID==CMD_DELIVER)&&(deliver.Registered_Delivery==0))////是短消息
   {
    RaiseEvent(SMS_STATE.DELIVER,deliver);
   }
   else if((deliver.Head.CommandID==CMD_DELIVER)&&(deliver.Registered_Delivery==1))////是状态报告
   {
    RaiseEvent(SMS_STATE.REPORT,deliver);
   }
   else//错误
   {
    RaiseEvent(SMS_STATE.DELIVER_ERROR,strError);
   }
   return deliver;

  }

  protected bool DeliverResponse(UInt32 SequenceID,UInt64 Msg_Id,UInt32 Result)
  {
   bool bOK=true;
   string strError=string.Empty;
   CMPP_DELIVER_RESP resp=new CMPP_DELIVER_RESP();
   resp.Head=new CMPP_HEAD();
   resp.Head.CommandID=CMPP30.CMD_DELIVER_RESP;
   resp.Head.SequenceID=SequenceID;
   resp.Msg_Id=Msg_Id;
   resp.Result=Result;

   DATA_PACKAGE dp=new DATA_PACKAGE();
   dp.SequenceID=resp.Head.SequenceID;
   dp.Command=resp.Head.CommandID;
   dp.SendCount=0;
   dp.Data=resp;
   dp.Status=1;
 
   Monitor.Enter(m_MessageQueue);
   m_MessageQueue.Enqueue(dp);
   Monitor.Exit(m_MessageQueue);

   return bOK;
  }

  protected bool ActiveTest()
  {
   bool bOK=true;
   TimeSpan ts=DateTime.Now-m_dtLastTransferTime;
   if(ts.TotalSeconds>m_iActiveTestSpan)
   {
    CMPP_HEAD Head=new CMPP_HEAD();
    Head.CommandID=CMPP30.CMD_ACTIVE_TEST;
    Head.SequenceID=CreateID();
    Head.TotalLength=12;
   
    DATA_PACKAGE dp=new DATA_PACKAGE();
    dp.SequenceID=Head.SequenceID;
    dp.Command=Head.CommandID;
    dp.SendCount=0;
    dp.Data=Head;
    dp.Status=1;
 
    Monitor.Enter(m_MessageQueue);
    m_MessageQueue.Enqueue(dp);
    Monitor.Exit(m_MessageQueue);

   }
   return bOK;

  }
  protected CMPP_ACTIVE_TEST_RESP ReadActiveTestResponse(CMPP_HEAD Head)
  {
   CMPP_ACTIVE_TEST_RESP resp=new CMPP_ACTIVE_TEST_RESP();
   resp.Head=Head;
   string strError=string.Empty;
   bool bOK=true;
   try
   {
    if(m_NetworkStream.DataAvailable)
    {
     Byte[] buffer=new Byte[resp.Head.TotalLength-Marshal.SizeOf(resp.Head)];
     m_NetworkStream.Read(buffer,0,buffer.Length);
     resp.Reserved=buffer[0];
     Monitor.Enter(SlidingWindow);
     for(int i=0;i<m_iSlidingWindowSize;i++)
     {
      if((SlidingWindow[i].Status==1)&&//已发送,等待回应
       (SlidingWindow[i].SequenceID==resp.Head.SequenceID)&&//序列号相同
       (SlidingWindow[i].Command==CMD_ACTIVE_TEST))//是ACTIVE_TEST
      {
       SlidingWindow[i].Status=0;//清空窗口
       break;
      }
     }
     Monitor.Exit(SlidingWindow);
    }
    
   }
   catch(Exception ex)
   {
    resp.Head.CommandID=CMD_ERROR;
    strError=ex.Message;
    bOK=false;
   }

   if(bOK)
    RaiseEvent(SMS_STATE.ACTIVE_TEST_RESPONSE,resp);
   else
    RaiseEvent(SMS_STATE.ACTIVE_TEST_RESPONSE_ERROR,strError);
   
   return resp;
  }
  protected bool ActiveTestResponse(UInt32 SequenceID)
  {
   bool bOK=true;
   CMPP_ACTIVE_TEST_RESP resp=new  CMPP_ACTIVE_TEST_RESP();
   resp.Head=new CMPP_HEAD();
   resp.Head.CommandID=CMPP30.CMD_ACTIVE_TEST_RESP;
   resp.Head.SequenceID=SequenceID;
   resp.Reserved=0;
   
   DATA_PACKAGE dp=new DATA_PACKAGE();
   dp.SequenceID=resp.Head.SequenceID;
   dp.Command=resp.Head.CommandID;
   dp.SendCount=0;
   dp.Data=resp;
   dp.Status=1;
 
   Monitor.Enter(m_MessageQueue);
   m_MessageQueue.Enqueue(dp);
   Monitor.Exit(m_MessageQueue);

   return bOK;
  }


  #endregion

  #region Public Methods
  //开始线程
  public void StartThread()
  {
   if(m_SendThread==null)
   {
    m_dtLastTransferTime=DateTime.Now;
    m_SendThread=new Thread(new ThreadStart(this.SendThreadProc));
    //m_SendThread.IsBackground=false;
    m_SendThread.Name=m_strSPID+"_Send";
    m_SendThread.Start();
   }
   if(m_ReceiveThread==null)
   {
    m_ReceiveThread=new Thread(new ThreadStart(this.ReceiveThreadProc));
    //m_ReceiveThread.IsBackground=false;
    m_ReceiveThread.Name=m_strSPID+"_Receive";
    m_ReceiveThread.Start();
   }
   m_eventConnect.Set();

  }
  //结束线程
  public void EndThread()
  {
   m_eventSend.Reset();
   m_eventReceive.Reset();
   m_eventReceiveExit.Set();
   m_eventSendExit.Set();
  }
  //发送函数
  public bool Submit(string Message,
   string[] Destination,
   string Source,
   string ServiceID,
   CODING Coding,
   bool NeedReport,
   byte FeeUserType,
   byte FeeType,
   int InfoFee,
   string FeeUser,
   string LinkID)
  {
   bool bOK=true;
   string strError=string.Empty;
   CMPP_SUBMIT submit=new CMPP_SUBMIT();
   submit.Head=new CMPP_HEAD();
   submit.Head.CommandID=CMPP30.CMD_SUBMIT;
   submit.Head.SequenceID=CreateID();
   submit.Msg_ID=0;
   submit.Pk_Total=1;
   submit.Pk_Number=1;
   submit.Registered_Delivery=Convert.ToByte(NeedReport);
   submit.Msg_Level=0;
   submit.Service_Id=ServiceID;
   submit.Fee_UserType=FeeUserType;
   submit.Fee_Terminal_Id=FeeUser;
   submit.Fee_Terminal_Type=0;//真实号码
   submit.TP_Pid=0;
   submit.TP_Udhi=0;
   submit.Msg_Fmt=(Byte)Coding;
   submit.Msg_Src=m_strSPID;
   submit.FeeType=string.Format("{0:d2}",FeeType);
   submit.FeeCode=InfoFee.ToString();
   submit.Valid_Time=string.Empty;
   submit.At_Time=string.Empty;
   submit.Src_Id=Source;
   submit.DestUsr_Tl=(Byte)Destination.Length;
   submit.Dest_Terminal_ID=Destination;
   submit.Dest_Terminal_Type=0;//真实号码
   submit.Msg_Length=(Byte)Utility.CountLength(Message,Coding);
   submit.Msg_Content=Message;
   submit.LinkID=LinkID;
   
   
   DATA_PACKAGE dp=new DATA_PACKAGE();
   dp.SequenceID=submit.Head.SequenceID;
   dp.Command=submit.Head.CommandID;
   dp.SendCount=0;
   dp.Data=submit;
   dp.Status=1;
 
   Monitor.Enter(m_MessageQueue);
   m_MessageQueue.Enqueue(dp);
   Monitor.Exit(m_MessageQueue);

   return bOK;
  }
  #endregion
 }
 //异步发送回调函数
 public delegate bool CMPPAsyncSubmit(string Message,
          string[] Destination,
          string Source,
          string ServiceID,
          CODING Coding,
          bool NeedReport,
          byte FeeUserType,
          byte FeeType,
          int InfoFee,
          string FeeUser,
          string LinkID);

}

 【待续】

相关文章推荐

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

中国移动短信网关CMPP3.0 C#源代码:使用示例

Using Tiray.SMS...Tiray.SMS.CMPP30 m_CMPP=new Tiray.SMS.CMPP30();...//启动CMPPm_CMPP=new CMPP30(txt_CM...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

.Net/C# 实现 中国移动 CMPP v3.0 ISMG <-> SP 收发短信的 SP 客户端 (CMPP SP Client)

/*  .Net/C# 实现 中国移动 CMPP v3.0 ISMG SP 收发短信的 SP 客户端 (CMPP SP Client)  本程序严格按  《中国移动通信企业标准》之《中国移动通...
  • my98800
  • my98800
  • 2016年04月05日 08:34
  • 1075

基于 CMPP3.0 协议 SP 端的实现(C#)

      最近一段是时间项目可能需要提供短信功能,客户作为一个 SP 端提供短信业务;网上搜索了一番,找到了比较不错的 CMPP30 类的代码,该代码基本实现了 CMPP3.0 协议中的短信收发、网...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Net/C# 实现 中国移动 CMPP v3.0 ISMG SP 收发短信的 SP 客户端 (第2版) (CMPP SP Client)

/* .Net/C# 实现 中国移动 CMPP v3.0 ISMG SP 收发短信的 SP 客户端 (第2版)(CMPP SP Client) 增加了 CMPP Client 类本程序严格按 《中国...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:中国移动短信网关CMPP3.0 C#源代码:CMPP30类(2)
举报原因:
原因补充:

(最多只允许输入30个字)