From 731e20d0a3ed023f845dc618fea21c391411edb6 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 10 Apr 2017 22:09:56 +0200 Subject: [PATCH] switch BanListItem classes to new serialization system --- .../src/serialiser/rsbanlistitems.cc | 299 ++---------------- libretroshare/src/serialiser/rsbanlistitems.h | 44 +-- libretroshare/src/serialiser/rsbaseserial.cc | 23 +- libretroshare/src/serialiser/rsbaseserial.h | 14 +- libretroshare/src/serialiser/rsserial.h | 7 +- .../src/serialization/rstypeserializer.h | 36 ++- 6 files changed, 93 insertions(+), 330 deletions(-) diff --git a/libretroshare/src/serialiser/rsbanlistitems.cc b/libretroshare/src/serialiser/rsbanlistitems.cc index b22c16d2b..a110ec00f 100644 --- a/libretroshare/src/serialiser/rsbanlistitems.cc +++ b/libretroshare/src/serialiser/rsbanlistitems.cc @@ -25,7 +25,8 @@ #include "serialiser/rsbaseserial.h" #include "serialiser/rsbanlistitems.h" -#include "serialiser/rstlvbanlist.h" + +#include "serialization/rstypeserializer.h" /*** #define RSSERIAL_DEBUG 1 @@ -35,300 +36,36 @@ /*************************************************************************/ -RsBanListItem::~RsBanListItem() -{ - return; -} - void RsBanListItem::clear() { peerList.TlvClear(); } -std::ostream &RsBanListItem::print(std::ostream &out, uint16_t indent) +void RsBanListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - printRsItemBase(out, "RsBanListItem", indent); - uint16_t int_Indent = indent + 2; - peerList.print(out, int_Indent); - - printRsItemEnd(out, "RsBanListItem", indent); - return out; + RsTypeSerializer::serial_process(j,ctx,peerList,"peerList") ; } - -uint32_t RsBanListSerialiser::sizeList(RsBanListItem *item) +void RsBanListConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - uint32_t s = 8; /* header */ - s += item->peerList.TlvSize(); - - return s; + RsTypeSerializer::serial_process(j,ctx,type,"type") ; + RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process (j,ctx,update_time,"update_time") ; + RsTypeSerializer::serial_process (j,ctx,banned_peers,"banned_peers") ; } - -uint32_t RsBanListSerialiser::sizeListConfig(RsBanListConfigItem *item) +RsItem *RsBanListSerialiser::create_item(uint16_t service_id,uint8_t item_sub_id) { - uint32_t s = 8; /* header */ - s += 4 ; // type - s += item->banned_peers.TlvSize(); - s += 8 ; // update time - s += item->peerId.serial_size() ; + if(service_id != RS_SERVICE_TYPE_BANLIST) + return NULL ; - return s; -} -/* serialise the data to the buffer */ -bool RsBanListSerialiser::serialiseList(RsBanListItem *item, void *data, uint32_t *pktsize) -{ - uint32_t tlvsize = sizeList(item); - uint32_t offset = 0; - - if (*pktsize < tlvsize) - return false; /* not enough space */ - - *pktsize = tlvsize; - - bool ok = true; - - ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); - -#ifdef RSSERIAL_DEBUG - std::cerr << "RsDsdvSerialiser::serialiseRoute() Header: " << ok << std::endl; - std::cerr << "RsDsdvSerialiser::serialiseRoute() Size: " << tlvsize << std::endl; -#endif - - /* skip the header */ - offset += 8; - - /* add mandatory parts first */ - ok &= item->peerList.SetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - ok = false; -#ifdef RSSERIAL_DEBUG - std::cerr << "RsDsdvSerialiser::serialiseRoute() Size Error! " << std::endl; -#endif - } - - return ok; -} -/* serialise the data to the buffer */ -bool RsBanListSerialiser::serialiseListConfig(RsBanListConfigItem *item, void *data, uint32_t *pktsize) -{ - uint32_t tlvsize = sizeListConfig(item); - uint32_t offset = 0; - - if (*pktsize < tlvsize) - return false; /* not enough space */ - - *pktsize = tlvsize; - - bool ok = true; - - ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); - -#ifdef RSSERIAL_DEBUG - std::cerr << "RsBanListSerialiser::serialiseRoute() Header: " << ok << std::endl; - std::cerr << "RsBanListSerialiser::serialiseRoute() Size: " << tlvsize << std::endl; -#endif - - /* skip the header */ - offset += 8; - - ok &= setRawUInt32(data, tlvsize, &offset,item->type); - ok &= item->peerId.serialise(data, tlvsize, offset); - ok &= setRawTimeT(data, tlvsize, &offset,item->update_time); - - /* add mandatory parts first */ - ok &= item->banned_peers.SetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) + switch(item_sub_id) { - ok = false; -#ifdef RSSERIAL_DEBUG - std::cerr << "RsBanListSerialiser::serialiseRoute() Size Error! " << std::endl; -#endif - } - - return ok; -} -RsBanListItem *RsBanListSerialiser::deserialiseList(void *data, uint32_t *pktsize) -{ - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t tlvsize = getRsItemSize(data); - - uint32_t offset = 0; - - - if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || - (RS_SERVICE_TYPE_BANLIST != getRsItemService(rstype)) || - (RS_PKT_SUBTYPE_BANLIST_ITEM != getRsItemSubType(rstype))) - { - return NULL; /* wrong type */ - } - - if (*pktsize < tlvsize) /* check size */ - return NULL; /* not enough data */ - - /* set the packet length */ - *pktsize = tlvsize; - - bool ok = true; - - /* ready to load */ - RsBanListItem *item = new RsBanListItem(); - item->clear(); - - /* skip the header */ - offset += 8; - - /* add mandatory parts first */ - ok &= item->peerList.GetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; -} -RsBanListConfigItem *RsBanListSerialiser::deserialiseListConfig(void *data, uint32_t *pktsize) -{ - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t tlvsize = getRsItemSize(data); - - uint32_t offset = 0; - - - if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || - (RS_SERVICE_TYPE_BANLIST != getRsItemService(rstype)) || - (RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM != getRsItemSubType(rstype))) - { - return NULL; /* wrong type */ - } - - if (*pktsize < tlvsize) /* check size */ - return NULL; /* not enough data */ - - /* set the packet length */ - *pktsize = tlvsize; - - bool ok = true; - - /* ready to load */ - RsBanListConfigItem *item = new RsBanListConfigItem(); - item->clear(); - - /* skip the header */ - offset += 8; - - ok &= getRawUInt32(data, tlvsize, &offset,&item->type); - ok &= item->peerId.deserialise(data, tlvsize, offset); - ok &= getRawTimeT(data, tlvsize, &offset,item->update_time); - - /* add mandatory parts first */ - ok &= item->banned_peers.GetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; -} - -/*************************************************************************/ - -uint32_t RsBanListSerialiser::size(RsItem *i) -{ - RsBanListItem *dri; - RsBanListConfigItem *drc; - - if (NULL != (dri = dynamic_cast(i))) - { - return sizeList(dri); - } - - if (NULL != (drc = dynamic_cast(i))) - { - return sizeListConfig(drc); - } - return 0; -} - -bool RsBanListSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize) -{ - RsBanListItem *dri; - RsBanListConfigItem *drc; - - if (NULL != (dri = dynamic_cast(i))) - { - return serialiseList(dri, data, pktsize); - } - if (NULL != (drc = dynamic_cast(i))) - { - return serialiseListConfig(drc, data, pktsize); - } - return false; -} - -RsItem *RsBanListSerialiser::deserialise(void *data, uint32_t *pktsize) -{ - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - - if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || - (RS_SERVICE_TYPE_BANLIST != getRsItemService(rstype))) - { - return NULL; /* wrong type */ - } - - switch(getRsItemSubType(rstype)) - { - case RS_PKT_SUBTYPE_BANLIST_ITEM: - return deserialiseList(data, pktsize); - break; - case RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM: - return deserialiseListConfig(data, pktsize); - break; - default: - return NULL; - break; + case RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM: return new RsBanListConfigItem ; + case RS_PKT_SUBTYPE_BANLIST_ITEM: return new RsBanListItem ; + default: + std::cerr << "(EE) unknown item subtype " << (int)item_sub_id << " in RsBanListSerialiser::create_item()" << std::endl; + return NULL ; } } -void RsBanListConfigItem::clear() -{ - banned_peers.TlvClear() ; -} - -std::ostream &RsBanListConfigItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsBanListConfigItem", indent); - uint16_t int_Indent = indent + 2; - banned_peers.print(out, int_Indent); - - printRsItemEnd(out, "RsBanListConfigItem", indent); - return out; -} - -/*************************************************************************/ - - diff --git a/libretroshare/src/serialiser/rsbanlistitems.h b/libretroshare/src/serialiser/rsbanlistitems.h index 372f71a5d..cd5cb8cec 100644 --- a/libretroshare/src/serialiser/rsbanlistitems.h +++ b/libretroshare/src/serialiser/rsbanlistitems.h @@ -29,12 +29,12 @@ #include #include "serialiser/rsserviceids.h" -#include "serialiser/rsserial.h" #include "serialiser/rstlvbanlist.h" +#include "serialization/rsserializer.h" #define RS_PKT_SUBTYPE_BANLIST_ITEM_deprecated 0x01 -#define RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM_deprecated 0x02 -#define RS_PKT_SUBTYPE_BANLIST_ITEM 0x03 +#define RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM_deprecated 0x02 +#define RS_PKT_SUBTYPE_BANLIST_ITEM 0x03 #define RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM 0x04 /**************************************************************************/ @@ -42,17 +42,15 @@ class RsBanListItem: public RsItem { public: - RsBanListItem() - :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, - RS_PKT_SUBTYPE_BANLIST_ITEM) + RsBanListItem() :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_ITEM) { setPriorityLevel(QOS_PRIORITY_RS_BANLIST_ITEM); return; } - virtual ~RsBanListItem(); + virtual ~RsBanListItem(){} virtual void clear(); - std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); RsTlvBanList peerList; }; @@ -64,40 +62,22 @@ public: :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM) {} virtual ~RsBanListConfigItem(){} - virtual void clear(); + virtual void clear() { banned_peers.TlvClear() ; } - std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); uint32_t type ; RsPeerId peerId ; - time_t update_time ; + time_t update_time ; RsTlvBanList banned_peers; }; -class RsBanListSerialiser: public RsSerialType +class RsBanListSerialiser: public RsSerializer { public: - RsBanListSerialiser() - :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST) - { return; } - virtual ~RsBanListSerialiser() - { return; } - - virtual uint32_t size(RsItem *); - virtual bool serialise (RsItem *item, void *data, uint32_t *size); - virtual RsItem * deserialise(void *data, uint32_t *size); - -private: - - virtual uint32_t sizeList(RsBanListItem *); - virtual bool serialiseList (RsBanListItem *item, void *data, uint32_t *size); - virtual RsBanListItem *deserialiseList(void *data, uint32_t *size); - - virtual uint32_t sizeListConfig(RsBanListConfigItem *); - virtual bool serialiseListConfig (RsBanListConfigItem *item, void *data, uint32_t *size); - virtual RsBanListConfigItem *deserialiseListConfig(void *data, uint32_t *size); - + RsBanListSerialiser() :RsSerializer(RS_SERVICE_TYPE_BANLIST) {} + virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) ; }; /**************************************************************************/ diff --git a/libretroshare/src/serialiser/rsbaseserial.cc b/libretroshare/src/serialiser/rsbaseserial.cc index 2ef67cd5b..44bd249d1 100644 --- a/libretroshare/src/serialiser/rsbaseserial.cc +++ b/libretroshare/src/serialiser/rsbaseserial.cc @@ -35,7 +35,7 @@ /* UInt8 get/set */ -bool getRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t *out) +bool getRawUInt8(const void *data, uint32_t size, uint32_t *offset, uint8_t *out) { /* first check there is space */ if (size < *offset + 1) @@ -71,7 +71,7 @@ bool setRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t in) } /* UInt16 get/set */ -bool getRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t *out) +bool getRawUInt16(const void *data, uint32_t size, uint32_t *offset, uint16_t *out) { /* first check there is space */ if (size < *offset + 2) @@ -113,7 +113,7 @@ bool setRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t in) /* UInt32 get/set */ -bool getRawUInt32(void *data, uint32_t size, uint32_t *offset, uint32_t *out) +bool getRawUInt32(const void *data, uint32_t size, uint32_t *offset, uint32_t *out) { /* first check there is space */ if (size < *offset + 4) @@ -155,7 +155,7 @@ bool setRawUInt32(void *data, uint32_t size, uint32_t *offset, uint32_t in) /* UInt64 get/set */ -bool getRawUInt64(void *data, uint32_t size, uint32_t *offset, uint64_t *out) +bool getRawUInt64(const void *data, uint32_t size, uint32_t *offset, uint64_t *out) { /* first check there is space */ if (size < *offset + 8) @@ -195,7 +195,7 @@ bool setRawUInt64(void *data, uint32_t size, uint32_t *offset, uint64_t in) return true; } -bool getRawUFloat32(void *data,uint32_t size,uint32_t *offset,float& f) +bool getRawUFloat32(const void *data, uint32_t size, uint32_t *offset, float& f) { uint32_t n ; if(!getRawUInt32(data, size, offset, &n) ) @@ -208,6 +208,13 @@ bool getRawUFloat32(void *data,uint32_t size,uint32_t *offset,float& f) bool setRawUFloat32(void *data,uint32_t size,uint32_t *offset,float f) { + uint32_t sz = 4; + + if ( !data || size <= *offset || size < sz + *offset ) + { + std::cerr << "(EE) not enough room. SIZE+offset=" << sz+*offset << " and size is only " << size << std::endl; + return false; + } if(f < 0.0f) { std::cerr << "(EE) Cannot serialise invalid negative float value " << f << " in " << __PRETTY_FUNCTION__ << std::endl; @@ -229,7 +236,7 @@ uint32_t getRawStringSize(const std::string &outStr) return outStr.length() + 4; } -bool getRawString(void *data, uint32_t size, uint32_t *offset, std::string &outStr) +bool getRawString(const void *data, uint32_t size, uint32_t *offset, std::string &outStr) { #warning Gio: "I had to change this. It seems like a bug to not clear the string. Should make sure it's not introducing any side effect." outStr.clear(); @@ -284,7 +291,7 @@ bool setRawString(void *data, uint32_t size, uint32_t *offset, const std::string return true; } -bool getRawTimeT(void *data,uint32_t size,uint32_t *offset,time_t& t) +bool getRawTimeT(const void *data,uint32_t size,uint32_t *offset,time_t& t) { uint64_t T ; bool res = getRawUInt64(data,size,offset,&T) ; @@ -292,7 +299,7 @@ bool getRawTimeT(void *data,uint32_t size,uint32_t *offset,time_t& t) return res ; } -bool setRawTimeT(void *data,uint32_t size,uint32_t *offset,const time_t& t) +bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const time_t& t) { return setRawUInt64(data,size,offset,t) ; } diff --git a/libretroshare/src/serialiser/rsbaseserial.h b/libretroshare/src/serialiser/rsbaseserial.h index 25e0f57cc..650fc9e6d 100644 --- a/libretroshare/src/serialiser/rsbaseserial.h +++ b/libretroshare/src/serialiser/rsbaseserial.h @@ -48,27 +48,27 @@ * ******************************************************************/ -bool getRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t *out); +bool getRawUInt8(const void *data, uint32_t size, uint32_t *offset, uint8_t *out); bool setRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t in); -bool getRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t *out); +bool getRawUInt16(const void *data, uint32_t size, uint32_t *offset, uint16_t *out); bool setRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t in); -bool getRawUInt32(void *data, uint32_t size, uint32_t *offset, uint32_t *out); +bool getRawUInt32(const void *data, uint32_t size, uint32_t *offset, uint32_t *out); bool setRawUInt32(void *data, uint32_t size, uint32_t *offset, uint32_t in); -bool getRawUInt64(void *data, uint32_t size, uint32_t *offset, uint64_t *out); +bool getRawUInt64(const void *data, uint32_t size, uint32_t *offset, uint64_t *out); bool setRawUInt64(void *data, uint32_t size, uint32_t *offset, uint64_t in); -bool getRawUFloat32(void *data, uint32_t size, uint32_t *offset, float& out); +bool getRawUFloat32(const void *data, uint32_t size, uint32_t *offset, float& out); bool setRawUFloat32(void *data, uint32_t size, uint32_t *offset, float in); uint32_t getRawStringSize(const std::string &outStr); -bool getRawString(void *data, uint32_t size, uint32_t *offset, std::string &outStr); +bool getRawString(const void *data, uint32_t size, uint32_t *offset, std::string &outStr); bool setRawString(void *data, uint32_t size, uint32_t *offset, const std::string &inStr); +bool getRawTimeT(const void *data, uint32_t size, uint32_t *offset, time_t& outStr); bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const time_t& inStr); -bool getRawTimeT(void *data, uint32_t size, uint32_t *offset, time_t& outStr); #endif diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index 722eec9ec..21e0fcec7 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -84,7 +84,12 @@ class RsItem: public RsMemoryManagement::SmallObject virtual ~RsItem(); virtual void clear() = 0; - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0) = 0; + virtual std::ostream &print(std::ostream &out, uint16_t indent = 0) + { + std::cerr << "(EE) RsItem::print() called by an item using new serialization classes, but not derived! " << std::endl; +#warning This method should normally call serial_process(PRINT,ctx) + return out; + } void print_string(std::string &out, uint16_t indent = 0); /* source / destination id */ diff --git a/libretroshare/src/serialization/rstypeserializer.h b/libretroshare/src/serialization/rstypeserializer.h index 878ec3e6b..349689c0b 100644 --- a/libretroshare/src/serialization/rstypeserializer.h +++ b/libretroshare/src/serialization/rstypeserializer.h @@ -2,6 +2,7 @@ #include "serialiser/rsserial.h" #include "serialiser/rstlvbase.h" +#include "serialiser/rstlvlist.h" #include "retroshare/rsflags.h" #include "retroshare/rsids.h" @@ -183,6 +184,11 @@ class RsTypeSerializer template static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, t_RsGenericIdType& member); template static uint32_t serial_size(const t_RsGenericIdType& /* member */); template static void print_data(const std::string& name,const t_RsGenericIdType& /* member */); + + template static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const t_RsTlvList& member); + template static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, t_RsTlvList& member); + template static uint32_t serial_size(const t_RsTlvList& /* member */); + template static void print_data(const std::string& name,const t_RsTlvList& /* member */); }; //=================================================================================================// @@ -208,7 +214,35 @@ uint32_t RsTypeSerializer::serial_size(const t_RsGenericIdType -void RsTypeSerializer::print_data(const std::string& name,const t_RsGenericIdType& member) +void RsTypeSerializer::print_data(const std::string& /* name */,const t_RsGenericIdType& member) { std::cerr << " [RsGenericId<" << std::hex << UNIQUE_IDENTIFIER << ">] : " << member << std::endl; } + +//=================================================================================================// +// t_RsTlvList<> // +//=================================================================================================// + +template +bool RsTypeSerializer::serialize (uint8_t data[], uint32_t size, uint32_t &offset, const t_RsTlvList& member) +{ + return (*const_cast *>(&member)).SetTlv(data,size,&offset) ; +} + +template +bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, t_RsTlvList& member) +{ + return member.GetTlv(const_cast(data),size,&offset) ; +} + +template +uint32_t RsTypeSerializer::serial_size(const t_RsTlvList& member) +{ + return member.TlvSize(); +} + +template +void RsTypeSerializer::print_data(const std::string& /* name */,const t_RsTlvList& member) +{ + std::cerr << " [t_RsTlvString<" << std::hex << TLV_TYPE << ">] : size=" << member.mList.size() << std::endl; +}