From ca7b76ef27281af14b2203d3db1632c9b99d2a0a Mon Sep 17 00:00:00 2001 From: Tom Henderson Date: Sun, 25 May 2008 08:04:57 -0700 Subject: [PATCH] Add tag serialize methods; add serialize routines to Address --- src/node/address.cc | 24 ++++++++++++++++++++++++ src/node/address.h | 21 +++++++++++++++++++++ src/node/socket.cc | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/node/address.cc b/src/node/address.cc index 3db5f1c8d..fb49bf8ce 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -107,6 +107,30 @@ Address::Register (void) return type; } +uint32_t +Address::GetSerializedSize (void) const +{ + return 1 + 1 + m_len; +} + +void +Address::Serialize (uint8_t* buf, uint32_t len) const +{ + NS_ASSERT (len >= static_cast (m_len + 2)); + buf[0] = m_type; + buf[1] = m_len; + for (uint8_t i = 0; i < m_len; i++) + { + buf[i+2] = m_data[i]; + } +} + +Address +Address::Deserialize (const uint8_t* buf) +{ + return Address (buf[0], buf + 2, buf[1]); +} + ATTRIBUTE_HELPER_CPP (Address); diff --git a/src/node/address.h b/src/node/address.h index e5b6c82e3..cf0d0d847 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -153,6 +153,27 @@ public: * \returns a new type id. */ static uint8_t Register (void); + /** + * Get the number of bytes needed to serialize the underlying Address + * Typically, this is GetLength () + 2 + * + * \returns the number of bytes required for an Address in serialized form + */ + uint32_t GetSerializedSize (void) const; + /** + * Serialize this address in host byte order to a byte buffer + * + * \param buf output buffer that gets written with this Address + * \param len length of output buffer + */ + void Serialize (uint8_t* buf, uint32_t len) const; + /** + * \param buf buffer to read address from + * \returns an Address + * + * The input address buffer is expected to be in host byte order format. + */ + static Address Deserialize (const uint8_t* buf); ATTRIBUTE_HELPER_HEADER_1 (Address); private: diff --git a/src/node/socket.cc b/src/node/socket.cc index 07ebaf401..768fdd170 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -288,20 +288,35 @@ SocketRxAddressTag::GetInstanceTypeId (void) const { return GetTypeId (); } -uint32_t +uint32_t SocketRxAddressTag::GetSerializedSize (void) const -{ - return 0; +{ + return m_address.GetSerializedSize (); } -void +void SocketRxAddressTag::Serialize (TagBuffer i) const -{ - // for local use in stack only +{ + uint8_t len = m_address.GetSerializedSize (); + uint8_t* buffer = new uint8_t[len]; + memset (buffer, 0, len); + m_address.Serialize (buffer, len); + i.Write (buffer, len); + delete [] buffer; } -void +void SocketRxAddressTag::Deserialize (TagBuffer i) -{ - // for local use in stack only +{ + uint8_t type = i.ReadU8 (); + uint8_t len = i.ReadU8 (); + // Len is the length of the address starting from buffer[2] + NS_ASSERT (len >= 2); + uint8_t* buffer = new uint8_t[len]; + memset (buffer, 0, len); + buffer[0] = type; + buffer[1] = len; + i.Read (buffer+2, len); // ReadU8 consumes a byte + m_address = Address::Deserialize (buffer); + delete [] buffer; } SocketIpTtlTag::SocketIpTtlTag () @@ -335,20 +350,21 @@ SocketIpTtlTag::GetInstanceTypeId (void) const { return GetTypeId (); } + uint32_t SocketIpTtlTag::GetSerializedSize (void) const { - return 0; + return 1; } void SocketIpTtlTag::Serialize (TagBuffer i) const { - // for local use in stack only + i.WriteU8 (m_ttl); } void SocketIpTtlTag::Deserialize (TagBuffer i) { - // for local use in stack only + m_ttl = i.ReadU8 (); } }//namespace ns3