1

I am trying to define a packet whose length is determined during an ns-3 simulation (think of it as a packet sent on the downlink containing schedule information whose length depends on the number of nodes in the network which can join/leave the network during simulation). Does anyone have any idea how I could approach this?

4

2 回答 2

0

关于如何做到这一点的最简单的介绍是http://www.nsnam.org/support/faq/miscellaneous/#table

实际上,如果您想扩展此代码以存储可变大小的数据结构,您可以这样做:

class MyHeader : public Header
{
public:
  // new methods
  void AppendData (uint16_t data);
  std::vector<uint16_t> GetData (void) const;

  static TypeId GetTypeId (void);
  // overridden from Header
  virtual uint32_t GetSerializedSize (void) const;
  virtual void Serialize (Buffer::Iterator start) const;
  virtual uint32_t Deserialize (Buffer::Iterator start);
  virtual void Print (std::ostream &os) const;
private:
  std::vector<uint16_t> m_data;
};

我将跳过明显的 GetData/AppendData 方法。相反,我们可以专注于 Serialize/Deserialize 方法:

uint32_t 
MyHeader::GetSerializedSize (void) const
{
  // two bytes of data to store
  return m_data.size() * 2;
}
void 
MyHeader::Serialize (Buffer::Iterator start) const
{
  start.WriteHtonU32(GetSerializedSize());
  for (std::vector<uint16_t>::const_iterator i = m_data.begin(); i != m_data.end(); i++) 
    {
      start.WriteHtonU16 (*i);
    }
}
uint32_t 
MyHeader::Deserialize (Buffer::Iterator start)
{
  uint32_t len = start.ReadNtohU32 ();
  for (uint32_t i = 0; i < len; i++) {
    m_data.append(start.ReadNtohU16())
  }
  return 4+len*2;
}
于 2013-04-18T20:16:29.680 回答
0

传统的解决方案是先发送长度,后发送数据:

+------------+---------------------+
| uint32_t n | n - 4 bytes of data |
+------------+---------------------+

要解码,请读取前四个字节,然后使用这些字节中的值来确定还有多少数据。

于 2013-04-17T22:58:44.027 回答