From 3453a3e57dcddcd514a1be031cfd37a76a993790 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 11 Apr 2017 23:05:27 +0200 Subject: [PATCH] switched FileTransfer items to new serialization --- libretroshare/src/chat/rschatitems.cc | 2 +- .../src/serialiser/rsfiletransferitems.cc | 765 ++---------------- .../src/serialiser/rsfiletransferitems.h | 103 +-- libretroshare/src/serialiser/rstlvfileitem.h | 2 +- .../src/serialization/rstypeserializer.cc | 60 +- 5 files changed, 110 insertions(+), 822 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index a430b1119..88e7abe8a 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -87,7 +87,7 @@ void RsChatLobbyBouncingObject::serial_process(RsItem::SerializeJob j, Serialize RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,nick,"nick") ; if(include_signature) - RsTypeSerializer::serial_process(j,ctx,signature,"signature") ; + RsTypeSerializer::serial_process(j,ctx,signature,"signature") ; } void RsChatLobbyMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) diff --git a/libretroshare/src/serialiser/rsfiletransferitems.cc b/libretroshare/src/serialiser/rsfiletransferitems.cc index 0caabc2f8..0be0412b7 100644 --- a/libretroshare/src/serialiser/rsfiletransferitems.cc +++ b/libretroshare/src/serialiser/rsfiletransferitems.cc @@ -28,6 +28,8 @@ #include "serialiser/rstlvbase.h" #include "serialiser/rsfiletransferitems.h" +#include "serialization/rstypeserializer.h" + /*** * #define RSSERIAL_DEBUG 1 * #define DEBUG_TRANSFERS 1 @@ -40,758 +42,115 @@ #include -/**********************************************************************************************/ -/* SERIALISER STUFF */ -/**********************************************************************************************/ - -RsFileTransferItem *RsFileTransferSerialiser::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_FILE_TRANSFER != getRsItemService(rstype)) - { - return NULL; /* wrong type */ - } - - switch(getRsItemSubType(rstype)) - { - case RS_PKT_SUBTYPE_FT_CACHE_ITEM: return deserialise_RsFileTransferCacheItem(data, *pktsize); - case RS_PKT_SUBTYPE_FT_DATA_REQUEST: return deserialise_RsFileTransferDataRequestItem(data, *pktsize); - case RS_PKT_SUBTYPE_FT_DATA: return deserialise_RsFileTransferDataItem(data, *pktsize); - case RS_PKT_SUBTYPE_FT_CHUNK_MAP_REQUEST: return deserialise_RsFileTransferChunkMapRequestItem(data,*pktsize) ; - case RS_PKT_SUBTYPE_FT_CHUNK_MAP: return deserialise_RsFileTransferChunkMapItem(data,*pktsize) ; - case RS_PKT_SUBTYPE_FT_CHUNK_CRC_REQUEST: return deserialise_RsFileTransferSingleChunkCrcRequestItem(data,*pktsize) ; - case RS_PKT_SUBTYPE_FT_CHUNK_CRC: return deserialise_RsFileTransferSingleChunkCrcItem(data,*pktsize) ; - default: - std::cerr << "RsFileTransferSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl; - return NULL; - } - return NULL; -} - -/**********************************************************************************************/ -/* OUTPUTS */ -/**********************************************************************************************/ - -std::ostream& RsFileTransferCacheItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferCacheItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); - out << "CacheId: " << cacheType << "/" << cacheSubId << std::endl; - file.print(out, int_Indent); - printRsItemEnd(out, "RsFileTransferCacheItem", indent); - return out; -} -std::ostream& RsFileTransferDataRequestItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferDataRequestItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); - out << "FileOffset: " << fileoffset; - out << " ChunkSize: " << chunksize << std::endl; - file.print(out, int_Indent); - printRsItemEnd(out, "RsFileTransferDataRequestItem", indent); - return out; -} -std::ostream& RsFileTransferChunkMapItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferChunkMapItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); out << "PeerId: " << PeerId() << std::endl ; - printIndent(out, int_Indent); out << " hash: " << hash << std::endl ; - printIndent(out, int_Indent); out << "chunks: " << std::hex << compressed_map._map[0] << std::dec << "..." << std::endl ; - printRsItemEnd(out, "RsFileTransferChunkMapItem", indent); - return out; -} -std::ostream& RsFileTransferChunkMapRequestItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferChunkMapRequestItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); out << "PeerId: " << PeerId() << std::endl ; - printIndent(out, int_Indent); out << " hash: " << hash << std::endl ; - printRsItemEnd(out, "RsFileTransferChunkMapRequestItem", indent); - return out; -} -std::ostream& RsFileTransferSingleChunkCrcRequestItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferSingleChunkCrcRequestItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); out << "PeerId: " << PeerId() << std::endl ; - printIndent(out, int_Indent); out << " hash: " << hash << std::endl ; - printIndent(out, int_Indent); out << " chunk: " << chunk_number << "..." << std::endl ; - printRsItemEnd(out, "RsFileTransferSingleChunkCrcRequestItem", indent); - return out; -} -std::ostream& RsFileTransferSingleChunkCrcItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferSingleChunkCrcItem", indent); - uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); out << "PeerId: " << PeerId() << std::endl ; - printIndent(out, int_Indent); out << " hash: " << hash << std::endl ; - printIndent(out, int_Indent); out << " chunk: " << chunk_number << "..." << std::endl ; - printIndent(out, int_Indent); out << " sha1: " << check_sum.toStdString() << "..." << std::endl ; - printRsItemEnd(out, "RsFileTransferSingleChunkCrcItem", indent); - return out; -} -std::ostream& RsFileTransferDataItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsFileTransferDataItem", indent); - uint16_t int_Indent = indent + 2; - fd.print(out, int_Indent); - printRsItemEnd(out, "RsFileTransferDataItem", indent); - return out; -} - -/**********************************************************************************************/ -/* SERIAL SIZE */ -/**********************************************************************************************/ - -uint32_t RsFileTransferDataRequestItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += 8; /* offset */ - s += 4; /* chunksize */ - s += file.TlvSize(); - - return s; -} -uint32_t RsFileTransferCacheItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += 4; /* type/subid */ - s += file.TlvSize(); - - return s; -} -uint32_t RsFileTransferDataItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += fd.TlvSize(); - - return s; -} -uint32_t RsFileTransferChunkMapRequestItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += 1 ; // is_client - s += hash.serial_size() ; // hash - - return s; -} -uint32_t RsFileTransferChunkMapItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += 1 ; // is_client - s += hash.serial_size() ; // hash - s += 4 ; // compressed map size - s += 4 * compressed_map._map.size() ; // compressed chunk map - - return s; -} -uint32_t RsFileTransferSingleChunkCrcItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += hash.serial_size() ; // hash - s += 4 ; // chunk number - s += check_sum.serial_size() ; // sha1 - - return s; -} -uint32_t RsFileTransferSingleChunkCrcRequestItem::serial_size() -{ - uint32_t s = 8; /* header */ - s += hash.serial_size() ; // hash - s += 4 ; // chunk number - - return s; -} - -/*************************************************************************/ - void RsFileTransferDataRequestItem::clear() { file.TlvClear(); fileoffset = 0; chunksize = 0; } -void RsFileTransferCacheItem::clear() -{ - cacheType = 0; - cacheSubId = 0; - file.TlvClear(); -} void RsFileTransferDataItem::clear() { fd.TlvClear(); } -/**********************************************************************************************/ -/* SERIALISATION */ -/**********************************************************************************************/ - -bool RsFileTransferItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvsize, uint32_t& offset) +void RsFileTransferDataRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - tlvsize = serial_size() ; - offset = 0; - - if (pktsize < tlvsize) - return false; /* not enough space */ - - pktsize = tlvsize; - - if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize)) - { - std::cerr << "RsFileTransferItem::serialise_header(): ERROR. Not enough size!" << std::endl; - return false ; - } -#ifdef RSSERIAL_DEBUG - std::cerr << "RsFileItemSerialiser::serialiseData() Header: " << ok << std::endl; -#endif - offset += 8; - - return true ; + RsTypeSerializer::serial_process (j,ctx,fileoffset,"fileoffset") ; + RsTypeSerializer::serial_process (j,ctx,chunksize, "chunksize") ; + RsTypeSerializer::serial_process(j,ctx,file, "file") ; } -/* serialise the data to the buffer */ -bool RsFileTransferChunkMapRequestItem::serialise(void *data, uint32_t& pktsize) +void RsFileTransferDataItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= setRawUInt8(data, tlvsize, &offset, is_client); - ok &= hash.serialise(data, tlvsize, offset) ; - - if (offset != tlvsize) - { - ok = false; - std::cerr << "RsFileItemSerialiser::serialiseData() Size Error! " << std::endl; - } - - return ok; -} -bool RsFileTransferSingleChunkCrcRequestItem::serialise(void *data, uint32_t& pktsize) -{ - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= hash.serialise(data, tlvsize, offset) ; - ok &= setRawUInt32(data, tlvsize, &offset, chunk_number) ; - - if (offset != tlvsize) - { - ok = false; - std::cerr << "RsFileItemSerialiser::serialiseData() Size Error! " << std::endl; - } - - return ok; -} -bool RsFileTransferSingleChunkCrcItem::serialise(void *data, uint32_t& pktsize) -{ - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= hash.serialise(data, tlvsize, offset) ; - ok &= setRawUInt32(data, tlvsize, &offset, chunk_number) ; - - ok &= check_sum.serialise(data,tlvsize,offset) ; - - //ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[0]) ; - //ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[1]) ; - //ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[2]) ; - //ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[3]) ; - //ok &= setRawUInt32(data, tlvsize, &offset, check_sum.fourbytes[4]) ; - - if (offset != tlvsize) - { - ok = false; - std::cerr << "RsFileItemSerialiser::serialiseData() Size Error! " << std::endl; - } - - return ok; -} -bool RsFileTransferChunkMapItem::serialise(void *data, uint32_t& pktsize) -{ - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= setRawUInt8(data, tlvsize, &offset, is_client); - ok &= hash.serialise(data, tlvsize, offset) ; - ok &= setRawUInt32(data, tlvsize, &offset, compressed_map._map.size()); - - for(uint32_t i=0;i(j,ctx,fd,"fd") ; } -/* serialise the data to the buffer */ -bool RsFileTransferDataItem::serialise(void *data, uint32_t& pktsize) +void RsFileTransferChunkMapRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= fd.SetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - ok = false; - std::cerr << "RsFileItemSerialiser::serialiseData() Size Error! " << std::endl; - } - -#ifdef DEBUG_TRANSFERS - std::cerr << "RsFileItemSerialiser::serialiseData() at: " << RsUtil::AccurateTimeString() << std::endl; - print(std::cerr, 10); -#endif - - return ok; + RsTypeSerializer::serial_process (j,ctx,is_client,"is_client") ; + RsTypeSerializer::serial_process (j,ctx,hash, "hash") ; } -/* serialise the data to the buffer */ -bool RsFileTransferDataRequestItem::serialise(void *data, uint32_t& pktsize) +void RsFileTransferChunkMapItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= setRawUInt64(data, tlvsize, &offset, fileoffset); - ok &= setRawUInt32(data, tlvsize, &offset, chunksize); - ok &= file.SetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - ok = false; - std::cerr << "RsFileTransferDataRequestItem::serialise() Size Error! " << std::endl; - } - - /*** Debugging Transfer rates. - * print timestamp, and file details so we can workout packet lags. - ***/ - -#ifdef DEBUG_TRANSFERS - std::cerr << "RsFileTransferDataRequestItem::serialise() at: " << RsUtil::AccurateTimeString() << std::endl; - print(std::cerr, 10); -#endif - - return ok; + RsTypeSerializer::serial_process (j,ctx,is_client, "is_client") ; + RsTypeSerializer::serial_process (j,ctx,hash, "hash") ; + RsTypeSerializer::serial_process (j,ctx,compressed_map,"compressed_map") ; } -/* serialise the data to the buffer */ -bool RsFileTransferCacheItem::serialise(void *data, uint32_t& pktsize) +void RsFileTransferSingleChunkCrcRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - uint32_t tlvsize,offset=0; - bool ok = true; - - if(!serialise_header(data,pktsize,tlvsize,offset)) - return false ; - - /* add mandatory parts first */ - ok &= setRawUInt16(data, tlvsize, &offset, cacheType); - ok &= setRawUInt16(data, tlvsize, &offset, cacheSubId); - ok &= file.SetTlv(data, tlvsize, &offset); - - if (offset != tlvsize) - { - ok = false; -#ifdef RSSERIAL_DEBUG - std::cerr << "RsFileItemSerialiser::serialiseItem() Size Error! " << std::endl; -#endif - } - - return ok; + RsTypeSerializer::serial_process (j,ctx,hash, "hash") ; + RsTypeSerializer::serial_process(j,ctx,chunk_number,"chunk_number") ; } -/**********************************************************************************************/ -/* DESERIALISATION */ -/**********************************************************************************************/ - -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferCacheItem(void *data, uint32_t pktsize) +void RsFileTransferSingleChunkCrcItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - - if(RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_CACHE_ITEM != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferCacheItem(): wong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferCacheItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - bool ok = true; - - /* ready to load */ - RsFileTransferCacheItem *item = new RsFileTransferCacheItem(); - item->clear(); - - /* skip the header */ - offset += 8; - - /* get mandatory parts first */ - ok &= getRawUInt16(data, rssize, &offset, &(item->cacheType)); - ok &= getRawUInt16(data, rssize, &offset, &(item->cacheSubId)); - ok &= item->file.GetTlv(data, rssize, &offset); - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; + RsTypeSerializer::serial_process (j,ctx,hash, "hash") ; + RsTypeSerializer::serial_process(j,ctx,chunk_number,"chunk_number") ; + RsTypeSerializer::serial_process (j,ctx,check_sum, "check_sum") ; } -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapRequestItem(void *data, uint32_t pktsize) +//===================================================================================================// +// CompressedChunkMap // +//===================================================================================================// + +template<> uint32_t RsTypeSerializer::serial_size(const CompressedChunkMap& s) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - - if(RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_CHUNK_MAP_REQUEST != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapRequestItem(): wong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapRequestItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - bool ok = true; - - /* ready to load */ - RsFileTransferChunkMapRequestItem *item = new RsFileTransferChunkMapRequestItem(); - item->clear(); - - /* skip the header */ - offset += 8; - uint8_t tmp ; - ok &= getRawUInt8(data, rssize, &offset, &tmp); item->is_client = tmp; - ok &= item->hash.deserialise(data, rssize, offset) ; // File hash - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; + return 4 + 4*s._map.size() ; } -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferDataItem(void *data, uint32_t pktsize) +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const CompressedChunkMap& s) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); + bool ok = true ; - uint32_t offset = 0; + ok &= setRawUInt32(data, size, &offset, s._map.size()); - if(RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_DATA != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataItem(): wong subtype!" << std::endl; - return NULL; /* wrong type */ - } + for(uint32_t i=0;iclear(); - - /* skip the header */ - offset += 8; - - /* get mandatory parts first */ - ok &= item->fd.GetTlv(data, rssize, &offset); - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - -#ifdef DEBUG_TRANSFERS - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataItem() at: " << RsUtil::AccurateTimeString() << std::endl; - item->print(std::cerr, 10); -#endif - - return item; + return ok; } - -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferDataRequestItem(void *data, uint32_t pktsize) +template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,CompressedChunkMap& s) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - - if (RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_DATA_REQUEST != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataRequestItem(): wrong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataRequestItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - RsFileTransferDataRequestItem *item = new RsFileTransferDataRequestItem() ; - - bool ok = true; - - /* skip the header */ - offset += 8; - - /* get mandatory parts first */ - ok &= getRawUInt64(data, rssize, &offset, &item->fileoffset); - ok &= getRawUInt32(data, rssize, &offset, &item->chunksize); - ok &= item->file.GetTlv(data, rssize, &offset); - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - /*** Debugging Transfer rates. - * print timestamp, and file details so we can workout packet lags. - ***/ - -#ifdef DEBUG_TRANSFERS - std::cerr << "RsFileItemSerialiser::deserialise_RsFileTransferDataRequestItem() at: " << RsUtil::AccurateTimeString() << std::endl; - item->print(std::cerr, 10); -#endif - - return item; -} -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapItem(void *data, uint32_t pktsize) -{ - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - - if (RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_CHUNK_MAP != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapItem(): wrong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - bool ok = true; - - /* ready to load */ - RsFileTransferChunkMapItem *item = new RsFileTransferChunkMapItem(); - item->clear(); - - /* skip the header */ - offset += 8; - uint8_t tmp ; - ok &= getRawUInt8(data, rssize, &offset, &tmp); item->is_client = tmp; - ok &= item->hash.deserialise(data, rssize, offset) ; // File hash - uint32_t size =0; - ok &= getRawUInt32(data, rssize, &offset, &size); + uint32_t S =0; + bool ok = getRawUInt32(data, size, &offset, &S); if(ok) { - item->compressed_map._map.resize(size) ; + s._map.resize(S) ; - for(uint32_t i=0;icompressed_map._map[i])); + for(uint32_t i=0;i void RsTypeSerializer::print_data(const std::string& n, const CompressedChunkMap& s) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - - if (RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_CHUNK_CRC_REQUEST != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcRequestItem(): wrong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcRequestItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - bool ok = true; - - /* ready to load */ - RsFileTransferSingleChunkCrcRequestItem *item = new RsFileTransferSingleChunkCrcRequestItem(); - item->clear(); - - /* skip the header */ - offset += 8; - ok &= item->hash.deserialise(data, rssize, offset) ; - ok &= getRawUInt32(data, rssize, &offset, &(item->chunk_number)); - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; + std::cerr << " [Compressed chunk map] " << n << " : length=" << s._map.size() << std::endl; } -RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcItem(void *data, uint32_t pktsize) + +//===================================================================================================// +// Serializer // +//===================================================================================================// + +RsItem *RsFileTransferSerialiser::create_item(uint16_t service_type,uint8_t item_type) { - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); + if(service_type != RS_SERVICE_TYPE_FILE_TRANSFER) + return NULL ; - uint32_t offset = 0; - - if (RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_FILE_TRANSFER != getRsItemType(rstype) || RS_PKT_SUBTYPE_FT_CHUNK_CRC != getRsItemSubType(rstype)) - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcItem(): wrong subtype!" << std::endl; - return NULL; /* wrong type */ - } - - if (pktsize < rssize) /* check size */ - { - std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcItem(): size inconsistency!" << std::endl; - return NULL; /* not enough data */ - } - - bool ok = true; - - /* ready to load */ - RsFileTransferSingleChunkCrcItem *item = new RsFileTransferSingleChunkCrcItem(); - item->clear(); - - /* skip the header */ - offset += 8; - ok &= item->hash.deserialise(data, rssize, offset) ; - ok &= getRawUInt32(data, rssize, &offset, &(item->chunk_number)); - ok &= item->check_sum.deserialise(data,rssize,offset) ; - - if (offset != rssize) - { - /* error */ - delete item; - return NULL; - } - - if (!ok) - { - delete item; - return NULL; - } - - return item; + switch(item_type) + { + case RS_PKT_SUBTYPE_FT_DATA_REQUEST : return new RsFileTransferDataRequestItem(); + case RS_PKT_SUBTYPE_FT_DATA : return new RsFileTransferDataItem(); + case RS_PKT_SUBTYPE_FT_CHUNK_MAP_REQUEST : return new RsFileTransferChunkMapRequestItem(); + case RS_PKT_SUBTYPE_FT_CHUNK_MAP : return new RsFileTransferChunkMapItem(); + case RS_PKT_SUBTYPE_FT_CHUNK_CRC_REQUEST : return new RsFileTransferSingleChunkCrcRequestItem(); + case RS_PKT_SUBTYPE_FT_CHUNK_CRC : return new RsFileTransferSingleChunkCrcItem() ; + default: + return NULL ; + } } -/*************************************************************************/ diff --git a/libretroshare/src/serialiser/rsfiletransferitems.h b/libretroshare/src/serialiser/rsfiletransferitems.h index fa989861d..50b3c2a26 100644 --- a/libretroshare/src/serialiser/rsfiletransferitems.h +++ b/libretroshare/src/serialiser/rsfiletransferitems.h @@ -29,10 +29,10 @@ #include "retroshare/rstypes.h" #include "serialiser/rsserial.h" -//#include "serialiser/rstlvtypes.h" #include "serialiser/rstlvfileitem.h" #include "serialiser/rsserviceids.h" +#include "serialization/rsserializer.h" const uint8_t RS_PKT_SUBTYPE_FT_DATA_REQUEST = 0x01; const uint8_t RS_PKT_SUBTYPE_FT_DATA = 0x02; @@ -53,20 +53,11 @@ const uint8_t RS_PKT_SUBTYPE_FT_CACHE_REQUEST = 0x0B; class RsFileTransferItem: public RsItem { public: - RsFileTransferItem(uint8_t ft_subtype) - : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_FILE_TRANSFER,ft_subtype) - {} + RsFileTransferItem(uint8_t ft_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_FILE_TRANSFER,ft_subtype) {} virtual ~RsFileTransferItem() {} - virtual bool serialise(void *data,uint32_t& size) = 0 ; - virtual uint32_t serial_size() = 0 ; - - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0) = 0; virtual void clear() = 0 ; - - protected: - bool serialise_header(void *data, uint32_t& pktsize, uint32_t& tlvsize, uint32_t& offset) ; }; class RsFileTransferDataRequestItem: public RsFileTransferItem @@ -77,13 +68,9 @@ class RsFileTransferDataRequestItem: public RsFileTransferItem setPriorityLevel(QOS_PRIORITY_RS_FILE_REQUEST) ; } virtual ~RsFileTransferDataRequestItem() {} - - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear(); - std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); // Private data part. // @@ -103,11 +90,9 @@ class RsFileTransferDataItem: public RsFileTransferItem } virtual ~RsFileTransferDataItem() { clear() ; } - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear(); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); - virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + virtual void clear(); // Private data part. // @@ -122,10 +107,9 @@ class RsFileTransferChunkMapRequestItem: public RsFileTransferItem setPriorityLevel(QOS_PRIORITY_RS_FILE_MAP_REQUEST) ; } virtual ~RsFileTransferChunkMapRequestItem() {} - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; virtual void clear() {} - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); + + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); // Private data part. // @@ -142,11 +126,9 @@ class RsFileTransferChunkMapItem: public RsFileTransferItem setPriorityLevel(QOS_PRIORITY_RS_FILE_MAP) ; } virtual ~RsFileTransferChunkMapItem() {} - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear() {} - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); + virtual void clear() {} // Private data part. // @@ -163,11 +145,9 @@ class RsFileTransferSingleChunkCrcRequestItem: public RsFileTransferItem setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC_REQUEST) ; } virtual ~RsFileTransferSingleChunkCrcRequestItem() {} - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear() {} - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); + virtual void clear() {} // Private data part. // @@ -183,11 +163,9 @@ class RsFileTransferSingleChunkCrcItem: public RsFileTransferItem setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC) ; } virtual ~RsFileTransferSingleChunkCrcItem() {} - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear() {} - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); + virtual void clear() {} // Private data part. // @@ -196,65 +174,16 @@ class RsFileTransferSingleChunkCrcItem: public RsFileTransferItem Sha1CheckSum check_sum ; // CRC32 map of the file. }; -class RsFileTransferCacheItem: public RsFileTransferItem -{ - public: - RsFileTransferCacheItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_CACHE_ITEM) - { - setPriorityLevel(QOS_PRIORITY_RS_CACHE_ITEM); - } - - virtual ~RsFileTransferCacheItem(){ clear() ; } - virtual bool serialise(void *data,uint32_t& size) ; - virtual uint32_t serial_size() ; - virtual void clear(); - virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); - - // private part. - // - uint16_t cacheType; - uint16_t cacheSubId; - RsTlvFileItem file; /* file information */ -}; - - /**************************************************************************/ -class RsFileTransferSerialiser: public RsSerialType +class RsFileTransferSerialiser: public RsSerializer { public: - RsFileTransferSerialiser(): RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_FILE_TRANSFER) {} + RsFileTransferSerialiser(): RsSerializer(RS_SERVICE_TYPE_FILE_TRANSFER) {} virtual ~RsFileTransferSerialiser() {} - virtual uint32_t size(RsItem *item) - { - RsFileTransferItem *ftitem = dynamic_cast(item); - if (!ftitem) - { - return 0; - } - return ftitem->serial_size() ; - } - virtual bool serialise(RsItem *item, void *data, uint32_t *size) - { - RsFileTransferItem *ftitem = dynamic_cast(item); - if (!ftitem) - { - return false; - } - return ftitem->serialise(data,*size) ; - } - virtual RsFileTransferItem *deserialise(void *data, uint32_t *size); - - private: - RsFileTransferItem *deserialise_RsFileTransferCacheItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferChunkMapRequestItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferChunkMapItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferDataRequestItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferDataItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferSingleChunkCrcItem(void *data, uint32_t pktsize); - RsFileTransferItem *deserialise_RsFileTransferSingleChunkCrcRequestItem(void *data, uint32_t pktsize); + RsItem *create_item(uint16_t service_type,uint8_t item_type) ; }; diff --git a/libretroshare/src/serialiser/rstlvfileitem.h b/libretroshare/src/serialiser/rstlvfileitem.h index 62745b658..1a474e60c 100644 --- a/libretroshare/src/serialiser/rstlvfileitem.h +++ b/libretroshare/src/serialiser/rstlvfileitem.h @@ -42,7 +42,7 @@ virtual ~RsTlvFileItem() { return; } virtual uint32_t TlvSize() const; virtual void TlvClear(); virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const; -virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); virtual std::ostream &print(std::ostream &out, uint16_t indent) const; uint64_t filesize; /// Mandatory: size of file to be downloaded diff --git a/libretroshare/src/serialization/rstypeserializer.cc b/libretroshare/src/serialization/rstypeserializer.cc index fc9563fe6..5a8def1bd 100644 --- a/libretroshare/src/serialization/rstypeserializer.cc +++ b/libretroshare/src/serialization/rstypeserializer.cc @@ -10,34 +10,15 @@ static const uint32_t MAX_SERIALIZED_ARRAY_SIZE = 500 ; -#ifdef REMOVE -template T ntoh(T t) -{ - if(sizeof(T) == 8) return t; - if(sizeof(T) == 4) return ntohl(t) ; - if(sizeof(T) == 2) return ntohs(t) ; - if(sizeof(T) == 1) return t ; - - std::cerr << "(EE) unhandled type of size " << sizeof(T) << " in ntoh<>" << std::endl; - return t; -} -template T hton(T t) -{ - if(sizeof(T) == 8) return t; - if(sizeof(T) == 4) return htonl(t) ; - if(sizeof(T) == 2) return htons(t) ; - if(sizeof(T) == 1) return t ; - - std::cerr << "(EE) unhandled type of size " << sizeof(T) << " in hton<>" << std::endl; - return t; -} -#endif - //=================================================================================================// // Integer types // //=================================================================================================// -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member) +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const bool& member) +{ + return setRawUInt8(data,size,&offset,member); +} +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member) { return setRawUInt8(data,size,&offset,member); } @@ -53,6 +34,14 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3 { return setRawTimeT(data,size,&offset,member); } + +template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, bool& member) +{ + uint8_t m ; + bool ok = getRawUInt8(data,size,&offset,&m); + member = m ; + return ok; +} template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint8_t& member) { return getRawUInt8(data,size,&offset,&member); @@ -69,6 +58,11 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz { return getRawTimeT(data,size,&offset,member); } + +template<> uint32_t RsTypeSerializer::serial_size(const bool& /* member*/) +{ + return 1; +} template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/) { return 1; @@ -86,6 +80,10 @@ template<> uint32_t RsTypeSerializer::serial_size(const time_t& /* member*/) return 8; } +template<> void RsTypeSerializer::print_data(const std::string& n, const bool & V) +{ + std::cerr << " [bool ] " << n << ": " << V << std::endl; +} template<> void RsTypeSerializer::print_data(const std::string& n, const uint8_t & V) { std::cerr << " [uint8_t ] " << n << ": " << V << std::endl; @@ -230,25 +228,27 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeS } //=================================================================================================// -// Signatures // +// TlvItems // //=================================================================================================// -template<> uint32_t RsTypeSerializer::serial_size(const RsTlvKeySignature& s) +template<> uint32_t RsTypeSerializer::serial_size(const RsTlvItem& s) { return s.TlvSize() ; } -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const RsTlvKeySignature& s) +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const RsTlvItem& s) { return s.SetTlv(data,size,&offset) ; } -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,RsTlvKeySignature& s) +template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,RsTlvItem& s) { return s.GetTlv((void*)data,size,&offset) ; } -template<> void RsTypeSerializer::print_data(const std::string& n, const RsTlvKeySignature& s) +template<> void RsTypeSerializer::print_data(const std::string& n, const RsTlvItem& s) { - std::cerr << " [Signature] " << n << " : key_id=" << s.keyId << ", length=" << s.signData.bin_len << std::endl; + // can we call TlvPrint inside this? + + std::cerr << " [" << typeid(s).name() << "] " << n << std::endl; }