X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/3abd3952c1f4441f0dd6eae9883b2d01ed9cd56b..3615d9bf0b254442e2fddee45475dbd634cf703a:/rpc/connection.cc diff --git a/rpc/connection.cc b/rpc/connection.cc index 33e891c..cc9f03c 100644 --- a/rpc/connection.cc +++ b/rpc/connection.cc @@ -20,20 +20,11 @@ connection::connection(chanmgr *m1, int f1, int l1) } connection::~connection() { + closeconn(); VERIFY(dead_); VERIFY(!wpdu_.buf.size()); } -void connection::incref() { - lock rl(ref_m_); - refno_++; -} - -bool connection::isdead() { - lock ml(m_); - return dead_; -} - void connection::closeconn() { { lock ml(m_); @@ -47,29 +38,6 @@ void connection::closeconn() { PollMgr::Instance().block_remove_fd(fd_); } -void connection::decref() { - bool dead = false; - { - lock rl(ref_m_); - refno_--; - VERIFY(refno_>=0); - if (refno_==0) { - lock ml(m_); - dead = dead_; - } - } - if (dead) - delete this; -} - -int connection::compare(connection *another) { - if (create_time_ > another->create_time_) - return 1; - if (create_time_ < another->create_time_) - return -1; - return 0; -} - bool connection::send(const string & b) { lock ml(m_); @@ -131,7 +99,7 @@ void connection::write_cb(int s) { send_complete_.notify_one(); } -//fd_ is ready to be read +// fd_ is ready to be read void connection::read_cb(int s) { lock ml(m_); VERIFY(fd_ == s); @@ -154,8 +122,8 @@ void connection::read_cb(int s) { } if (rpdu_.buf.size() && rpdu_.buf.size() == rpdu_.solong) { - if (mgr_->got_pdu(this, rpdu_.buf)) { - //chanmgr has successfully consumed the pdu + if (mgr_->got_pdu(shared_from_this(), rpdu_.buf)) { + // chanmgr has successfully consumed the pdu rpdu_.buf.clear(); rpdu_.solong = 0; } @@ -277,12 +245,8 @@ tcpsconn::~tcpsconn() pipe_[1].close(); th_.join(); - // close all the active connections - map::iterator i; - for (i = conns_.begin(); i != conns_.end(); i++) { - i->second->closeconn(); - i->second->decref(); - } + for (auto & i : conns_) + i.second->closeconn(); } void tcpsconn::process_accept() { @@ -295,19 +259,13 @@ void tcpsconn::process_accept() { } IF_LEVEL(2) LOG("accept_loop got connection fd=" << s1 << " " << inet_ntoa(sin.sin_addr) << ":" << ntoh(sin.sin_port)); - connection *ch = new connection(mgr_, s1, lossy_); + auto ch = make_shared(mgr_, s1, lossy_); - // garbage collect all dead connections with refcount of 1 + // garbage collect dead connections for (auto i = conns_.begin(); i != conns_.end();) { - if (i->second->isdead() && i->second->ref() == 1) { - IF_LEVEL(2) LOG("accept_loop garbage collected fd=" << 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. + if (i->second->isdead()) conns_.erase(i++); - } else + else ++i; } @@ -347,16 +305,16 @@ void tcpsconn::accept_conn() { } } -connection * connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) { +shared_ptr connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) { 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_LEVEL(1) LOG_NONMEMBER("failed to " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port)); close(s); - return NULL; + return nullptr; } IF_LEVEL(2) LOG_NONMEMBER("connect_to_dst fd=" << s << " to dst " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port)); - return new connection(mgr, s, lossy); + return make_shared(mgr, s, lossy); }