From 5f88ebb43acc7b7fa3ac272a444677dd72ccb63d Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 14 Nov 2018 23:14:32 -0500 Subject: fix bugs in `terminate` --- include/salticidae/conn.h | 13 ++++++++----- include/salticidae/event.h | 2 +- include/salticidae/network.h | 10 ++++------ 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 73b3022..480809f 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -123,12 +123,11 @@ class ConnPool { protected: /** Close the IO and clear all on-going or planned events. */ virtual void stop() { - if (fd == -1) return; + if (!self_ref) return; ev_connect.clear(); ev_socket.clear(); send_buffer.get_queue().unreg_handler(); ::close(fd); - fd = -1; self_ref = nullptr; /* remove the self-cycle */ } @@ -189,7 +188,7 @@ class ConnPool { .get_queue() .reg_handler(this->ec, [conn, client_fd] (MPSCWriteBuffer::queue_t &) { - if (conn->ready_send && conn->fd != -1) + if (conn->ready_send && conn->self_ref) { conn->ev_socket.del(); conn->ev_socket.add(Event::READ | Event::WRITE); @@ -340,9 +339,13 @@ class ConnPool { void terminate(const conn_t &conn, bool blocking = true) { int fd = conn->fd; - conn->worker->get_tcall()->call([conn](ThreadCall::Handle &) { + auto worker = conn->worker; + if (worker) + worker->get_tcall()->call([conn](ThreadCall::Handle &) { + conn->stop(); + }, blocking); + else conn->stop(); - }, blocking); remove_conn(fd); } }; diff --git a/include/salticidae/event.h b/include/salticidae/event.h index 616f598..3fd11b6 100644 --- a/include/salticidae/event.h +++ b/include/salticidae/event.h @@ -85,7 +85,7 @@ class Event { static inline void fd_then(uv_poll_t *h, int status, int events) { if (status != 0) { - SALTICIDAE_LOG_WARN("%s", uv_strerror(status)); + //SALTICIDAE_LOG_WARN("%s", uv_strerror(status)); return; } auto event = static_cast(h->data); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index a63976b..60d8f20 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -405,7 +405,7 @@ void MsgNetwork::Conn::on_read() { ConnPool::Conn::on_read(); auto &recv_buffer = this->recv_buffer; auto mn = get_net(); - while (fd != -1) + while (self_ref) { if (msg_state == Conn::HEADER) { @@ -472,11 +472,6 @@ void PeerNetwork::Conn::on_setup() { SALTICIDAE_LOG_INFO("peer ping-pong timeout"); conn->terminate(); }); - if (this->get_mode() == Conn::ConnMode::ACTIVE) - { - peer_id = this->get_addr(); - if (pn->id_mode == IP_BASED) peer_id.port = 0; - } /* the initial ping-pong to set up the connection */ tcall_reset_timeout(worker, conn, pn->conn_timeout); pn->send_msg(MsgPing(pn->listen_port), *conn); @@ -584,6 +579,9 @@ void PeerNetwork::start_active_conn(const NetAddr &addr) { auto conn = static_pointer_cast(MsgNet::_connect(addr)); assert(p->conn == nullptr); p->conn = conn; + conn->peer_id = addr; + if (id_mode == IP_BASED) + conn->peer_id.port = 0; } /* end: functions invoked by the dispatcher */ -- cgit v1.2.3