X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5fd8cc8409d0efadc07dfe8d6774ad9ff477663d..130f2d53438eb6193accb445aca52fa8e2fe4158:/rpc/marshall.h diff --git a/rpc/marshall.h b/rpc/marshall.h index e0370d1..27cebbb 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -11,7 +11,6 @@ #include #include #include "lang/verify.h" -#include "lang/algorithm.h" struct req_header { req_header(int x=0, int p=0, int c = 0, int s = 0, int xi = 0): @@ -29,18 +28,14 @@ struct reply_header { int ret; }; -typedef uint64_t rpc_checksum_t; typedef int rpc_sz_t; -enum { - //size of initial buffer allocation - DEFAULT_RPC_SZ = 1024, -#if RPC_CHECKSUMMING - //size of rpc_header includes a 4-byte int to be filled by tcpchan and uint64_t checksum - RPC_HEADER_SZ = static_max::value + sizeof(rpc_sz_t) + sizeof(rpc_checksum_t) -#else - RPC_HEADER_SZ = static_max::value + sizeof(rpc_sz_t) -#endif +//size of initial buffer allocation +#define DEFAULT_RPC_SZ 1024 +#define RPC_HEADER_SZ (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t)) + +struct pass { + template inline pass(Args&&...) {} }; class marshall { @@ -57,6 +52,10 @@ class marshall { _ind = RPC_HEADER_SZ; } + template marshall(const Args&... args) : marshall() { + (void)pass{(*this << args)...}; + } + ~marshall() { if (_buf) free(_buf); @@ -84,9 +83,6 @@ class marshall { int saved_sz = _ind; //leave the first 4-byte empty for channel to fill size of pdu _ind = sizeof(rpc_sz_t); -#if RPC_CHECKSUMMING - _ind += sizeof(rpc_checksum_t); -#endif pack(h.xid); pack(h.proc); pack((int)h.clt_nonce); @@ -99,9 +95,6 @@ class marshall { int saved_sz = _ind; //leave the first 4-byte empty for channel to fill size of pdu _ind = sizeof(rpc_sz_t); -#if RPC_CHECKSUMMING - _ind += sizeof(rpc_checksum_t); -#endif pack(h.xid); pack(h.ret); _ind = saved_sz; @@ -115,6 +108,7 @@ class marshall { return; } }; + marshall& operator<<(marshall &, bool); marshall& operator<<(marshall &, unsigned int); marshall& operator<<(marshall &, int); @@ -125,6 +119,40 @@ marshall& operator<<(marshall &, short); marshall& operator<<(marshall &, unsigned long long); marshall& operator<<(marshall &, const std::string &); +template marshall & +operator<<(marshall &m, std::vector v) +{ + m << (unsigned int) v.size(); + for(unsigned i = 0; i < v.size(); i++) + m << v[i]; + return m; +} + +template marshall & +operator<<(marshall &m, const std::map &d) { + typename std::map::const_iterator i; + + m << (unsigned int) d.size(); + + for (i = d.begin(); i != d.end(); i++) { + m << i->first << i->second; + } + return m; +} + +template marshall & +operator<<(marshall &m, const std::list &d) { + m << std::vector(d.begin(), d.end()); + return m; +} + +template marshall & +operator<<(marshall &m, const std::pair &d) { + m << d.first; + m << d.second; + return m; +} + class unmarshall { private: char *_buf; @@ -175,9 +203,6 @@ class unmarshall { void unpack_req_header(req_header *h) { //the first 4-byte is for channel to fill size of pdu _ind = sizeof(rpc_sz_t); -#if RPC_CHECKSUMMING - _ind += sizeof(rpc_checksum_t); -#endif unpack(&h->xid); unpack(&h->proc); unpack((int *)&h->clt_nonce); @@ -189,13 +214,19 @@ class unmarshall { void unpack_reply_header(reply_header *h) { //the first 4-byte is for channel to fill size of pdu _ind = sizeof(rpc_sz_t); -#if RPC_CHECKSUMMING - _ind += sizeof(rpc_checksum_t); -#endif unpack(&h->xid); unpack(&h->ret); _ind = RPC_HEADER_SZ; } + + template + void iterate(OutputIterator i, int n) { + while (n--) { + typename OutputIterator::value_type t; + *this >> t; + *i++ = t; + } + } }; unmarshall& operator>>(unmarshall &, bool &); @@ -208,54 +239,50 @@ unmarshall& operator>>(unmarshall &, int &); unmarshall& operator>>(unmarshall &, unsigned long long &); unmarshall& operator>>(unmarshall &, std::string &); -template marshall & -operator<<(marshall &m, std::vector v) -{ - m << (unsigned int) v.size(); - for(unsigned i = 0; i < v.size(); i++) - m << v[i]; - return m; -} - template unmarshall & operator>>(unmarshall &u, std::vector &v) { unsigned n; u >> n; - for(unsigned i = 0; i < n; i++){ - C z; - u >> z; - v.push_back(z); - } + v.clear(); + while (n--) { + C c; + u >> c; + v.push_back(c); + } return u; } -template marshall & -operator<<(marshall &m, const std::map &d) { - typename std::map::const_iterator i; - - m << (unsigned int) d.size(); - - for (i = d.begin(); i != d.end(); i++) { - m << i->first << i->second; - } - return m; -} - template unmarshall & operator>>(unmarshall &u, std::map &d) { - unsigned int n; + unsigned n; u >> n; - d.clear(); - - for (unsigned int lcv = 0; lcv < n; lcv++) { - A a; - B b; - u >> a >> b; - d[a] = b; - } + while (n--) { + A a; + B b; + u >> a >> b; + d[a] = b; + } return u; } +template unmarshall & +operator>>(unmarshall &u, std::list &l) { + unsigned n; + u >> n; + l.clear(); + while (n--) { + C c; + u >> c; + l.push_back(c); + } + return u; +} + +template unmarshall & +operator>>(unmarshall &u, std::pair &d) { + return u >> d.first >> d.second; +} + #endif