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_latest_connection(shared_ptr<connection> & ch);
56 void update_xid_rep(xid_t xid, lock & m_lock);
61 nonce_t srv_nonce_ = 0;
62 bool bind_done_ = false;
64 bool reachable_ = true;
66 shared_ptr<connection> chan_;
68 mutex m_; // protect insert/delete to calls[]
71 bool destroy_wait_ = false;
74 map<int, caller *> calls_;
76 // xid starts with 1 and latest received reply starts with 0
78 list<xid_t> xid_rep_window_ = {0};
81 void clear() { buf.clear(); xid = -1; }
82 bool isvalid() { return xid != -1; }
87 int xid_rep_done_ = -1;
89 int call1(proc_id_t proc, milliseconds to, string & rep, marshall & req);
92 inline int call_m(proc_id_t proc, milliseconds to, R & r, marshall && req) {
94 int intret = call1(proc, to, rep, req);
95 if (intret < 0) return intret;
96 unmarshall u(rep, true, r);
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::unmarshall_reply_failure;
106 bool got_pdu(const shared_ptr<connection> & c, const string & b);
110 rpcc(const string & d);
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");
129 return call_m(proc.id, to, r, forward<marshall>(marshall(args...)));
133 // rpc server endpoint.
134 class rpcs : private connection_delegate {
136 using proc_id_t = rpc_protocol::proc_id_t;
138 using proc_t = rpc_protocol::proc_t<S>;
139 using nonce_t = rpc_protocol::nonce_t;
140 using xid_t = rpc_protocol::xid_t;
143 NEW, // new RPC, not a duplicate
144 INPROGRESS, // duplicate of an RPC we're still processing
145 DONE, // duplicate of an RPC we already replied to (have reply)
146 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
149 // state about an in-progress or completed RPC, for at-most-once.
150 // if cb_present is true, then the RPC is complete and a reply
151 // has been sent; in that case buf points to a copy of the reply,
152 // and sz holds the size of the reply.
154 reply_t (xid_t _xid) : xid(_xid), cb_present(false) {}
155 reply_t (xid_t _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
157 bool cb_present; // whether the reply buffer is valid
158 string buf; // the reply buffer
164 // provide at most once semantics by maintaining a window of replies
165 // per client that that client hasn't acknowledged receiving yet.
166 // indexed by client nonce.
167 map<nonce_t, list<reply_t>> reply_window_;
169 void add_reply(nonce_t clt_nonce, xid_t xid, const string & b);
171 rpcstate_t check_duplicate_and_update(nonce_t clt_nonce, xid_t xid,
172 xid_t rep_xid, string & b);
174 // latest connection to the client
175 map<nonce_t, shared_ptr<connection>> conns_;
177 bool reachable_ = true;
179 // map proc # to function
180 map<proc_id_t, handler *> procs_;
182 mutex procs_m_; // protect insert/delete to procs[]
183 mutex reply_window_m_; // protect reply window et al
184 mutex conns_m_; // protect conns_
186 void dispatch(shared_ptr<connection> c, const string & buf);
188 unique_ptr<thread_pool> dispatchpool_{new thread_pool(6, false)};
189 unique_ptr<connection_listener> listener_;
191 // RPC handler for clients binding
192 rpc_protocol::status rpcbind(nonce_t & r);
194 bool got_pdu(const shared_ptr<connection> & c, const string & b);
198 rpcs(in_port_t port);
201 void set_reachable(bool r) { reachable_ = r; }
203 template<class P, class F, class C=void> inline void reg(proc_t<P> proc, F f, C *c=nullptr) {
204 static_assert(is_valid_registration<P, F>::value, "RPC handler registered with incorrect argument types");
205 struct ReturnOnFailure {
206 static inline int unmarshall_args_failure() {
207 return rpc_protocol::unmarshall_args_failure;
211 VERIFY(procs_.count(proc.id) == 0);
212 procs_[proc.id] = marshalled_func<F, ReturnOnFailure>::wrap(f, c);
213 VERIFY(procs_.count(proc.id) >= 1);