}
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_);
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_);
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);
}
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;
}
pipe_[1].close();
th_.join();
- // close all the active connections
- map<int, connection *>::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() {
}
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<connection>(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;
}
}
}
-connection * connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) {
+shared_ptr<connection> 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<connection>(mgr, s, lossy);
}