diff options
author | Determinant <[email protected]> | 2018-07-19 16:33:23 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2018-07-19 16:33:23 -0400 |
commit | d54981cb19f923fab8c4aa1adc369076b8a50962 (patch) | |
tree | b2712feb0617bf5a3f1d4c51ec717b7ca05261bb | |
parent | b4bf23c07601560d708fbdd7c20aca20b630c983 (diff) |
...
-rw-r--r-- | include/salticidae/conn.h | 4 | ||||
-rw-r--r-- | src/conn.cpp | 48 |
2 files changed, 24 insertions, 28 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 1a47904..f352320 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -174,10 +174,9 @@ class ConnPool { void recv_data(evutil_socket_t, short); void send_data(evutil_socket_t, short); void conn_server(evutil_socket_t, short); - void try_conn(); public: - Conn(): self_ref(this) {} + Conn(): self_ref(this), ready_send(false) {} Conn(const Conn &) = delete; Conn(Conn &&other) = delete; @@ -219,6 +218,7 @@ class ConnPool { ev_connect.clear(); ::close(fd); fd = -1; + self_ref = nullptr; /* remove the self-cycle */ } /** Called when new data is available. */ diff --git a/src/conn.cpp b/src/conn.cpp index e600ec9..ba197a0 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -150,7 +150,6 @@ void ConnPool::accept_client(evutil_socket_t fd, short) { std::bind(&Conn::send_data, conn_ptr, _1, _2)); conn->ev_read.add(); conn->ev_write.add(); - conn->ready_send = false; add_conn(conn); SALTICIDAE_LOG_INFO("created %s", std::string(*conn).c_str()); conn->on_setup(); @@ -169,7 +168,6 @@ void ConnPool::Conn::conn_server(evutil_socket_t fd, short events) { ev_read.add(); ev_write.add(); ev_connect.clear(); - ready_send = false; SALTICIDAE_LOG_INFO("connected to peer %s", std::string(*this).c_str()); on_setup(); } @@ -220,30 +218,9 @@ void ConnPool::Conn::terminate() { close(); /* inform the upper layer the connection will be destroyed */ on_teardown(); - conn->self_ref = nullptr; /* remove the self-cycle */ } } -void ConnPool::Conn::try_conn() { - auto conn = self(); /* pin the connection */ - struct sockaddr_in sockin; - memset(&sockin, 0, sizeof(struct sockaddr_in)); - sockin.sin_family = AF_INET; - sockin.sin_addr.s_addr = addr.ip; - sockin.sin_port = addr.port; - - if (::connect(fd, (struct sockaddr *)&sockin, - sizeof(struct sockaddr_in)) < 0 && errno != EINPROGRESS) - { - SALTICIDAE_LOG_INFO("cannot connect to %s", std::string(addr).c_str()); - terminate(); - return; - } - ev_connect = Event(cpool->eb, fd, EV_WRITE, - std::bind(&Conn::conn_server, this, _1, _2)); - ev_connect.add_with_timeout(cpool->conn_server_timeout); -} - ConnPool::conn_t ConnPool::connect(const NetAddr &addr) { int fd; int one = 1; @@ -260,9 +237,28 @@ ConnPool::conn_t ConnPool::connect(const NetAddr &addr) { conn->cpool = this; conn->mode = Conn::ACTIVE; conn->addr = addr; - conn->try_conn(); - add_conn(conn); - SALTICIDAE_LOG_INFO("created %s", std::string(*conn).c_str()); + + struct sockaddr_in sockin; + memset(&sockin, 0, sizeof(struct sockaddr_in)); + sockin.sin_family = AF_INET; + sockin.sin_addr.s_addr = addr.ip; + sockin.sin_port = addr.port; + + if (::connect(fd, (struct sockaddr *)&sockin, + sizeof(struct sockaddr_in)) < 0 && errno != EINPROGRESS) + { + SALTICIDAE_LOG_INFO("cannot connect to %s", std::string(addr).c_str()); + conn->terminate(); + } + else + { + conn->ev_connect = Event(eb, fd, EV_WRITE, + std::bind(&Conn::conn_server, conn.get(), _1, _2)); + conn->ev_connect.add_with_timeout(conn_server_timeout); + + add_conn(conn); + SALTICIDAE_LOG_INFO("created %s", std::string(*conn).c_str()); + } return conn; } |