13 #include "lang/verify.h"
16 req_header(int x=0, int p=0, int c = 0, int s = 0, int xi = 0):
17 xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {}
20 unsigned int clt_nonce;
21 unsigned int srv_nonce;
26 reply_header(int x=0, int r=0): xid(x), ret(r) {}
33 //size of initial buffer allocation
34 #define DEFAULT_RPC_SZ 1024
35 #define RPC_HEADER_SZ (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t))
38 template<typename... Args> inline pass(Args&&...) {}
43 char *_buf; // Base of the raw bytes buffer (dynamically readjusted)
44 int _capa; // Capacity of the buffer
45 int _ind; // Read/write head position
49 _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ);
51 _capa = DEFAULT_RPC_SZ;
55 template <typename... Args> marshall(const Args&... args) : marshall() {
56 (void)pass{(*this << args)...};
64 int size() { return _ind;}
65 char *cstr() { return _buf;}
67 void rawbyte(unsigned char);
68 void rawbytes(const char *, int);
70 // Return the current content (excluding header) as a string
71 std::string get_content() {
72 return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ);
75 // Return the current content (excluding header) as a string
82 void pack_req_header(const req_header &h) {
84 //leave the first 4-byte empty for channel to fill size of pdu
85 _ind = sizeof(rpc_sz_t);
88 pack((int)h.clt_nonce);
89 pack((int)h.srv_nonce);
94 void pack_reply_header(const reply_header &h) {
96 //leave the first 4-byte empty for channel to fill size of pdu
97 _ind = sizeof(rpc_sz_t);
103 void take_buf(char **b, int *s) {
112 marshall& operator<<(marshall &, bool);
113 marshall& operator<<(marshall &, unsigned int);
114 marshall& operator<<(marshall &, int);
115 marshall& operator<<(marshall &, unsigned char);
116 marshall& operator<<(marshall &, char);
117 marshall& operator<<(marshall &, unsigned short);
118 marshall& operator<<(marshall &, short);
119 marshall& operator<<(marshall &, unsigned long long);
120 marshall& operator<<(marshall &, const std::string &);
122 template <class C> marshall &
123 operator<<(marshall &m, std::vector<C> v)
125 m << (unsigned int) v.size();
126 for(unsigned i = 0; i < v.size(); i++)
131 template <class A, class B> marshall &
132 operator<<(marshall &m, const std::map<A,B> &d) {
133 typename std::map<A,B>::const_iterator i;
135 m << (unsigned int) d.size();
137 for (i = d.begin(); i != d.end(); i++) {
138 m << i->first << i->second;
143 template <class A> marshall &
144 operator<<(marshall &m, const std::list<A> &d) {
145 m << std::vector<A>(d.begin(), d.end());
149 template <class A, class B> marshall &
150 operator<<(marshall &m, const std::pair<A,B> &d) {
163 unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {}
164 unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {}
165 unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false)
167 //take the content which does not exclude a RPC header from a string
171 if (_buf) free(_buf);
174 //take contents from another unmarshall object
175 void take_in(unmarshall &another);
177 //take the content which does not exclude a RPC header from a string
178 void take_content(const std::string &s) {
179 _sz = s.size()+RPC_HEADER_SZ;
180 _buf = (char *)realloc(_buf,_sz);
182 _ind = RPC_HEADER_SZ;
183 memcpy(_buf+_ind, s.data(), s.size());
187 bool ok() { return _ok; }
188 char *cstr() { return _buf;}
190 unsigned int rawbyte();
191 void rawbytes(std::string &s, unsigned int n);
193 int ind() { return _ind;}
194 int size() { return _sz;}
195 void unpack(int *); //non-const ref
196 void take_buf(char **b, int *sz) {
203 void unpack_req_header(req_header *h) {
204 //the first 4-byte is for channel to fill size of pdu
205 _ind = sizeof(rpc_sz_t);
208 unpack((int *)&h->clt_nonce);
209 unpack((int *)&h->srv_nonce);
211 _ind = RPC_HEADER_SZ;
214 void unpack_reply_header(reply_header *h) {
215 //the first 4-byte is for channel to fill size of pdu
216 _ind = sizeof(rpc_sz_t);
219 _ind = RPC_HEADER_SZ;
222 template <class OutputIterator>
223 void iterate(OutputIterator i, int n) {
225 typename OutputIterator::value_type t;
232 unmarshall& operator>>(unmarshall &, bool &);
233 unmarshall& operator>>(unmarshall &, unsigned char &);
234 unmarshall& operator>>(unmarshall &, char &);
235 unmarshall& operator>>(unmarshall &, unsigned short &);
236 unmarshall& operator>>(unmarshall &, short &);
237 unmarshall& operator>>(unmarshall &, unsigned int &);
238 unmarshall& operator>>(unmarshall &, int &);
239 unmarshall& operator>>(unmarshall &, unsigned long long &);
240 unmarshall& operator>>(unmarshall &, std::string &);
242 template <class C> unmarshall &
243 operator>>(unmarshall &u, std::vector<C> &v)
256 template <class A, class B> unmarshall &
257 operator>>(unmarshall &u, std::map<A,B> &d) {
270 template <class C> unmarshall &
271 operator>>(unmarshall &u, std::list<C> &l) {
283 template <class A, class B> unmarshall &
284 operator>>(unmarshall &u, std::pair<A,B> &d) {
285 return u >> d.first >> d.second;