feat(traffic): count traffic by peer and by connection

Every server now splits its packet counts by peer: its own connections
(players on auth and worlds), its master link, or other servers (the
worlds on chat, every server on master, a world's chat link, which is
now counted too). HTTP requests carrying X-Darkflame-Server count as
another server's, the dashboard counts its own requests to the UGC
server, and each HTTP client address is counted. The report also gets
each RakNet connection's statistics (worlds name the player on it),
trimmed to the 32 busiest with the rest summed. Counting stays on the
main loop, except the dashboard's UGC fetches, which only touch the
locked recorder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-29 18:23:31 -05:00
parent 8a53562629
commit 9e8f161dd8
10 changed files with 273 additions and 27 deletions

View File

@@ -7,6 +7,7 @@
#include "RakPeerInterface.h"
#include "ReplicaManager.h"
#include "NetworkIDManager.h"
#include "TrafficStats.h"
class Logger;
class dConfig;
@@ -58,6 +59,10 @@ public:
using TrafficSink = std::function<void(ServerTraffic& report)>;
void SetTrafficSink(TrafficSink sink) { mTrafficSink = std::move(sink); }
// Names who is on a connection in the traffic report (a world fills in the player's account and character)
using ConnectionIdentity = std::function<void(const SystemAddress& sysAddr, TrafficStats::Connection& connection)>;
void SetConnectionIdentity(ConnectionIdentity identity) { mConnectionIdentity = std::move(identity); }
bool IsConnected(const SystemAddress& sysAddr);
const std::string& GetIP() const { return mIP; }
const int GetPort() const { return mPort; }
@@ -92,8 +97,10 @@ private:
};
bool Startup();
// Traffic diagnostics (TrafficStats): count one packet, and send the report when it is due
void CountTraffic(const Packet* packet);
void CountTraffic(const RakNet::BitStream& bitStream, bool broadcast, const SystemAddress& sysAddr);
void CountTraffic(const Packet* packet, TrafficStats::Peer peer);
void CountTraffic(const RakNet::BitStream& bitStream, bool broadcast, const SystemAddress& sysAddr, TrafficStats::Peer peer);
// Who mPeer's connections are: other servers on master and chat (the worlds connect to chat), players elsewhere
TrafficStats::Peer PeerOfConnections() const;
void ReportTraffic();
// Adds the peer's connections to the report's link statistics (changes since the last report)
void AddLinkStats(RakPeerInterface* peer, uint64_t peerIndex, ServerTraffic& report, uint64_t& pingSum, std::map<uint64_t, LinkCounters>& seen);
@@ -136,6 +143,7 @@ protected:
SendObserver mSendObserver;
TrafficSink mTrafficSink;
ConnectionIdentity mConnectionIdentity;
// RakNet's per-connection statistics are totals since the connection opened; the last ones seen, for deltas
std::map<uint64_t, LinkCounters> mLinkCounters;
};