bool LLBC_Array::DeSerializeInl(LLBC_Stream &s, bool extended) { if (UNLIKELY(!_objFactory)) { return false; } Clear(); LLBC_STREAM_BEGIN_READ(s, bool, false); uint64 size = 0; LLBC_STREAM_READ(size); for (uint64 i = 0; i < size; i++) { LLBC_Object *o = _objFactory->CreateObject(); if (!(!extended ? s.Read(*o) : s.ReadEx(*o))) { o->Release(); Clear(); return false; } PushBack(o); o->Release(); } LLBC_STREAM_END_READ_RET(true); }
bool LLBC_Dictionary::DeSerializeInl(LLBC_Stream &s, bool extended) { if (!_objFactory) { return false; } this->Clear(); LLBC_STREAM_BEGIN_READ(s, bool, false); uint64 bucketSize = 0; LLBC_STREAM_READ(bucketSize); this->SetHashBucketSize(static_cast<size_type>(bucketSize)); uint64 size = 0; LLBC_STREAM_READ(size); for (uint64 i = 0; i < size; i++) { uint8 intKeyFlag = 0; LLBC_STREAM_READ(intKeyFlag); if (intKeyFlag) { int intKey = 0; LLBC_STREAM_READ(intKey); LLBC_Object *o = _objFactory->CreateObject(); if (!(!extended ? s.Read(*o) : s.ReadEx(*o))) { o->Release(); return false; } this->Insert(intKey, o); o->Release(); } else { LLBC_String strKey; LLBC_STREAM_READ(strKey); LLBC_Object *o = _objFactory->CreateObject(); if (!(!extended ? s.Read(*o) : s.ReadEx(*o))) { o->Release(); return false; } this->Insert(strKey, o); o->Release(); } } LLBC_STREAM_END_READ_RET(true); }
int pyllbc_Service::Send(int sessionId, int opcode, PyObject *data, int status, PyObject *parts) { // Started check. if (UNLIKELY(!this->IsStarted())) { pyllbc_SetError("service not start"); return LLBC_RTN_FAILED; } // Build parts, if exists. LLBC_PacketHeaderParts *cLayerParts = NULL; if (parts && _llbcSvcType != LLBC_IService::Raw) { if (!(cLayerParts = this->BuildCLayerParts(parts))) return LLBC_RTN_FAILED; } // Serialize python layer 'data' object to stream. LLBC_Stream stream; const int ret = this->SerializePyObj2Stream(data, stream); if (UNLIKELY(ret != LLBC_RTN_OK)) { LLBC_XDelete(cLayerParts); return LLBC_RTN_FAILED; } // Build packet & send. LLBC_Packet *packet = LLBC_New(LLBC_Packet); packet->Write(stream.GetBuf(), stream.GetPos()); packet->SetSessionId(sessionId); if (_llbcSvcType != LLBC_IService::Raw) { packet->SetOpcode(opcode); packet->SetStatus(status); if (cLayerParts) { cLayerParts->SetToPacket(*packet); LLBC_Delete(cLayerParts); } } if (UNLIKELY(_llbcSvc->Send(packet) == LLBC_RTN_FAILED)) { pyllbc_TransferLLBCError(__FILE__, __LINE__); return LLBC_RTN_FAILED; } return LLBC_RTN_OK; }
void LLBC_Variant::Serialize(LLBC_Stream &stream) const { stream.Write(_holder.type); if (IsRaw()) { stream.Write(_holder.raw.uint64Val); } else if (IsStr()) { stream.Write(_holder.str); } else if (IsDict()) { if (!_holder.dict) { stream.Write(static_cast<uint32>(0)); } else { stream.Write(static_cast<uint32>(_holder.dict->size())); for (DictConstIter it = _holder.dict->begin(); it != _holder.dict->end(); it ++) { stream.Write(it->first); stream.Write(it->second); } } } }
bool LLBC_Variant::DeSerialize(LLBC_Stream &stream) { BecomeNil(); if (!stream.Read(_holder.type)) { return false; } if (IsNil()) { return true; } if (IsRaw()) { if (!stream.Read(_holder.raw.uint64Val)) { _holder.type = LLBC_VariantType::VT_NIL; return false; } } else if (IsStr()) { if (!stream.Read(_holder.str)) { _holder.type = LLBC_VariantType::VT_NIL; return false; } } else if (IsDict()) { uint32 count = 0; if (!stream.Read(count)) { _holder.type = LLBC_VariantType::VT_NIL; return false; } if (count == 0) { return true; } _holder.dict = new Dict; for (uint32 i = 0; i < count; i ++) { LLBC_Variant key; LLBC_Variant val; if (!stream.Read(key) || !stream.Read(val)) { LLBC_XDelete(_holder.dict); _holder.type = LLBC_VariantType::VT_NIL; return false; } _holder.dict->insert(std::make_pair(key, val)); } return true; } return false; }