progress in converting Chat items to new serialization model

This commit is contained in:
csoler 2017-04-04 22:53:50 +02:00
parent f2fa70e395
commit 9438d60609
4 changed files with 92 additions and 45 deletions

View File

@ -222,7 +222,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
mGixs->requestKey(obj->signature.keyId,peer_list,RsIdentityUsage(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id)); mGixs->requestKey(obj->signature.keyId,peer_list,RsIdentityUsage(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id));
uint32_t size = obj->signed_serial_size() ; uint32_t size = obj->serial_size_no_signature() ;
RsTemporaryMemory memory(size) ; RsTemporaryMemory memory(size) ;
#ifdef DEBUG_CHAT_LOBBIES #ifdef DEBUG_CHAT_LOBBIES
@ -230,7 +230,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
std::cerr << " signature id: " << obj->signature.keyId << std::endl; std::cerr << " signature id: " << obj->signature.keyId << std::endl;
#endif #endif
if(!obj->serialise_signed_part(memory,size)) if(!obj->serialize_no_signature(memory,size))
{ {
std::cerr << " (EE) Cannot serialise message item. " << std::endl; std::cerr << " (EE) Cannot serialise message item. " << std::endl;
return false ; return false ;
@ -239,7 +239,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
uint32_t error_status ; uint32_t error_status ;
RsIdentityUsage use_info(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id) ; RsIdentityUsage use_info(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id) ;
if(!mGixs->validateData(memory,obj->signed_serial_size(),obj->signature,false,use_info,error_status)) if(!mGixs->validateData(memory,obj->serial_size_no_signature(),obj->signature,false,use_info,error_status))
{ {
bool res = false ; bool res = false ;
@ -970,10 +970,10 @@ bool DistributedChatService::locked_initLobbyBouncableObject(const ChatLobbyId&
// now sign the object, if the lobby expects it // now sign the object, if the lobby expects it
uint32_t size = item.signed_serial_size() ; uint32_t size = item.serial_size_no_signature() ;
RsTemporaryMemory memory(size) ; RsTemporaryMemory memory(size) ;
if(!item.serialise_signed_part(memory,size)) if(!item.serialize_no_signature(memory,size))
{ {
std::cerr << "(EE) Cannot sign message item. " << std::endl; std::cerr << "(EE) Cannot sign message item. " << std::endl;
return false ; return false ;

View File

@ -35,6 +35,8 @@
//#define CHAT_DEBUG 1 //#define CHAT_DEBUG 1
static const uint32_t RS_CHAT_SERIALIZER_FLAGS_NO_SIGNATURE = 0x0001;
#ifdef TO_BE_REMOVED #ifdef TO_BE_REMOVED
std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent) std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent)
{ {
@ -474,7 +476,7 @@ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
} }
#endif #endif
void RsChatLobbyBouncingObject::serial_process_special(RsItem::SerializeJob j,SerializeContext& ctx,bool include_signature) void RsChatLobbyBouncingObject::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
RsTypeSerializer::TlvString tt(nick,TLV_TYPE_STR_NAME) ; RsTypeSerializer::TlvString tt(nick,TLV_TYPE_STR_NAME) ;
@ -482,7 +484,7 @@ void RsChatLobbyBouncingObject::serial_process_special(RsItem::SerializeJob j,Se
RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ; RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ;
RsTypeSerializer::serial_process(j,ctx,tt ,"nick") ; RsTypeSerializer::serial_process(j,ctx,tt ,"nick") ;
if(include_signature) if(!(ctx.mFlags & RS_CHAT_SERIALIZER_FLAGS_NO_SIGNATURE))
RsTypeSerializer::serial_process(j,ctx,signature,"signature") ; RsTypeSerializer::serial_process(j,ctx,signature,"signature") ;
} }
@ -490,17 +492,9 @@ void RsChatLobbyMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext&
{ {
RsChatMsgItem::serial_process(j,ctx) ; RsChatMsgItem::serial_process(j,ctx) ;
if(j == RsItem::SERIALIZE)
{
SerializeContext ctx2(NULL,0) ;
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") ; RsTypeSerializer::serial_process(j,ctx,parent_msg_id,"parent_msg_id") ;
RsChatLobbyBouncingObject::serial_process_special(j,ctx,true) ; RsChatLobbyBouncingObject::serial_process(j,ctx) ;
} }
#ifdef TO_BE_REMOVED #ifdef TO_BE_REMOVED
@ -536,7 +530,6 @@ 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)
@ -571,6 +564,7 @@ bool RsChatLobbyMsgItem::serialise_signed_part(void *data, uint32_t& pktsize)
#endif #endif
return ok ; return ok ;
} }
#endif
void RsChatLobbyListRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsChatLobbyListRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
@ -592,17 +586,17 @@ bool RsChatLobbyListRequestItem::serialise(void *data, uint32_t& pktsize)
} }
#endif #endif
template<> void RsTypeSerializer::serial_process<VisibleChatLobbyInfo>(RsItem::SerializeJob j,SerializeContext& ctx,VisibleChatLobbyInfo& info) template<> void RsTypeSerializer::serial_process(RsItem::SerializeJob j,SerializeContext& ctx,VisibleChatLobbyInfo& info)
{ {
RsTypeSerializer::serial_process<uint64_t>(info.id) ; RsTypeSerializer::serial_process<uint64_t>(info.id) ;
TlvString tt1(info.name ,TLV_TYPE_STR_NAME) ; TlvString tt1(info.name ,TLV_TYPE_STR_NAME) ;
TlvString tt2(info.topic,TLV_TYPE_STR_NAME) ; TlvString tt2(info.topic,TLV_TYPE_STR_NAME) ;
RsTypeSerializer::serial_process(info.name) ; RsTypeSerializer::serial_process(j,ctx,info.name,"info.name") ;
RsTypeSerializer::serial_process(info.topic) ; RsTypeSerializer::serial_process(j,ctx,info.topic,"info.topic") ;
RsTypeSerializer::serial_process<uint32_t>(info.count) ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,info.count,"info.count") ;
RsTypeSerializer::serial_process<uint32_t>(info.flags.toUInt32()) ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,info.flags.toUInt32(),"info.flags") ;
} }
void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
@ -642,6 +636,18 @@ bool RsChatLobbyListItem::serialise(void *data, uint32_t& pktsize)
} }
#endif #endif
void RsChatLobbyEventItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::TlvString tt(string1,TLV_TYPE_STR_NAME) ;
RsTypeSerializer::serial_process<uint8_t>(j,ctx,event_type,"event_type") ;
RsTypeSerializer::serial_process (j,ctx,string1,"string1") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,sendTime,"sendTime") ;
RsChatLobbyBouncingObject::serial_process(j,ctx,!(ctx.mFlags & RS_CHAT_SERIALIZER_FLAGS_NO_SIGNATURE));
}
#ifdef TO_BE_REMOVED
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() ;
@ -702,6 +708,7 @@ bool RsChatLobbyEventItem::serialise_signed_part(void *data, uint32_t& pktsize)
#endif #endif
return ok ; return ok ;
} }
#endif
void RsChatLobbyUnsubscribeItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsChatLobbyUnsubscribeItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
@ -771,7 +778,7 @@ void RsChatLobbyInviteItem::serial_process(RsItem::SerializeJob j,SerializeConte
{ {
RsTypeSerializer::serial_process<uint64_t>(j,ctx,lobby_id,"lobby_id") ; RsTypeSerializer::serial_process<uint64_t>(j,ctx,lobby_id,"lobby_id") ;
TlvString s(message,TLV_TYPE_STR_NAME) ; RsTypeSerializer::TlvString s(lobby_name,TLV_TYPE_STR_NAME) ;
RsTypeSerializer::serial_process(j,ctx,s,"lobby_name") ; RsTypeSerializer::serial_process(j,ctx,s,"lobby_name") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,lobby_flags.toUInt32(),"lobby_flags") ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,lobby_flags.toUInt32(),"lobby_flags") ;
@ -809,7 +816,7 @@ bool RsChatLobbyInviteItem::serialise(void *data, uint32_t& pktsize)
void RsPrivateChatMsgConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsPrivateChatMsgConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
uint32_t x=0 ; uint32_t x=0 ;
TlvString s(message,TLV_TYPE_STR_MSG) ; RsTypeSerializer::TlvString s(message,TLV_TYPE_STR_MSG) ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,x,"place holder value") ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,x,"place holder value") ;
RsTypeSerializer::serial_process (j,ctx,configPeerId,"configPeerId") ; RsTypeSerializer::serial_process (j,ctx,configPeerId,"configPeerId") ;
@ -956,7 +963,7 @@ bool RsChatStatusItem::serialise(void *data, uint32_t& pktsize)
void RsChatAvatarItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsChatAvatarItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
RsTypeSerializer::serial_process<RsTypeSerializer::BinaryDataBlock>(j,ctx,RsTypeSerializer::BinaryDataBlock(data,image_size)) ; RsTypeSerializer::serial_process<RsTypeSerializer::BinaryDataBlock_ref>(j,ctx,RsTypeSerializer::BinaryDataBlock_ref(image_data,image_size)) ;
} }
#ifdef TO_BE_REMOVED #ifdef TO_BE_REMOVED
@ -1004,7 +1011,7 @@ bool RsChatAvatarItem::serialise(void *data, uint32_t& pktsize)
void RsChatLobbyConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) void RsChatLobbyConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{ {
RsTypeSerializer::serial_process<uint32_t>(j,ctx,lobby_Id,"lobby_Id") ; RsTypeSerializer::serial_process<uint64_t>(j,ctx,lobby_Id,"lobby_Id") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,flags,"flags") ; RsTypeSerializer::serial_process<uint32_t>(j,ctx,flags,"flags") ;
} }
@ -1134,7 +1141,6 @@ RsChatLobbyListItem::RsChatLobbyListItem(void *data,uint32_t)
if (!ok) if (!ok)
std::cerr << "Unknown error while deserializing." << std::endl ; std::cerr << "Unknown error while deserializing." << std::endl ;
} }
#endif
bool RsChatLobbyBouncingObject::deserialise_from_memory(void *data,uint32_t rssize,uint32_t& offset) bool RsChatLobbyBouncingObject::deserialise_from_memory(void *data,uint32_t rssize,uint32_t& offset)
{ {
@ -1172,7 +1178,6 @@ RsChatLobbyEventItem::RsChatLobbyEventItem(void *data,uint32_t /*size*/)
std::cerr << "Unknown error while deserializing." << std::endl ; std::cerr << "Unknown error while deserializing." << std::endl ;
} }
#ifdef TO_BE_REMOVED
RsChatLobbyUnsubscribeItem::RsChatLobbyUnsubscribeItem(void *data,uint32_t /*size*/) RsChatLobbyUnsubscribeItem::RsChatLobbyUnsubscribeItem(void *data,uint32_t /*size*/)
: RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE) : RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE)
{ {
@ -1393,3 +1398,43 @@ RsChatAvatarItem::RsChatAvatarItem(void *data,uint32_t /*size*/)
#endif #endif
uint32_t RsChatLobbyBouncingObject::serial_size_no_signature() const
{
SerializeContext ctx(NULL,0);
ctx.mOffset = 8;
ctx.mFlags = RS_CHAT_SERIALIZER_FLAGS_NO_SIGNATURE ;
const_cast<RsChatLobbyBouncingObject*>(this)->serial_process(RsItem::SERIALIZE,ctx) ;
return ctx.mOffset ;
}
bool RsChatLobbyBouncingObject::serialize_no_signature(uint8_t *data,uint32_t size) const
{
SerializeContext ctx(data,0);
uint32_t tlvsize = serial_size_no_signature() ;
if(tlvsize > size)
throw std::runtime_error("Cannot serialise: not enough room.") ;
if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize))
{
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
return false ;
}
ctx.mOffset = 8;
ctx.mSize = tlvsize;
ctx.mFlags = RS_CHAT_SERIALIZER_FLAGS_NO_SIGNATURE ;
const_cast<RsChatLobbyBouncingObject*>(this)->serial_process(RsItem::SERIALIZE,ctx) ;
if(ctx.mSize != ctx.mOffset)
{
std::cerr << "RsChatSerializer::serialise_item(): ERROR. offset does not match expected size!" << std::endl;
return false ;
}
return true ;
}

