5 #include <sys/socket.h>
6 #include <netinet/in.h>
10 #include "marshall_wrap.h"
11 #include "connection.h"
15 static const unsigned int bind = 1; // handler number reserved for bind
16 static const int timeout_failure = -1;
17 static const int unmarshal_args_failure = -2;
18 static const int unmarshal_reply_failure = -3;
19 static const int atmostonce_failure = -4;
20 static const int oldsrv_failure = -5;
21 static const int bind_failure = -6;
22 static const int cancel_failure = -7;
25 // rpc client endpoint.
26 // manages a xid space per destination socket
27 // threaded: multiple threads can be sending RPCs,
28 class rpcc : public chanmgr {
32 //manages per rpc info
34 caller(int _xid, string *_rep) : xid(_xid), rep(_rep) {}
44 void get_refconn(connection **ch);
45 void update_xid_rep(int xid);
49 unsigned int clt_nonce_;
50 unsigned int srv_nonce_;
59 mutex m_; // protect insert/delete to calls[]
65 map<int, caller *> calls_;
66 list<int> xid_rep_window_;
69 request() { clear(); }
70 void clear() { buf.clear(); xid = -1; }
71 bool isvalid() { return xid != -1; }
75 struct request dup_req_;
79 rpcc(const string & d, bool retrans=true);
85 static const TO to_max;
86 static const TO to_min;
87 static TO to(int x) { TO t; t.to = x; return t;}
89 unsigned int id() { return clt_nonce_; }
91 int bind(TO to = to_max);
93 void set_reachable(bool r) { reachable_ = r; }
97 int islossy() { return lossytest_ > 0; }
99 int call1(proc_t proc, marshall &req, string &rep, TO to);
101 bool got_pdu(connection *c, const string & b);
104 int call_m(proc_t proc, marshall &req, R & r, TO to);
106 template<class R, typename ...Args>
107 inline int call(proc_t proc, R & r, const Args&... args);
109 template<class R, typename ...Args>
110 inline int call_timeout(proc_t proc, TO to, R & r, const Args&... args);
113 template<class R> int
114 rpcc::call_m(proc_t proc, marshall &req, R & r, TO to)
117 int intret = call1(proc, req, rep, to);
118 unmarshall u(rep, true);
119 if (intret < 0) return intret;
121 if (u.okdone() != true) {
122 cerr << "rpcc::call_m: failed to unmarshall the reply. You are probably " <<
123 "calling RPC 0x" << hex << proc << " with the wrong return type." << endl;
125 return rpc_const::unmarshal_reply_failure;
130 template<class R, typename... Args> inline int
131 rpcc::call(proc_t proc, R & r, const Args&... args)
133 return call_timeout(proc, rpcc::to_max, r, args...);
136 template<class R, typename... Args> inline int
137 rpcc::call_timeout(proc_t proc, const rpcc::TO to, R & r, const Args&... args)
140 return call_m(proc, m, r, to);
143 // rpc server endpoint.
144 class rpcs : public chanmgr {
147 NEW, // new RPC, not a duplicate
148 INPROGRESS, // duplicate of an RPC we're still processing
149 DONE, // duplicate of an RPC we already replied to (have reply)
150 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
155 // state about an in-progress or completed RPC, for at-most-once.
156 // if cb_present is true, then the RPC is complete and a reply
157 // has been sent; in that case buf points to a copy of the reply,
158 // and sz holds the size of the reply.
160 reply_t (int _xid) : xid(_xid), cb_present(false) {}
161 reply_t (int _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
163 bool cb_present; // whether the reply buffer is valid
164 string buf; // the reply buffer
170 // provide at most once semantics by maintaining a window of replies
171 // per client that that client hasn't acknowledged receiving yet.
172 // indexed by client nonce.
173 map<unsigned int, list<reply_t> > reply_window_;
175 void free_reply_window(void);
176 void add_reply(unsigned int clt_nonce, int xid, const string & b);
178 rpcstate_t checkduplicate_and_update(unsigned int clt_nonce,
179 int xid, int rep_xid, string & b);
181 void updatestat(proc_t proc);
183 // latest connection to the client
184 map<unsigned int, connection *> conns_;
187 const size_t counting_;
189 map<proc_t, size_t> counts_;
193 // map proc # to function
194 map<proc_t, handler *> procs_;
196 mutex procs_m_; // protect insert/delete to procs[]
197 mutex count_m_; //protect modification of counts
198 mutex reply_window_m_; // protect reply window et al
199 mutex conss_m_; // protect conns_
208 void dispatch(djob_t *);
210 // internal handler registration
211 void reg1(proc_t proc, handler *);
213 ThrPool* dispatchpool_;
217 rpcs(in_port_t port, size_t counts=0);
219 inline in_port_t port() { return listener_->port(); }
220 //RPC handler for clients binding
221 int rpcbind(unsigned int &r, int a);
223 void set_reachable(bool r) { reachable_ = r; }
225 bool got_pdu(connection *c, const string & b);
227 template<class F, class C=void> void reg(proc_t proc, F f, C *c=nullptr);
230 struct ReturnOnFailure {
231 static inline int unmarshall_args_failure() {
232 return rpc_const::unmarshal_args_failure;
236 template<class F, class C> void rpcs::reg(proc_t proc, F f, C *c) {
237 reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));