diff --git a/libretroshare/src/chat/distantchat.cc b/libretroshare/src/chat/distantchat.cc index 70fb88d8c..3f0e26b83 100644 --- a/libretroshare/src/chat/distantchat.cc +++ b/libretroshare/src/chat/distantchat.cc @@ -218,7 +218,7 @@ void DistantChatService::receiveData(const RsGxsTunnelService::RsGxsTunnelId &tu contact.from_id = tinfo.source_gxs_id ; } - RsItem *item = RsChatSerialiser().deserialise(data,&data_size) ; + RsItem *item = RsChatSerialiser().deserialise(data,data_size) ; if(item != NULL) { diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 90cf78fa6..3e2bd7d5a 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -29,10 +29,13 @@ #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvbase.h" +#include "serialization/rstypeserializer.h" + #include "chat/rschatitems.h" //#define CHAT_DEBUG 1 +#ifdef TO_BE_REMOVED std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent) { printRsItemBase(out, "RsChatMsgItem", indent); @@ -220,57 +223,44 @@ std::ostream& RsChatAvatarItem::print(std::ostream &out, uint16_t indent) 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); - uint32_t rssize = getRsItemSize(data); + if(service_id != RS_SERVICE_TYPE_CHAT) + return NULL ; -#ifdef CHAT_DEBUG - std::cerr << "deserializing packet..."<< std::endl ; -#endif - // look what we have... - if (*pktsize < rssize) /* check size */ + switch(item_sub_id) { -#ifdef CHAT_DEBUG - std::cerr << "chat deserialisation: not enough size: pktsize=" << *pktsize << ", rssize=" << rssize << std::endl ; -#endif - return NULL; /* not enough data */ - } - - /* set the packet length */ - *pktsize = rssize; - - /* ready to load */ - - if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_CHAT != getRsItemService(rstype))) - { -#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) ; + case RS_PKT_SUBTYPE_DEFAULT: return new RsChatMsgItem() ; + case RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG: return new RsPrivateChatMsgConfigItem() ; + case RS_PKT_SUBTYPE_DISTANT_INVITE_CONFIG: return new RsPrivateChatDistantInviteConfigItem() ; + 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() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE: return new RsChatLobbyInviteItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE: return new RsChatLobbyConnectChallengeItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE: return new RsChatLobbyUnsubscribeItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_SIGNED_EVENT:return new RsChatLobbyEventItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_REQUEST:return new RsChatLobbyListRequestItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST: return new RsChatLobbyListItem() ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG: return new RsChatLobbyConfigItem() ; default: std::cerr << "Unknown packet type in chat!" << std::endl ; 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 s = 8; /* header */ @@ -423,6 +413,7 @@ uint32_t RsChatLobbyConfigItem::serial_size() return s; } +#endif /*************************************************************************/ @@ -435,6 +426,7 @@ RsChatAvatarItem::~RsChatAvatarItem() } } +#ifdef TO_BE_REMOVED /* serialise the data to the buffer */ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize) { @@ -481,21 +473,40 @@ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize) #endif 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); - ok &= setRawUInt64(data, tlvsize, &offset, msg_id); - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, nick); + RsTypeSerializer::serial_process(j,ctx,lobby_id,"lobby_id") ; + RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ; + RsTypeSerializer::serial_process(j,ctx,tt ,"nick") ; 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 */ bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize) { @@ -528,6 +539,8 @@ bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize) #endif return ok ; } +#endif + /* serialise the data to the buffer */ 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 ; } +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) { uint32_t tlvsize = serial_size() ; @@ -575,6 +593,27 @@ bool RsChatLobbyListRequestItem::serialise(void *data, uint32_t& pktsize) pktsize = tlvsize ; return ok ; } +#endif + +template<> void RsTypeSerializer::serial_process(RsItem::SerializeJob j,SerializeContext& ctx,VisibleChatLobbyInfo& info) +{ + RsTypeSerializer::serial_process(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(info.count) ; + RsTypeSerializer::serial_process(info.flags.toUInt32()) ; +} + +void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) +{ + RsTypeSerializer::serial_process(j,ctx,lobbies,"lobbies") ; +} + +#ifdef TO_BE_REMOVED bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize) { uint32_t tlvsize = serial_size() ; @@ -604,6 +643,8 @@ bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize) } return ok ; } +#endif + bool RsChatLobbyEventItem::serialise(void *data, uint32_t& pktsize) { uint32_t tlvsize = serial_size() ; diff --git a/libretroshare/src/chat/rschatitems.h b/libretroshare/src/chat/rschatitems.h index 13b42c18f..2ce127196 100644 --- a/libretroshare/src/chat/rschatitems.h +++ b/libretroshare/src/chat/rschatitems.h @@ -27,6 +27,7 @@ #include "openssl/bn.h" #include "retroshare/rstypes.h" +#include "serialization/rsserializer.h" #include "serialiser/rstlvkeys.h" #include "serialiser/rsserviceids.h" #include "serialiser/rsserial.h" @@ -90,11 +91,9 @@ class RsChatItem: public RsItem } virtual ~RsChatItem() {} - virtual void clear() {} - virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ; + virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) {} // derived from RsItem, but should be removed - virtual bool serialise(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ? - virtual uint32_t serial_size() = 0 ; // deserialise is handled using a constructor + virtual void clear() {} }; /*! @@ -107,14 +106,14 @@ public: RsChatMsgItem() :RsChatItem(RS_PKT_SUBTYPE_DEFAULT) {} RsChatMsgItem(uint8_t subtype) :RsChatItem(subtype) {} - RsChatMsgItem(void *data,uint32_t size,uint8_t subtype = RS_PKT_SUBTYPE_DEFAULT) ; // deserialization + //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 ? - virtual uint32_t serial_size() ; // deserialise is handled using a constructor + // derived from RsItem + + void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); + virtual void clear() {} uint32_t chatFlags; uint32_t sendTime; @@ -150,7 +149,7 @@ protected: // serialise() methods, otherwise the wrong method will be called when serialising from this top level class. 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) ; }; @@ -413,19 +412,11 @@ class RsChatDHPublicKeyItem: public RsChatItem const RsChatDHPublicKeyItem& operator=(const RsChatDHPublicKeyItem&) { return *this ;} }; -class RsChatSerialiser: public RsSerialType +class RsChatSerialiser: public RsSerializer { public: - RsChatSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_CHAT) {} + RsChatSerialiser() :RsSerializer(RS_SERVICE_TYPE_CHAT) {} - virtual uint32_t size (RsItem *item) - { - return static_cast(item)->serial_size() ; - } - virtual bool serialise(RsItem *item, void *data, uint32_t *size) - { - return static_cast(item)->serialise(data,*size) ; - } - virtual RsItem *deserialise (void *data, uint32_t *size) ; + virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) ; }; diff --git a/libretroshare/src/serialization/rsserializer.cc b/libretroshare/src/serialization/rsserializer.cc index f7926f8e2..b17785038 100644 --- a/libretroshare/src/serialization/rsserializer.cc +++ b/libretroshare/src/serialization/rsserializer.cc @@ -59,10 +59,10 @@ uint32_t RsSerializer::size(RsItem *item) { SerializeContext ctx(NULL,0); - ctx.mSize = 8 ; // header size + ctx.mOffset = 8 ; // header size item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ; - return ctx.mSize ; + return ctx.mOffset ; } void RsSerializer::print(RsItem *item) diff --git a/libretroshare/src/serialization/rstypeserializer.h b/libretroshare/src/serialization/rstypeserializer.h index 37c1f9dc7..1e4d5b20b 100644 --- a/libretroshare/src/serialization/rstypeserializer.h +++ b/libretroshare/src/serialization/rstypeserializer.h @@ -13,7 +13,7 @@ class RsTypeSerializer { switch(j) { - case RsItem::SIZE_ESTIMATE: ctx.mSize += serial_size(member) ; + case RsItem::SIZE_ESTIMATE: ctx.mOffset += serial_size(member) ; break ; case RsItem::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ;