Skip to content

Commit

Permalink
feat(net): attribute proper packet names for timeout
Browse files Browse the repository at this point in the history
if a player times out instead of getting the packet hash they will get the packet name
  • Loading branch information
AvarianKnight committed Nov 3, 2023
1 parent 847710f commit 7800517
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 84 deletions.
52 changes: 13 additions & 39 deletions code/components/citizen-server-gui/src/PlayerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,11 @@

#include <deque>

#include "net/PacketNames.h"

static void SvPlayerList_Render(fx::ServerInstanceBase* instance);
static SvGuiModule svplayerlist("Player List", "svplayerlist", ImGuiWindowFlags_NoCollapse, SvPlayerList_Render);

static std::pair<uint32_t, const char*> g_knownPackets[]
{
{ HashRageString("msgConVars"),"msgConVars" },
{ HashRageString("msgEnd"), "msgEnd" },
{ HashRageString("msgEntityCreate"),"msgEntityCreate" },
{ HashRageString("msgNetGameEvent"),"msgNetGameEvent" },
{ HashRageString("msgObjectIds"),"msgObjectIds" },
{ HashRageString("msgPackedAcks"),"msgPackedAcks" },
{ HashRageString("msgPackedClones"),"msgPackedClones" },
{ HashRageString("msgPaymentRequest"),"msgPaymentRequest" },
{ HashRageString("msgRequestObjectIds"),"msgRequestObjectIds" },
{ HashRageString("msgResStart"),"msgResStart" },
{ HashRageString("msgResStop"),"msgResStop" },
{ HashRageString("msgRoute"),"msgRoute" },
{ HashRageString("msgRpcEntityCreation"),"msgRpcEntityCreation" },
{ HashRageString("msgRpcNative"),"msgRpcNative" },
{ HashRageString("msgServerCommand"),"msgServerCommand" },
{ HashRageString("msgServerEvent"),"msgServerEvent" },
{ HashRageString("msgTimeSync"),"msgTimeSync" },
{ HashRageString("msgTimeSyncReq"),"msgTimeSyncReq" },
{ HashRageString("msgWorldGrid"),"msgWorldGrid" },
{ HashRageString("msgFrame"),"msgFrame" },
{ HashRageString("msgIHost"),"msgIHost" },
{ HashRageString("gameStateAck"),"gameStateAck" },
{ HashRageString("gameStateNAck"),"gameStateNAck" },
{ HashRageString("msgNetEvent"),"msgNetEvent" },
};

