封装好的UDP Helper

//UDP Socket 头文件
#ifndef __UDPSOCKET_H__
#define __UDPSOCKET_H__

#include <string>
#include <vector>
#include <windows.h>
#include "UDPPacket.h"


using namespace std;

typedef std::vector<sockaddr_in> VTRemoteAddr;

class UDPSocket
{
public:
    UDPSocket(void);
    ~UDPSocket(void);

    bool InitSocket(int nLocalPort, string strLocalAddr = "" );

    void AddRemoteAddr(int nRemotePort, string strRemoteIP );

    bool SendtoRemote(char* pBuffer, int length);

    bool SendtoRemote(int nMsgType , char* pBuffer, int length, int nReceiveMode = 0 ); // Receive 0 仅发送给DDS模块 1 仅发送给仪表模块 2 DDS模块和仪表模块同时发送 

    int RecefromRemote(char* pBuffer, int length);

    // unsigned short CalcCrc16( unsigned char * pData, int nLength );

private:
    SOCKET m_hSocket;// = NULL;
    VTRemoteAddr m_vRemoteAddr;
};

#endif

//源文件

#include "UDPPacket.h"

#include <iostream>
using namespace std;



bool DecodeBuff(char* pBuff,// 解包的buffer
    int nBuffLen, // Buffer长度
    int& nMsgType,// 消息类型
    int& nMsgLength,// 消息长度
    void* pMsg) // 消息内容
{

    //CString csLog = "701 DecodeBuff start.";


    //csLog.Format( );


    //OutputDebugString( "");


    if ( NULL == pBuff ||
        0 == nBuffLen ||
        NULL == pMsg )
    {
        return false;
    }

    //CString csLog;
    int nLeftDecodeSize = nBuffLen;

    nLeftDecodeSize -= sizeof( int );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }

    //csLog = _T( "701 DecodeBuff MsgType success. ");

    //OutputDebugString(csLog);

    memcpy( &nMsgType, pBuff, sizeof(int));
    pBuff += sizeof(int);

    nLeftDecodeSize -= sizeof( int );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }
    //csLog = _T( "701 DecodeBuff nMsgLength success. ");

    //OutputDebugString(csLog);
    memcpy( &nMsgLength, pBuff, sizeof(int));
    pBuff += sizeof(int); 

    if ( nMsgLength < 0 )
    {
        return false;
    }

    nLeftDecodeSize -= nMsgLength;
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }
    memcpy( pMsg, pBuff, nMsgLength);
    pBuff += nMsgLength;

    //csLog = _T( "701 DecodeBuff Msg Content success. ");

    //OutputDebugString(csLog);

    nLeftDecodeSize -= sizeof( unsigned short );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }

    //csLog = _T( "701 DecodeBuff CRC Key success. ");

    //OutputDebugString(csLog);

    unsigned short sCRCTmp = 0;

    memcpy(&sCRCTmp, pBuff, sizeof(unsigned short)); 

    unsigned short sCRCKey = CalcCrc16( (unsigned char*)pMsg,nMsgLength);

    return (sCRCKey == sCRCKey) ? true:false;


}


bool EncodeBuff(int nMsgType, // 消息类型
    int nMsgLength,// 消息长度
    void* pMsg, // 消息体
    char* pRetBuff,// 封装后的buff
    int& nRetBuffLen ) // Buff长度
{
    if ( 0 == nMsgLength ||
        NULL == pMsg ||
        NULL == pRetBuff )
    {
        return false;
    }
    nRetBuffLen = 0;

    memcpy( pRetBuff, &nMsgType, sizeof(int));
    nRetBuffLen += sizeof(int);
    pRetBuff+= sizeof(int);


    memcpy(pRetBuff, &nMsgLength, sizeof(int));
    nRetBuffLen += sizeof(int);
    pRetBuff+= sizeof(int);

    memcpy( pRetBuff, pMsg,nMsgLength );
    nRetBuffLen += nMsgLength;
    pRetBuff+= nMsgLength;

    unsigned short sCrcKey = CalcCrc16( (unsigned char*)pMsg,nMsgLength);

    memcpy( pRetBuff, &sCrcKey, sizeof(unsigned short));
    nRetBuffLen += sizeof(unsigned short);
    return true;
}



unsigned short CalcCrc16( unsigned char * pData, int nLength )
{
    unsigned short cRc_16 = 0x0000;    // 初始化
    const unsigned short cnCRC_16 = 0x8005;
    unsigned long cRctable_16[256];
    unsigned short i,j,k;

    for (i=0,k=0;i<256;i++,k++)
    {
        cRc_16 = i<<8;
        for (j=8;j>0;j--)
        {
            if (cRc_16&0x8000)                    //反转时cRc_16&0x0001
                cRc_16 = (cRc_16<<=1)^cnCRC_16;    //反转时cRc_16=(cRc_16>>=1)^gEnpoly
            else
                cRc_16<<=1;                        //反转时cRc_16>>=1
        }
        cRctable_16[k] = cRc_16;
    }

    while (nLength>0)
    {
        cRc_16 = (cRc_16 << 8) ^ cRctable_16[((cRc_16>>8) ^ *pData) & 0xff]; 
        nLength--;
        pData++;
    }

    return cRc_16; 
}
// UDPPacket 头文件
#ifndef __UDPPACKET_H__
#define __UDPPACKET_H__

