X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/a5f10a497bebfc680bf418193f1fd9f1ad7cc417..f0dcb6b97d6d40f67698d1f71ac26970f1776f82:/rpc/marshall.h diff --git a/rpc/marshall.h b/rpc/marshall.h index 69b10df..7b716e2 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -4,21 +4,18 @@ #include "types.h" #include "rpc_protocol.h" -class marshall; -class unmarshall; - // // Marshall and unmarshall objects // class marshall { private: - string buf_ = string(DEFAULT_RPC_SZ, 0); // Raw bytes buffer - size_t index_ = RPC_HEADER_SZ; // Read/write head position + string buf_ = string(rpc_protocol::DEFAULT_RPC_SZ, 0); // Raw bytes buffer + size_t index_ = rpc_protocol::RPC_HEADER_SZ; // Read/write head position public: template - marshall(const Args&... args) { + marshall(const Args & ... args) { (void)pass{(*this << args)...}; } @@ -32,14 +29,14 @@ class marshall { // with header inline operator string() const { return buf_.substr(0,index_); } // without header - inline string content() const { return buf_.substr(RPC_HEADER_SZ,index_-RPC_HEADER_SZ); } + inline string content() const { return buf_.substr(rpc_protocol::RPC_HEADER_SZ,index_-rpc_protocol::RPC_HEADER_SZ); } // letting S be a defaulted template parameter forces the compiler to - // delay looking up operator<<(marshall&, rpc_sz_t) until we define it + // delay looking up operator<<(marshall &, rpc_sz_t) until we define it // (i.e. we define an operator for marshalling uint32_t) - template inline void + template inline void pack_header(const T & h) { - VERIFY(sizeof(T)+sizeof(S) <= RPC_HEADER_SZ); + VERIFY(sizeof(T)+sizeof(S) <= rpc_protocol::RPC_HEADER_SZ); size_t saved_sz = index_; index_ = 0; *this << (S)(saved_sz - sizeof(S)) << (T)h; @@ -54,11 +51,13 @@ class unmarshall { bool ok_ = false; public: - unmarshall(const string &s, bool has_header) - : buf_(s),index_(RPC_HEADER_SZ) { + template + unmarshall(const string & s, bool has_header, Args && ... args) + : buf_(s),index_(rpc_protocol::RPC_HEADER_SZ) { if (!has_header) - buf_.insert(0, RPC_HEADER_SZ, 0); - ok_ = (buf_.size() >= RPC_HEADER_SZ); + buf_.insert(0, rpc_protocol::RPC_HEADER_SZ, 0); + ok_ = (buf_.size() >= rpc_protocol::RPC_HEADER_SZ); + (void)pass{(*this >> args)...}; } bool ok() const { return ok_; } @@ -74,11 +73,11 @@ class unmarshall { template inline void unpack_header(T & h) { - VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ); + VERIFY(sizeof(T)+sizeof(rpc_protocol::rpc_sz_t) <= rpc_protocol::RPC_HEADER_SZ); // first 4 bytes hold length field - index_ = sizeof(rpc_sz_t); + index_ = sizeof(rpc_protocol::rpc_sz_t); *this >> h; - index_ = RPC_HEADER_SZ; + index_ = rpc_protocol::RPC_HEADER_SZ; } template inline T _grab() { T t; *this >> t; return t; } @@ -89,8 +88,8 @@ class unmarshall { // #define MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _d_) \ -inline marshall & operator<<(marshall &m, _c_ x) { _d_ y = hton((_d_)x); m.rawbytes(&y, sizeof(_d_)); return m; } \ -inline unmarshall & operator>>(unmarshall &u, _c_ &x) { _d_ y; u.rawbytes(&y, sizeof(_d_)); x = (_c_)ntoh(y); return u; } +inline marshall & operator<<(marshall & m, _c_ x) { _d_ y = hton((_d_)x); m.rawbytes(&y, sizeof(_d_)); return m; } \ +inline unmarshall & operator>>(unmarshall & u, _c_ & x) { _d_ y; u.rawbytes(&y, sizeof(_d_)); x = (_c_)ntoh(y); return u; } #define MARSHALL_RAW_NETWORK_ORDER(_c_) MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _c_) @@ -133,13 +132,13 @@ operator<<(marshall & m, tuple && t) { } template inline unmarshall & -tuple_unmarshall_imp(unmarshall & u, tuple t, tuple_indices) { +tuple_unmarshall_imp(unmarshall & u, tuple t, tuple_indices) { (void)pass{(u >> get(t))...}; return u; } template unmarshall & -operator>>(unmarshall & u, tuple && t) { +operator>>(unmarshall & u, tuple && t) { using Indices = typename make_tuple_indices::type; return tuple_unmarshall_imp(u, t, Indices()); } @@ -149,13 +148,13 @@ operator>>(unmarshall & u, tuple && t) { // // Implements struct marshalling via tuple marshalling of members. -#define MARSHALLABLE(_c_) \ -inline unmarshall & operator>>(unmarshall &u, _c_ &a) { return u >> a._tuple_(); } \ -inline marshall & operator<<(marshall &m, const _c_ a) { return m << a._tuple_(); } +#define MARSHALLABLE_STRUCT(_c_) \ +inline unmarshall & operator>>(unmarshall & u, _c_ & a) { return u >> a._tuple_(); } \ +inline marshall & operator<<(marshall & m, const _c_ a) { return m << a._tuple_(); } // our first two marshallable structs... -MARSHALLABLE(request_header) -MARSHALLABLE(reply_header) +MARSHALLABLE_STRUCT(rpc_protocol::request_header) +MARSHALLABLE_STRUCT(rpc_protocol::reply_header) // // Marshalling for STL containers @@ -164,9 +163,9 @@ MARSHALLABLE(reply_header) // this overload is visible for type A only if A::cbegin and A::cend exist template inline typename enable_if::value, marshall>::type & -operator<<(marshall &m, const A &x) { +operator<<(marshall & m, const A & x) { m << (unsigned int)x.size(); - for (const auto &a : x) + for (const auto & a : x) m << a; return m; } @@ -174,7 +173,7 @@ operator<<(marshall &m, const A &x) { // visible for type A if A::emplace_back(a) makes sense template inline typename enable_if::value, unmarshall>::type & -operator>>(unmarshall &u, A &x) { +operator>>(unmarshall & u, A & x) { unsigned n = u._grab(); x.clear(); while (n--) @@ -184,18 +183,18 @@ operator>>(unmarshall &u, A &x) { // std::pair template inline marshall & -operator<<(marshall &m, const pair &d) { +operator<<(marshall & m, const pair & d) { return m << d.first << d.second; } template inline unmarshall & -operator>>(unmarshall &u, pair &d) { +operator>>(unmarshall & u, pair & d) { return u >> d.first >> d.second; } // std::map template inline unmarshall & -operator>>(unmarshall &u, map &x) { +operator>>(unmarshall & u, map & x) { unsigned n = u._grab(); x.clear(); while (n--) @@ -204,13 +203,13 @@ operator>>(unmarshall &u, map &x) { } // std::string -inline marshall & operator<<(marshall &m, const string &s) { +inline marshall & operator<<(marshall & m, const string & s) { m << (uint32_t)s.size(); m.rawbytes(s.data(), s.size()); return m; } -inline unmarshall & operator>>(unmarshall &u, string &s) { +inline unmarshall & operator>>(unmarshall & u, string & s) { uint32_t sz = u._grab(); if (u.ok()) { s.resize(sz); @@ -224,12 +223,12 @@ inline unmarshall & operator>>(unmarshall &u, string &s) { // template typename enable_if::value, marshall>::type & -operator<<(marshall &m, E e) { +operator<<(marshall & m, E e) { return m << from_enum(e); } template typename enable_if::value, unmarshall>::type & -operator>>(unmarshall &u, E &e) { +operator>>(unmarshall & u, E & e) { e = to_enum(u._grab>()); return u; } @@ -238,11 +237,11 @@ operator>>(unmarshall &u, E &e) { // Recursive marshalling // -inline marshall & operator<<(marshall &m, marshall &n) { +inline marshall & operator<<(marshall & m, marshall & n) { return m << n.content(); } -inline unmarshall & operator>>(unmarshall &u, unmarshall &v) { +inline unmarshall & operator>>(unmarshall & u, unmarshall & v) { v = unmarshall(u._grab(), false); return u; }