Added console command to show connected peers

This commit is contained in:
SChernykh 2021-10-24 16:04:30 +02:00
parent 148b9dd294
commit 111324b6e0
3 changed files with 34 additions and 1 deletions

View file

@ -69,7 +69,7 @@ typedef struct cmd {
cmdfunc *func; cmdfunc *func;
} cmd; } 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[] = { static cmd cmds[] = {
{ STRCONST("help"), "", "display list of commands", do_help }, { STRCONST("help"), "", "display list of commands", do_help },
@ -77,6 +77,7 @@ static cmd cmds[] = {
{ STRCONST("loglevel"), "<level>", "set log level", do_loglevel }, { STRCONST("loglevel"), "<level>", "set log level", do_loglevel },
{ STRCONST("addpeers"), "<peeraddr>", "add peer", do_addpeers }, { STRCONST("addpeers"), "<peeraddr>", "add peer", do_addpeers },
{ STRCONST("droppeers"), "", "disconnect all peers", do_droppeers }, { STRCONST("droppeers"), "", "disconnect all peers", do_droppeers },
{ STRCONST("peers"), "", "show all peers", do_showpeers },
{ STRCONST("exit"), "", "terminate p2pool", do_exit }, { STRCONST("exit"), "", "terminate p2pool", do_exit },
{ STRCNULL, NULL, NULL, NULL } { STRCNULL, NULL, NULL, NULL }
}; };
@ -129,6 +130,14 @@ static int do_droppeers(p2pool *m_pool, const char * /* args */)
return 0; 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 */) static int do_exit(p2pool *m_pool, const char * /* args */)
{ {
bkg_jobs_tracker.wait(); bkg_jobs_tracker.wait();

View file

@ -278,6 +278,7 @@ void P2PServer::update_peer_list()
}); });
if (result) { if (result) {
client->m_lastPeerListRequestTime = std::chrono::system_clock::now();
++client->m_peerListPendingRequests; ++client->m_peerListPendingRequests;
} }
} }
@ -693,6 +694,17 @@ void P2PServer::print_status()
); );
} }
void P2PServer::show_peers()
{
MutexLock lock(m_clientsListLock);
for (P2PClient* client = static_cast<P2PClient*>(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {
if (client->m_listenPort >= 0) {
LOGINFO(0, (client->m_isIncoming ? "I " : "O ") << client->m_pingTime << " ms\t" << static_cast<char*>(client->m_addrString));
}
}
}
void P2PServer::on_timer() void P2PServer::on_timer()
{ {
++m_timerCounter; ++m_timerCounter;
@ -832,7 +844,9 @@ P2PServer::P2PClient::P2PClient()
, m_handshakeInvalid(false) , m_handshakeInvalid(false)
, m_listenPort(-1) , m_listenPort(-1)
, m_nextPeerListRequest(0) , m_nextPeerListRequest(0)
, m_lastPeerListRequestTime{}
, m_peerListPendingRequests(0) , m_peerListPendingRequests(0)
, m_pingTime(0)
, m_lastAlive(0) , m_lastAlive(0)
, m_lastBroadcastTimestamp(0) , m_lastBroadcastTimestamp(0)
, m_lastBlockrequestTimestamp(0) , m_lastBlockrequestTimestamp(0)
@ -856,7 +870,9 @@ void P2PServer::P2PClient::reset()
m_handshakeInvalid = false; m_handshakeInvalid = false;
m_listenPort = -1; m_listenPort = -1;
m_nextPeerListRequest = 0; m_nextPeerListRequest = 0;
m_lastPeerListRequestTime = {};
m_peerListPendingRequests = 0; m_peerListPendingRequests = 0;
m_pingTime = 0;
m_lastAlive = 0; m_lastAlive = 0;
m_lastBroadcastTimestamp = 0; m_lastBroadcastTimestamp = 0;
m_lastBlockrequestTimestamp = 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) { if (bytes_left >= 2u + num_peers * 19u) {
bytes_read = 2u + num_peers * 19u; bytes_read = 2u + num_peers * 19u;
using namespace std::chrono;
m_pingTime = duration_cast<milliseconds>(system_clock::now() - m_lastPeerListRequestTime).count();
--m_peerListPendingRequests; --m_peerListPendingRequests;
if (!on_peer_list_response(buf + 1)) { if (!on_peer_list_response(buf + 1)) {
ban(DEFAULT_BAN_TIME); ban(DEFAULT_BAN_TIME);

View file

@ -108,8 +108,12 @@ public:
bool m_handshakeComplete; bool m_handshakeComplete;
bool m_handshakeInvalid; bool m_handshakeInvalid;
int m_listenPort; int m_listenPort;
time_t m_nextPeerListRequest; time_t m_nextPeerListRequest;
std::chrono::system_clock::time_point m_lastPeerListRequestTime;
int m_peerListPendingRequests; int m_peerListPendingRequests;
int64_t m_pingTime;
time_t m_lastAlive; time_t m_lastAlive;
time_t m_lastBroadcastTimestamp; time_t m_lastBroadcastTimestamp;
time_t m_lastBlockrequestTimestamp; time_t m_lastBlockrequestTimestamp;
@ -123,6 +127,7 @@ public:
uint64_t get_peerId() const { return m_peerId; } uint64_t get_peerId() const { return m_peerId; }
void print_status() override; void print_status() override;
void show_peers();
private: private:
p2pool* m_pool; p2pool* m_pool;