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) {}
31 typedef uint64_t rpc_checksum_t;
34 //size of initial buffer allocation
35 #define DEFAULT_RPC_SZ 1024
36 #define RPC_HEADER_SZ_NO_CHECKSUM (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t))
38 //size of rpc_header includes a 4-byte int to be filled by tcpchan and uint64_t checksum
39 #define RPC_HEADER_SZ (RPC_HEADER_SZ_NO_CHECKSUM + sizeof(rpc_checksum_t))
41 #define RPC_HEADER_SZ (RPC_HEADER_SZ_NO_CHECKSUM)
46 char *_buf; // Base of the raw bytes buffer (dynamically readjusted)
47 int _capa; // Capacity of the buffer
48 int _ind; // Read/write head position
52 _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ);
54 _capa = DEFAULT_RPC_SZ;
63 int size() { return _ind;}
64 char *cstr() { return _buf;}
66 void rawbyte(unsigned char);
67 void rawbytes(const char *, int);
69 // Return the current content (excluding header) as a string
70 std::string get_content() {
71 return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ);
74 // Return the current content (excluding header) as a string
81 void pack_req_header(const req_header &h) {
83 //leave the first 4-byte empty for channel to fill size of pdu
84 _ind = sizeof(rpc_sz_t);
86 _ind += sizeof(rpc_checksum_t);
90 pack((int)h.clt_nonce);
91 pack((int)h.srv_nonce);
96 void pack_reply_header(const reply_header &h) {
98 //leave the first 4-byte empty for channel to fill size of pdu
99 _ind = sizeof(rpc_sz_t);
101 _ind += sizeof(rpc_checksum_t);
108 void take_buf(char **b, int *s) {
116 marshall& operator<<(marshall &, bool);
117 marshall& operator<<(marshall &, unsigned int);
118 marshall& operator<<(marshall &, int);
119 marshall& operator<<(marshall &, unsigned char);
120 marshall& operator<<(marshall &, char);
121 marshall& operator<<(marshall &, unsigned short);
122 marshall& operator<<(marshall &, short);
123 marshall& operator<<(marshall &, unsigned long long);
124 marshall& operator<<(marshall &, const std::string &);
133 unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {}
134 unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {}
135 unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false)
137 //take the content which does not exclude a RPC header from a string
141 if (_buf) free(_buf);
144 //take contents from another unmarshall object
145 void take_in(unmarshall &another);
147 //take the content which does not exclude a RPC header from a string
148 void take_content(const std::string &s) {
149 _sz = s.size()+RPC_HEADER_SZ;
150 _buf = (char *)realloc(_buf,_sz);
152 _ind = RPC_HEADER_SZ;
153 memcpy(_buf+_ind, s.data(), s.size());
157 bool ok() { return _ok; }
158 char *cstr() { return _buf;}
160 unsigned int rawbyte();
161 void rawbytes(std::string &s, unsigned int n);
163 int ind() { return _ind;}
164 int size() { return _sz;}
165 void unpack(int *); //non-const ref
166 void take_buf(char **b, int *sz) {
173 void unpack_req_header(req_header *h) {
174 //the first 4-byte is for channel to fill size of pdu
175 _ind = sizeof(rpc_sz_t);
177 _ind += sizeof(rpc_checksum_t);
181 unpack((int *)&h->clt_nonce);
182 unpack((int *)&h->srv_nonce);
184 _ind = RPC_HEADER_SZ;
187 void unpack_reply_header(reply_header *h) {
188 //the first 4-byte is for channel to fill size of pdu
189 _ind = sizeof(rpc_sz_t);
191 _ind += sizeof(rpc_checksum_t);
195 _ind = RPC_HEADER_SZ;
199 unmarshall& operator>>(unmarshall &, bool &);
200 unmarshall& operator>>(unmarshall &, unsigned char &);
201 unmarshall& operator>>(unmarshall &, char &);
202 unmarshall& operator>>(unmarshall &, unsigned short &);
203 unmarshall& operator>>(unmarshall &, short &);
204 unmarshall& operator>>(unmarshall &, unsigned int &);
205 unmarshall& operator>>(unmarshall &, int &);
206 unmarshall& operator>>(unmarshall &, unsigned long long &);
207 unmarshall& operator>>(unmarshall &, std::string &);
209 template <class C> marshall &
210 operator<<(marshall &m, std::vector<C> v)
212 m << (unsigned int) v.size();
213 for(unsigned i = 0; i < v.size(); i++)
218 template <class C> unmarshall &
219 operator>>(unmarshall &u, std::vector<C> &v)
223 for(unsigned i = 0; i < n; i++){
231 template <class A, class B> marshall &
232 operator<<(marshall &m, const std::map<A,B> &d) {
233 typename std::map<A,B>::const_iterator i;
235 m << (unsigned int) d.size();
237 for (i = d.begin(); i != d.end(); i++) {
238 m << i->first << i->second;
243 template <class A, class B> unmarshall &
244 operator>>(unmarshall &u, std::map<A,B> &d) {
250 for (unsigned int lcv = 0; lcv < n; lcv++) {