bool EncodeBuff( int nMsgType, // 消息类型
                 int nMsgLength,// 消息长度
                 void* pMsg, // 消息体
                 char* pRetBuff,// 封装后的buff
                 int& nRetBuffLen ); // Buff长度


bool DecodeBuff( char* pBuff,// 解包的buffer
                 int nBuffLen, // Buffer长度
                 int& nMsgType,// 封装的消息类型
                 int& nMsgLength,// 消息长度
                 void* pMsg); // 消息内容




// CRC校验算法 
unsigned short CalcCrc16( unsigned char * pData, int nLength );

#endif

//源文件
#include "UDPPacket.h"

#include <iostream>
using namespace std;

bool DecodeBuff(char* pBuff,// 解包的buffer
    int nBuffLen, // Buffer长度
    int& nMsgType,// 消息类型
    int& nMsgLength,// 消息长度
    void* pMsg) // 消息内容
{

    //CString csLog = "701 DecodeBuff start.";


    //csLog.Format( );


    //OutputDebugString( "");


    if ( NULL == pBuff ||
        0 == nBuffLen ||
        NULL == pMsg )
    {
        return false;
    }

    //CString csLog;
    int nLeftDecodeSize = nBuffLen;

    nLeftDecodeSize -= sizeof( int );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }

    //csLog = _T( "701 DecodeBuff MsgType success. ");

    //OutputDebugString(csLog);

    memcpy( &nMsgType, pBuff, sizeof(int));
    pBuff += sizeof(int);

    nLeftDecodeSize -= sizeof( int );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }
    //csLog = _T( "701 DecodeBuff nMsgLength success. ");

    //OutputDebugString(csLog);
    memcpy( &nMsgLength, pBuff, sizeof(int));
    pBuff += sizeof(int); 

    if ( nMsgLength < 0 )
    {
        return false;
    }

    nLeftDecodeSize -= nMsgLength;
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }
    memcpy( pMsg, pBuff, nMsgLength);
    pBuff += nMsgLength;

    //csLog = _T( "701 DecodeBuff Msg Content success. ");

    //OutputDebugString(csLog);

    nLeftDecodeSize -= sizeof( unsigned short );
    if ( nLeftDecodeSize < 0 )
    {
        return false;
    }

    //csLog = _T( "701 DecodeBuff CRC Key success. ");

    //OutputDebugString(csLog);

    unsigned short sCRCTmp = 0;

    memcpy(&sCRCTmp, pBuff, sizeof(unsigned short)); 

    unsigned short sCRCKey = CalcCrc16( (unsigned char*)pMsg,nMsgLength);

    return (sCRCKey == sCRCKey) ? true:false;


}


bool EncodeBuff(int nMsgType, // 消息类型
    int nMsgLength,// 消息长度
    void* pMsg, // 消息体
    char* pRetBuff,// 封装后的buff
    int& nRetBuffLen ) // Buff长度
{
    if ( 0 == nMsgLength ||
        NULL == pMsg ||
        NULL == pRetBuff )
    {
        return false;
    }
    nRetBuffLen = 0;

    memcpy( pRetBuff, &nMsgType, sizeof(int));
    nRetBuffLen += sizeof(int);
    pRetBuff+= sizeof(int);


    memcpy(pRetBuff, &nMsgLength, sizeof(int));
    nRetBuffLen += sizeof(int);
    pRetBuff+= sizeof(int);

    memcpy( pRetBuff, pMsg,nMsgLength );
    nRetBuffLen += nMsgLength;
    pRetBuff+= nMsgLength;

    unsigned short sCrcKey = CalcCrc16( (unsigned char*)pMsg,nMsgLength);

    memcpy( pRetBuff, &sCrcKey, sizeof(unsigned short));
    nRetBuffLen += sizeof(unsigned short);
    return true;
}



unsigned short CalcCrc16( unsigned char * pData, int nLength )
{
    unsigned short cRc_16 = 0x0000;    // 初始化
    const unsigned short cnCRC_16 = 0x8005;
    unsigned long cRctable_16[256];
    unsigned short i,j,k;

    for (i=0,k=0;i<256;i++,k++)
    {
        cRc_16 = i<<8;
        for (j=8;j>0;j--)
        {
            if (cRc_16&0x8000)                    //反转时cRc_16&0x0001
                cRc_16 = (cRc_16<<=1)^cnCRC_16;    //反转时cRc_16=(cRc_16>>=1)^gEnpoly
            else
                cRc_16<<=1;                        //反转时cRc_16>>=1
        }
        cRctable_16[k] = cRc_16;
    }

    while (nLength>0)
    {
        cRc_16 = (cRc_16 << 8) ^ cRctable_16[((cRc_16>>8) ^ *pData) & 0xff]; 
        nLength--;
        pData++;
    }

    return cRc_16; 
}

 

posted @ 2020-04-25 13:57  骄傲到自负  阅读(347)  评论(0)    收藏  举报