mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-28 08:07:31 -04:00
made file transfer a service. Removed all the old FT code from pqiHandler, and got rid of deprecated CRC32 computation/exchange methods
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-initdev@6782 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
4e4b4f0822
commit
8e7fe9f79b
26 changed files with 1505 additions and 3197 deletions
File diff suppressed because it is too large
Load diff
|
@ -1,331 +0,0 @@
|
|||
#ifndef RS_BASE_ITEMS_H
|
||||
#define RS_BASE_ITEMS_H
|
||||
|
||||
/*
|
||||
* libretroshare/src/serialiser: rsbaseitems.h
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "retroshare/rstypes.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
#include "serialiser/rstlvtypes.h"
|
||||
|
||||
const uint8_t RS_PKT_TYPE_FILE = 0x01;
|
||||
const uint8_t RS_PKT_TYPE_CACHE = 0x02;
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_REQUEST = 0x01;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_DATA = 0x02;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_TRANSFER = 0x03;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CHUNK_MAP_REQUEST = 0x04;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CHUNK_MAP = 0x05;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CRC32_MAP_REQUEST = 0x06;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CRC32_MAP = 0x07;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CHUNK_CRC_REQUEST = 0x08;
|
||||
const uint8_t RS_PKT_SUBTYPE_FI_CHUNK_CRC = 0x09;
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_CACHE_ITEM = 0x01;
|
||||
const uint8_t RS_PKT_SUBTYPE_CACHE_REQUEST = 0x02;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsFileRequest: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileRequest()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_FILE,
|
||||
RS_PKT_SUBTYPE_FI_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_REQUEST) ;
|
||||
}
|
||||
virtual ~RsFileRequest();
|
||||
virtual void clear();
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
uint64_t fileoffset; /* start of data requested */
|
||||
uint32_t chunksize; /* size of data requested */
|
||||
RsTlvFileItem file; /* file information */
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsFileData: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileData()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_FILE,
|
||||
RS_PKT_SUBTYPE_FI_DATA)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_DATA) ;
|
||||
}
|
||||
virtual ~RsFileData();
|
||||
virtual void clear();
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
RsTlvFileData fd;
|
||||
};
|
||||
|
||||
class RsFileChunkMapRequest: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileChunkMapRequest()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CHUNK_MAP_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_MAP_REQUEST) ;
|
||||
}
|
||||
virtual ~RsFileChunkMapRequest() {}
|
||||
virtual void clear() {}
|
||||
|
||||
bool is_client ; // is the request for a client, or a server ?
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
class RsFileChunkMap: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileChunkMap()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CHUNK_MAP)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_MAP) ;
|
||||
}
|
||||
virtual ~RsFileChunkMap() {}
|
||||
virtual void clear() {}
|
||||
|
||||
bool is_client ; // is the request for a client, or a server ?
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
CompressedChunkMap compressed_map ; // Chunk map of the file.
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
class RsFileCRC32MapRequest: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileCRC32MapRequest()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CRC32_MAP_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_CRC_REQUEST) ;
|
||||
}
|
||||
virtual ~RsFileCRC32MapRequest() {}
|
||||
virtual void clear() {}
|
||||
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
class RsFileSingleChunkCrcRequest: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileSingleChunkCrcRequest()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CHUNK_CRC_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC_REQUEST) ;
|
||||
}
|
||||
virtual ~RsFileSingleChunkCrcRequest() {}
|
||||
virtual void clear() {}
|
||||
|
||||
std::string hash ; // hash of the file for which we request the crc
|
||||
uint32_t chunk_number ; // chunk number
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
|
||||
class RsFileCRC32Map: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileCRC32Map()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CRC32_MAP)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_CRC) ;
|
||||
}
|
||||
virtual ~RsFileCRC32Map() {}
|
||||
virtual void clear() {}
|
||||
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
CRC32Map crc_map ; // CRC32 map of the file.
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
class RsFileSingleChunkCrc: public RsItem
|
||||
{
|
||||
public:
|
||||
RsFileSingleChunkCrc()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE, RS_PKT_TYPE_FILE, RS_PKT_SUBTYPE_FI_CHUNK_CRC)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC) ;
|
||||
}
|
||||
virtual ~RsFileSingleChunkCrc() {}
|
||||
virtual void clear() {}
|
||||
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
uint32_t chunk_number ;
|
||||
Sha1CheckSum check_sum ; // CRC32 map of the file.
|
||||
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsFileItemSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsFileItemSerialiser()
|
||||
:RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_FILE)
|
||||
{ return; }
|
||||
virtual ~RsFileItemSerialiser() { 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:
|
||||
|
||||
/* sub types */
|
||||
virtual uint32_t sizeReq(RsFileRequest *);
|
||||
virtual uint32_t sizeData(RsFileData *);
|
||||
virtual uint32_t sizeChunkMapReq(RsFileChunkMapRequest *);
|
||||
virtual uint32_t sizeChunkMap(RsFileChunkMap *);
|
||||
virtual uint32_t sizeChunkCrcReq(RsFileSingleChunkCrcRequest *);
|
||||
virtual uint32_t sizeChunkCrc(RsFileSingleChunkCrc *);
|
||||
virtual uint32_t sizeCRC32MapReq(RsFileCRC32MapRequest *);
|
||||
virtual uint32_t sizeCRC32Map(RsFileCRC32Map *);
|
||||
|
||||
virtual bool serialiseReq (RsFileRequest *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseData (RsFileData *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseChunkMapReq(RsFileChunkMapRequest *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseChunkMap(RsFileChunkMap *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseCRC32MapReq(RsFileCRC32MapRequest *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseCRC32Map(RsFileCRC32Map *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseChunkCrcReq(RsFileSingleChunkCrcRequest *item, void *data, uint32_t *size);
|
||||
virtual bool serialiseChunkCrc(RsFileSingleChunkCrc *item, void *data, uint32_t *size);
|
||||
|
||||
virtual RsFileRequest *deserialiseReq(void *data, uint32_t *size);
|
||||
virtual RsFileData *deserialiseData(void *data, uint32_t *size);
|
||||
virtual RsFileChunkMapRequest *deserialiseChunkMapReq(void *data, uint32_t *size);
|
||||
virtual RsFileChunkMap *deserialiseChunkMap(void *data, uint32_t *size);
|
||||
virtual RsFileCRC32MapRequest *deserialiseCRC32MapReq(void *data, uint32_t *size);
|
||||
virtual RsFileCRC32Map *deserialiseCRC32Map(void *data, uint32_t *size);
|
||||
virtual RsFileSingleChunkCrcRequest *deserialiseChunkCrcReq(void *data, uint32_t *size);
|
||||
virtual RsFileSingleChunkCrc *deserialiseChunkCrc(void *data, uint32_t *size);
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsCacheRequest: public RsItem
|
||||
{
|
||||
public:
|
||||
RsCacheRequest()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_CACHE,
|
||||
RS_PKT_SUBTYPE_CACHE_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_CACHE_REQUEST);
|
||||
}
|
||||
virtual ~RsCacheRequest();
|
||||
virtual void clear();
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
uint16_t cacheType;
|
||||
uint16_t cacheSubId;
|
||||
RsTlvFileItem file; /* file information */
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsCacheItem: public RsItem
|
||||
{
|
||||
public:
|
||||
RsCacheItem()
|
||||
:RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_CACHE,
|
||||
RS_PKT_SUBTYPE_CACHE_ITEM)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_CACHE_ITEM);
|
||||
}
|
||||
virtual ~RsCacheItem();
|
||||
virtual void clear();
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
uint16_t cacheType;
|
||||
uint16_t cacheSubId;
|
||||
RsTlvFileItem file; /* file information */
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsCacheItemSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsCacheItemSerialiser()
|
||||
:RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
|
||||
RS_PKT_TYPE_CACHE)
|
||||
{ return; }
|
||||
virtual ~RsCacheItemSerialiser() { 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:
|
||||
|
||||
/* sub types */
|
||||
virtual uint32_t sizeReq(RsCacheRequest *);
|
||||
virtual bool serialiseReq (RsCacheRequest *item, void *data, uint32_t *size);
|
||||
virtual RsCacheRequest * deserialiseReq(void *data, uint32_t *size);
|
||||
|
||||
virtual uint32_t sizeItem(RsCacheItem *);
|
||||
virtual bool serialiseItem (RsCacheItem *item, void *data, uint32_t *size);
|
||||
virtual RsCacheItem * deserialiseItem(void *data, uint32_t *size);
|
||||
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsServiceSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsServiceSerialiser()
|
||||
:RsSerialType(RS_PKT_VERSION_SERVICE, 0, 0)
|
||||
{ return; }
|
||||
virtual ~RsServiceSerialiser()
|
||||
{ return; }
|
||||
|
||||
virtual uint32_t size(RsItem *);
|
||||
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
|
||||
virtual RsItem * deserialise(void *data, uint32_t *size);
|
||||
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
#endif
|
||||
|
900
libretroshare/src/serialiser/rsfiletransferitems.cc
Normal file
900
libretroshare/src/serialiser/rsfiletransferitems.cc
Normal file
|
@ -0,0 +1,900 @@
|
|||
|
||||
/*
|
||||
* libretroshare/src/serialiser: rsbaseitems.cc
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "serialiser/rsbaseserial.h"
|
||||
#include "serialiser/rstlvbase.h"
|
||||
#include "serialiser/rsfiletransferitems.h"
|
||||
|
||||
/***
|
||||
* #define RSSERIAL_DEBUG 1
|
||||
* #define DEBUG_TRANSFERS 1
|
||||
***/
|
||||
|
||||
|
||||
#ifdef DEBUG_TRANSFERS
|
||||
#include "util/rsprint.h"
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/**********************************************************************************************/
|
||||
/* 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_REQUEST: return deserialise_RsFileTransferCacheRequestItem(data, *pktsize);
|
||||
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:
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**********************************************************************************************/
|
||||
/* OUTPUTS */
|
||||
/**********************************************************************************************/
|
||||
|
||||
std::ostream& RsFileTransferCacheRequestItem::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
printRsItemBase(out, "RsFileTransferCacheRequestItem", indent);
|
||||
uint16_t int_Indent = indent + 2;
|
||||
printIndent(out, int_Indent);
|
||||
out << "CacheId: " << cacheType << "/" << cacheSubId << std::endl;
|
||||
file.print(out, int_Indent);
|
||||
printRsItemEnd(out, "RsFileTransferCacheRequestItem", indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
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 RsFileTransferCacheRequestItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 4; /* type/subid */
|
||||
s += file.TlvSize();
|
||||
|
||||
return s;
|
||||
}
|
||||
uint32_t RsFileTransferChunkMapRequestItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 1 ; // is_client
|
||||
s += GetTlvStringSize(hash) ; // hash
|
||||
|
||||
return s;
|
||||
}
|
||||
uint32_t RsFileTransferChunkMapItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 1 ; // is_client
|
||||
s += GetTlvStringSize(hash) ; // 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 += GetTlvStringSize(hash) ; // hash
|
||||
s += 4 ; // chunk number
|
||||
s += 20 ; // sha1
|
||||
|
||||
return s;
|
||||
}
|
||||
uint32_t RsFileTransferSingleChunkCrcRequestItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += GetTlvStringSize(hash) ; // 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();
|
||||
}
|
||||
void RsFileTransferCacheRequestItem::clear()
|
||||
{
|
||||
cacheType = 0;
|
||||
cacheSubId = 0;
|
||||
file.TlvClear();
|
||||
}
|
||||
|
||||
/**********************************************************************************************/
|
||||
/* SERIALISATION */
|
||||
/**********************************************************************************************/
|
||||
|
||||
bool RsFileTransferItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvsize, uint32_t& offset)
|
||||
{
|
||||
tlvsize = serial_size() ;
|
||||
offset = 0;
|
||||
|
||||
if (pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
pktsize = tlvsize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsFileTransferChunkMapRequestItem::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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, hash);
|
||||
|
||||
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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, hash);
|
||||
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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, hash);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, chunk_number) ;
|
||||
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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, hash);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, compressed_map._map.size());
|
||||
|
||||
for(uint32_t i=0;i<compressed_map._map.size();++i)
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, compressed_map._map[i]);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
std::cerr << "RsFileItemSerialiser::serialiseData() Size Error! " << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsFileTransferCacheRequestItem::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 &= setRawUInt16(data, tlvsize, &offset, cacheType);
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, cacheSubId);
|
||||
ok &= file.SetTlv(data, tlvsize, &offset);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
std::cerr << "RsFileItemSerialiser::serialiseReq() Size Error! " << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsFileTransferDataItem::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 &= 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;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsFileTransferDataRequestItem::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 &= 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;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsFileTransferCacheItem::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 &= 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;
|
||||
}
|
||||
|
||||
/**********************************************************************************************/
|
||||
/* DESERIALISATION */
|
||||
/**********************************************************************************************/
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferCacheRequestItem(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_CACHE_REQUEST != getRsItemSubType(rstype))
|
||||
{
|
||||
std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferCacheRequest(): wong subtype!" << std::endl;
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
||||
if (pktsize < rssize) /* check size */
|
||||
{
|
||||
std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferCacheRequest(): size inconsistency!" << std::endl;
|
||||
return NULL; /* not enough data */
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* ready to load */
|
||||
RsFileTransferCacheRequestItem *item = new RsFileTransferCacheRequestItem();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferCacheItem(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_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;
|
||||
}
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferChunkMapRequestItem(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_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 &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->hash); // file hash
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
/* error */
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferDataItem(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_DATA != getRsItemSubType(rstype))
|
||||
{
|
||||
std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataItem(): wong subtype!" << std::endl;
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
||||
if (pktsize < rssize) /* check size */
|
||||
{
|
||||
std::cerr << "RsFileTransferSerialiser::deserialise_RsFileTransferDataItem(): size inconsistency!" << std::endl;
|
||||
return NULL; /* not enough data */
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* ready to load */
|
||||
RsFileTransferDataItem *item = new RsFileTransferDataItem();
|
||||
item->clear();
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferDataRequestItem(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_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 &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->hash); // file hash
|
||||
uint32_t size =0;
|
||||
ok &= getRawUInt32(data, rssize, &offset, &size);
|
||||
|
||||
if(ok)
|
||||
{
|
||||
item->compressed_map._map.resize(size) ;
|
||||
|
||||
for(uint32_t i=0;i<size && ok;++i)
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->compressed_map._map[i]));
|
||||
}
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
/* error */
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcRequestItem(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_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 &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->hash); // file hash
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->chunk_number));
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
/* error */
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
RsFileTransferItem *RsFileTransferSerialiser::deserialise_RsFileTransferSingleChunkCrcItem(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_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 &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->hash); // file hash
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->chunk_number));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[0]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[1]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[2]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[3]));
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->check_sum.fourbytes[4]));
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
/* error */
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
/*************************************************************************/
|
271
libretroshare/src/serialiser/rsfiletransferitems.h
Normal file
271
libretroshare/src/serialiser/rsfiletransferitems.h
Normal file
|
@ -0,0 +1,271 @@
|
|||
#pragma once
|
||||
|
||||
/*
|
||||
* libretroshare/src/serialiser: rsbaseitems.h
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "retroshare/rstypes.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
#include "serialiser/rstlvtypes.h"
|
||||
#include "serialiser/rsserviceids.h"
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_DATA_REQUEST = 0x01;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_DATA = 0x02;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CHUNK_MAP_REQUEST = 0x04;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CHUNK_MAP = 0x05;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CHUNK_CRC_REQUEST = 0x08;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CHUNK_CRC = 0x09;
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CACHE_ITEM = 0x0A;
|
||||
const uint8_t RS_PKT_SUBTYPE_FT_CACHE_REQUEST = 0x0B;
|
||||
|
||||
//const uint8_t RS_PKT_SUBTYPE_FT_TRANSFER = 0x03;
|
||||
//const uint8_t RS_PKT_SUBTYPE_FT_CRC32_MAP_REQUEST = 0x06;
|
||||
//const uint8_t RS_PKT_SUBTYPE_FT_CRC32_MAP = 0x07;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsFileTransferItem: public RsItem
|
||||
{
|
||||
public:
|
||||
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
|
||||
{
|
||||
public:
|
||||
RsFileTransferDataRequestItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_DATA_REQUEST)
|
||||
{
|
||||
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);
|
||||
|
||||
// Private data part.
|
||||
//
|
||||
uint64_t fileoffset; /* start of data requested */
|
||||
uint32_t chunksize; /* size of data requested */
|
||||
RsTlvFileItem file; /* file information */
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
class RsFileTransferDataItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferDataItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_DATA)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_FILE_DATA) ;
|
||||
}
|
||||
virtual ~RsFileTransferDataItem() { 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 data part.
|
||||
//
|
||||
RsTlvFileData fd;
|
||||
};
|
||||
|
||||
class RsFileTransferChunkMapRequestItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferChunkMapRequestItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_CHUNK_MAP_REQUEST)
|
||||
{
|
||||
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);
|
||||
|
||||
// Private data part.
|
||||
//
|
||||
bool is_client ; // is the request for a client, or a server ?
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
};
|
||||
|
||||
class RsFileTransferChunkMapItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferChunkMapItem()
|
||||
:RsFileTransferItem(RS_PKT_SUBTYPE_FT_CHUNK_MAP)
|
||||
{
|
||||
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);
|
||||
|
||||
// Private data part.
|
||||
//
|
||||
bool is_client ; // is the request for a client, or a server ?
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
CompressedChunkMap compressed_map ; // Chunk map of the file.
|
||||
};
|
||||
|
||||
class RsFileTransferSingleChunkCrcRequestItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferSingleChunkCrcRequestItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_CHUNK_CRC_REQUEST)
|
||||
{
|
||||
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);
|
||||
|
||||
// Private data part.
|
||||
//
|
||||
std::string hash ; // hash of the file for which we request the crc
|
||||
uint32_t chunk_number ; // chunk number
|
||||
};
|
||||
|
||||
class RsFileTransferSingleChunkCrcItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferSingleChunkCrcItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_CHUNK_CRC)
|
||||
{
|
||||
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);
|
||||
|
||||
// Private data part.
|
||||
//
|
||||
std::string hash ; // hash of the file for which we request the chunk map
|
||||
uint32_t chunk_number ;
|
||||
Sha1CheckSum check_sum ; // CRC32 map of the file.
|
||||
};
|
||||
|
||||
class RsFileTransferCacheRequestItem: public RsFileTransferItem
|
||||
{
|
||||
public:
|
||||
RsFileTransferCacheRequestItem() :RsFileTransferItem(RS_PKT_SUBTYPE_FT_CACHE_REQUEST)
|
||||
{
|
||||
setPriorityLevel(QOS_PRIORITY_RS_CACHE_REQUEST);
|
||||
}
|
||||
virtual ~RsFileTransferCacheRequestItem() {}
|
||||
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);
|
||||
|
||||
// private part
|
||||
//
|
||||
uint16_t cacheType;
|
||||
uint16_t cacheSubId;
|
||||
RsTlvFileItem file; /* file information */
|
||||
};
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
RsFileTransferSerialiser(): RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_FILE_TRANSFER) {}
|
||||
|
||||
virtual ~RsFileTransferSerialiser() {}
|
||||
|
||||
virtual uint32_t size(RsItem *item)
|
||||
{
|
||||
return dynamic_cast<RsFileTransferItem*>(item)->serial_size() ;
|
||||
}
|
||||
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
|
||||
{
|
||||
return dynamic_cast<RsFileTransferItem*>(item)->serialise(data,*size) ;
|
||||
}
|
||||
virtual RsFileTransferItem *deserialise(void *data, uint32_t *size);
|
||||
|
||||
private:
|
||||
RsFileTransferItem *deserialise_RsFileTransferCacheRequestItem(void *data, uint32_t pktsize);
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
|
@ -38,15 +38,16 @@
|
|||
*/
|
||||
|
||||
/* These are Cache Only */
|
||||
const uint16_t RS_SERVICE_TYPE_FILE_INDEX = 0x0001;
|
||||
const uint16_t RS_SERVICE_TYPE_FILE_INDEX = 0x0001;
|
||||
|
||||
/* These are Services only */
|
||||
const uint16_t RS_SERVICE_TYPE_DISC = 0x0011;
|
||||
const uint16_t RS_SERVICE_TYPE_CHAT = 0x0012;
|
||||
const uint16_t RS_SERVICE_TYPE_MSG = 0x0013;
|
||||
const uint16_t RS_SERVICE_TYPE_TURTLE = 0x0014;
|
||||
const uint16_t RS_SERVICE_TYPE_TUNNEL = 0x0015;
|
||||
const uint16_t RS_SERVICE_TYPE_HEARTBEAT = 0x0016;
|
||||
const uint16_t RS_SERVICE_TYPE_DISC = 0x0011;
|
||||
const uint16_t RS_SERVICE_TYPE_CHAT = 0x0012;
|
||||
const uint16_t RS_SERVICE_TYPE_MSG = 0x0013;
|
||||
const uint16_t RS_SERVICE_TYPE_TURTLE = 0x0014;
|
||||
const uint16_t RS_SERVICE_TYPE_TUNNEL = 0x0015;
|
||||
const uint16_t RS_SERVICE_TYPE_HEARTBEAT = 0x0016;
|
||||
const uint16_t RS_SERVICE_TYPE_FILE_TRANSFER = 0x0017;
|
||||
|
||||
/* BanList Still Testing at the moment - Service Only */
|
||||
const uint16_t RS_SERVICE_TYPE_BANLIST = 0x0101;
|
||||
|
|
99
libretroshare/src/serialiser/rsserviceserialiser.cc
Normal file
99
libretroshare/src/serialiser/rsserviceserialiser.cc
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* libretroshare/src/serialiser: rsserviceserialiser.cc
|
||||
*
|
||||
* 3P/PQI network interface for RetroShare.
|
||||
*
|
||||
* Copyright 2013-2013 by Cyril Soler & Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rsserviceserialiser.h"
|
||||
|
||||
uint32_t RsServiceSerialiser::size(RsItem *i)
|
||||
{
|
||||
RsRawItem *item = dynamic_cast<RsRawItem *>(i);
|
||||
|
||||
if (item)
|
||||
{
|
||||
return item->getRawLength();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* serialise the data to the buffer */
|
||||
bool RsServiceSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
|
||||
{
|
||||
RsRawItem *item = dynamic_cast<RsRawItem *>(i);
|
||||
if (!item)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef RSSERIAL_DEBUG
|
||||
std::cerr << "RsServiceSerialiser::serialise() serializing raw item. pktsize : " << *pktsize;
|
||||
#endif
|
||||
|
||||
uint32_t tlvsize = item->getRawLength();
|
||||
#ifdef RSSERIAL_DEBUG
|
||||
std::cerr << "tlvsize : " << tlvsize << std::endl;
|
||||
#endif
|
||||
|
||||
if (*pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
if (tlvsize > getRsPktMaxSize())
|
||||
return false; /* packet too big */
|
||||
|
||||
*pktsize = tlvsize;
|
||||
|
||||
/* its serialised already!!! */
|
||||
memcpy(data, item->getRawData(), tlvsize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RsItem *RsServiceSerialiser::deserialise(void *data, uint32_t *pktsize)
|
||||
{
|
||||
/* get the type and size */
|
||||
uint32_t rstype = getRsItemId(data);
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
|
||||
if (RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype))
|
||||
{
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
||||
if (*pktsize < rssize) /* check size */
|
||||
return NULL; /* not enough data */
|
||||
|
||||
if (rssize > getRsPktMaxSize())
|
||||
return NULL; /* packet too big */
|
||||
|
||||
/* set the packet length */
|
||||
*pktsize = rssize;
|
||||
|
||||
RsRawItem *item = new RsRawItem(rstype, rssize);
|
||||
void *item_data = item->getRawData();
|
||||
|
||||
memcpy(item_data, data, rssize);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
40
libretroshare/src/serialiser/rsserviceserialiser.h
Normal file
40
libretroshare/src/serialiser/rsserviceserialiser.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* libretroshare/src/serialiser: rsserviceserialiser.h
|
||||
*
|
||||
* 3P/PQI network interface for RetroShare.
|
||||
*
|
||||
* Copyright 2013-2013 by Cyril Soler & Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "rsserial.h"
|
||||
|
||||
class RsServiceSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsServiceSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, 0, 0) { }
|
||||
virtual ~RsServiceSerialiser() { }
|
||||
|
||||
virtual uint32_t size(RsItem *);
|
||||
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
|
||||
virtual RsItem * deserialise(void *data, uint32_t *size);
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue