5 #include <sys/socket.h>
6 #include <netinet/in.h>
8 #include "rpc_protocol.h"
11 #include "marshall_wrap.h"
12 #include "connection.h"
15 static constexpr milliseconds to_max{12000};
16 static constexpr milliseconds to_min{100};
19 template<class P, class R, class ...Args> struct is_valid_call : false_type {};
21 template<class S, class R, class ...Args>
22 struct is_valid_call<S(R &, Args...), R, Args...> : true_type {};
24 template<class P, class F> struct is_valid_registration : false_type {};
26 template<class S, class R, class ...Args>
27 struct is_valid_registration<S(R &, typename decay<Args>::type...), S(R &, Args...)> : true_type {};
29 template<class P, class C, class S, class R, class ...Args>
30 struct is_valid_registration<P, S(C::*)(R &, Args...)> : is_valid_registration<P, S(R &, Args...)> {};
32 // rpc client endpoint.
33 // manages a xid space per destination socket
34 // threaded: multiple threads can be sending RPCs,
35 class rpcc : private connection_delegate {
37 using proc_id_t = rpc_protocol::proc_id_t;
39 using proc_t = rpc_protocol::proc_t<S>;
40 using nonce_t = rpc_protocol::nonce_t;
41 using xid_t = rpc_protocol::xid_t;
43 // manages per rpc info
45 caller(xid_t _xid, string *_rep) : xid(_xid), rep(_rep) {}
55 void get_refconn(shared_ptr<connection> & ch);
56 void update_xid_rep(xid_t xid);
68 shared_ptr<connection> chan_;
70 mutex m_; // protect insert/delete to calls[]
76 map<int, caller *> calls_;
77 list<xid_t> xid_rep_window_;
80 void clear() { buf.clear(); xid = -1; }
81 bool isvalid() { return xid != -1; }
88 int call1(proc_id_t proc, marshall &req, string &rep, milliseconds to);
91 int call_m(proc_id_t proc, marshall &req, R & r, milliseconds to) {
93 int intret = call1(proc, req, rep, to);
94 unmarshall u(rep, true);
95 if (intret < 0) return intret;
97 if (u.okdone() != true) {
98 LOG("rpcc::call_m: failed to unmarshall the reply. You are probably " <<
99 "calling RPC 0x" << hex << proc << " with the wrong return type.");
101 return rpc_protocol::unmarshal_reply_failure;
106 bool got_pdu(const shared_ptr<connection> & c, const string & b);
110 rpcc(const string & d, bool retrans=true);
113 nonce_t id() { return clt_nonce_; }
115 int bind(milliseconds to = rpc::to_max);
117 void set_reachable(bool r) { reachable_ = r; }
121 template<class P, class R, typename ...Args>
122 inline int call(proc_t<P> proc, R & r, const Args&... args) {
123 return call_timeout(proc, rpc::to_max, r, args...);
126 template<class P, class R, typename ...Args>
127 inline int call_timeout(proc_t<P> proc, milliseconds to, R & r, const Args&... args) {
128 static_assert(is_valid_call<P, R, Args...>::value, "RPC called with incorrect argument types");
130 return call_m(proc.id, m, r, to);
134 // rpc server endpoint.
135 class rpcs : private connection_delegate {
137 using proc_id_t = rpc_protocol::proc_id_t;
139 using proc_t = rpc_protocol::proc_t<S>;
140 using nonce_t = rpc_protocol::nonce_t;
141 using xid_t = rpc_protocol::xid_t;
144 NEW, // new RPC, not a duplicate
145 INPROGRESS, // duplicate of an RPC we're still processing
146 DONE, // duplicate of an RPC we already replied to (have reply)
147 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
150 // state about an in-progress or completed RPC, for at-most-once.
151 // if cb_present is true, then the RPC is complete and a reply
152 // has been sent; in that case buf points to a copy of the reply,
153 // and sz holds the size of the reply.
155 reply_t (xid_t _xid) : xid(_xid), cb_present(false) {}
156 reply_t (xid_t _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
158 bool cb_present; // whether the reply buffer is valid
159 string buf; // the reply buffer
165 // provide at most once semantics by maintaining a window of replies
166 // per client that that client hasn't acknowledged receiving yet.
167 // indexed by client nonce.
168 map<nonce_t, list<reply_t>> reply_window_;
170 void free_reply_window(void);
171 void add_reply(nonce_t clt_nonce, xid_t xid, const string & b);
173 rpcstate_t checkduplicate_and_update(nonce_t clt_nonce, xid_t xid,
174 xid_t rep_xid, string & b);
176 // latest connection to the client
177 map<nonce_t, shared_ptr<connection>> conns_;
181 // map proc # to function
182 map<proc_id_t, handler *> procs_;
184 mutex procs_m_; // protect insert/delete to procs[]
185 mutex reply_window_m_; // protect reply window et al
186 mutex conns_m_; // protect conns_
188 void dispatch(shared_ptr<connection> c, const string & buf);
190 // internal handler registration
191 void reg1(proc_id_t proc, handler *);
193 unique_ptr<thread_pool> dispatchpool_;
194 unique_ptr<connection_listener> listener_;
196 // RPC handler for clients binding
197 rpc_protocol::status rpcbind(nonce_t &r);
199 bool got_pdu(const shared_ptr<connection> & c, const string & b);
203 rpcs(in_port_t port);
206 void set_reachable(bool r) { reachable_ = r; }
208 template<class P, class F, class C=void> inline void reg(proc_t<P> proc, F f, C *c=nullptr) {
209 static_assert(is_valid_registration<P, F>::value, "RPC handler registered with incorrect argument types");
210 struct ReturnOnFailure {
211 static inline int unmarshall_args_failure() {
212 return rpc_protocol::unmarshal_args_failure;
215 reg1(proc.id, marshalled_func<F, ReturnOnFailure>::wrap(f, c));