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>;
41 // manages per rpc info
43 caller(int _xid, string *_rep) : xid(_xid), rep(_rep) {}
53 void get_refconn(shared_ptr<connection> & ch);
54 void update_xid_rep(int xid);
58 unsigned int clt_nonce_;
59 unsigned int srv_nonce_;
66 shared_ptr<connection> chan_;
68 mutex m_; // protect insert/delete to calls[]
74 map<int, caller *> calls_;
75 list<int> xid_rep_window_;
78 void clear() { buf.clear(); xid = -1; }
79 bool isvalid() { return xid != -1; }
86 int call1(proc_id_t proc, marshall &req, string &rep, milliseconds to);
89 int call_m(proc_id_t proc, marshall &req, R & r, milliseconds to) {
91 int intret = call1(proc, req, rep, to);
92 unmarshall u(rep, true);
93 if (intret < 0) return intret;
95 if (u.okdone() != true) {
96 LOG("rpcc::call_m: failed to unmarshall the reply. You are probably " <<
97 "calling RPC 0x" << hex << proc << " with the wrong return type.");
99 return rpc_protocol::unmarshal_reply_failure;
104 bool got_pdu(const shared_ptr<connection> & c, const string & b);
108 rpcc(const string & d, bool retrans=true);
111 unsigned int id() { return clt_nonce_; }
113 int bind(milliseconds to = rpc::to_max);
115 void set_reachable(bool r) { reachable_ = r; }
119 template<class P, class R, typename ...Args>
120 inline int call(proc_t<P> proc, R & r, const Args&... args) {
121 return call_timeout(proc, rpc::to_max, r, args...);
124 template<class P, class R, typename ...Args>
125 inline int call_timeout(proc_t<P> proc, milliseconds to, R & r, const Args&... args) {
126 static_assert(is_valid_call<P, R, Args...>::value, "RPC called with incorrect argument types");
128 return call_m(proc.id, m, r, to);
132 // rpc server endpoint.
133 class rpcs : private connection_delegate {
135 using proc_id_t = rpc_protocol::proc_id_t;
137 using proc_t = rpc_protocol::proc_t<S>;
140 NEW, // new RPC, not a duplicate
141 INPROGRESS, // duplicate of an RPC we're still processing
142 DONE, // duplicate of an RPC we already replied to (have reply)
143 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
146 // state about an in-progress or completed RPC, for at-most-once.
147 // if cb_present is true, then the RPC is complete and a reply
148 // has been sent; in that case buf points to a copy of the reply,
149 // and sz holds the size of the reply.
151 reply_t (int _xid) : xid(_xid), cb_present(false) {}
152 reply_t (int _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
154 bool cb_present; // whether the reply buffer is valid
155 string buf; // the reply buffer
161 // provide at most once semantics by maintaining a window of replies
162 // per client that that client hasn't acknowledged receiving yet.
163 // indexed by client nonce.
164 map<unsigned int, list<reply_t>> reply_window_;
166 void free_reply_window(void);
167 void add_reply(unsigned int clt_nonce, int xid, const string & b);
169 rpcstate_t checkduplicate_and_update(unsigned int clt_nonce,
170 int xid, int rep_xid, string & b);
172 void updatestat(proc_id_t proc);
174 // latest connection to the client
175 map<unsigned int, shared_ptr<connection>> conns_;
178 const size_t counting_;
180 map<proc_id_t, size_t> counts_;
184 // map proc # to function
185 map<proc_id_t, handler *> procs_;
187 mutex procs_m_; // protect insert/delete to procs[]
188 mutex count_m_; // protect modification of counts
189 mutex reply_window_m_; // protect reply window et al
190 mutex conns_m_; // protect conns_
192 void dispatch(shared_ptr<connection> c, const string & buf);
194 // internal handler registration
195 void reg1(proc_id_t proc, handler *);
197 unique_ptr<thread_pool> dispatchpool_;
198 unique_ptr<tcpsconn> listener_;
200 // RPC handler for clients binding
201 rpc_protocol::status rpcbind(unsigned int &r, int a);
203 bool got_pdu(const shared_ptr<connection> & c, const string & b);
207 rpcs(in_port_t port, size_t counts=0);
210 void set_reachable(bool r) { reachable_ = r; }
212 template<class P, class F, class C=void> void reg(proc_t<P> proc, F f, C *c=nullptr) {
213 static_assert(is_valid_registration<P, F>::value, "RPC handler registered with incorrect argument types");
214 struct ReturnOnFailure {
215 static inline int unmarshall_args_failure() {
216 return rpc_protocol::unmarshal_args_failure;
219 reg1(proc.id, marshalled_func<F, ReturnOnFailure>::wrap(f, c));