diff --git a/src/console_commands.cpp b/src/console_commands.cpp index 7e256d1..7625016 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -69,7 +69,7 @@ typedef struct cmd { cmdfunc *func; } cmd; -static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_exit; +static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_showpeers, do_exit; static cmd cmds[] = { { STRCONST("help"), "", "display list of commands", do_help }, @@ -77,6 +77,7 @@ static cmd cmds[] = { { STRCONST("loglevel"), "", "set log level", do_loglevel }, { STRCONST("addpeers"), "", "add peer", do_addpeers }, { STRCONST("droppeers"), "", "disconnect all peers", do_droppeers }, + { STRCONST("peers"), "", "show all peers", do_showpeers }, { STRCONST("exit"), "", "terminate p2pool", do_exit }, { STRCNULL, NULL, NULL, NULL } }; @@ -129,6 +130,14 @@ static int do_droppeers(p2pool *m_pool, const char * /* args */) return 0; } +static int do_showpeers(p2pool* m_pool, const char* /* args */) +{ + if (m_pool->p2p_server()) { + m_pool->p2p_server()->show_peers(); + } + return 0; +} + static int do_exit(p2pool *m_pool, const char * /* args */) { bkg_jobs_tracker.wait(); diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index e6f7ee9..ba14d32 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -278,6 +278,7 @@ void P2PServer::update_peer_list() }); if (result) { + client->m_lastPeerListRequestTime = std::chrono::system_clock::now(); ++client->m_peerListPendingRequests; } } @@ -693,6 +694,17 @@ void P2PServer::print_status() ); } +void P2PServer::show_peers() +{ + MutexLock lock(m_clientsListLock); + + for (P2PClient* client = static_cast(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast(client->m_next)) { + if (client->m_listenPort >= 0) { + LOGINFO(0, (client->m_isIncoming ? "I " : "O ") << client->m_pingTime << " ms\t" << static_cast(client->m_addrString)); + } + } +} + void P2PServer::on_timer() { ++m_timerCounter; @@ -832,7 +844,9 @@ P2PServer::P2PClient::P2PClient() , m_handshakeInvalid(false) , m_listenPort(-1) , m_nextPeerListRequest(0) + , m_lastPeerListRequestTime{} , m_peerListPendingRequests(0) + , m_pingTime(0) , m_lastAlive(0) , m_lastBroadcastTimestamp(0) , m_lastBlockrequestTimestamp(0) @@ -856,7 +870,9 @@ void P2PServer::P2PClient::reset() m_handshakeInvalid = false; m_listenPort = -1; m_nextPeerListRequest = 0; + m_lastPeerListRequestTime = {}; m_peerListPendingRequests = 0; + m_pingTime = 0; m_lastAlive = 0; m_lastBroadcastTimestamp = 0; m_lastBlockrequestTimestamp = 0; @@ -1053,6 +1069,9 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size) if (bytes_left >= 2u + num_peers * 19u) { bytes_read = 2u + num_peers * 19u; + using namespace std::chrono; + m_pingTime = duration_cast(system_clock::now() - m_lastPeerListRequestTime).count(); + --m_peerListPendingRequests; if (!on_peer_list_response(buf + 1)) { ban(DEFAULT_BAN_TIME); diff --git a/src/p2p_server.h b/src/p2p_server.h index 1bb4e31..88ab127 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -108,8 +108,12 @@ public: bool m_handshakeComplete; bool m_handshakeInvalid; int m_listenPort; + time_t m_nextPeerListRequest; + std::chrono::system_clock::time_point m_lastPeerListRequestTime; int m_peerListPendingRequests; + int64_t m_pingTime; + time_t m_lastAlive; time_t m_lastBroadcastTimestamp; time_t m_lastBlockrequestTimestamp; @@ -123,6 +127,7 @@ public: uint64_t get_peerId() const { return m_peerId; } void print_status() override; + void show_peers(); private: p2pool* m_pool;