Lots of clean-ups and simplifications
[invirt/third/libt4.git] / rpc / connection.cc
index fec7a4c..6e865e8 100644 (file)
@@ -25,7 +25,7 @@ connection::connection(chanmgr *m1, int f1, int l1)
 
     signal(SIGPIPE, SIG_IGN);
 
-    VERIFY(gettimeofday(&create_time_, NULL) == 0);
+    create_time_ = std::chrono::steady_clock::now();
 
     PollMgr::Instance()->add_callback(fd_, CB_RDONLY, this);
 }
@@ -98,13 +98,9 @@ connection::ref()
 int
 connection::compare(connection *another)
 {
-    if (create_time_.tv_sec > another->create_time_.tv_sec)
+    if (create_time_ > another->create_time_)
         return 1;
-    if (create_time_.tv_sec < another->create_time_.tv_sec)
-        return -1;
-    if (create_time_.tv_usec > another->create_time_.tv_usec)
-        return 1;
-    if (create_time_.tv_usec < another->create_time_.tv_usec)
+    if (create_time_ < another->create_time_)
         return -1;
     return 0;
 }
@@ -137,9 +133,9 @@ connection::send(char *b, int sz)
         ml.unlock();
                PollMgr::Instance()->block_remove_fd(fd_);
         ml.lock();
-       }else{
+       } else {
                if (wpdu_.solong == wpdu_.sz) {
-               }else{
+               } else {
                        //should be rare to need to explicitly add write callback
                        PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
                        while (!dead_ && wpdu_.solong >= 0 && wpdu_.solong < wpdu_.sz) {
@@ -293,7 +289,7 @@ tcpsconn::tcpsconn(chanmgr *m1, int port, int lossytest)
        sin.sin_port = htons(port);
 
        tcp_ = socket(AF_INET, SOCK_STREAM, 0);
-       if(tcp_ < 0){
+       if (tcp_ < 0) {
                perror("tcpsconn::tcpsconn accept_loop socket:");
                VERIFY(0);
        }
@@ -302,12 +298,12 @@ tcpsconn::tcpsconn(chanmgr *m1, int port, int lossytest)
        setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
        setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
 
-       if(bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0){
+       if (bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0) {
                perror("accept_loop tcp bind:");
                VERIFY(0);
        }
 
-       if(listen(tcp_, 1000) < 0) {
+       if (listen(tcp_, 1000) < 0) {
                perror("tcpsconn::tcpsconn listen:");
                VERIFY(0);
        }
@@ -359,21 +355,21 @@ tcpsconn::process_accept()
                        s1, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
        connection *ch = new connection(mgr_, s1, lossy_);
 
-        // garbage collect all dead connections with refcount of 1
-        std::map<int, connection *>::iterator i;
-        for (i = conns_.begin(); i != conns_.end();) {
-                if (i->second->isdead() && i->second->ref() == 1) {
-                       jsl_log(JSL_DBG_2, "accept_loop garbage collected fd=%d\n",
-                                       i->second->channo());
-                        i->second->decref();
-                        // Careful not to reuse i right after erase. (i++) will
-                        // be evaluated before the erase call because in C++,
-                        // there is a sequence point before a function call.
-                        // See http://en.wikipedia.org/wiki/Sequence_point.
-                        conns_.erase(i++);
-                } else
-                        ++i;
-        }
+    // garbage collect all dead connections with refcount of 1
+    std::map<int, connection *>::iterator i;
+    for (i = conns_.begin(); i != conns_.end();) {
+        if (i->second->isdead() && i->second->ref() == 1) {
+            jsl_log(JSL_DBG_2, "accept_loop garbage collected fd=%d\n",
+                    i->second->channo());
+            i->second->decref();
+            // Careful not to reuse i right after erase. (i++) will
+            // be evaluated before the erase call because in C++,
+            // there is a sequence point before a function call.
+            // See http://en.wikipedia.org/wiki/Sequence_point.
+            conns_.erase(i++);
+        } else
+            ++i;
+    }
 
        conns_[ch->channo()] = ch;
 }
@@ -385,7 +381,6 @@ tcpsconn::accept_conn()
        int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
 
     try {
-
         while (1) {
             FD_ZERO(&rfds);
             FD_SET(pipe_[0], &rfds);
@@ -417,17 +412,16 @@ tcpsconn::accept_conn()
     }
     catch (thread_exit_exception e)
     {
-        return;
     }
 }
 
 connection *
 connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy)
 {
-       int s= socket(AF_INET, SOCK_STREAM, 0);
+       int s = socket(AF_INET, SOCK_STREAM, 0);
        int yes = 1;
        setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
-       if(connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
+       if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
                jsl_log(JSL_DBG_1, "rpcc::connect_to_dst failed to %s:%d\n",
                                inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
                close(s);