Deserialize a vector header

49 views
Skip to first unread message

Najma

unread,
Jun 3, 2020, 12:47:33 PM6/3/20
to ns-3-users
My header is a vector in which each element is a byte (uint8_t) and its size is dynamic. In the Deserialize method in the header class, I need to read the buffer and assign it to a vector. I think the problem with my code is that it reads the first byte all the time. Is there any idea to solve it.

Header::Deserialize(Buffer::Iterator start )
{
   /* Read one byte at a time into the vector until iterator reaches the end */
   m_vectorList.clear();
   uint8_t m_temp ;
   uint32_t buffersize = start.GetSize();

   while (m_bufferSize > 0)
   {    
   
   m_temp = start.ReadU8();
   m_vectorList.push_back(m_temp);
   --m_bufferSize;
   }
       return GetSerializedSize();
}

Adil Alsuhaim

unread,
Jun 5, 2020, 7:48:40 PM6/5/20
to ns-3-users
First of all, I am going to assume that your class name is not really "Header" since that's the type in ns-3, although the ns-3 type is ns3::Header. To clear confusion, you should probably chose a different name. Also, try to follow the naming convention of the way ns-3 does the C++ names. An identifier starting with m_ is a class's instance variable, so try not to name local variables like that. 

So, you have a header of variable length, and when we have headers like that, the header itself should include a length/size before that data of variable length, and from there we know how many bytes to read afterwards. So, when you "serialize" the header, you should format it with a length field coming before the the vector items. This is very common practice with headers of potentially variable length such as the IPv4 which starts with protocol version followed by the header's length (most of the time, the IPv4 header length is 20 Bytes, but it can be longer if it has options). 

Your loop control variable is m_bufferSize, but I am not sure if that's set anywhere since your start.GetSize() is stored in buffersize and NOT m_bufferSize. 

The way I would do this is as follows, I am not sure if it's perfect, but it should look something like this:

uint32_t
MyHeader::Deserialize (Buffer::Iterator start)
{
  m_vectorList
.clear (); // I am assuming we will have a 'Get' function to acquire this
  uint32_t header_length
= start.ReadU16 (); //assuming header length is a 2-bytes, which limits the size of the vector to 65KB
 
 
for (uint32_t i=0; i<header_length; i++)
 
{
    m_vectorList
.push_back ( start.ReadU8() );
 
}
 
return GetSerializedSize();
}

uint32_t
MyHeader::GetSerializedSize ()
{
 
return 2 + m_vectorList.size(); // 2 for the header length (2-bytes) and then whatever number of bytes in the vector.
}



Cheers!

Adil







Reply all
Reply to author
Forward
0 new messages