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))
39 char *_buf; // Base of the raw bytes buffer (dynamically readjusted)
40 int _capa; // Capacity of the buffer
41 int _ind; // Read/write head position
45 _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ);
47 _capa = DEFAULT_RPC_SZ;
56 int size() { return _ind;}
57 char *cstr() { return _buf;}
59 void rawbyte(unsigned char);
60 void rawbytes(const char *, int);
62 // Return the current content (excluding header) as a string
63 std::string get_content() {
64 return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ);
67 // Return the current content (excluding header) as a string
74 void pack_req_header(const req_header &h) {
76 //leave the first 4-byte empty for channel to fill size of pdu
77 _ind = sizeof(rpc_sz_t);
80 pack((int)h.clt_nonce);
81 pack((int)h.srv_nonce);
86 void pack_reply_header(const reply_header &h) {
88 //leave the first 4-byte empty for channel to fill size of pdu
89 _ind = sizeof(rpc_sz_t);
95 void take_buf(char **b, int *s) {
104 marshall& operator<<(marshall &, bool);
105 marshall& operator<<(marshall &, unsigned int);
106 marshall& operator<<(marshall &, int);
107 marshall& operator<<(marshall &, unsigned char);
108 marshall& operator<<(marshall &, char);
109 marshall& operator<<(marshall &, unsigned short);
110 marshall& operator<<(marshall &, short);
111 marshall& operator<<(marshall &, unsigned long long);
112 marshall& operator<<(marshall &, const std::string &);
114 template <class C> marshall &
115 operator<<(marshall &m, std::vector<C> v)
117 m << (unsigned int) v.size();
118 for(unsigned i = 0; i < v.size(); i++)
123 template <class A, class B> marshall &
124 operator<<(marshall &m, const std::map<A,B> &d) {
125 typename std::map<A,B>::const_iterator i;
127 m << (unsigned int) d.size();
129 for (i = d.begin(); i != d.end(); i++) {
130 m << i->first << i->second;
135 template <class A> marshall &
136 operator<<(marshall &m, const std::list<A> &d) {
137 m << std::vector<A>(d.begin(), d.end());
141 template <class A, class B> marshall &
142 operator<<(marshall &m, const std::pair<A,B> &d) {
155 unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {}
156 unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {}
157 unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false)
159 //take the content which does not exclude a RPC header from a string
163 if (_buf) free(_buf);
166 //take contents from another unmarshall object
167 void take_in(unmarshall &another);
169 //take the content which does not exclude a RPC header from a string
170 void take_content(const std::string &s) {
171 _sz = s.size()+RPC_HEADER_SZ;
172 _buf = (char *)realloc(_buf,_sz);
174 _ind = RPC_HEADER_SZ;
175 memcpy(_buf+_ind, s.data(), s.size());
179 bool ok() { return _ok; }
180 char *cstr() { return _buf;}
182 unsigned int rawbyte();
183 void rawbytes(std::string &s, unsigned int n);
185 int ind() { return _ind;}
186 int size() { return _sz;}
187 void unpack(int *); //non-const ref
188 void take_buf(char **b, int *sz) {
195 void unpack_req_header(req_header *h) {
196 //the first 4-byte is for channel to fill size of pdu
197 _ind = sizeof(rpc_sz_t);
200 unpack((int *)&h->clt_nonce);
201 unpack((int *)&h->srv_nonce);
203 _ind = RPC_HEADER_SZ;
206 void unpack_reply_header(reply_header *h) {
207 //the first 4-byte is for channel to fill size of pdu
208 _ind = sizeof(rpc_sz_t);
211 _ind = RPC_HEADER_SZ;
215 unmarshall& operator>>(unmarshall &, bool &);
216 unmarshall& operator>>(unmarshall &, unsigned char &);
217 unmarshall& operator>>(unmarshall &, char &);
218 unmarshall& operator>>(unmarshall &, unsigned short &);
219 unmarshall& operator>>(unmarshall &, short &);
220 unmarshall& operator>>(unmarshall &, unsigned int &);
221 unmarshall& operator>>(unmarshall &, int &);
222 unmarshall& operator>>(unmarshall &, unsigned long long &);
223 unmarshall& operator>>(unmarshall &, std::string &);
225 template <class C> unmarshall &
226 operator>>(unmarshall &u, std::vector<C> &v)
239 template <class A, class B> unmarshall &
240 operator>>(unmarshall &u, std::map<A,B> &d) {
253 template <class C> unmarshall &
254 operator>>(unmarshall &u, std::list<C> &l) {
266 template <class A, class B> unmarshall &
267 operator>>(unmarshall &u, std::pair<A,B> &d) {
268 return u >> d.first >> d.second;