From 3ae885f5b212c6d180b7a66226c812f228b9caf6 Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 5 Dec 2025 20:37:03 +0500 Subject: [PATCH 1/2] connected peer count --- include/libp2p/host/basic_host.hpp | 2 ++ include/libp2p/network/connection_manager.hpp | 2 ++ src/host/basic_host.cpp | 4 ++++ src/network/connection_manager.cpp | 10 ++++++---- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/libp2p/host/basic_host.hpp b/include/libp2p/host/basic_host.hpp index fbfeb55c..5cd7dbb5 100644 --- a/include/libp2p/host/basic_host.hpp +++ b/include/libp2p/host/basic_host.hpp @@ -229,6 +229,8 @@ namespace libp2p::host { */ StreamProtocols getSupportedProtocols() const; + size_t getConnectedPeerCount() const; + private: std::shared_ptr idmgr_; std::shared_ptr listener_; diff --git a/include/libp2p/network/connection_manager.hpp b/include/libp2p/network/connection_manager.hpp index ff11efcf..bd91c8f9 100644 --- a/include/libp2p/network/connection_manager.hpp +++ b/include/libp2p/network/connection_manager.hpp @@ -54,6 +54,8 @@ namespace libp2p::network { const peer::PeerId &peer_id, const std::shared_ptr &conn); + size_t getConnectedPeerCount() const; + private: std::unordered_map> connections_; diff --git a/src/host/basic_host.cpp b/src/host/basic_host.cpp index cba6791e..f9a103b6 100644 --- a/src/host/basic_host.cpp +++ b/src/host/basic_host.cpp @@ -238,4 +238,8 @@ namespace libp2p::host { StreamProtocols BasicHost::getSupportedProtocols() const { return listener_->getSupportedProtocols(); } + + size_t BasicHost::getConnectedPeerCount() const { + return connection_manager_->getConnectedPeerCount(); + } } // namespace libp2p::host diff --git a/src/network/connection_manager.cpp b/src/network/connection_manager.cpp index 44e2ad95..82e8a83e 100644 --- a/src/network/connection_manager.cpp +++ b/src/network/connection_manager.cpp @@ -33,8 +33,8 @@ namespace libp2p::network { return out; } - ConnectionManager::ConnectionSPtr - ConnectionManager::getBestConnectionForPeer(const peer::PeerId &p) const { + ConnectionManager::ConnectionSPtr ConnectionManager::getBestConnectionForPeer( + const peer::PeerId &p) const { auto it = connections_.find(p); if (it != connections_.end()) { for (const auto &conn : it->second) { @@ -79,8 +79,7 @@ namespace libp2p::network { return out; } - ConnectionManager::ConnectionManager( - std::shared_ptr bus) + ConnectionManager::ConnectionManager(std::shared_ptr bus) : bus_(std::move(bus)) {} void ConnectionManager::collectGarbage() { @@ -162,4 +161,7 @@ namespace libp2p::network { } } + size_t ConnectionManager::getConnectedPeerCount() const { + return connections_.size(); + } } // namespace libp2p::network From 1415e95e7b59866a365823e78a0b73415a47ce9d Mon Sep 17 00:00:00 2001 From: turuslan Date: Wed, 10 Dec 2025 15:26:15 +0500 Subject: [PATCH 2/2] connection close error --- include/libp2p/network/connection_manager.hpp | 4 ++++ src/network/connection_manager.cpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/include/libp2p/network/connection_manager.hpp b/include/libp2p/network/connection_manager.hpp index bd91c8f9..3e6d4852 100644 --- a/include/libp2p/network/connection_manager.hpp +++ b/include/libp2p/network/connection_manager.hpp @@ -20,6 +20,10 @@ namespace libp2p::event::network { channel_decl>; + using OnConnectionClosedChannel = + channel_decl>; + /// fired when all connections to peer closed using OnPeerDisconnectedChannel = channel_decl; diff --git a/src/network/connection_manager.cpp b/src/network/connection_manager.cpp index 82e8a83e..16f4d8ed 100644 --- a/src/network/connection_manager.cpp +++ b/src/network/connection_manager.cpp @@ -124,6 +124,8 @@ namespace libp2p::network { // ignore errors (void)conn->close(); } + bus_->getChannel().publish( + conn); } closing_connections_to_peer_.reset(); @@ -152,6 +154,9 @@ namespace libp2p::network { [[maybe_unused]] auto erased = it->second.erase(conn); if (erased == 0) { log()->error("inconsistency in onConnectionClosed, connection not found"); + } else { + bus_->getChannel().publish( + conn); } if (it->second.empty()) {