Chinaunix首页 | 论坛 | 博客
  • 博客访问: 556
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-13 11:35
文章分类
文章存档

2013年(2)

我的朋友
最近访客

分类: C/C++

2013-07-15 21:52:27

一段序列化于反序列化的封装,准备再在此基础上 添加对结构的序列化、反序列化的自动生成工具。 不过怎么向下兼容还没考虑好,准备看完proto以后再动手

#   define  TCP_MAX_BUFFER    1024*16
#endif


#ifndef _WINDEF_
typedef unsigned char       BYTE;
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;


#if WIN32
typedef long long       int64_t;
#endif


#ifndef WIN32
#   define _atoi64 atoll
#endif


#define     SERVER_PACEKTVER    1
#define     PROTOL_HEADER_SIZE  8


/*
{
    short cmd;
    short cVersion;
};
*/




typedef unsigned long long __u64;
#define de_htonll(x) \
({ \
    __u64 __x = (x); \
    ((__u64)( \
    (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
    (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
    (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
    (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) | \
    (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
    (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
    (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
    (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
})




inline long long htonll(long long hvalue)
{
    short  i=20;
    {
        return hvalue;
    } 
    {
        return (long long)de_htonll(hvalue);
    }
}


inline long long ntohll(long long nvalue)
{
    short  i=20;
    {
        return nvalue;
    } 
    {
        return (long long)de_htonll(nvalue);
    }
}




inline string HexDumpImp(const void *pdata, unsigned int len)
{
        string outstr;
       if(pdata == 0 || len == 0)
        {
            return "";
        }
    
        int cnt = 0;
        int n = 0;
        int cnt2 = 0;
        stringstream  sstr;
        const char *data = (const char *)pdata;
        sstr<<"Address               Hexadecimal values                  Printable\n";
        sstr<<"-------  -----------------------------------------------  -------------\n";
        unsigned char buffer[20];
        unsigned int rpos = 0;


        while ( 1 )
        {
                if(len <= rpos)
                {
                        break;
                }
                if(len >= rpos + 16)
                {
                        memcpy(buffer, data + rpos, 16);
                        rpos += 16;
                        cnt = 16;
                }
                else
                {
                        memcpy(buffer, data + rpos, len - rpos);
                        cnt = len - rpos;
                        rpos = len;
                }
                if(cnt <= 0)
                {
                        outstr = sstr.str();
                        return outstr;
                }


                sstr << setw(7) << ( int ) rpos << "  ";


                cnt2 = 0;
                for ( n = 0; n < 16; n++ )
                {
                        cnt2 = cnt2 + 1;
                        if ( cnt2 <= cnt )
                        {
                                sstr << hex << setw(2) << setfill ( '0' ) <<  (unsigned int)buffer[n];
                        }
                        else
                        {
                                sstr << "  ";
                        }
                        sstr << " ";
                }


                sstr << setfill ( ' ' );


                sstr << " ";
                cnt2 = 0;
                for ( n = 0; n < 16; n++ )
                {
                        cnt2 = cnt2 + 1;
                        if ( cnt2 <= cnt )
                        {
                                if ( buffer[n] < 32 || 126 < buffer[n] )
                                {
                                        sstr << '.';
                                }
                                else
                                {
                                        sstr << buffer[n];
                                }
                        }
                }
                sstr << "\n";
                sstr << dec;
        }


        outstr = sstr.str();
        return outstr;
}






template
class PacketBase
{
public:
    char *packet_buf(void)   {return m_strBuf;}
    unsigned packet_size(void)    {return m_nPacketSize;}
protected:
PacketBase(void){}
    ~PacketBase(void){}
    enum
    {
        PACKET_HEADER_SIZE = _header_size,
        PACKET_BUFFER_SIZE = _buffer_size
    };
    unsigned  m_nBufPos;


protected:
    bool _copy(const void *pInBuf, unsigned nLen)
    {
        if(NULL==pInBuf || nLen > PACKET_BUFFER_SIZE){
            return false;
}


        _reset();
        memcpy(m_strBuf, pInBuf, nLen);
        m_nPacketSize = nLen;
        return true;
    }
    void _begin(short nCmdType, short cVersion)
    {
        _reset();


        short cmdType = htons(nCmdType);
        
        short version = htons(cVersion);
    }


    void _SetBegin(short nCmdType)
    {
        short cmdType = htons(nCmdType);
    }
public:
    short GetCmdType(void)
    {
        short nCmdType;
        return ntohs(nCmdType);
    }
    short GetVersion(void)
    {
        short c;
        return ntohs(c);
    }
    short GetBodyLength(void)
    {
        short nLen;
        return ntohs(nLen);
    }


protected:
    void _end(unsigned /*sequence*/ = 0)
    {
        short nBody = m_nPacketSize - PACKET_HEADER_SIZE;
        short bodyLen = htons(nBody);
    }


    void _reset(void)
    {
        memset(m_strBuf, 0, PACKET_BUFFER_SIZE);
        m_nBufPos = PACKET_HEADER_SIZE;
        m_nPacketSize = PACKET_HEADER_SIZE;
    }
    bool _Read(char *pOut, unsigned nLen)
    {
        if((nLen + m_nBufPos) > m_nPacketSize )
            return false ;


        memcpy(pOut, m_strBuf + m_nBufPos, nLen);
        m_nBufPos += nLen;
        return true;
    }
    bool _ReadDel(char *pOut, unsigned nLen)
    {
        if(!_Read(pOut, nLen)){
            return false;
}
        memcpy(m_strBuf + m_nBufPos - nLen, m_strBuf + m_nBufPos, PACKET_BUFFER_SIZE - m_nBufPos);
        m_nBufPos -= nLen;
        m_nPacketSize -= nLen;
        _end();
        return true;
    }
    void _readundo(unsigned nLen)
    {
        m_nBufPos -= nLen;
    }
    {
        if((nLen + m_nBufPos) > m_nPacketSize){
            return NULL; 
}
        char *p = &m_strBuf[m_nBufPos];
        m_nBufPos += nLen;
        return p;


    }
    bool _Write(const char *pIn, unsigned nLen)
    {
        if( (nLen + m_nPacketSize) > PACKET_BUFFER_SIZE){
            return false ;
}
        memcpy(m_strBuf+m_nPacketSize, pIn, nLen);
        m_nPacketSize += nLen;
        return true;
    }
    bool _Insert(const char *pIn, unsigned nLen)
    {
        if((nLen + m_nPacketSize) > PACKET_BUFFER_SIZE){
            return false;
}
        memcpy(m_strBuf+PACKET_HEADER_SIZE+nLen, m_strBuf+PACKET_HEADER_SIZE, m_nPacketSize-PACKET_HEADER_SIZE);
        memcpy(m_strBuf+PACKET_HEADER_SIZE, pIn, nLen);
        m_nPacketSize += nLen;
        _end();
        return true;
    }
    bool _writezero(void)
    {
        if((m_nPacketSize + 1) > PACKET_BUFFER_SIZE){
            return false ;
}
        memset(m_strBuf+m_nPacketSize, '\0', sizeof(char)) ;
        m_nPacketSize ++;
        return true;
    }
    void _readHeader(char *pOut, unsigned nLen, unsigned nPos)
    {
        if( nPos+nLen <= PACKET_HEADER_SIZE)
        {
            memcpy(pOut, m_strBuf+nPos, nLen) ;
        }
    }
    void _writeHeader(char *pIn, unsigned nLen, unsigned nPos)
    {
        if( nPos+nLen <= PACKET_HEADER_SIZE)
        {
            memcpy(m_strBuf+nPos, pIn, nLen) ;
        }
    }
};


template
class InputPacket: public PacketBase
{
public:
    typedef PacketBase base;


    int ReadInt(void)
    {
        int nValue = -1; 
        base::_Read((char*)&nValue, sizeof(int)); 
        return ntohl(nValue);
    } 


    short ReadShort(void)
    {
        short nValue = -1; 
        base::_Read((char*)&nValue, sizeof(short)); 
        return ntohs(nValue);
    }


    BYTE ReadByte(void)
    {
        BYTE nValue = -1; 
        base::_Read((char*)&nValue, sizeof(BYTE)); 
        return nValue;
    }


    int64_t ReadInt64(void)
    {
        int64_t nValue = -1; 
        base::_Read((char*)&nValue, sizeof(int64_t));
        return ntohll(nValue);
    }


    bool ReadString(char *pOutString, unsigned nMaxLen)
    {
        int nLen = ReadInt();
            return false;
}
        if(nLen > nMaxLen || nLen+base::m_nBufPos>base::m_nPacketSize )
        {
            base::_readundo(sizeof(unsigned));
            return false;
        }
        return base::_Read(pOutString, nLen);
    }


    char *ReadChar(void)
    {
        unsigned nLen = ReadInt();
        if(nLen == (unsigned)-1 || nLen+base::m_nBufPos>base::m_nPacketSize) {
base::_readundo(sizeof(unsigned));
return NULL;
}
        return base::_readpoint(nLen);
    }


    bool ReadString(string &str)
    {
        char *p = ReadChar();
        str =  (p == NULL ? "" : p);
return p != NULL;
    }


    int ReadBinary(char *pBuf, unsigned nMaxLen)
    {
        unsigned nLen = ReadInt();
        if(nLen == (unsigned)-1 || nLen > nMaxLen || nLen+base::m_nBufPos>base::m_nPacketSize )
        {
            base::_readundo(sizeof(unsigned));
            return -1;
        }
        if(base::_Read(pBuf, nLen)){
            return nLen ;
}
        return 0;
    }
    void Reset(void)
    {
        base::_reset();
    }
    bool Copy(const void *pInBuf, unsigned nLen)
    {
        return base::_copy(pInBuf, nLen);
    }
};


template
class OutputPacket: public PacketBase
{
public:
    OutputPacket(void){}
public:
    typedef PacketBase base;


    bool WriteInt(int nValue)
    {
        int value = htonl(nValue);
        return base::_Write((char*)&value, sizeof(int));
    }


    bool WriteInt64(int64_t nValue)     
    {
        int64_t value = htonll(nValue);
        return base::_Write((char*)&value, sizeof(int64_t));
    }


    bool WriteByte(BYTE nValue)     
    {
        return base::_Write((char*)&nValue, sizeof(BYTE));
    }


    bool WriteShort(short nValue)
    {
        short value = htons(nValue);
        return base::_Write((char*)&value, sizeof(short));
    }


    bool InsertInt(int nValue)
    {
        int value = htonl(nValue);
        return base::_Insert((char*)&value, sizeof(int));
    }


    bool InsertByte(BYTE nValue)    
    {
        return base::_Insert((char*)&nValue, sizeof(BYTE));
    }


    bool WriteString(const char *pString)
    {
        int nLen = (int)strlen(pString) ;
        WriteInt(nLen + 1) ;
        return base::_Write(pString, nLen) && base::_writezero();
    }


    bool WriteString(const string &strDate)
    {
        int nLen = strDate.size();
        WriteInt(nLen + 1) ;
        return base::_Write(strDate.c_str(), nLen) && base::_writezero();
    }


    bool WriteBinary(const char *pBuf, unsigned nLen)
    {
        WriteInt(nLen) ;
        return base::_Write(pBuf, nLen) ;
    }
    bool Copy(const void *pInBuf, unsigned nLen)
    {
        return base::_copy(pInBuf, nLen);
    }
    void Begin(short nCommand, char cVersion = SERVER_PACEKTVER)
    {
        base::_begin(nCommand, cVersion);
    }
    void End(void)
    {
        base::_end();
    }
   
    void SetBegin(short nCommand)
    {
        base::_SetBegin(nCommand);
    }
};


typedef InputPacket   InputPacket;
typedef OutputPacket  OutputPacket;

阅读(221) | 评论(0) | 转发(0) |
0

上一篇:加班

下一篇:没有了

给主人留下些什么吧!~~