Includes cleanups
[invirt/third/libt4.git] / rpc / rpc.h
index 065cabc..02c7c62 100644 (file)
--- a/rpc/rpc.h
+++ b/rpc/rpc.h
 #include "marshall_wrap.h"
 #include "connection.h"
 
+namespace rpc {
+    static constexpr milliseconds to_max{12000};
+    static constexpr milliseconds to_min{100};
+}
+
 class rpc_const {
     public:
         static const unsigned int bind = 1;   // handler number reserved for bind
@@ -26,7 +31,6 @@ class rpc_const {
 // manages a xid space per destination socket
 // threaded: multiple threads can be sending RPCs,
 class rpcc : public chanmgr {
-
     private:
 
         //manages per rpc info
@@ -41,7 +45,7 @@ class rpcc : public chanmgr {
             cond c;
         };
 
-        void get_refconn(connection **ch);
+        void get_refconn(shared_ptr<connection> & ch);
         void update_xid_rep(int xid);
 
 
@@ -54,7 +58,7 @@ class rpcc : public chanmgr {
         bool retrans_;
         bool reachable_;
 
-        connection *chan_;
+        shared_ptr<connection> chan_;
 
         mutex m_; // protect insert/delete to calls[]
         mutex chan_m_;
@@ -66,52 +70,42 @@ class rpcc : public chanmgr {
         list<int> xid_rep_window_;
 
         struct request {
-            request() { clear(); }
             void clear() { buf.clear(); xid = -1; }
             bool isvalid() { return xid != -1; }
             string buf;
-            int xid;
+            int xid = -1;
         };
-        struct request dup_req_;
+        request dup_req_;
         int xid_rep_done_;
+
+        int call1(proc_t proc, marshall &req, string &rep, milliseconds to);
+
+        template<class R>
+            int call_m(proc_t proc, marshall &req, R & r, milliseconds to);
     public:
 
         rpcc(const string & d, bool retrans=true);
         ~rpcc();
 
-        struct TO {
-            int to;
-        };
-        static const TO to_max;
-        static const TO to_min;
-        static TO to(int x) { TO t; t.to = x; return t;}
-
         unsigned int id() { return clt_nonce_; }
 
-        int bind(TO to = to_max);
+        int bind(milliseconds to = rpc::to_max);
 
         void set_reachable(bool r) { reachable_ = r; }
 
         void cancel();
 
-        int islossy() { return lossytest_ > 0; }
-
-        int call1(proc_t proc, marshall &req, string &rep, TO to);
-
-        bool got_pdu(connection *c, const string & b);
-
-        template<class R>
-            int call_m(proc_t proc, marshall &req, R & r, TO to);
+        bool got_pdu(const shared_ptr<connection> & c, const string & b);
 
         template<class R, typename ...Args>
             inline int call(proc_t proc, R & r, const Args&... args);
 
         template<class R, typename ...Args>
-            inline int call_timeout(proc_t proc, TO to, R & r, const Args&... args);
+            inline int call_timeout(proc_t proc, milliseconds to, R & r, const Args&... args);
 };
 
 template<class R> int 
-rpcc::call_m(proc_t proc, marshall &req, R & r, TO to) 
+rpcc::call_m(proc_t proc, marshall &req, R & r, milliseconds to) 
 {
     string rep;
     int intret = call1(proc, req, rep, to);
@@ -130,11 +124,11 @@ rpcc::call_m(proc_t proc, marshall &req, R & r, TO to)
 template<class R, typename... Args> inline int
 rpcc::call(proc_t proc, R & r, const Args&... args)
 {
-    return call_timeout(proc, rpcc::to_max, r, args...);
+    return call_timeout(proc, rpc::to_max, r, args...);
 }
 
 template<class R, typename... Args> inline int
-rpcc::call_timeout(proc_t proc, const rpcc::TO to, R & r, const Args&... args)
+rpcc::call_timeout(proc_t proc, const milliseconds to, R & r, const Args&... args)
 {
     marshall m{args...};
     return call_m(proc, m, r, to);
@@ -181,7 +175,7 @@ class rpcs : public chanmgr {
     void updatestat(proc_t proc);
 
     // latest connection to the client
-    map<unsigned int, connection *> conns_;
+    map<unsigned int, shared_ptr<connection>> conns_;
 
     // counting
     const size_t counting_;
@@ -196,22 +190,18 @@ class rpcs : public chanmgr {
     mutex procs_m_; // protect insert/delete to procs[]
     mutex count_m_;  //protect modification of counts
     mutex reply_window_m_; // protect reply window et al
-    mutex conss_m_; // protect conns_
+    mutex conns_m_; // protect conns_
 
 
     protected:
 
-    struct djob_t {
-        connection *conn;
-        string buf;
-    };
-    void dispatch(djob_t *);
+    void dispatch(shared_ptr<connection> c, const string & buf);
 
     // internal handler registration
     void reg1(proc_t proc, handler *);
 
-    ThrPool* dispatchpool_;
-    tcpsconn *listener_;
+    unique_ptr<ThrPool> dispatchpool_;
+    unique_ptr<tcpsconn> listener_;
 
     public:
     rpcs(in_port_t port, size_t counts=0);
@@ -222,19 +212,19 @@ class rpcs : public chanmgr {
 
     void set_reachable(bool r) { reachable_ = r; }
 
-    bool got_pdu(connection *c, const string & b);
+    bool got_pdu(const shared_ptr<connection> & c, const string & b);
 
-    template<class F, class C=void> void reg(proc_t proc, F f, C *c=nullptr);
-};
+    struct ReturnOnFailure {
+        static inline int unmarshall_args_failure() {
+            return rpc_const::unmarshal_args_failure;
+        }
+    };
 
-struct ReturnOnFailure {
-    static inline int unmarshall_args_failure() {
-        return rpc_const::unmarshal_args_failure;
+    template<class F, class C=void> void reg(proc_t proc, F f, C *c=nullptr) {
+        reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
     }
-};
 
-template<class F, class C> void rpcs::reg(proc_t proc, F f, C *c) {
-    reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
-}
+    void start();
+};
 
 #endif