- 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;
+ }