/* * ===================================================================================== * * Filename: msg.cpp * * Description: * * Version: 1.0 * Created: 2021年03月24日 16时48分42秒 * Revision: none * Compiler: gcc * * Author: Li Chao (), * Organization: * * ===================================================================================== */ #include "msg.h" namespace bhome_shm { bool MsgMetaV1::Parse(const void *p) { assert(p); *this = *static_cast(p); return tag_ == kMsgMetaTag; } void MsgMetaV1::Pack(void *p) { *static_cast(p) = *this; } bool Msg::Build(SharedMemory &shm, const MQId &src_id, const void *data, const size_t size, const bool refcount) { if (!data || !size) { return false; } void *p = shm.Alloc(sizeof(MsgMetaV1) + size); if (!p) { return false; } RefCount *rc = 0; if (refcount) { rc = shm.New(); if (!rc) { shm.Dealloc(p); return false; } } MsgMetaV1 meta; meta.data_size_ = size; meta.src_id_ = src_id; meta.Pack(p); memcpy(static_cast(p) + sizeof(meta), data, size); Msg(p, rc).swap(*this); return true; } void Msg::FreeFrom(SharedMemory &shm) { shm.Dealloc(ptr_); shm.Delete(count_); } } // namespace bhome_shm