4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 #include "connection.h"
21 static const unsigned int bind = 1; // handler number reserved for bind
22 static const int timeout_failure = -1;
23 static const int unmarshal_args_failure = -2;
24 static const int unmarshal_reply_failure = -3;
25 static const int atmostonce_failure = -4;
26 static const int oldsrv_failure = -5;
27 static const int bind_failure = -6;
28 static const int cancel_failure = -7;
31 // rpc client endpoint.
32 // manages a xid space per destination socket
33 // threaded: multiple threads can be sending RPCs,
34 class rpcc : public chanmgr {
38 //manages per rpc info
40 caller(int xxid, unmarshall *un);
51 void get_refconn(connection **ch);
52 void update_xid_rep(int xid);
56 unsigned int clt_nonce_;
57 unsigned int srv_nonce_;
66 mutex m_; // protect insert/delete to calls[]
72 map<int, caller *> calls_;
73 list<int> xid_rep_window_;
76 request() { clear(); }
77 void clear() { buf.clear(); xid = -1; }
78 bool isvalid() { return xid != -1; }
82 struct request dup_req_;
86 rpcc(const string & d, bool retrans=true);
92 static const TO to_max;
93 static const TO to_min;
94 static TO to(int x) { TO t; t.to = x; return t;}
96 unsigned int id() { return clt_nonce_; }
98 int bind(TO to = to_max);
100 void set_reachable(bool r) { reachable_ = r; }
104 int islossy() { return lossytest_ > 0; }
106 int call1(proc_t proc,
107 marshall &req, unmarshall &rep, TO to);
109 bool got_pdu(connection *c, char *b, size_t sz);
112 int call_m(proc_t proc, marshall &req, R & r, TO to);
114 template<class R, typename ...Args>
115 inline int call(proc_t proc, R & r, const Args&... args);
117 template<class R, typename ...Args>
118 inline int call_timeout(proc_t proc, TO to, R & r, const Args&... args);
121 template<class R> int
122 rpcc::call_m(proc_t proc, marshall &req, R & r, TO to)
125 int intret = call1(proc, req, u, to);
126 if (intret < 0) return intret;
128 if (u.okdone() != true) {
129 fprintf(stderr, "rpcc::call_m: failed to unmarshall the reply."
130 "You are probably calling RPC 0x%x with wrong return "
133 return rpc_const::unmarshal_reply_failure;
138 template<class R, typename... Args> inline int
139 rpcc::call(proc_t proc, R & r, const Args&... args)
141 return call_timeout(proc, rpcc::to_max, r, args...);
144 template<class R, typename... Args> inline int
145 rpcc::call_timeout(proc_t proc, const rpcc::TO to, R & r, const Args&... args)
148 return call_m(proc, m, r, to);
151 bool operator<(const sockaddr_in &a, const sockaddr_in &b);
153 // rpc server endpoint.
154 class rpcs : public chanmgr {
157 NEW, // new RPC, not a duplicate
158 INPROGRESS, // duplicate of an RPC we're still processing
159 DONE, // duplicate of an RPC we already replied to (have reply)
160 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
165 // state about an in-progress or completed RPC, for at-most-once.
166 // if cb_present is true, then the RPC is complete and a reply
167 // has been sent; in that case buf points to a copy of the reply,
168 // and sz holds the size of the reply.
176 reply_t (int _xid, char *_buf, size_t _sz) {
183 bool cb_present; // whether the reply buffer is valid
184 char *buf; // the reply buffer
185 size_t sz; // the size of reply buffer
191 // provide at most once semantics by maintaining a window of replies
192 // per client that that client hasn't acknowledged receiving yet.
193 // indexed by client nonce.
194 map<unsigned int, list<reply_t> > reply_window_;
196 void free_reply_window(void);
197 void add_reply(unsigned int clt_nonce, int xid, char *b, size_t sz);
199 rpcstate_t checkduplicate_and_update(unsigned int clt_nonce,
200 int xid, int rep_xid,
201 char **b, size_t *sz);
203 void updatestat(proc_t proc);
205 // latest connection to the client
206 map<unsigned int, connection *> conns_;
209 const size_t counting_;
211 map<proc_t, size_t> counts_;
216 // map proc # to function
217 map<proc_t, handler *> procs_;
219 mutex procs_m_; // protect insert/delete to procs[]
220 mutex count_m_; //protect modification of counts
221 mutex reply_window_m_; // protect reply window et al
222 mutex conss_m_; // protect conns_
228 djob_t (connection *c, char *b, size_t bsz):buf(b),sz(bsz),conn(c) {}
233 void dispatch(djob_t *);
235 // internal handler registration
236 void reg1(proc_t proc, handler *);
238 ThrPool* dispatchpool_;
242 rpcs(unsigned int port, size_t counts=0);
244 inline unsigned int port() { return listener_->port(); }
245 //RPC handler for clients binding
246 int rpcbind(unsigned int &r, int a);
248 void set_reachable(bool r) { reachable_ = r; }
250 bool got_pdu(connection *c, char *b, size_t sz);
252 template<class F, class C=void> void reg(proc_t proc, F f, C *c=nullptr);
255 struct ReturnOnFailure {
256 static inline int unmarshall_args_failure() {
257 return rpc_const::unmarshal_args_failure;
261 template<class F, class C> void rpcs::reg(proc_t proc, F f, C *c) {
262 reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
265 sockaddr_in make_sockaddr(const string &hostandport);
266 sockaddr_in make_sockaddr(const string &host, const string &port);