lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
src/msg.h
@@ -74,12 +74,13 @@
      RefCount count_;
      const uint32_t tag_ = kMsgTag;
      const uint32_t size_ = 0;
      const uint32_t capacity_ = 0;
      const int64_t id_ = 0;
      std::atomic<int64_t> timestamp_;
      bool managed_ = false;
      uint32_t size_ = 0;
      Meta(uint32_t size) :
          size_(size), id_(NewId()), timestamp_(NowSec()) {}
          capacity_(size), id_(NewId()), timestamp_(NowSec()) {}
   };
   OffsetType offset_;
   static void *Alloc(const size_t size)
@@ -111,6 +112,7 @@
         };
         Pack1(head_len, [&](void *p, int len) { head.SerializeToArray(p, len); });
         Pack1(body_len, [&](void *p, int len) { body.SerializeToArray(p, len); });
         meta()->size_ = 4 + head_len + 4 + body_len;
      }
      return addr;
   }
@@ -120,6 +122,7 @@
      void *addr = get();
      if (addr) {
         memcpy(addr, content.data(), content.size());
         meta()->size_ = content.size();
      }
      return addr;
   }
@@ -174,11 +177,11 @@
      uint32_t head_len = head.ByteSizeLong();
      uint32_t body_len = body.ByteSizeLong();
      uint32_t size = sizeof(head_len) + head_len + sizeof(body_len) + body_len;
      return valid() && (meta()->size_ >= size) && Pack(head, head_len, body, body_len);
      return valid() && (meta()->capacity_ >= size) && Pack(head, head_len, body, body_len);
   }
   inline bool Make(const std::string &content) { return Make(content.size()) && Pack(content); }
   inline bool Fill(const std::string &content) { return valid() && (meta()->size_ >= content.size()) && Pack(content); }
   inline bool Fill(const std::string &content) { return valid() && (meta()->capacity_ >= content.size()) && Pack(content); }
   inline bool Make(const size_t size) { return Make(Alloc(size)); }
@@ -209,6 +212,11 @@
      p += 4;
      return head.ParseFromArray(p, msg_size);
   }
   std::string content() const
   {
      auto p = get<char>();
      return p ? std::string(p, meta()->size_) : std::string();
   }
   std::string body() const
   {
      auto p = get<char>();