struct PlayerAdvancedData
{
void Reset()
Expand All @@ -67,12 +41,12 @@ struct PlayerAdvancedData
// the callbacks will add to this automatically over time
std::atomic_uint32_t bytesOutRaw;
std::atomic_uint32_t bytesInRaw;
// # of times each packet was seen. (Array sizeof g_knownPackets)
std::atomic_uint16_t pktOccurancesRecv[std::size(g_knownPackets)];
std::atomic_uint16_t pktOccurancesSend[std::size(g_knownPackets)];
// # of bytes used per packet type. (Array sizeof g_knownPackets)
std::atomic_uint16_t pktBytesRecv[std::size(g_knownPackets)];
std::atomic_uint16_t pktBytesSend[std::size(g_knownPackets)];
// # of times each packet was seen. (Array sizeof net::PacketNames)
std::atomic_uint16_t pktOccurancesRecv[std::size(net::PacketNames)];
std::atomic_uint16_t pktOccurancesSend[std::size(net::PacketNames)];
// # of bytes used per packet type. (Array sizeof net::PacketNames)
std::atomic_uint16_t pktBytesRecv[std::size(net::PacketNames)];
std::atomic_uint16_t pktBytesSend[std::size(net::PacketNames)];

// After it is time for a Graph update, the collect function will shove the number into the record here(graph requires float type)
std::deque<float> clientOutBytes;
Expand Down Expand Up @@ -165,7 +139,7 @@ void RecvFromClient_Callback(fx::Client* client, uint32_t packetId, net::Buffer&
// first 4 bytes are the rageHash
uint32_t pktHash = *(uint32_t*)data.data();
int i = 0;
for (auto hash : g_knownPackets)
for (auto hash : net::PacketNames)
{
if (hash.first == pktHash)
{
Expand Down Expand Up @@ -197,7 +171,7 @@ void SendToClient_Callback(fx::Client* client, int channel, const net::Buffer& b
// first 4 bytes are the rageHash
uint32_t pktHash = *(uint32_t*)data.data();
int i = 0;
for (auto hash : g_knownPackets)
for (auto hash : net::PacketNames)
{
if (hash.first == pktHash)
{
Expand Down Expand Up @@ -267,17 +241,17 @@ static void CollectAdvancedDataForPlayer(PlayerAdvancedData* entry)
entry->pktsSeenRecv.clear();
entry->pktsSeenSend.clear();

for (int i = 0; i < std::size(g_knownPackets); i++)
for (int i = 0; i < std::size(net::PacketNames); i++)
{
if (entry->pktOccurancesRecv[i] > 0)
{
entry->pktsSeenRecv.push_back(fmt::sprintf("%s[x%d] - %dB\n", g_knownPackets[i].second, entry->pktOccurancesRecv[i], entry->pktBytesRecv[i]));
entry->pktsSeenRecv.push_back(fmt::sprintf("%s[x%d] - %dB\n", net::PacketNames[i].second, entry->pktOccurancesRecv[i], entry->pktBytesRecv[i]));
entry->pktOccurancesRecv[i] = 0;
entry->pktBytesRecv[i] = 0;
}
if (entry->pktOccurancesSend[i] > 0)
{
entry->pktsSeenSend.push_back(fmt::sprintf("%s[x%d] - %dB\n", g_knownPackets[i].second, entry->pktOccurancesSend[i], entry->pktBytesSend[i]));
entry->pktsSeenSend.push_back(fmt::sprintf("%s[x%d] - %dB\n", net::PacketNames[i].second, entry->pktOccurancesSend[i], entry->pktBytesSend[i]));
entry->pktOccurancesSend[i] = 0;
entry->pktBytesSend[i] = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions code/components/citizen-server-impl/include/GameServerNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace fx
uint32_t type;
uint32_t timeAgo;
size_t size;
// if type is msgNetEvent this will be the event name
// if the type is a known packet it will be the packet name, i.e. msgStateBag
std::string eventName;
};

Expand Down
12 changes: 12 additions & 0 deletions code/components/citizen-server-impl/src/GameServerNet.ENet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <enet/enet.h>

#include <FixedBuffer.h>
#include <net/PacketNames.h>

namespace fx
{
Expand Down Expand Up @@ -314,6 +315,17 @@ namespace fx
{
info.eventName = std::string{ (const char*)packet->data + 8 };
}
else
{
for (const auto hash : net::PacketNames)
{
if (hash.first == info.type)
{
info.eventName = std::string{ hash.second };
break;
}
}
}

if (auto outIt = outgoingCommandsMap.find(packet); outIt != outgoingCommandsMap.end())
{
Expand Down
51 changes: 6 additions & 45 deletions code/components/debug-net/src/NetDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <ConsoleHost.h>

#include <net/PacketNames.h>

#include <imgui.h>
#include <imguivariouscontrols.h>

Expand All @@ -32,12 +34,11 @@ const int g_netOverlaySampleCount = 150;
class RageHashList
{
public:
template<int Size>
RageHashList(const char* (&list)[Size])
RageHashList()
{
for (int i = 0; i < Size; i++)
for (const auto packet : net::PacketNames)
{
m_lookupList.insert({ HashRageString(list[i]), list[i] });
m_lookupList.emplace(packet);
}
}

Expand All @@ -57,47 +58,7 @@ class RageHashList
std::map<uint32_t, std::string_view> m_lookupList;
};

// rg -i '"msg' | grep -oP '"msg[A-Z].*?"' | sed 's/"$/",/g' | sort -u | clip
static const char* g_knownPackets[]
{
"msgArrayUpdate",
"msgCloneAcks",
"msgCloneRemove",
"msgConVars",
"msgConfirm",
"msgEnd",
"msgEntityCreate",
"msgFrame",
"msgHeHost",
"msgIHost",
"msgIQuit",
"msgNetEvent",
"msgNetGameEvent",
"msgObjectIds",
"msgPackedAcks",
"msgPackedClones",
"msgPaymentRequest",
"msgReassembledEvent",
"msgRequestObjectIds",
"msgResStart",
"msgResStop",
"msgRoute",
"msgRpcEntityCreation",
"msgRpcNative",
"msgServerCommand",
"msgServerEvent",
"msgStateBag",
"msgTimeSync",
"msgTimeSyncReq",
"msgWorldGrid",
"msgWorldGrid3",

// manual list that doesn't start with 'msg'
"gameStateAck",
"gameStateNAck",
};

static RageHashList g_hashes{ g_knownPackets };
static RageHashList g_hashes;

class NetOverlayMetricSink : public INetMetricSink
{
Expand Down

0 comments on commit 7800517

Please sign in to comment.