From: Peter Iannucci Date: Sat, 28 Sep 2013 03:45:34 +0000 (-0400) Subject: Split out marshall code into a new file X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/commitdiff_plain/46fb2b4bbe3a0a8516ab04cfafa895a882c70f86?ds=inline;hp=24bebc0ecf83446c7371eff69042322aab34976a Split out marshall code into a new file --- diff --git a/Makefile b/Makefile index 363cbb3..e2a20d9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ EXTRA_TARGETS ?= all: lock_demo lock_server lock_tester rsm_tester rpc/rpctest $(EXTRA_TARGETS) -rpc/librpc.a: rpc/rpc.o rpc/connection.o rpc/pollmgr.o rpc/thr_pool.o +rpc/librpc.a: rpc/rpc.o rpc/marshall.o rpc/connection.o rpc/pollmgr.o rpc/thr_pool.o rm -f $@ ar cq $@ $^ ranlib rpc/librpc.a diff --git a/rpc/marshall.cc b/rpc/marshall.cc new file mode 100644 index 0000000..5c2b10c --- /dev/null +++ b/rpc/marshall.cc @@ -0,0 +1,114 @@ +#include "types.h" +#include "marshall.h" + +marshall & +operator<<(marshall &m, uint8_t x) { + m.rawbyte(x); + return m; +} + +marshall & +operator<<(marshall &m, uint16_t x) { + x = hton(x); + m.rawbytes((char *)&x, 2); + return m; +} + +marshall & +operator<<(marshall &m, uint32_t x) { + x = hton(x); + m.rawbytes((char *)&x, 4); + return m; +} + +marshall & operator<<(marshall &m, int32_t x) { return m << (uint32_t) x; } +marshall & operator<<(marshall &m, int8_t x) { return m << (uint8_t)x; } +marshall & operator<<(marshall &m, bool x) { return m << (uint8_t)x; } +marshall & operator<<(marshall &m, int16_t x) { return m << (uint16_t)x; } +marshall & operator<<(marshall &m, uint64_t x) { return m << (uint32_t)(x>>32) << (uint32_t)x; } + +marshall & +operator<<(marshall &m, const string &s) { + m << (unsigned int) s.size(); + m.rawbytes(s.data(), s.size()); + return m; +} + +void marshall::pack_req_header(const request_header &h) { + size_t saved_sz = index_; + //leave the first 4-byte empty for channel to fill size of pdu + index_ = sizeof(rpc_sz_t); + *this << h.xid << h.proc << h.clt_nonce << h.srv_nonce << h.xid_rep; + index_ = saved_sz; +} + +void marshall::pack_reply_header(const reply_header &h) { + size_t saved_sz = index_; + //leave the first 4-byte empty for channel to fill size of pdu + index_ = sizeof(rpc_sz_t); + *this << h.xid << h.ret; + index_ = saved_sz; +} + +// take the contents from another unmarshall object +void +unmarshall::take_in(unmarshall &another) +{ + if(buf_) + free(buf_); + another.take_buf(&buf_, &sz_); + index_ = RPC_HEADER_SZ; + ok_ = sz_ >= RPC_HEADER_SZ?true:false; +} + +inline bool +unmarshall::ensure(size_t n) { + if (index_+n > sz_) + ok_ = false; + return ok_; +} + +inline uint8_t +unmarshall::rawbyte() +{ + if (!ensure(1)) + return 0; + return (uint8_t)buf_[index_++]; +} + +void +unmarshall::rawbytes(string &ss, size_t n) +{ + VERIFY(ensure(n)); + ss.assign(buf_+index_, n); + index_ += n; +} + +template +void +unmarshall::rawbytes(T &t) +{ + const size_t n = sizeof(T); + VERIFY(ensure(n)); + memcpy(&t, buf_+index_, n); + t = ntoh(t); + index_ += n; +} + +unmarshall & operator>>(unmarshall &u, bool &x) { x = (bool)u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, uint8_t &x) { x = u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, int8_t &x) { x = (int8_t)u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, uint16_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int16_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, uint32_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int32_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, size_t &x) { uint32_t xx; u.rawbytes(xx); x = xx; return u; } +unmarshall & operator>>(unmarshall &u, uint64_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int64_t &x) { u.rawbytes(x); return u; } + +unmarshall & operator>>(unmarshall &u, string &s) { + unsigned sz = u.grab(); + if(u.ok()) + u.rawbytes(s, sz); + return u; +} diff --git a/rpc/rpc.cc b/rpc/rpc.cc index 9f1d90c..90d9608 100644 --- a/rpc/rpc.cc +++ b/rpc/rpc.cc @@ -676,117 +676,6 @@ int rpcs::rpcbind(unsigned int &r, int) { return 0; } -marshall & -operator<<(marshall &m, uint8_t x) { - m.rawbyte(x); - return m; -} - -marshall & -operator<<(marshall &m, uint16_t x) { - x = hton(x); - m.rawbytes((char *)&x, 2); - return m; -} - -marshall & -operator<<(marshall &m, uint32_t x) { - x = hton(x); - m.rawbytes((char *)&x, 4); - return m; -} - -marshall & operator<<(marshall &m, int32_t x) { return m << (uint32_t) x; } -marshall & operator<<(marshall &m, int8_t x) { return m << (uint8_t)x; } -marshall & operator<<(marshall &m, bool x) { return m << (uint8_t)x; } -marshall & operator<<(marshall &m, int16_t x) { return m << (uint16_t)x; } -marshall & operator<<(marshall &m, uint64_t x) { return m << (uint32_t)(x>>32) << (uint32_t)x; } - -marshall & -operator<<(marshall &m, const string &s) { - m << (unsigned int) s.size(); - m.rawbytes(s.data(), s.size()); - return m; -} - -void marshall::pack_req_header(const request_header &h) { - size_t saved_sz = index_; - //leave the first 4-byte empty for channel to fill size of pdu - index_ = sizeof(rpc_sz_t); - *this << h.xid << h.proc << h.clt_nonce << h.srv_nonce << h.xid_rep; - index_ = saved_sz; -} - -void marshall::pack_reply_header(const reply_header &h) { - size_t saved_sz = index_; - //leave the first 4-byte empty for channel to fill size of pdu - index_ = sizeof(rpc_sz_t); - *this << h.xid << h.ret; - index_ = saved_sz; -} - -// take the contents from another unmarshall object -void -unmarshall::take_in(unmarshall &another) -{ - if(buf_) - free(buf_); - another.take_buf(&buf_, &sz_); - index_ = RPC_HEADER_SZ; - ok_ = sz_ >= RPC_HEADER_SZ?true:false; -} - -inline bool -unmarshall::ensure(size_t n) { - if (index_+n > sz_) - ok_ = false; - return ok_; -} - -inline uint8_t -unmarshall::rawbyte() -{ - if (!ensure(1)) - return 0; - return (uint8_t)buf_[index_++]; -} - -void -unmarshall::rawbytes(string &ss, size_t n) -{ - VERIFY(ensure(n)); - ss.assign(buf_+index_, n); - index_ += n; -} - -template -void -unmarshall::rawbytes(T &t) -{ - const size_t n = sizeof(T); - VERIFY(ensure(n)); - memcpy(&t, buf_+index_, n); - t = ntoh(t); - index_ += n; -} - -unmarshall & operator>>(unmarshall &u, bool &x) { x = (bool)u.rawbyte(); return u; } -unmarshall & operator>>(unmarshall &u, uint8_t &x) { x = u.rawbyte(); return u; } -unmarshall & operator>>(unmarshall &u, int8_t &x) { x = (int8_t)u.rawbyte(); return u; } -unmarshall & operator>>(unmarshall &u, uint16_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, int16_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, uint32_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, int32_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, size_t &x) { uint32_t xx; u.rawbytes(xx); x = xx; return u; } -unmarshall & operator>>(unmarshall &u, uint64_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, int64_t &x) { u.rawbytes(x); return u; } -unmarshall & operator>>(unmarshall &u, string &s) { - unsigned sz = u.grab(); - if(u.ok()) - u.rawbytes(s, sz); - return u; -} - bool operator<(const sockaddr_in &a, const sockaddr_in &b){ return ((a.sin_addr.s_addr < b.sin_addr.s_addr) || ((a.sin_addr.s_addr == b.sin_addr.s_addr) &&