• 报文拼接与解析


    #ifndef _NMS_MESSAGE_H_
    #define _NMS_MESSAGE_H_
    #include "config.h"
    #define LENGTH 5  //报文长度
    #define TRANSTYPE 4  //交易代码
    #define SUBTYPE 2   //标志字段
    #define APPID 12  //应用标识
    #define PASSWD 21  //应用程序密码
    #define APPSNO 35  //应用流水号
    #define MESSAGETYPE 3 //短信类型0表示中文;1表示英文;2表示UCS2码;21,4为压缩PDU码
    #define RECVID 255  //接收方地址
    #define CONTENT1002 160  //正文内容
    #define CONTENT3002 280
    #define ACK 1  //0表示不回复,1表示需要回执,2表示需回复,3表示需要回执+需回复
    #define REPLY 30  //回复地址
    #define PRIORITY 2  //优先级0~9
    #define REP 2  //重复次数
    #define SUBAPP 3   //子应用号
    #define CHECKFLAG 2  //标志位
    
    class CMessagePasswd
    {
    public:
        CMessagePasswd(string transtype,string subtype,string appid,string passwd);
        ~CMessagePasswd(){};
        string getString();
        int getLength() const;//Length值
        int getSize() const;//报文总长度
    private:
        string Length;
        string TransType;
        string SubType;
        string AppId;
        string Passwd;
    };    
    
    class CMessageLong{
    public:
        CMessageLong(string transtype,string subtype,string appid,string appserialno,string messagetype,string recvid,string ack,string reply,
            string priority,string rep,string subapp,string checkflag,string content);
        ~CMessageLong(){};
        string getString();
        int getLength() const;//Length值
        int getSize() const;//报文总长度
    private:
        string Length;
        string TransType;
        string SubType;
        string AppId;
        string AppSerialNo;
        string MessageType;
        string RecvId;
        string Ack;
        string Reply;
        string Priority;
        string Rep;
        string SubApp;
        string CheckFlag;
        string Content;//变长字段,长度另外计算
    };
    
    class CMessageReply{
    public:
        CMessageReply(string s);
        ~CMessageReply(){};
        //const修饰类的成员函数会禁止该函数改变类的成员变量的值
        bool Success() const;//成功返回TRUE,失败返回FALSE
        string GetFailReason() const;//返回失败原因
        string getLength() const;//备用,返回响应报文长度
        string getRetCode() const;//备用, 返回响应代码
    private:
        int temp;
        string Length;
        string RetCode;
        string RetMsg;
    };
    
    #endif
    #include "NmsMessage.h"
    
    CMessagePasswd::CMessagePasswd(string transtype,string subtype,string appid,string passwd)
    {
        TransType = transtype;
        TransType.resize(TRANSTYPE,'	');
        SubType = subtype;
        SubType.resize(SUBTYPE,'	');
        AppId = appid;
        AppId.resize(APPID,'	');
        Passwd = passwd;
        Passwd.resize(PASSWD,'	');
    }
    
    string CMessagePasswd::getString()
    {
        string x;
        char temp[10];
        itoa(getLength(),temp,10);
        Length = temp;
        Length.resize(LENGTH,'	');
    
        x.append(Length);
        x.append(TransType);
        x.append(SubType);
        x.append(AppId);
        x.append(Passwd);
        return x;
    }
    
    int CMessagePasswd::getLength() const//Length值
    {
        return getSize()-LENGTH;
    }
    
    int CMessagePasswd::getSize() const//报文总长度
    {
        return LENGTH+TRANSTYPE+SUBTYPE+APPID+PASSWD;
    }
    
    CMessageLong::CMessageLong(string transtype,string subtype,string appid,string appserialno,string messagetype,string recvid,string ack,string reply,
        string priority,string rep,string subapp,string checkflag,string content)
    {
        TransType = transtype;
        TransType.resize(TRANSTYPE,'	');
        SubType = subtype;
        SubType.resize(SUBTYPE,'	');
        AppId = appid;
        AppId.resize(APPID,'	');
        AppSerialNo = appserialno;
        AppSerialNo.resize(APPSNO,'	');
        MessageType = messagetype;
        MessageType.resize(MESSAGETYPE,'	');
        RecvId = recvid;
        RecvId.resize(RECVID,'	');
        Ack = ack;
        Ack.resize(ACK,'	');
        Reply = reply;
        Reply.resize(REPLY,'	');
        Priority = priority;
        Priority.resize(PRIORITY,'	');
        Rep = rep;
        Rep.resize(REP,'	');
        SubApp = subapp;
        SubApp.resize(SUBAPP,'	');
        CheckFlag = checkflag;
        CheckFlag.resize(CHECKFLAG,'	');
        Content = content;
        //Content.resize(CONTENT3011,'	');
    }
    
    string CMessageLong::getString()
    {
        string x;
        char temp[10];
        itoa(getLength(),temp,10);
        Length = temp;
        Length.resize(LENGTH,'	');
    
        x.append(Length);
        x.append(TransType);
        x.append(SubType);
        x.append(AppId);
        x.append(AppSerialNo);
        x.append(MessageType);
        x.append(RecvId);
        x.append(Ack);
        x.append(Reply);
        x.append(Priority);
        x.append(Rep);
        x.append(SubApp);
        x.append(CheckFlag);
        x.append(Content);
        return x;
    }
    int CMessageLong::getLength() const//Length值
    {
        return getSize()-LENGTH;
    }
    int CMessageLong::getSize() const//报文总长度
    {
        return LENGTH+TRANSTYPE+SUBTYPE+APPID+APPSNO+MESSAGETYPE+RECVID+ACK+REPLY+PRIORITY+REP+SUBAPP+CHECKFLAG+Content.size();
    }
    
    CMessageReply::CMessageReply(string s)
    {
        Length = s.substr(0,5);
        RetCode = s.substr(5,4);
        temp = atoi(Length.c_str());
        RetMsg = s.substr(9,temp-4);
    }
    bool CMessageReply::Success() const//成功返回TRUE,失败返回FALSE
    {
        if (RetCode == "0000")
            return true;
        else return false;
    }
    string CMessageReply::GetFailReason() const//返回失败原因
    {
        return RetMsg;
    
    }
    string CMessageReply::getLength() const//备用,返回响应报文长度
    {
        return Length;
    }
    string CMessageReply::getRetCode() const//备用, 返回响应代码
    {
        return RetCode;
    }
  • 相关阅读:
    如何在项目中添加Log4net_web.config
    数据库语法集合
    在asp.net 中web.config配置错误页
    【AngularJs】---JSONP跨域访问数据传输(JSON_CALLBACK)
    菜鸟教程下笔记借鉴
    AngularJs ng-repeat重复项异常解决方案
    AngularJS
    基于node安装gulp-一些命令
    理解Flexbox:你需要知道的一切
    深入理解 flex 布局以及计算_Flexbox, Layout
  • 原文地址:https://www.cnblogs.com/fwst/p/4673601.html
Copyright © 2020-2023  润新知