From 0e05b12422171bd247ac4418abf85e4949d85a90 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 19 Jun 2018 14:47:23 -0400
Subject: [PATCH] network engine: make MessageStats public, add packet counter

---
 include/opendht/callbacks.h      | 10 ++++++++++
 include/opendht/network_engine.h | 15 +++++----------
 src/network_engine.cpp           |  3 +++
 3 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h
index 91ebd84d..0b10f665 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 1afc93e1..7d8035f2 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 c38a79ae..ca1c094f 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));
-- 
GitLab