started converting rschatitems to new serialization

This commit is contained in:
csoler 2017-04-04 15:33:59 +02:00
parent f7e396856e
commit 32ce9aea3a
5 changed files with 106 additions and 74 deletions

View File

@ -218,7 +218,7 @@ void DistantChatService::receiveData(const RsGxsTunnelService::RsGxsTunnelId &tu
contact.from_id = tinfo.source_gxs_id ; contact.from_id = tinfo.source_gxs_id ;
} }
RsItem *item = RsChatSerialiser().deserialise(data,&data_size) ; RsItem *item = RsChatSerialiser().deserialise(data,data_size) ;
if(item != NULL) if(item != NULL)
{ {

View File

@ -29,10 +29,13 @@
#include "serialiser/rsbaseserial.h" #include "serialiser/rsbaseserial.h"
#include "serialiser/rstlvbase.h" #include "serialiser/rstlvbase.h"
#include "serialization/rstypeserializer.h"
#include "chat/rschatitems.h" #include "chat/rschatitems.h"
//#define CHAT_DEBUG 1 //#define CHAT_DEBUG 1
#ifdef TO_BE_REMOVED
std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent) std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent)
{ {
printRsItemBase(out, "RsChatMsgItem", indent); printRsItemBase(out, "RsChatMsgItem", indent);
@ -220,57 +223,44 @@ std::ostream& RsChatAvatarItem::print(std::ostream &out, uint16_t indent)
return out; return out;
} }
RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize) #endif
RsItem *RsChatSerialiser::create_item(uint16_t service_id,uint8_t item_sub_id)
{ {
uint32_t rstype = getRsItemId(data); if(service_id != RS_SERVICE_TYPE_CHAT)
uint32_t rssize = getRsItemSize(data); return NULL ;
#ifdef CHAT_DEBUG switch(item_sub_id)
std::cerr << "deserializing packet..."<< std::endl ;
#endif
// look what we have...
if (*pktsize < rssize) /* check size */
{ {
#ifdef CHAT_DEBUG case RS_PKT_SUBTYPE_DEFAULT: return new RsChatMsgItem() ;
std::cerr << "chat deserialisation: not enough size: pktsize=" << *pktsize << ", rssize=" << rssize << std::endl ; case RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG: return new RsPrivateChatMsgConfigItem() ;
#endif case RS_PKT_SUBTYPE_DISTANT_INVITE_CONFIG: return new RsPrivateChatDistantInviteConfigItem() ;
return NULL; /* not enough data */ case RS_PKT_SUBTYPE_CHAT_STATUS: return new RsChatStatusItem() ;
} case RS_PKT_SUBTYPE_CHAT_AVATAR: return new RsChatAvatarItem() ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_SIGNED_MSG: return new RsChatLobbyMsgItem() ;
/* set the packet length */ case RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE: return new RsChatLobbyInviteItem() ;
*pktsize = rssize; case RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE: return new RsChatLobbyConnectChallengeItem() ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE: return new RsChatLobbyUnsubscribeItem() ;
/* ready to load */ case RS_PKT_SUBTYPE_CHAT_LOBBY_SIGNED_EVENT:return new RsChatLobbyEventItem() ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_REQUEST:return new RsChatLobbyListRequestItem() ;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_CHAT != getRsItemService(rstype))) case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST: return new RsChatLobbyListItem() ;
{ case RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG: return new RsChatLobbyConfigItem() ;
#ifdef CHAT_DEBUG
std::cerr << "chat deserialisation: wrong type !" << std::endl ;
#endif
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_DEFAULT: return new RsChatMsgItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG: return new RsPrivateChatMsgConfigItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_DISTANT_INVITE_CONFIG: return new RsPrivateChatDistantInviteConfigItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_STATUS: return new RsChatStatusItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_AVATAR: return new RsChatAvatarItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_SIGNED_MSG: return new RsChatLobbyMsgItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE: return new RsChatLobbyInviteItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE: return new RsChatLobbyConnectChallengeItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE: return new RsChatLobbyUnsubscribeItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_SIGNED_EVENT: return new RsChatLobbyEventItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_REQUEST: return new RsChatLobbyListRequestItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST: return new RsChatLobbyListItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG: return new RsChatLobbyConfigItem(data,*pktsize) ;
default: default:
std::cerr << "Unknown packet type in chat!" << std::endl ; std::cerr << "Unknown packet type in chat!" << std::endl ;
return NULL ; return NULL ;
} }
} }
void RsChatMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::TlvString tt(str,TLV_TYPE_STR_MSG) ;
RsTypeSerializer::serial_process(j,ctx,chatFlags,"chatflags") ;
RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ;
RsTypeSerializer::serial_process(j,ctx,tt,"message") ;
}
#ifdef TO_BE_REMOVED
uint32_t RsChatMsgItem::serial_size() uint32_t RsChatMsgItem::serial_size()
{ {
uint32_t s = 8; /* header */ uint32_t s = 8; /* header */
@ -423,6 +413,7 @@ uint32_t RsChatLobbyConfigItem::serial_size()
return s; return s;
} }
#endif
/*************************************************************************/ /*************************************************************************/
@ -435,6 +426,7 @@ RsChatAvatarItem::~RsChatAvatarItem()
} }
} }
#ifdef TO_BE_REMOVED
/* serialise the data to the buffer */ /* serialise the data to the buffer */
bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize) bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
{ {
@ -481,21 +473,40 @@ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
#endif #endif
return ok ; return ok ;
} }
#endif
bool RsChatLobbyBouncingObject::serialise_to_memory(void *data,uint32_t tlvsize,uint32_t& offset,bool include_signature) bool RsChatLobbyBouncingObject::serialise_to_memory(RsItem::SerializeJob j,SerializeContext& ctx,bool include_signature)
{ {
bool ok = true ; TlvString tt(nick,TLV_TYPE_STR_NAME) ;
ok &= setRawUInt64(data, tlvsize, &offset, lobby_id); RsTypeSerializer::serial_process(j,ctx,lobby_id,"lobby_id") ;
ok &= setRawUInt64(data, tlvsize, &offset, msg_id); RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ;
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, nick); RsTypeSerializer::serial_process(j,ctx,tt ,"nick") ;
if(include_signature) if(include_signature)
ok &= signature.SetTlv(data, tlvsize, &offset); RsTypeSerializer::serial_process(j,ctx,signature,"signature") ;
return ok ; return true ;
} }
void RsChatLobbyMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsChatMsgItem::serial_process(j,ctx) ;
if(j == RsItem::SERIALIZE)
{
SerializeContext ctx2 ;
serial_process(RsItem::SIZE_ESTIMATE,ctx2);
setRsItemHeader(ctx.mData, ctx.mSize, PacketId(), ctx2.mOffset); // correct header!
}
RsTypeSerializer::serial_process(j,ctx,parent_msg_id,"parent_msg_id") ;
ctx.mOk &= RsChatLobbyBouncingObject::serialise_to_memory(j,ctx,true) ;
}
#ifdef TO_BE_REMOVED
/* serialise the data to the buffer */ /* serialise the data to the buffer */
bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize) bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize)
{ {
@ -528,6 +539,8 @@ bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize)
#endif #endif
return ok ; return ok ;
} }
#endif
/* serialise the data to the buffer */ /* serialise the data to the buffer */
bool RsChatLobbyMsgItem::serialise_signed_part(void *data, uint32_t& pktsize) bool RsChatLobbyMsgItem::serialise_signed_part(void *data, uint32_t& pktsize)
{ {
@ -562,7 +575,12 @@ bool RsChatLobbyMsgItem::serialise_signed_part(void *data, uint32_t& pktsize)
return ok ; return ok ;
} }
void RsChatLobbyListRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
// nothing to do. This is an empty item.
}
#ifdef TO_BE_REMOVED
bool RsChatLobbyListRequestItem::serialise(void *data, uint32_t& pktsize) bool RsChatLobbyListRequestItem::serialise(void *data, uint32_t& pktsize)
{ {
uint32_t tlvsize = serial_size() ; uint32_t tlvsize = serial_size() ;
@ -575,6 +593,27 @@ bool RsChatLobbyListRequestItem::serialise(void *data, uint32_t& pktsize)
pktsize = tlvsize ; pktsize = tlvsize ;
return ok ; return ok ;
} }
#endif
template<> void RsTypeSerializer::serial_process<VisibleChatLobbyInfo>(RsItem::SerializeJob j,SerializeContext& ctx,VisibleChatLobbyInfo& info)
{
RsTypeSerializer::serial_process<uint64_t>(info.id) ;
TlvString tt1(info.name ,TLV_TYPE_STR_NAME) ;
TlvString tt2(info.topic,TLV_TYPE_STR_NAME) ;
RsTypeSerializer::serial_process(info.name) ;
RsTypeSerializer::serial_process(info.topic) ;
RsTypeSerializer::serial_process<uint32_t>(info.count) ;
RsTypeSerializer::serial_process<uint32_t>(info.flags.toUInt32()) ;
}
void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,lobbies,"lobbies") ;
}
#ifdef TO_BE_REMOVED
bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize) bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize)
{ {
uint32_t tlvsize = serial_size() ; uint32_t tlvsize = serial_size() ;
@ -604,6 +643,8 @@ bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize)
} }
return ok ; return ok ;
} }
#endif
bool RsChatLobbyEventItem::serialise(void *data, uint32_t& pktsize) bool RsChatLobbyEventItem::serialise(void *data, uint32_t& pktsize)
{ {
uint32_t tlvsize = serial_size() ; uint32_t tlvsize = serial_size() ;

View File

@ -27,6 +27,7 @@
#include "openssl/bn.h" #include "openssl/bn.h"
#include "retroshare/rstypes.h" #include "retroshare/rstypes.h"
#include "serialization/rsserializer.h"
#include "serialiser/rstlvkeys.h" #include "serialiser/rstlvkeys.h"
#include "serialiser/rsserviceids.h" #include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h" #include "serialiser/rsserial.h"
@ -90,11 +91,9 @@ class RsChatItem: public RsItem
} }
virtual ~RsChatItem() {} virtual ~RsChatItem() {}
virtual void clear() {} virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) {} // derived from RsItem, but should be removed
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ? virtual void clear() {}
virtual uint32_t serial_size() = 0 ; // deserialise is handled using a constructor
}; };
/*! /*!
@ -107,14 +106,14 @@ public:
RsChatMsgItem() :RsChatItem(RS_PKT_SUBTYPE_DEFAULT) {} RsChatMsgItem() :RsChatItem(RS_PKT_SUBTYPE_DEFAULT) {}
RsChatMsgItem(uint8_t subtype) :RsChatItem(subtype) {} RsChatMsgItem(uint8_t subtype) :RsChatItem(subtype) {}
RsChatMsgItem(void *data,uint32_t size,uint8_t subtype = RS_PKT_SUBTYPE_DEFAULT) ; // deserialization //RsChatMsgItem() {}
virtual ~RsChatMsgItem() {} virtual ~RsChatMsgItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ? // derived from RsItem
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx);
virtual void clear() {}
uint32_t chatFlags; uint32_t chatFlags;
uint32_t sendTime; uint32_t sendTime;
@ -150,7 +149,7 @@ protected:
// serialise() methods, otherwise the wrong method will be called when serialising from this top level class. // serialise() methods, otherwise the wrong method will be called when serialising from this top level class.
uint32_t serialized_size(bool include_signature) ; uint32_t serialized_size(bool include_signature) ;
bool serialise_to_memory(void *data,uint32_t tlvsize,uint32_t& offset,bool include_signature) ; bool serialise_to_memory(RsItem::SerializeJob j, SerializeContext &ctx, bool include_signature) ;
bool deserialise_from_memory(void *data,uint32_t rssize,uint32_t& offset) ; bool deserialise_from_memory(void *data,uint32_t rssize,uint32_t& offset) ;
}; };
@ -413,19 +412,11 @@ class RsChatDHPublicKeyItem: public RsChatItem
const RsChatDHPublicKeyItem& operator=(const RsChatDHPublicKeyItem&) { return *this ;} const RsChatDHPublicKeyItem& operator=(const RsChatDHPublicKeyItem&) { return *this ;}
}; };
class RsChatSerialiser: public RsSerialType class RsChatSerialiser: public RsSerializer
{ {
public: public:
RsChatSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_CHAT) {} RsChatSerialiser() :RsSerializer(RS_SERVICE_TYPE_CHAT) {}
virtual uint32_t size (RsItem *item) virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) ;
{
return static_cast<RsChatItem *>(item)->serial_size() ;
}
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
{
return static_cast<RsChatItem *>(item)->serialise(data,*size) ;
}
virtual RsItem *deserialise (void *data, uint32_t *size) ;
}; };

View File

@ -59,10 +59,10 @@ uint32_t RsSerializer::size(RsItem *item)
{ {
SerializeContext ctx(NULL,0); SerializeContext ctx(NULL,0);
ctx.mSize = 8 ; // header size ctx.mOffset = 8 ; // header size
item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ; item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ;
return ctx.mSize ; return ctx.mOffset ;
} }
void RsSerializer::print(RsItem *item) void RsSerializer::print(RsItem *item)

View File

@ -13,7 +13,7 @@ class RsTypeSerializer
{ {
switch(j) switch(j)
{ {
case RsItem::SIZE_ESTIMATE: ctx.mSize += serial_size(member) ; case RsItem::SIZE_ESTIMATE: ctx.mOffset += serial_size(member) ;
break ; break ;
case RsItem::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; case RsItem::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ;