From 54f5abf3d4dac9cdf04846d5b952799c31753c9d Mon Sep 17 00:00:00 2001 From: Determinant Date: Sat, 29 Jun 2019 14:13:59 -0400 Subject: ... --- include/salticidae/conn.h | 10 +++------- include/salticidae/event.h | 2 ++ include/salticidae/network.h | 35 ++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index c34521a..a1b5633 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -68,7 +68,6 @@ class ConnPool { enum ConnMode { ACTIVE, /**< the connection is established by connect() */ PASSIVE, /**< the connection is established by accept() */ - DEAD, /**< the connection is dead */ }; protected: @@ -77,7 +76,7 @@ class ConnPool { int fd; Worker *worker; ConnPool *cpool; - std::atomic mode; + ConnMode mode; NetAddr addr; MPSCWriteBuffer send_buffer; @@ -115,7 +114,7 @@ class ConnPool { Conn(Conn &&other) = delete; virtual ~Conn() { - std::atomic_thread_fence(std::memory_order_acquire); + //std::atomic_thread_fence(std::memory_order_acquire); SALTICIDAE_LOG_INFO("destroyed %s", std::string(*this).c_str()); } @@ -132,7 +131,6 @@ class ConnPool { const X509 *get_peer_cert() const { return peer_cert.get(); } ConnMode get_mode() const { return mode; } ConnPool *get_pool() const { return cpool; } - MPSCWriteBuffer &get_send_buffer() { return send_buffer; } /** Write data to the connection (non-blocking). The data will be sent * whenever I/O is available. */ @@ -234,8 +232,7 @@ class ConnPool { } void enable_send_buffer(const conn_t &conn, int client_fd) { - conn->get_send_buffer() - .get_queue() + conn->send_buffer.get_queue() .reg_handler(this->ec, [conn, client_fd] (MPSCWriteBuffer::queue_t &) { if (conn->ready_send) @@ -252,7 +249,6 @@ class ConnPool { /* the caller should finalize all the preparation */ tcall.async_call([this, conn, client_fd](ThreadCall::Handle &) { try { - assert(conn->mode != Conn::ConnMode::DEAD); auto cpool = conn->cpool; if (cpool->enable_tls) { diff --git a/include/salticidae/event.h b/include/salticidae/event.h index 9b8bea1..78ae12d 100644 --- a/include/salticidae/event.h +++ b/include/salticidae/event.h @@ -305,6 +305,8 @@ class TimedFdEvent: public FdEvent, public TimerEvent { return *this; } + ~TimedFdEvent() { clear(); } + void clear() { TimerEvent::clear(); FdEvent::clear(); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index f5fba90..2b0c5b3 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -282,7 +282,11 @@ class PeerNetwork: public MsgNetwork { public: Conn(): MsgNet::Conn(), peer(nullptr) {} NetAddr get_peer_addr() { - return peer ? peer->peer_addr : NetAddr(); + auto ret = *(static_cast( + get_net()->disp_tcall->call([this](ThreadCall::Handle &h) { + h.set_result(peer ? peer->peer_addr : NetAddr()); + }).get())); + return ret; } PeerNetwork *get_net() { @@ -344,7 +348,10 @@ class PeerNetwork: public MsgNetwork { ev_ping_timer.del(); } public: - ~Peer() {} + ~Peer() { + if (inbound_conn) inbound_conn->peer = nullptr; + if (outbound_conn) outbound_conn->peer = nullptr; + } }; std::unordered_map pending_peers; @@ -601,9 +608,7 @@ void PeerNetwork::on_setup(const ConnPool::conn_t &_conn) { assert(!ev_timeout); ev_timeout = TimerEvent(worker->get_ec(), [=](TimerEvent &) { try { - SALTICIDAE_LOG_INFO("peer ping-pong timeout %s <-> %s", - std::string(listen_addr).c_str(), - std::string(conn->get_peer_addr()).c_str()); + SALTICIDAE_LOG_INFO("peer ping-pong timeout"); this->worker_terminate(conn); } catch (...) { worker->error_callback(std::current_exception()); } }); @@ -735,8 +740,7 @@ void PeerNetwork::replace_conn(const conn_t &conn) { auto &old_conn = it->second; if (old_conn != conn) { - if (old_conn->get_mode() != Conn::ConnMode::DEAD) - this->disp_terminate(old_conn); + this->disp_terminate(old_conn); pending_peers.erase(it); } } @@ -768,12 +772,11 @@ template void PeerNetwork::ping_handler(MsgPing &&msg, const conn_t &conn) { this->disp_tcall->async_call([this, conn, msg=std::move(msg)](ThreadCall::Handle &) { try { - auto conn_mode = conn->get_mode(); - if (conn_mode == ConnPool::Conn::DEAD) return; + if (conn->is_terminated()) return; if (!msg.claimed_addr.is_null()) { auto peer_id = gen_peer_id(conn, msg.claimed_addr, msg.nonce); - if (conn_mode == Conn::ConnMode::PASSIVE) + if (conn->get_mode() == Conn::ConnMode::PASSIVE) { pinfo_slock_t _g(known_peers_lock); pinfo_ulock_t __g(pid2peer_lock); @@ -799,7 +802,7 @@ void PeerNetwork::ping_handler(MsgPing &&msg, const conn_t &conn) { return; } auto &old_conn = p->inbound_conn; - if (old_conn && old_conn->get_mode() != Conn::ConnMode::DEAD) + if (old_conn && !old_conn->is_terminated()) { SALTICIDAE_LOG_DEBUG("%s terminating old connection %s", std::string(listen_addr).c_str(), @@ -839,12 +842,11 @@ template void PeerNetwork::pong_handler(MsgPong &&msg, const conn_t &conn) { this->disp_tcall->async_call([this, conn, msg=std::move(msg)](ThreadCall::Handle &) { try { - auto conn_mode = conn->get_mode(); - if (conn_mode == ConnPool::Conn::DEAD) return; + if (conn->is_terminated()) return; if (!msg.claimed_addr.is_null()) { auto peer_id = gen_peer_id(conn, msg.claimed_addr, msg.nonce); - if (conn_mode == Conn::ConnMode::ACTIVE) + if (conn->get_mode() == Conn::ConnMode::ACTIVE) { pinfo_ulock_t _g(known_peers_lock); pinfo_ulock_t __g(pid2peer_lock); @@ -861,7 +863,7 @@ void PeerNetwork::pong_handler(MsgPong &&msg, const conn_t &conn) { return; } auto &old_conn = p->outbound_conn; - if (old_conn && old_conn->get_mode() != Conn::ConnMode::DEAD) + if (old_conn && !old_conn->is_terminated()) { SALTICIDAE_LOG_DEBUG("%s terminating old connection %s", std::string(listen_addr).c_str(), @@ -895,7 +897,7 @@ void PeerNetwork::pong_handler(MsgPong &&msg, const conn_t &conn) { { SALTICIDAE_LOG_WARN("multiple peer addresses share the same identity"); known_peers.erase(old_peer_addr); - if (p->conn && p->conn->get_mode() != Conn::ConnMode::DEAD) + if (p->conn && !p->conn->is_terminated()) this->disp_terminate(p->conn); } old_peer_addr = peer_addr; @@ -1182,7 +1184,6 @@ typedef struct clientnetwork_conn_t clientnetwork_conn_t; typedef enum msgnetwork_conn_mode_t { CONN_MODE_ACTIVE, CONN_MODE_PASSIVE, - CONN_MODE_DEAD } msgnetwork_conn_mode_t; typedef enum peernetwork_id_mode_t { -- cgit v1.2.3-70-g09d2