Tcp socket metrics: define INetdEventListener callback

This patch adds a onTcpSocketStatsEvent() callback method to the
INetdEventListener binder interface that the system server implements.

This function is called by netd for every sock_diag poll performed and
is used to reports to the system server tcp socket stats per network.

Bug: 64147860
Test: manual tests
Change-Id: I0f3c96b3b6e0e2647e662cb61ef16ee7814eac59
diff --git a/server/TcpSocketMonitor.cpp b/server/TcpSocketMonitor.cpp
index a6d71b7..89d1be2 100644
--- a/server/TcpSocketMonitor.cpp
+++ b/server/TcpSocketMonitor.cpp
@@ -24,6 +24,7 @@
 #include <netinet/tcp.h>
 #include <linux/tcp.h>
 
+#include "Controllers.h"
 #include "DumpWriter.h"
 #include "SockDiag.h"
 #include "TcpSocketMonitor.h"
@@ -223,6 +224,28 @@
             it++;
         }
     }
+
+    const auto listener = gCtls->eventReporter.getNetdEventListener();
+    if (listener != nullptr) {
+        std::vector<int> netIds;
+        std::vector<int> sentPackets;
+        std::vector<int> lostPackets;
+        std::vector<int> rtts;
+        std::vector<int> sentAckDiffs;
+        for (auto const& stats : mNetworkStats) {
+            int32_t nSockets = stats.second.nSockets;
+            if (nSockets == 0) {
+                continue;
+            }
+            netIds.push_back(stats.first);
+            sentPackets.push_back(stats.second.sent);
+            lostPackets.push_back(stats.second.lost);
+            rtts.push_back(stats.second.rttUs / nSockets);
+            sentAckDiffs.push_back(stats.second.sentAckDiffMs / nSockets);
+        }
+        listener->onTcpSocketStatsEvent(netIds, sentPackets, lostPackets, rtts, sentAckDiffs);
+    }
+
     mLastPoll = now;
 }