View File

@ -137,17 +137,17 @@ public:
virtual RsChatLobbyBouncingObject *duplicate() const = 0 ; virtual RsChatLobbyBouncingObject *duplicate() const = 0 ;
// returns the size in bytes of the data chunk to sign. uint32_t serial_size_no_signature() const ;
bool serialize_no_signature(uint8_t *data,uint32_t size) const ;
virtual uint32_t signed_serial_size() =0;
virtual bool serialise_signed_part(void *data,uint32_t& size) = 0;
protected: protected:
// The functions below handle the serialisation of data that is specific to the bouncing object level. // The functions below handle the serialisation of data that is specific to the bouncing object level.
// They are called by serial_size() and serialise() from children, but should not overload the serial_size() and // They are called by serial_size() and serialise() from children, but should not overload the serial_size() and
// 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.
void serial_process_special(RsItem::SerializeJob j,SerializeContext& ctx,bool include_signature); void serial_process(RsItem::SerializeJob j,SerializeContext& ctx);
virtual uint32_t PacketId() const= 0;
}; };
class RsChatLobbyMsgItem: public RsChatMsgItem, public RsChatLobbyBouncingObject class RsChatLobbyMsgItem: public RsChatMsgItem, public RsChatLobbyBouncingObject
@ -160,10 +160,10 @@ public:
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx) ; void serial_process(RsItem::SerializeJob j,SerializeContext& ctx) ;
virtual uint32_t signed_serial_size() ;
virtual bool serialise_signed_part(void *data,uint32_t& size) ;// Isn't it better that items can serialize themselves ?
ChatLobbyMsgId parent_msg_id ; // Used for threaded chat. ChatLobbyMsgId parent_msg_id ; // Used for threaded chat.
protected:
virtual uint32_t PacketId() const { return RsChatMsgItem::PacketId() ; }
}; };
class RsChatLobbyEventItem: public RsChatItem, public RsChatLobbyBouncingObject class RsChatLobbyEventItem: public RsChatItem, public RsChatLobbyBouncingObject
@ -176,14 +176,14 @@ class RsChatLobbyEventItem: public RsChatItem, public RsChatLobbyBouncingObject
// //
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); void serial_process(RsItem::SerializeJob j,SerializeContext& ctx);
virtual uint32_t signed_serial_size() ;
virtual bool serialise_signed_part(void *data,uint32_t& size) ;
// members. // members.
// //
uint8_t event_type ; // used for defining the type of event. uint8_t event_type ; // used for defining the type of event.
std::string string1; // used for any string std::string string1; // used for any string
uint32_t sendTime; // used to check for old looping messages uint32_t sendTime; // used to check for old looping messages
protected:
virtual uint32_t PacketId() const { return RsChatItem::PacketId() ; }
}; };
class RsChatLobbyListRequestItem: public RsChatItem class RsChatLobbyListRequestItem: public RsChatItem
@ -243,7 +243,7 @@ class RsChatLobbyInviteItem: public RsChatItem
{ {
public: public:
RsChatLobbyInviteItem() :RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE) {} RsChatLobbyInviteItem() :RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE) {}
virtual ~RsChatLobbyInviteItem() {} virtual ~RsChatLobbyInviteItem() {}
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx); void serial_process(RsItem::SerializeJob j,SerializeContext& ctx);
@ -279,6 +279,7 @@ class RsPrivateChatMsgConfigItem: public RsChatItem
std::string message; std::string message;
uint32_t recvTime; uint32_t recvTime;
}; };
class RsChatLobbyConfigItem: public RsChatItem class RsChatLobbyConfigItem: public RsChatItem
{ {
public: public:

View File

@ -8,12 +8,13 @@ class SerializeContext
public: public:
SerializeContext(uint8_t *data,uint32_t size) SerializeContext(uint8_t *data,uint32_t size)
: mData(data),mSize(size),mOffset(0),mOk(true) {} : mData(data),mSize(size),mOffset(0),mOk(true),mFlags(0) {}
unsigned char *mData ; unsigned char *mData ;
uint32_t mSize ; uint32_t mSize ;
uint32_t mOffset ; uint32_t mOffset ;
bool mOk ; bool mOk ;
uint32_t mFlags ;
}; };
@ -57,10 +58,10 @@ class RsTypeSerializer
} }
protected: protected:
template<typename T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member); template<class T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member);
template<typename T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member); template<class T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member);
template<typename T> static uint32_t serial_size(const T& /* member */); template<class T> static uint32_t serial_size(const T& /* member */);
template<typename T> static void print_data(const std::string& name,const T& /* member */); template<class T> static void print_data(const std::string& name,const T& /* member */);
}; };