报文拼接与解析

#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,'\t');
    SubType = subtype;
    SubType.resize(SUBTYPE,'\t');
    AppId = appid;
    AppId.resize(APPID,'\t');
    Passwd = passwd;
    Passwd.resize(PASSWD,'\t');
}

string CMessagePasswd::getString()
{
    string x;
    char temp[10];
    itoa(getLength(),temp,10);
    Length = temp;
    Length.resize(LENGTH,'\t');

    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,'\t');
    SubType = subtype;
    SubType.resize(SUBTYPE,'\t');
    AppId = appid;
    AppId.resize(APPID,'\t');
    AppSerialNo = appserialno;
    AppSerialNo.resize(APPSNO,'\t');
    MessageType = messagetype;
    MessageType.resize(MESSAGETYPE,'\t');
    RecvId = recvid;
    RecvId.resize(RECVID,'\t');
    Ack = ack;
    Ack.resize(ACK,'\t');
    Reply = reply;
    Reply.resize(REPLY,'\t');
    Priority = priority;
    Priority.resize(PRIORITY,'\t');
    Rep = rep;
    Rep.resize(REP,'\t');
    SubApp = subapp;
    SubApp.resize(SUBAPP,'\t');
    CheckFlag = checkflag;
    CheckFlag.resize(CHECKFLAG,'\t');
    Content = content;
    //Content.resize(CONTENT3011,'\t');
}

string CMessageLong::getString()
{
    string x;
    char temp[10];
    itoa(getLength(),temp,10);
    Length = temp;
    Length.resize(LENGTH,'\t');

    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;
}

 

posted @ 2015-07-24 15:19  凤舞十天  阅读(1376)  评论(0编辑  收藏  举报