首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 實作 CMPP 3.0 協議 SP 端:短信收發與異步支援

     編輯:關於C#

CMPP(China Mobile Peer-to-Peer)是中國移動短信網關通訊協議。本文介紹基於 CMPP 3.0 協議的 SP 端 C# 實作核心 API 設計,涵蓋同步/異步發送、連線管理、狀態報告與 PROVISION 訂購介面支援。

核心類別:CMPP30

封裝與 ISMG(短信中心)的 TCP 長連線、封包編解碼、重連機制與事件驅動模型。

建構與連線管理


public class CMPP30
{
    public CMPP30(string spid, string password, string address, int port);
    
    public void Start();  // 啟動連線、驗證、啟動接收循環
    public void Stop();   // 停止服務,釋放資源(不可重複呼叫,需重新實例化)
}

同步發送


// 完整參數版本
public CMPP_SUBMIT_RESP Send(
    string text, CEncoding encoding, string sourceId,
    string[] destinations, string serviceId, bool needReport,
    FeeType feeType, FeeUserType feeUserType, string feeUser,
    bool realUser, int informationFee, string linkId);

// 封包物件版本(推薦,可獲取 ISMG 回傳的 MsgId)
public CMPP_SUBMIT_RESP Send(CMPP_SUBMIT submit);

標準異步模式(APM)


public IAsyncResult BeginSend(
    string text, CEncoding encoding, string sourceId,
    string[] destinations, string serviceId, bool needReport,
    FeeType feeType, FeeUserType feeUserType, string feeUser,
    bool realUser, int informationFee, string linkId,
    AsyncCallback callback, object asyncState);

public IAsyncResult BeginSend(CMPP_SUBMIT submit, AsyncCallback cb, object asyncState);

public CMPP_SUBMIT_RESP EndSend(IAsyncResult ar);

採用 .NET 標準 Begin/End 模式,支援 AsyncCallback 與 asyncState,便於整合進舊版非同步流程。

事件通知


public event EventHandler<SMSEventArgs> SMS;
// SMS_EVENT 列舉指示事件類型:收到下行、狀態報告、連線斷開等

所有與 ISMG 交互(含主動發送回應、被動接收)均觸發 SMS 事件,統一處理入口。

PROVISION 正向訂購介面支援

基於 SOAP/WebService 實作 SyncOrderRelationReq/Resp 交互。提供抽象基類 Provision,繼承 System.Web.Services.WebService:


[WebService(Namespace = "https://www.monternet.com/dsmp/schemas/")]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public abstract class Provision : WebService
{
    protected abstract void OnSyncOrderRelationReq(
        string id, AddressInfo sendAddr, AddressInfo destAddr,
        UserID feeUserID, UserID destUserID, string linkID,
        int actionID, int actionReasonID, string spid,
        string serviceID, int accessMode, byte[] featureStr,
        out int hResult);

    [WebMethod]
    [SoapHeader("TransactionID", Direction = SoapHeaderDirection.InOut)]
    [SoapDocumentMethod("sim.SyncOrderRelation", 
        RequestElementName = "SyncOrderRelationReq",
        ResponseElementName = "SyncOrderRelationResp")]
    public void SyncOrderRelationReq(/* 參數省略 */) { /* 模板方法實作 */ }
}

使用方式:建立 .asmx,後台類別繼承 Provision 並實作 OnSyncOrderRelationReq,記錄用戶訂購/取消資訊。

PROVISION 反向訂購介面

封裝移動端發布的 dsmp.wsdl Web 服務,核心邏輯同樣依賴 Provision 類別處理 SyncOrderRelationReq。

設計亮點總結

  • ✅ 完整實現 CMPP 3.0 封包結構與狀態機
  • ✅ 標準 APM 異步模式,非自訂 ThreadPool 佇列
  • ✅ Send 回傳 CMPP_SUBMIT_RESP 含 MsgId,便於追蹤對應
  • ✅ 事件驅動統一處理上行、狀態報告、連線狀態
  • ✅ PROVISION 介面抽象化,降低 SOAP/XML 處理複雜度
  • ⚠️ Start/Stop 單次使用限制需文件化
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved