Lots more clean-ups
[invirt/third/libt4.git] / rpc / rpc.h
index 20a0aa9..19ec96a 100644 (file)
--- a/rpc/rpc.h
+++ b/rpc/rpc.h
@@ -1,19 +1,19 @@
 #ifndef rpc_h
 #define rpc_h
 
 #ifndef rpc_h
 #define rpc_h
 
+#include "types.h"
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
-#include <list>
-#include <map>
-#include <stdio.h>
 
 #include "thr_pool.h"
 #include "marshall.h"
 
 #include "thr_pool.h"
 #include "marshall.h"
+#include "marshall_wrap.h"
 #include "connection.h"
 
 #include "connection.h"
 
-#ifdef DMALLOC
-#include "dmalloc.h"
-#endif
+namespace rpc {
+    static constexpr milliseconds to_max{12000};
+    static constexpr milliseconds to_min{100};
+}
 
 class rpc_const {
     public:
 
 class rpc_const {
     public:
@@ -31,104 +31,90 @@ class rpc_const {
 // manages a xid space per destination socket
 // threaded: multiple threads can be sending RPCs,
 class rpcc : public chanmgr {
 // manages a xid space per destination socket
 // threaded: multiple threads can be sending RPCs,
 class rpcc : public chanmgr {
-
     private:
 
         //manages per rpc info
         struct caller {
     private:
 
         //manages per rpc info
         struct caller {
-            caller(unsigned int xxid, unmarshall *un);
-            ~caller();
+            caller(int _xid, string *_rep) : xid(_xid), rep(_rep) {}
 
 
-            unsigned int xid;
-            unmarshall *un;
+            int xid;
+            string *rep;
             int intret;
             int intret;
-            bool done;
-            std::mutex m;
-            std::condition_variable c;
+            bool done = false;
+            mutex m;
+            cond c;
         };
 
         void get_refconn(connection **ch);
         };
 
         void get_refconn(connection **ch);
-        void update_xid_rep(unsigned int xid);
+        void update_xid_rep(int xid);
 
 
         sockaddr_in dst_;
         unsigned int clt_nonce_;
         unsigned int srv_nonce_;
         bool bind_done_;
 
 
         sockaddr_in dst_;
         unsigned int clt_nonce_;
         unsigned int srv_nonce_;
         bool bind_done_;
-        unsigned int xid_;
+        int xid_;
         int lossytest_;
         bool retrans_;
         bool reachable_;
 
         connection *chan_;
 
         int lossytest_;
         bool retrans_;
         bool reachable_;
 
         connection *chan_;
 
-        std::mutex m_; // protect insert/delete to calls[]
-        std::mutex chan_m_;
+        mutex m_; // protect insert/delete to calls[]
+        mutex chan_m_;
 
         bool destroy_wait_;
 
         bool destroy_wait_;
-        std::condition_variable destroy_wait_c_;
+        cond destroy_wait_c_;
 
 
-        std::map<int, caller *> calls_;
-        std::list<unsigned int> xid_rep_window_;
+        map<int, caller *> calls_;
+        list<int> xid_rep_window_;
 
         struct request {
 
         struct request {
-            request() { clear(); }
             void clear() { buf.clear(); xid = -1; }
             bool isvalid() { return xid != -1; }
             void clear() { buf.clear(); xid = -1; }
             bool isvalid() { return xid != -1; }
-            std::string buf;
-            int xid;
+            string buf;
+            int xid = -1;
         };
         };
-        struct request dup_req_;
+        request dup_req_;
         int xid_rep_done_;
         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:
 
     public:
 
-        rpcc(sockaddr_in d, bool retrans=true);
+        rpcc(const string & d, bool retrans=true);
         ~rpcc();
 
         ~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_; }
 
         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();
 
         void set_reachable(bool r) { reachable_ = r; }
 
         void cancel();
-                
-                int islossy() { return lossytest_ > 0; }
-
-        int call1(unsigned int proc, 
-                marshall &req, unmarshall &rep, TO to);
-
-        bool got_pdu(connection *c, char *b, int sz);
 
 
-
-        template<class R>
-            int call_m(unsigned int proc, marshall &req, R & r, TO to);
+        bool got_pdu(connection *c, const string & b);
 
         template<class R, typename ...Args>
 
         template<class R, typename ...Args>
-            inline int call(unsigned int proc, R & r, const Args&... args);
+            inline int call(proc_t proc, R & r, const Args&... args);
 
         template<class R, typename ...Args>
 
         template<class R, typename ...Args>
-            inline int call_timeout(unsigned int 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 
 };
 
 template<class R> int 
-rpcc::call_m(unsigned int proc, marshall &req, R & r, TO to) 
+rpcc::call_m(proc_t proc, marshall &req, R & r, milliseconds to) 
 {
 {
-    unmarshall u;
-    int intret = call1(proc, req, u, to);
+    string rep;
+    int intret = call1(proc, req, rep, to);
+    unmarshall u(rep, true);
     if (intret < 0) return intret;
     u >> r;
     if (u.okdone() != true) {
     if (intret < 0) return intret;
     u >> r;
     if (u.okdone() != true) {
-        fprintf(stderr, "rpcc::call_m: failed to unmarshall the reply."
-                "You are probably calling RPC 0x%x with wrong return "
-                "type.\n", proc);
+        cerr << "rpcc::call_m: failed to unmarshall the reply.  You are probably " <<
+                "calling RPC 0x" << hex << proc << " with the wrong return type." << endl;
         VERIFY(0);
         return rpc_const::unmarshal_reply_failure;
     }
         VERIFY(0);
         return rpc_const::unmarshal_reply_failure;
     }
@@ -136,20 +122,18 @@ rpcc::call_m(unsigned int proc, marshall &req, R & r, TO to)
 }
 
 template<class R, typename... Args> inline int
 }
 
 template<class R, typename... Args> inline int
-rpcc::call(unsigned int proc, R & r, const Args&... args)
+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
 }
 
 template<class R, typename... Args> inline int
-rpcc::call_timeout(unsigned int 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);
 }
 
 {
     marshall m{args...};
     return call_m(proc, m, r, to);
 }
 
-bool operator<(const sockaddr_in &a, const sockaddr_in &b);
-
 // rpc server endpoint.
 class rpcs : public chanmgr {
 
 // rpc server endpoint.
 class rpcs : public chanmgr {
 
@@ -167,103 +151,82 @@ class rpcs : public chanmgr {
         // has been sent; in that case buf points to a copy of the reply,
         // and sz holds the size of the reply.
     struct reply_t {
         // has been sent; in that case buf points to a copy of the reply,
         // and sz holds the size of the reply.
     struct reply_t {
-        reply_t (unsigned int _xid) {
-            xid = _xid;
-            cb_present = false;
-            buf = NULL;
-            sz = 0;
-        }
-        reply_t (unsigned int _xid, char *_buf, int _sz) {
-            xid = _xid;
-            cb_present = true;
-            buf = _buf;
-            sz = _sz;
-        }
-        unsigned int xid;
+        reply_t (int _xid) : xid(_xid), cb_present(false) {}
+        reply_t (int _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
+        int xid;
         bool cb_present; // whether the reply buffer is valid
         bool cb_present; // whether the reply buffer is valid
-        char *buf;      // the reply buffer
-        int sz;         // the size of reply buffer
+        string buf;      // the reply buffer
     };
 
     };
 
-    int port_;
+    in_port_t port_;
     unsigned int nonce_;
 
     // provide at most once semantics by maintaining a window of replies
     // per client that that client hasn't acknowledged receiving yet.
         // indexed by client nonce.
     unsigned int nonce_;
 
     // provide at most once semantics by maintaining a window of replies
     // per client that that client hasn't acknowledged receiving yet.
         // indexed by client nonce.
-    std::map<unsigned int, std::list<reply_t> > reply_window_;
+    map<unsigned int, list<reply_t> > reply_window_;
 
     void free_reply_window(void);
 
     void free_reply_window(void);
-    void add_reply(unsigned int clt_nonce, unsigned int xid, char *b, int sz);
+    void add_reply(unsigned int clt_nonce, int xid, const string & b);
 
     rpcstate_t checkduplicate_and_update(unsigned int clt_nonce, 
 
     rpcstate_t checkduplicate_and_update(unsigned int clt_nonce, 
-            unsigned int xid, unsigned int rep_xid,
-            char **b, int *sz);
+            int xid, int rep_xid, string & b);
 
 
-    void updatestat(unsigned int proc);
+    void updatestat(proc_t proc);
 
     // latest connection to the client
 
     // latest connection to the client
-    std::map<unsigned int, connection *> conns_;
+    map<unsigned int, connection *> conns_;
 
     // counting
 
     // counting
-    const int counting_;
-    int curr_counts_;
-    std::map<int, int> counts_;
+    const size_t counting_;
+    size_t curr_counts_;
+    map<proc_t, size_t> counts_;
 
 
-    int lossytest_; 
     bool reachable_;
 
     // map proc # to function
     bool reachable_;
 
     // map proc # to function
-    std::map<int, handler *> procs_;
+    map<proc_t, handler *> procs_;
 
 
-    std::mutex procs_m_; // protect insert/delete to procs[]
-    std::mutex count_m_;  //protect modification of counts
-    std::mutex reply_window_m_; // protect reply window et al
-    std::mutex conss_m_; // protect conns_
+    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 conns_m_; // protect conns_
 
 
     protected:
 
     struct djob_t {
 
 
     protected:
 
     struct djob_t {
-        djob_t (connection *c, char *b, int bsz):buf(b),sz(bsz),conn(c) {}
-        char *buf;
-        int sz;
         connection *conn;
         connection *conn;
+        string buf;
     };
     void dispatch(djob_t *);
 
     // internal handler registration
     };
     void dispatch(djob_t *);
 
     // internal handler registration
-    void reg1(unsigned int proc, handler *);
+    void reg1(proc_t proc, handler *);
 
     ThrPool* dispatchpool_;
 
     ThrPool* dispatchpool_;
-    tcpsconn* listener_;
+    tcpsconn *listener_;
 
     public:
 
     public:
-    rpcs(unsigned int port, int counts=0);
+    rpcs(in_port_t port, size_t counts=0);
     ~rpcs();
     ~rpcs();
-    inline int port() { return listener_->port(); }
+    inline in_port_t port() { return listener_->port(); }
     //RPC handler for clients binding
     //RPC handler for clients binding
-    int rpcbind(int &r, int a);
+    int rpcbind(unsigned int &r, int a);
 
     void set_reachable(bool r) { reachable_ = r; }
 
 
     void set_reachable(bool r) { reachable_ = r; }
 
-    bool got_pdu(connection *c, char *b, int sz);
+    bool got_pdu(connection *c, const string & b);
 
 
-    template<class F, class C=void> void reg(unsigned int 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(unsigned int proc, F f, C *c) {
-    reg1(proc, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
-}
-
-void make_sockaddr(const char *hostandport, struct sockaddr_in *dst);
-void make_sockaddr(const char *host, const char *port,
-        struct sockaddr_in *dst);
-
 #endif
 #endif