commit b92a45d710354faef741ec57b27e92f8c6d767e0 Author: Matias F Date: Wed Aug 10 11:51:52 2011 -0300 Initial commit. Created PDU class. diff --git a/include/pdu.h b/include/pdu.h new file mode 100644 index 0000000..e2837e9 --- /dev/null +++ b/include/pdu.h @@ -0,0 +1,40 @@ +#ifndef __PDU_H +#define __PDU_H + + +#include + +class PDU { +public: + PDU(uint32_t pdu_flag, PDU *next_pdu = 0); + virtual ~PDU(); + + /* This PDU's header size only. */ + virtual uint32_t header_size() const = 0; + /* This PDU's trailer size only. Defaults to 0. */ + virtual uint32_t trailer_size() const { return 0; } + + /* The size of the whole chain of PDUs, including this one. */ + uint32_t size() const; + + inline const PDU *inner_pdu() const { return _inner_pdu; } + + /* When setting a new inner_pdu, the instance takes + * ownership of the object, therefore deleting it when + * it's no longer required. */ + void inner_pdu(PDU *next_pdu); + + /* Serializes the whole chain of PDU's, including this one. */ + uint8_t *serialize(); + + /* Serialize this PDU storing the result in buffer. */ + void serialize(uint8_t *buffer, uint32_t total_sz); +protected: + /* Each PDU's own implementation of serialization. */ + virtual void write_serialization(uint8_t *buffer, uint32_t total_sz) = 0; +private: + uint32_t _pdu_flag; + PDU *_inner_pdu; +}; + +#endif diff --git a/src/pdu.cpp b/src/pdu.cpp new file mode 100644 index 0000000..2b42bf0 --- /dev/null +++ b/src/pdu.cpp @@ -0,0 +1,41 @@ +#include +#include "pdu.h" + + +PDU::PDU(uint32_t pdu_flag, , PDU *next_pdu) : _pdu_flag(pdu_flag), _inner_pdu(next_pdu) { + +} + +PDU::~PDU() { + delete _inner_pdu; +} + +uint32_t PDU::size() const { + uint32_t sz = header_size() + trailer_size(); + PDU *ptr(_inner_pdu); + while(ptr) { + sz += ptr->header_size() + trailer_size(); + ptr = ptr->inner_pdu(); + } + return sz; +} + +void PDU::inner_pdu(PDU *next_pdu) { + delete _inner_pdu; + _inner_pdu = next_pdu; +} + +uint8_t *PDU::serialize() { + uint32_t sz(size()); + uint8_t *buffer = new uint8_t[sz]; + +} + +void PDU::serialize(uint8_t *buffer, uint32_t total_sz) { + uint32_t sz = header_size() + trailer_size(); + write_serialization(buffer, total_sz); + /* Must not happen... */ + std::assert(total_sz >= sz); + if(_inner_pdu) + _inner_pdu->serialize(buffer + header_size(), total_sz - sz); +}