lichao
2021-06-03 8967e7f2f8b94dc032135707e16c8a9f233d0db6
src/msg.cpp
@@ -17,89 +17,38 @@
 */
#include "msg.h"
#include "bh_util.h"
#include "defs.h"
#include "shm_socket.h"
namespace bhome_msg
{
/*TODO change msg format, header has proc info;
reply has errer msg
    center accept request and route.;
//*/
const uint32_t kMsgTag = 0xf1e2d3c4;
void *MsgI::Pack(SharedMemory &shm,
                 const uint32_t head_len, const ToArray &headToArray,
                 const uint32_t body_len, const ToArray &bodyToArray)
int ShmMsg::Release()
{
   void *addr = shm.Alloc(sizeof(head_len) + head_len + sizeof(body_len) + body_len);
   if (addr) {
      auto p = static_cast<char *>(addr);
      auto Pack1 = [&p](auto len, auto &writer) {
         Put32(p, len);
         p += sizeof(len);
         writer(p, len);
         p += len;
      };
      Pack1(head_len, headToArray);
      Pack1(body_len, bodyToArray);
   if (!valid()) {
      return 0;
   }
   return addr;
}
bool MsgI::ParseHead(BHMsgHead &head) const
{
   auto p = static_cast<char *>(ptr_.get());
   assert(p);
   uint32_t msg_size = Get32(p);
   p += 4;
   return head.ParseFromArray(p, msg_size);
}
// with ref count;
bool MsgI::MakeRC(SharedMemory &shm, void *p)
{
   if (!p) {
      return false;
   }
   RefCount *rc = shm.New<RefCount>();
   if (!rc) {
      shm.Dealloc(p);
      return false;
   }
   MsgI(p, rc).swap(*this);
   return true;
}
bool MsgI::Make(SharedMemory &shm, void *p)
{
   if (!p) {
      return false;
   }
   MsgI(p, 0).swap(*this);
   return true;
}
bool MsgI::EnableRefCount(SharedMemory &shm)
{
   if (!IsCounted()) {
      count_ = shm.New<RefCount>();
   }
   return IsCounted();
}
int MsgI::Release(SharedMemory &shm)
{
   if (IsCounted()) {
      const int n = count_->Dec();
      if (n != 0) {
         return n;
   auto n = meta()->count_.Dec();
   if (n == 0) {
      if (meta()->managed_) {
         int64_t free_cmd = (id() << 4) | EncodeCmd(eCmdFree);
         DefaultSender(shm()).Send(BHTopicCenterAddress(shm()), free_cmd);
      } else {
         Free();
      }
   } else if (n < 0) {
      // ns_log::GetTrace();
      LOG_FATAL() << "double release msg.";
      throw std::runtime_error("double release msg.");
   }
   // free data
   shm.Dealloc(ptr_);
   ptr_ = 0;
   shm.Delete(count_);
   count_ = 0;
   return 0;
   return n;
}
void ShmMsg::Free()
{
   assert(valid());
   shm().Dealloc(meta());
   offset_ = 0;
   assert(!valid());
}
} // namespace bhome_msg