Serialize and deserialize packet header

196 views
Skip to first unread message

Ricardo G.

unread,
Oct 13, 2014, 7:54:29 PM10/13/14
to ns-3-...@googlegroups.com
Hi,

I can't find the problem in my method, it works until it reaches the part about the string.

The fields are:

///The fields in the class are
uint16_t          m_id
;          
uint8_t           m_reserved
;    
Ipv4Address       m_local;      
Ipv4Address       m_source;      
double            m_x;          
double            m_y;          
double            m_xVel;        
double            m_yVel;        
double            m_x1;          
double            m_y1;          
double            m_x2;          
double            m_y2;          
uint16_t          m_initialTime
;
uint16_t          m_duration
;    
std
::string       m_message;

And the methods are:

uint32_t
DataHeader::GetSerializedSize () const
{
 
return 79 + m_message.length() + 1; // 79 bytes for fields and 1 for string terminal ('\0')
}

void
DataHeader::Serialize (Buffer::Iterator i) const
{
 
Buffer::Iterator start = i;
  i
.WriteU16 (m_id);
  i
.WriteU8 (m_reserved);
 
WriteTo (i, m_local);
 
WriteTo (i, m_source);
  i
.WriteHtonU64 (m_x);
  i
.WriteHtonU64 (m_y);
  i
.WriteHtonU64 (m_xVel);
  i
.WriteHtonU64 (m_yVel);
  i
.WriteHtonU64 (m_x1);
  i
.WriteHtonU64 (m_y1);
  i
.WriteHtonU64 (m_x2);
  i
.WriteHtonU64 (m_y2);
  i
.WriteU16 (m_initialTime);
  i
.WriteU16 (m_duration);

 
char tmpBuffer [m_message.length() + 1];
  strcpy
(tmpBuffer, m_message.c_str());
  i
.Write ((uint8_t *)tmpBuffer, strlen(tmpBuffer) + 1);
}

uint32_t
DataHeader::Deserialize (Buffer::Iterator start)
{
 
Buffer::Iterator i = start;
  m_id      
= i.ReadU16 ();
  m_reserved
= i.ReadU8 ();
 
ReadFrom (i, m_local);
 
ReadFrom (i, m_source);
  m_x    
= i.ReadNtohU64 ();
  m_y    
= i.ReadNtohU64 ();
  m_xVel
= i.ReadNtohU64 ();
  m_yVel
= i.ReadNtohU64 ();
  m_x1  
= i.ReadNtohU64 ();
  m_y1  
= i.ReadNtohU64 ();
  m_x2  
= i.ReadNtohU64 ();
  m_y2  
= i.ReadNtohU64 ();
  m_initialTime
= i.ReadU16 ();
  m_duration    
= i.ReadU16 ();

  uint8_t c
;
  uint32_t len
= 0;
 
Buffer::Iterator i2 = i;

 
do
   
{
      c  
= i2.ReadU8 ();
      len
++;
   
} while (c != 0);
 
char tmpBuffer [len];
  start
.Read ((uint8_t*)tmpBuffer, len);
  m_message
= tmpBuffer;

  uint32_t dist
= i.GetDistanceFrom (start);
  NS_ASSERT
(dist == GetSerializedSize ());
 
return dist;
}



I get the error: assert failed. cond="dist == GetSerializedSize ()" and printing the m_message and len variables I can see that it just reads 1 until the while condition it's satisfied and m_message gets garbage like this: Âôìûÿ

I''ve tried without the i2 variable.

Thank you.

Tommaso Pecorella

unread,
Oct 14, 2014, 3:40:33 AM10/14/14
to ns-3-...@googlegroups.com
Hi,

dunno about the wrong (maybe) string length count, but for sure
  start.Read ((uint8_t*)tmpBuffer, len);

should be
  i.Read ((uint8_t*)tmpBuffer, len);

Hope this helps,

T


Ricardo

unread,
Oct 14, 2014, 6:39:08 AM10/14/14
to ns-3-...@googlegroups.com
Hi, sorry I didn't point it out, but yes, I tried that.

My thinking was: use iterator i2 to count how many characters there are until a terminator ('\0') is found, then read that many bytes with iterator i. But it doesn't work.

Thanks.

JaNa

unread,
Oct 14, 2014, 9:51:33 PM10/14/14
to ns-3-...@googlegroups.com
Hi,

I guess, yes,  Thar is happen.
How about try serialize and deserialize a char part alone as follow

virtual void Serialize (Buffer::Iterator start) const
{
  Buffer::Iterator it = start;

  int length = Astring.length();
  char str[length] = Astring.c_str();
  for (int i=0;i<length;i++)
    {
      it.WriteU8 (str[i]);
    }
}

virtual uint32_t Deserialize (Buffer::Iterator start)
{
  Buffer::iterator it = start;

  int length = GetSerializedSize();
  char str[length] = 0;
  for (int i=0;i<length;i++)
    {
      str[i] = it.ReadU8 ();
    }
  AString = string (str);
  return GetSerializedSize();
}


I guess this will help.

JaNa

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards..

    """""""""""
:)   JaNa     :) 
    """""""""""
Reply all
Reply to author
Forward
0 new messages