4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 #include "connection.h"
20 static const unsigned int bind = 1; // handler number reserved for bind
21 static const int timeout_failure = -1;
22 static const int unmarshal_args_failure = -2;
23 static const int unmarshal_reply_failure = -3;
24 static const int atmostonce_failure = -4;
25 static const int oldsrv_failure = -5;
26 static const int bind_failure = -6;
27 static const int cancel_failure = -7;
30 // rpc client endpoint.
31 // manages a xid space per destination socket
32 // threaded: multiple threads can be sending RPCs,
33 class rpcc : public chanmgr {
37 //manages per rpc info
39 caller(unsigned int xxid, unmarshall *un);
47 std::condition_variable c;
50 void get_refconn(connection **ch);
51 void update_xid_rep(unsigned int xid);
55 unsigned int clt_nonce_;
56 unsigned int srv_nonce_;
65 std::mutex m_; // protect insert/delete to calls[]
69 std::condition_variable destroy_wait_c_;
71 std::map<int, caller *> calls_;
72 std::list<unsigned int> xid_rep_window_;
75 request() { clear(); }
76 void clear() { buf.clear(); xid = -1; }
77 bool isvalid() { return xid != -1; }
81 struct request dup_req_;
85 rpcc(sockaddr_in d, bool retrans=true);
91 static const TO to_max;
92 static const TO to_min;
93 static TO to(int x) { TO t; t.to = x; return t;}
95 unsigned int id() { return clt_nonce_; }
97 int bind(TO to = to_max);
99 void set_reachable(bool r) { reachable_ = r; }
103 int islossy() { return lossytest_ > 0; }
105 int call1(unsigned int proc,
106 marshall &req, unmarshall &rep, TO to);
108 bool got_pdu(connection *c, char *b, int sz);
112 int call_m(unsigned int proc, marshall &req, R & r, TO to);
114 template<class R, typename ...Args>
115 inline int call(unsigned int proc, R & r, const Args&... args);
117 template<class R, typename ...Args>
118 inline int call_timeout(unsigned int proc, TO to, R & r, const Args&... args);
121 template<class R> int
122 rpcc::call_m(unsigned int 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(unsigned int 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(unsigned int 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.
170 reply_t (unsigned int _xid) {
176 reply_t (unsigned int _xid, char *_buf, int _sz) {
183 bool cb_present; // whether the reply buffer is valid
184 char *buf; // the reply buffer
185 int 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 std::map<unsigned int, std::list<reply_t> > reply_window_;
196 void free_reply_window(void);
197 void add_reply(unsigned int clt_nonce, unsigned int xid, char *b, int sz);
199 rpcstate_t checkduplicate_and_update(unsigned int clt_nonce,
200 unsigned int xid, unsigned int rep_xid,
203 void updatestat(unsigned int proc);
205 // latest connection to the client
206 std::map<unsigned int, connection *> conns_;
211 std::map<int, int> counts_;
216 // map proc # to function
217 std::map<int, handler *> procs_;
219 std::mutex procs_m_; // protect insert/delete to procs[]
220 std::mutex count_m_; //protect modification of counts
221 std::mutex reply_window_m_; // protect reply window et al
222 std::mutex conss_m_; // protect conns_
228 djob_t (connection *c, char *b, int bsz):buf(b),sz(bsz),conn(c) {}
233 void dispatch(djob_t *);
235 // internal handler registration
236 void reg1(unsigned int proc, handler *);
238 ThrPool* dispatchpool_;
242 rpcs(unsigned int port, int counts=0);
244 inline int port() { return listener_->port(); }
245 //RPC handler for clients binding
246 int rpcbind(int &r, int a);
248 void set_reachable(bool r) { reachable_ = r; }
250 bool got_pdu(connection *c, char *b, int sz);
252 template<class F, class C=void> void reg(unsigned int 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(unsigned int proc, F f, C *c) {
262 reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
265 void make_sockaddr(const char *hostandport, struct sockaddr_in *dst);
266 void make_sockaddr(const char *host, const char *port,
267 struct sockaddr_in *dst);