lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
src/msg.h
@@ -36,7 +36,10 @@
class ShmMsg : private StaticDataRef<SharedMemory, ShmMsg>
{
public:
   static inline SharedMemory &shm() { return GetData(); }
private:
   static ShmSocket &Sender();
   // store ref count, msgs shareing the same data should also hold a pointer of the same RefCount object.
@@ -71,11 +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)
@@ -107,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;
   }
@@ -116,6 +122,7 @@
      void *addr = get();
      if (addr) {
         memcpy(addr, content.data(), content.size());
         meta()->size_ = content.size();
      }
      return addr;
   }
@@ -151,6 +158,10 @@
   int AddRef() const { return valid() ? meta()->count_.Inc() : 1; }
   int Release();
   void Free();
   void reset_managed(const bool val) const
   {
      if (valid()) { meta()->managed_ = val; }
   }
   template <class Body>
   inline bool Make(const BHMsgHead &head, const Body &body)
@@ -166,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)); }
@@ -201,6 +212,22 @@
      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>();
      assert(p);
      uint32_t size = Get32(p);
      p += 4;
      p += size;
      size = Get32(p);
      p += 4;
      return std::string(p, size);
   }
   template <class Body>
   bool ParseBody(Body &body) const
   {
@@ -224,7 +251,7 @@
constexpr inline bool IsCmd(int64_t msg) { return (msg & 1) != 0; }
// int64_t pack format: cmd data ,3bit cmd, 1bit flag.
enum MsgCmd {
   eCmdNodeInit = 0,      // upto 59bit ssn id
   eCmdNodeInit = 0,      // upto 56bit ssn id
   eCmdNodeInitReply = 1, // 31bit proc index,
   eCmdAllocRequest0 = 2, // 8bit size, 4bit socket index, 16bit proc index, 28bit id
   eCmdAllocReply0 = 3,   // 31bit ptr, 28bit id,