13 #include "lang/verify.h"
14 #include "lang/algorithm.h"
17 req_header(int x=0, int p=0, int c = 0, int s = 0, int xi = 0):
18 xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {}
21 unsigned int clt_nonce;
22 unsigned int srv_nonce;
27 reply_header(int x=0, int r=0): xid(x), ret(r) {}
32 typedef uint64_t rpc_checksum_t;
36 //size of initial buffer allocation
37 DEFAULT_RPC_SZ = 1024,
39 //size of rpc_header includes a 4-byte int to be filled by tcpchan and uint64_t checksum
40 RPC_HEADER_SZ = static_max<sizeof(req_header), sizeof(reply_header)>::value + sizeof(rpc_sz_t) + sizeof(rpc_checksum_t)
42 RPC_HEADER_SZ = static_max<sizeof(req_header), sizeof(reply_header)>::value + sizeof(rpc_sz_t)
48 char *_buf; // Base of the raw bytes buffer (dynamically readjusted)
49 int _capa; // Capacity of the buffer
50 int _ind; // Read/write head position
54 _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ);
56 _capa = DEFAULT_RPC_SZ;
65 int size() { return _ind;}
66 char *cstr() { return _buf;}
68 void rawbyte(unsigned char);
69 void rawbytes(const char *, int);
71 // Return the current content (excluding header) as a string
72 std::string get_content() {
73 return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ);
76 // Return the current content (excluding header) as a string
83 void pack_req_header(const req_header &h) {
85 //leave the first 4-byte empty for channel to fill size of pdu
86 _ind = sizeof(rpc_sz_t);
88 _ind += sizeof(rpc_checksum_t);
92 pack((int)h.clt_nonce);
93 pack((int)h.srv_nonce);
98 void pack_reply_header(const reply_header &h) {
100 //leave the first 4-byte empty for channel to fill size of pdu
101 _ind = sizeof(rpc_sz_t);
103 _ind += sizeof(rpc_checksum_t);
110 void take_buf(char **b, int *s) {
118 marshall& operator<<(marshall &, bool);
119 marshall& operator<<(marshall &, unsigned int);
120 marshall& operator<<(marshall &, int);
121 marshall& operator<<(marshall &, unsigned char);
122 marshall& operator<<(marshall &, char);
123 marshall& operator<<(marshall &, unsigned short);
124 marshall& operator<<(marshall &, short);
125 marshall& operator<<(marshall &, unsigned long long);
126 marshall& operator<<(marshall &, const std::string &);
135 unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {}
136 unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {}
137 unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false)
139 //take the content which does not exclude a RPC header from a string
143 if (_buf) free(_buf);
146 //take contents from another unmarshall object
147 void take_in(unmarshall &another);
149 //take the content which does not exclude a RPC header from a string
150 void take_content(const std::string &s) {
151 _sz = s.size()+RPC_HEADER_SZ;
152 _buf = (char *)realloc(_buf,_sz);
154 _ind = RPC_HEADER_SZ;
155 memcpy(_buf+_ind, s.data(), s.size());
159 bool ok() { return _ok; }
160 char *cstr() { return _buf;}
162 unsigned int rawbyte();
163 void rawbytes(std::string &s, unsigned int n);
165 int ind() { return _ind;}
166 int size() { return _sz;}
167 void unpack(int *); //non-const ref
168 void take_buf(char **b, int *sz) {
175 void unpack_req_header(req_header *h) {
176 //the first 4-byte is for channel to fill size of pdu
177 _ind = sizeof(rpc_sz_t);
179 _ind += sizeof(rpc_checksum_t);
183 unpack((int *)&h->clt_nonce);
184 unpack((int *)&h->srv_nonce);
186 _ind = RPC_HEADER_SZ;
189 void unpack_reply_header(reply_header *h) {
190 //the first 4-byte is for channel to fill size of pdu
191 _ind = sizeof(rpc_sz_t);
193 _ind += sizeof(rpc_checksum_t);
197 _ind = RPC_HEADER_SZ;
201 unmarshall& operator>>(unmarshall &, bool &);
202 unmarshall& operator>>(unmarshall &, unsigned char &);
203 unmarshall& operator>>(unmarshall &, char &);
204 unmarshall& operator>>(unmarshall &, unsigned short &);
205 unmarshall& operator>>(unmarshall &, short &);
206 unmarshall& operator>>(unmarshall &, unsigned int &);
207 unmarshall& operator>>(unmarshall &, int &);
208 unmarshall& operator>>(unmarshall &, unsigned long long &);
209 unmarshall& operator>>(unmarshall &, std::string &);
211 template <class C> marshall &
212 operator<<(marshall &m, std::vector<C> v)
214 m << (unsigned int) v.size();
215 for(unsigned i = 0; i < v.size(); i++)
220 template <class C> unmarshall &
221 operator>>(unmarshall &u, std::vector<C> &v)
225 for(unsigned i = 0; i < n; i++){
233 template <class A, class B> marshall &
234 operator<<(marshall &m, const std::map<A,B> &d) {
235 typename std::map<A,B>::const_iterator i;
237 m << (unsigned int) d.size();
239 for (i = d.begin(); i != d.end(); i++) {
240 m << i->first << i->second;
245 template <class A, class B> unmarshall &
246 operator>>(unmarshall &u, std::map<A,B> &d) {
252 for (unsigned int lcv = 0; lcv < n; lcv++) {