X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/4b9798f44ae94deabf87dd534337b55259272950..eeab3e6cade87c1fe0a5f3d93522e12ccb9ec2ab:/rpc/marshall.h?ds=sidebyside diff --git a/rpc/marshall.h b/rpc/marshall.h index 448240b..7a85d6b 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -12,101 +12,105 @@ #include #include "lang/verify.h" -struct req_header { - req_header(int x=0, int p=0, int c = 0, int s = 0, int xi = 0): - xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {} - int xid; - int proc; - unsigned int clt_nonce; - unsigned int srv_nonce; - int xid_rep; +struct request_header { + request_header(int x=0, int p=0, int c=0, int s=0, int xi=0) : + xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {} + int xid; + int proc; + unsigned int clt_nonce; + unsigned int srv_nonce; + int xid_rep; + request_header hton() const { + return { + htonl(xid), htonl(proc), htonl(clt_nonce), htonl(srv_nonce), htonl(xid_rep) + }; + } }; struct reply_header { - reply_header(int x=0, int r=0): xid(x), ret(r) {} - int xid; - int ret; + reply_header(int x=0, int r=0): xid(x), ret(r) {} + int xid; + int ret; + reply_header hton() const { + return { + htonl(xid), htonl(ret) + }; + } }; typedef int rpc_sz_t; -//size of initial buffer allocation +//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&&...) {} -}; +#define RPC_HEADER_SZ (std::max(sizeof(request_header), sizeof(reply_header)) + sizeof(rpc_sz_t)) class marshall { - private: - char *_buf; // Base of the raw bytes buffer (dynamically readjusted) - int _capa; // Capacity of the buffer - int _ind; // Read/write head position - - public: - marshall() { - _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ); - VERIFY(_buf); - _capa = DEFAULT_RPC_SZ; - _ind = RPC_HEADER_SZ; - } - - template marshall(const Args&... args) : marshall() { + private: + char *buf_; // Base of the raw bytes buffer (dynamically readjusted) + size_t capacity_; // Capacity of the buffer + size_t index_; // Read/write head position + + inline void reserve(size_t n) { + if((index_+n) > capacity_){ + capacity_ += std::max(capacity_, n); + VERIFY (buf_ != NULL); + buf_ = (char *)realloc(buf_, capacity_); + VERIFY(buf_); + } + } + public: + struct pass { template inline pass(Args&&...) {} }; + + template + + marshall(const Args&... args) { + buf_ = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ); + VERIFY(buf_); + capacity_ = DEFAULT_RPC_SZ; + index_ = RPC_HEADER_SZ; (void)pass{(*this << args)...}; } - ~marshall() { - if (_buf) - free(_buf); - } - - int size() { return _ind;} - char *cstr() { return _buf;} - - void rawbyte(unsigned char); - void rawbytes(const char *, int); - - // Return the current content (excluding header) as a string - std::string get_content() { - return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ); - } - - // Return the current content (excluding header) as a string - std::string str() { - return get_content(); - } - - void pack(int i); - - void pack_req_header(const req_header &h) { - int saved_sz = _ind; - //leave the first 4-byte empty for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - pack(h.xid); - pack(h.proc); - pack((int)h.clt_nonce); - pack((int)h.srv_nonce); - pack(h.xid_rep); - _ind = saved_sz; - } - - void pack_reply_header(const reply_header &h) { - int saved_sz = _ind; - //leave the first 4-byte empty for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - pack(h.xid); - pack(h.ret); - _ind = saved_sz; - } - - void take_buf(char **b, int *s) { - *b = _buf; - *s = _ind; - _buf = NULL; - _ind = 0; - return; - } + ~marshall() { + if (buf_) + free(buf_); + } + + int size() { return index_;} + char *cstr() { return buf_;} + const char *cstr() const { return buf_;} + + void rawbyte(unsigned char x) { + reserve(1); + buf_[index_++] = x; + } + + void rawbytes(const char *p, int n) { + reserve(n); + memcpy(buf_+index_, p, n); + index_ += n; + } + + // Return the current content (excluding header) as a string + std::string get_content() { + return std::string(buf_+RPC_HEADER_SZ,index_-RPC_HEADER_SZ); + } + + // Return the current content (excluding header) as a string + std::string str() { + return get_content(); + } + + void pack_req_header(const request_header &h); + void pack_reply_header(const reply_header &h); + + void take_buf(char **b, int *s) { + *b = buf_; + *s = index_; + buf_ = NULL; + index_ = 0; + return; + } }; marshall& operator<<(marshall &, bool); @@ -119,30 +123,11 @@ 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()); +operator<<(marshall &m, const A &x) { + m << (unsigned int) x.size(); + for (const auto &a : x) + m << a; return m; } @@ -154,70 +139,73 @@ operator<<(marshall &m, const std::pair &d) { } class unmarshall { - private: - char *_buf; - int _sz; - int _ind; - bool _ok; - public: - unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {} - unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {} - unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false) - { - //take the content which does not exclude a RPC header from a string - take_content(s); - } - ~unmarshall() { - if (_buf) free(_buf); - } - - //take contents from another unmarshall object - void take_in(unmarshall &another); - - //take the content which does not exclude a RPC header from a string - void take_content(const std::string &s) { - _sz = s.size()+RPC_HEADER_SZ; - _buf = (char *)realloc(_buf,_sz); - VERIFY(_buf); - _ind = RPC_HEADER_SZ; - memcpy(_buf+_ind, s.data(), s.size()); - _ok = true; - } - - bool ok() { return _ok; } - char *cstr() { return _buf;} - bool okdone(); - unsigned int rawbyte(); - void rawbytes(std::string &s, unsigned int n); - - int ind() { return _ind;} - int size() { return _sz;} - void unpack(int *); //non-const ref - void take_buf(char **b, int *sz) { - *b = _buf; - *sz = _sz; - _sz = _ind = 0; - _buf = NULL; - } - - void unpack_req_header(req_header *h) { - //the first 4-byte is for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - unpack(&h->xid); - unpack(&h->proc); - unpack((int *)&h->clt_nonce); - unpack((int *)&h->srv_nonce); - unpack(&h->xid_rep); - _ind = RPC_HEADER_SZ; - } - - void unpack_reply_header(reply_header *h) { - //the first 4-byte is for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - unpack(&h->xid); - unpack(&h->ret); - _ind = RPC_HEADER_SZ; - } + private: + char *buf_; + int sz_; + int index_; + bool ok_; + + inline bool ensure(size_t n); + public: + unmarshall(): buf_(NULL),sz_(0),index_(0),ok_(false) {} + unmarshall(char *b, int sz): buf_(b),sz_(sz),index_(),ok_(true) {} + unmarshall(const std::string &s) : buf_(NULL),sz_(0),index_(0),ok_(false) + { + //take the content which does not exclude a RPC header from a string + take_content(s); + } + ~unmarshall() { + if (buf_) free(buf_); + } + + //take contents from another unmarshall object + void take_in(unmarshall &another); + + //take the content which does not exclude a RPC header from a string + void take_content(const std::string &s) { + sz_ = s.size()+RPC_HEADER_SZ; + buf_ = (char *)realloc(buf_,sz_); + VERIFY(buf_); + index_ = RPC_HEADER_SZ; + memcpy(buf_+index_, s.data(), s.size()); + ok_ = true; + } + + bool ok() const { return ok_; } + char *cstr() { return buf_;} + bool okdone() const { return ok_ && index_ == sz_; } + + unsigned int rawbyte(); + void rawbytes(std::string &s, size_t n); + + int ind() { return index_;} + int size() { return sz_;} + void unpack(int *); //non-const ref + void take_buf(char **b, int *sz) { + *b = buf_; + *sz = sz_; + sz_ = index_ = 0; + buf_ = NULL; + } + + void unpack_req_header(request_header *h) { + //the first 4-byte is for channel to fill size of pdu + index_ = sizeof(rpc_sz_t); + unpack(&h->xid); + unpack(&h->proc); + unpack((int *)&h->clt_nonce); + unpack((int *)&h->srv_nonce); + unpack(&h->xid_rep); + index_ = RPC_HEADER_SZ; + } + + void unpack_reply_header(reply_header *h) { + //the first 4-byte is for channel to fill size of pdu + index_ = sizeof(rpc_sz_t); + unpack(&h->xid); + unpack(&h->ret); + index_ = RPC_HEADER_SZ; + } template inline A grab() { @@ -227,54 +215,20 @@ class unmarshall { } }; -unmarshall& operator>>(unmarshall &, bool &); -unmarshall& operator>>(unmarshall &, unsigned char &); -unmarshall& operator>>(unmarshall &, char &); -unmarshall& operator>>(unmarshall &, unsigned short &); -unmarshall& operator>>(unmarshall &, short &); -unmarshall& operator>>(unmarshall &, unsigned int &); -unmarshall& operator>>(unmarshall &, int &); -unmarshall& operator>>(unmarshall &, unsigned long long &); -unmarshall& operator>>(unmarshall &, std::string &); - -template unmarshall & -operator>>(unmarshall &u, std::vector &v) -{ - unsigned n; - u >> n; - v.clear(); - while (n--) { - C c; - u >> c; - v.push_back(c); - } - return u; +template unmarshall & operator>>(unmarshall &u, A &x) { + unsigned n = u.grab(); + x.clear(); + while (n--) + x.emplace_back(u.grab()); + return u; } template unmarshall & -operator>>(unmarshall &u, std::map &d) { - unsigned n; - u >> n; - d.clear(); - 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); - } +operator>>(unmarshall &u, std::map &x) { + unsigned n = u.grab(); + x.clear(); + while (n--) + x.emplace(u.grab>()); return u; } @@ -283,6 +237,32 @@ operator>>(unmarshall &u, std::pair &d) { return u >> d.first >> d.second; } +typedef std::function handler; + +// +// Automatic marshalling wrappers for RPC handlers +// + +// PAI 2013/09/19 +// C++11 does neither of these two things for us: +// 1) Declare variables using a parameter pack expansion, like so +// Args ...args; +// 2) Call a function with a std::tuple of the arguments it expects +// +// We implement an 'invoke' function for functions of the RPC handler +// signature, i.e. int(R & r, const Args...) +// +// One thing we need in order to accomplish this is a way to cause the compiler +// to specialize 'invoke' with a parameter pack containing a list of indices +// for the elements of the tuple. This will allow us to call the underlying +// function with the exploded contents of the tuple. The empty type +// tuple_indices accomplishes this. It will be passed in to +// 'invoke' as a parameter which will be ignored, but its type will force the +// compiler to specialize 'invoke' appropriately. + +// The following implementation of tuple_indices is redistributed under the MIT +// License as an insubstantial portion of the LLVM compiler infrastructure. + template struct tuple_indices {}; template struct make_indices_imp; template struct make_indices_imp, E> { @@ -291,10 +271,14 @@ template struct make_indices_imp struct make_indices_imp, E> { typedef tuple_indices type; }; -template struct make_tuple_indices { +template struct make_tuple_indices { typedef typename make_indices_imp, E>::type type; }; +// This class encapsulates the default response to runtime unmarshalling +// failures. The templated wrappers below may optionally use a different +// class. + struct VerifyOnFailure { static inline int unmarshall_args_failure() { VERIFY(0); @@ -302,70 +286,91 @@ struct VerifyOnFailure { } }; -typedef std::function handler; - -using std::move; -using std::get; -using std::tuple; -using std::decay; +// Here's the implementation of 'invoke'. It could be more general, but this +// meets our needs. -#include +// One for function pointers... template typename std::enable_if::value, int>::type invoke(F f, void *, R & r, args_type & t, tuple_indices) { - return f(r, move(get(t))...); + return f(r, std::move(std::get(t))...); } +// And one for pointers to member functions... + template typename std::enable_if::value, int>::type invoke(F f, C *c, R & r, args_type & t, tuple_indices) { - return (c->*f)(r, move(get(t))...); + return (c->*f)(r, std::move(std::get(t))...); } -template struct marshalled_func_imp; +// Here we specialize on the Signature template parameter to obtain the list of +// argument types. Note that we do not assume that the Functor parameter has +// the same pattern as Signature; this allows us to ignore the distinctions +// between various types of callable objects at this level of abstraction. + template struct marshalled_func_imp { - using result_type = R; - using args_type = tuple::type...>; - using index_type = typename make_tuple_indices::type; - - static inline int call(F f, C *c, unmarshall &u, marshall &m) { - args_type t{std::move(std::tuple{u.grab()...})}; - if (!u.okdone()) - return ErrorHandler::unmarshall_args_failure(); - R r; - int b = invoke(f, c, r, t, index_type()); - m << r; - return b; - } - static inline handler *wrap(F f, C *c=nullptr) { - typename decay::type f_ = f; - return new handler([f_, c](unmarshall &u, marshall &m) -> int { - return call(f_, c, u, m); + // This type definition corresponds to an empty struct with + // template parameters running from 0 up to (# args) - 1. + using Indices = typename make_tuple_indices::type; + // This type definition represents storage for f's unmarshalled + // arguments. std::decay is (most notably) stripping off const + // qualifiers. + using ArgsStorage = std::tuple::type...>; + // Allocate a handler (i.e. std::function) to hold the lambda + // which will unmarshall RPCs and call f. + return new handler([=](unmarshall &u, marshall &m) -> int { + // Unmarshall each argument with the correct type and store the + // result in a tuple. + ArgsStorage t = {u.grab::type>()...}; + // Verify successful unmarshalling of the entire input stream. + if (!u.okdone()) + return ErrorHandler::unmarshall_args_failure(); + // Allocate space for the RPC response -- will be passed into the + // function as an lvalue reference. + R r; + // Perform the invocation. Note that Indices() calls the default + // constructor of the empty struct with the special template + // parameters. + int b = invoke(f, c, r, t, Indices()); + // Marshall the response. + m << r; + // Make like a tree. + return b; }); } }; -template struct marshalled_func; +// More partial template specialization shenanigans to reduce the number of +// parameters which must be provided explicitly and to support a few common +// callable types. C++11 doesn't allow partial function template +// specialization, so we use classes (structs). + +template struct marshalled_func; template -struct marshalled_func : +struct marshalled_func : public marshalled_func_imp {}; template -struct marshalled_func : +struct marshalled_func : public marshalled_func_imp {}; template -struct marshalled_func, ErrorHandler> : +struct marshalled_func> : public marshalled_func_imp {}; #endif