CMPP(China Mobile Peer-to-Peer)是中國移動短信網關通訊協議。本文介紹基於 CMPP 3.0 協議的 SP 端 C# 實作核心 API 設計,涵蓋同步/異步發送、連線管理、狀態報告與 PROVISION 訂購介面支援。
封裝與 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);
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 事件,統一處理入口。
基於 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,記錄用戶訂購/取消資訊。
封裝移動端發布的 dsmp.wsdl Web 服務,核心邏輯同樣依賴 Provision 類別處理 SyncOrderRelationReq。
Send 回傳 CMPP_SUBMIT_RESP 含 MsgId,便於追蹤對應Start/Stop 單次使用限制需文件化