Serialize and send a data structure using Boost?

后端 未结 7 1402
半阙折子戏
半阙折子戏 2020-12-02 10:45

I have a data structure that looks like this:

typedef struct
{
  unsigned short m_short1;
  unsigned short m_short2;
  unsigned char m_character;
} MyDataType;
         


        
7条回答
  •  悲&欢浪女
    2020-12-02 11:17

    For such simple structure, boost::serialization is overkill and huge overhead.

    Do simpler:

    vector net(3,0);
    
    net[0]=htons(data.m_short1);
    net[1]=htons(data.m_short2);
    net[2]=htons(data.character);
    
    asio::async_write(socket,buffer((char*)&net.front(),6),callback);
    
    vector net(3,0);
    asio::async_read(socket,buffer((char*)&net.front(),6),callback);
    
    callback:
    data.m_short1=ntohs(net[0]);
    data.m_short2=ntohs(net[1]);
    data.character=ntohs(net[2]);
    

    And Save yourself HUGE overhead that boost::serialization has

    And if you private protocol where computers with same order of bytes work (big/little) that just send structure as is -- POD.

提交回复
热议问题