X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/a4175b2e216a20b86cc872dea8a08005c60617a5..dfe8486473094c0769fd1922329c3f0dfd8f43c0:/rpc/marshall.h diff --git a/rpc/marshall.h b/rpc/marshall.h index 644a220..fcb5bab 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -28,18 +28,11 @@ struct reply_header { int ret; }; -typedef uint64_t rpc_checksum_t; typedef int rpc_sz_t; //size of initial buffer allocation #define DEFAULT_RPC_SZ 1024 -#define RPC_HEADER_SZ_NO_CHECKSUM (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t)) -#if RPC_CHECKSUMMING -//size of rpc_header includes a 4-byte int to be filled by tcpchan and uint64_t checksum -#define RPC_HEADER_SZ (RPC_HEADER_SZ_NO_CHECKSUM + sizeof(rpc_checksum_t)) -#else -#define RPC_HEADER_SZ (RPC_HEADER_SZ_NO_CHECKSUM) -#endif +#define RPC_HEADER_SZ (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t)) class marshall { private: @@ -82,9 +75,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); @@ -97,9 +87,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; @@ -113,6 +100,7 @@ class marshall { return; } }; + marshall& operator<<(marshall &, bool); marshall& operator<<(marshall &, unsigned int); marshall& operator<<(marshall &, int); @@ -123,6 +111,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; @@ -173,9 +195,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); @@ -187,9 +206,6 @@ 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; @@ -206,54 +222,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