Cosmetic improvements.
[invirt/third/libt4.git] / rpc / rpc.h
1 #ifndef rpc_h
2 #define rpc_h
3
4 #include "types.h"
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 #include "rpc_protocol.h"
9 #include "thr_pool.h"
10 #include "marshall.h"
11 #include "marshall_wrap.h"
12 #include "connection.h"
13
14 namespace rpc {
15     static constexpr milliseconds to_max{12000};
16     static constexpr milliseconds to_min{100};
17 }
18
19 template<class P, class R, class ...Args> struct is_valid_call : false_type {};
20
21 template<class S, class R, class ...Args>
22 struct is_valid_call<S(R &, Args...), R, Args...> : true_type {};
23
24 template<class P, class F> struct is_valid_registration : false_type {};
25
26 template<class S, class R, class ...Args>
27 struct is_valid_registration<S(R &, typename decay<Args>::type...), S(R &, Args...)> : true_type {};
28
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...)> {};
31
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 {
36     private:
37         using proc_id_t = rpc_protocol::proc_id_t;
38         template <class S>
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;
42
43         // manages per rpc info
44         struct caller {
45             caller(xid_t _xid, string *_rep) : xid(_xid), rep(_rep) {}
46
47             int xid;
48             string *rep;
49             int intret;
50             bool done = false;
51             mutex m;
52             cond c;
53         };
54
55         void get_latest_connection(shared_ptr<connection> & ch);
56         void update_xid_rep(xid_t xid, lock & m_lock);
57
58
59         sockaddr_in dst_;
60         nonce_t clt_nonce_;
61         nonce_t srv_nonce_ = 0;
62         bool bind_done_ = false;
63         int lossytest_ = 0;
64         bool reachable_ = true;
65
66         shared_ptr<connection> chan_;
67
68         mutex m_; // protect insert/delete to calls[]
69         mutex chan_m_;
70
71         bool destroy_wait_ = false;
72         cond destroy_wait_c_;
73
74         map<int, caller *> calls_;
75
76         // xid starts with 1 and latest received reply starts with 0
77         xid_t xid_ = 1;
78         list<xid_t> xid_rep_window_ = {0};
79
80         struct request {
81             void clear() { buf.clear(); xid = -1; }
82             bool isvalid() { return xid != -1; }
83             string buf;
84             xid_t xid = -1;
85         };
86         request dup_req_;
87         int xid_rep_done_ = -1;
88
89         int call1(proc_id_t proc, milliseconds to, string & rep, marshall & req);
90
91         template<class R>
92         inline int call_m(proc_id_t proc, milliseconds to, R & r, marshall && req) {
93             string rep;
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.");
100                 VERIFY(0);
101                 return rpc_protocol::unmarshall_reply_failure;
102             }
103             return intret;
104         }
105
106         bool got_pdu(const shared_ptr<connection> & c, const string & b);
107
108     public:
109
110         rpcc(const string & d);
111         ~rpcc();
112
113         nonce_t id() { return clt_nonce_; }
114
115         int bind(milliseconds to = rpc::to_max);
116
117         void set_reachable(bool r) { reachable_ = r; }
118
119         void cancel();
120
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...);
124         }
125
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...)));
130         }
131 };
132
133 // rpc server endpoint.
134 class rpcs : private connection_delegate {
135     private:
136         using proc_id_t = rpc_protocol::proc_id_t;
137         template <class S>
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;
141
142         typedef enum {
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
147         } rpcstate_t;
148
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.
153         struct reply_t {
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) {}
156             xid_t xid;
157             bool cb_present; // whether the reply buffer is valid
158             string buf;      // the reply buffer
159         };
160
161         in_port_t port_;
162         nonce_t nonce_;
163
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_;
168
169         void add_reply(nonce_t clt_nonce, xid_t xid, const string & b);
170
171         rpcstate_t check_duplicate_and_update(nonce_t clt_nonce, xid_t xid,
172                 xid_t rep_xid, string & b);
173
174         // latest connection to the client
175         map<nonce_t, shared_ptr<connection>> conns_;
176
177         bool reachable_ = true;
178
179         // map proc # to function
180         map<proc_id_t, handler *> procs_;
181
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_
185
186         void dispatch(shared_ptr<connection> c, const string & buf);
187
188         unique_ptr<thread_pool> dispatchpool_{new thread_pool(6, false)};
189         unique_ptr<connection_listener> listener_;
190
191         // RPC handler for clients binding
192         rpc_protocol::status rpcbind(nonce_t & r);
193
194         bool got_pdu(const shared_ptr<connection> & c, const string & b);
195
196     public:
197
198         rpcs(in_port_t port);
199         ~rpcs();
200
201         void set_reachable(bool r) { reachable_ = r; }
202
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;
208                 }
209             };
210             lock pl(procs_m_);
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);
214         }
215
216         void start();
217 };
218
219 #endif