diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h index 91ebd84df8a738c9b31d02e38e2e98211d4315b1..0b10f6655d5646db7a7a6824023439affe13f645 100644 --- a/include/opendht/callbacks.h +++ b/include/opendht/callbacks.h @@ -83,6 +83,16 @@ struct OPENDHT_PUBLIC NodeInfo { MSGPACK_DEFINE_MAP(id, node_id, ipv4, ipv6) }; +struct OPENDHT_PUBLIC MessageStats { + unsigned packets {0}; + unsigned ping {0}; + unsigned find {0}; + unsigned get {0}; + unsigned put {0}; + unsigned listen {0}; + unsigned refresh {0}; +}; + /** * Dht configuration. */ diff --git a/include/opendht/network_engine.h b/include/opendht/network_engine.h index 1afc93e1eaf4731c6f1b175bc88a794b849188b0..7d8035f27b54e8eaf7d70e68120229eeaef70f53 100644 --- a/include/opendht/network_engine.h +++ b/include/opendht/network_engine.h @@ -27,6 +27,7 @@ #include "utils.h" #include "rng.h" #include "rate_limiter.h" +#include "callbacks.h" #include <vector> #include <string> @@ -408,6 +409,10 @@ public: return stats; } + MessageStats getMessageStats(bool in) { + return in ? in_stats : out_stats; + } + void blacklistNode(const Sp<Node>& n); std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, sa_family_t sa_f, size_t count) { @@ -457,16 +462,6 @@ private: */ void sendRequest(const Sp<Request>& request); - struct MessageStats { - unsigned ping {0}; - unsigned find {0}; - unsigned get {0}; - unsigned put {0}; - unsigned listen {0}; - unsigned refresh {0}; - }; - - // basic wrapper for socket sendto function int send(const char *buf, size_t len, int flags, const SockAddr& addr); diff --git a/src/network_engine.cpp b/src/network_engine.cpp index c38a79aec4d9da9a57dcffb79e2abadf3d586cb9..ca1c094f10dc9d8243c6652ef93ef74fa48959e6 100644 --- a/src/network_engine.cpp +++ b/src/network_engine.cpp @@ -403,6 +403,7 @@ NetworkEngine::isNodeBlacklisted(const SockAddr& addr) const void NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr& from) { + in_stats.packets++; if (isMartian(from)) { DHT_LOG.w("Received packet from martian node %s", from.toString().c_str()); return; @@ -663,6 +664,8 @@ NetworkEngine::send(const char *buf, size_t len, int flags, const SockAddr& addr #ifdef MSG_NOSIGNAL flags |= MSG_NOSIGNAL; #endif + + out_stats.packets++; if (sendto(s, buf, len, flags, addr.get(), addr.getLength()) == -1) { int err = errno; DHT_LOG.e("Can't send message to %s: %s", addr.toString().c_str(), strerror(err));