Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devtools): add a server version of neteventlog #1748

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions code/components/citizen-devtools/src/NetEventLog.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <StdInc.h>

#ifndef IS_FXSERVER
#include <ResourceEventComponent.h>
#include <ResourceManager.h>

#ifndef IS_FXSERVER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For moving this to citizen:server:gui, instead of this, the global define guard would instead be like #if !defined(IS_FXSERVER) || defined(COMPILING_CITIZEN_SERVER_GUI).

#include <NetLibrary.h>

#endif
#include <boost/circular_buffer.hpp>

#include <ConsoleHost.h>
Expand All @@ -18,17 +18,27 @@ struct EventLogEntry
std::string eventName;
size_t payloadSize;
bool out;
std::optional<std::string> eventSource;
};

static boost::circular_buffer<EventLogEntry> g_eventLog(150);
static boost::circular_buffer<EventLogEntry> g_eventLog(
// We want a bigger buffer on the server because it will send/receive *a lot* more events, and 150 will quickly get filled
#if IS_FXSERVER
600
#else
150
#endif
);
static std::mutex g_eventLogMutex;

static void EventLog_Add(const std::string& eventName, size_t payloadSize, bool out)
static void EventLog_Add(const std::string& eventName, size_t payloadSize, bool out, std::optional<std::string> eventSource = std::nullopt)
{
EventLogEntry eve;
eve.eventName = eventName;
eve.payloadSize = payloadSize;
eve.out = out;
// this should always be nullopt on client
eve.eventSource = eventSource;

std::unique_lock<std::mutex> lock(g_eventLogMutex);
g_eventLog.push_front(std::move(eve));
Expand All @@ -55,24 +65,46 @@ static InitFunction initFunction([]()

if (ImGui::Begin("Network Event Log", &eventLogEnabled))
{
ImGui::Columns(3);
// If we're on the server we're going to have the "Sender/Receiver" field so we need an extra column
ImGui::Columns(
#ifdef IS_FXSERVER
4
#else
3
#endif
);

{
ImGui::Text("Dir");
ImGui::NextColumn();
ImGui::Text("Name");
ImGui::NextColumn();
#ifdef IS_FXSERVER
ImGui::Text("Sender/Receiver");
ImGui::NextColumn();
#endif
ImGui::Text("Size");
ImGui::NextColumn();

std::unique_lock<std::mutex> lock(g_eventLogMutex);

for (auto& entry : g_eventLog)
{
ImGui::Text((entry.out) ? "C->S" : "S->C");

ImGui::Text(
#ifndef IS_FXSERVER
(entry.out) ? "C->S" : "S->C"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could rename out to toServer and flip it elsewhere.

#else
(entry.out) ? "S->C" : "C->S"
#endif
);
ImGui::NextColumn();
ImGui::Text("%s", entry.eventName.c_str());
ImGui::NextColumn();
#if IS_FXSERVER
ImGui::Text("%s", entry.eventSource.value_or("server").c_str());
ImGui::NextColumn();
#endif
ImGui::Text("%lld B", entry.payloadSize);
ImGui::NextColumn();
}
Expand All @@ -92,17 +124,30 @@ static InitFunction initFunction([]()
{
if (eventSource.find("net:") == 0)
{
#if IS_FXSERVER
EventLog_Add(eventName, eventPayload.size(), false, std::optional{ eventSource.substr(4) });
#else
EventLog_Add(eventName, eventPayload.size(), false);
#endif

}
});

eventManager->OnClientEventTriggered.Connect([](const std::string_view& eventName, size_t& eventSize, const std::optional<std::string_view>& eventTarget)
{
EventLog_Add(std::string{ eventName }, eventSize, true, std::string{ eventTarget.value_or("-1") });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of showing the target as a number, maybe showing their name (plus a shortcut to the citizen:server:gui player list entry for them?) could work.

(similarly, instead of enabling this for the server in citizen:devtools, one could just files it in citizen:server:gui using the COMPILING_CITIZEN_SERVER_IMPL define, solving the callout issue as well)

});

}, INT32_MAX);

#ifndef IS_FXSERVER
NetLibrary::OnNetLibraryCreate.Connect([](NetLibrary* library)
{
library->OnTriggerServerEvent.Connect([](const std::string& eventName, const std::string& eventPayload)
{
EventLog_Add(eventName, eventPayload.size(), true);
});
});
});
#endif
});

Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class RESOURCES_CORE_EXPORT ResourceEventManagerComponent : public fwRefCountabl
//
fwEvent<const std::string&, const std::string&, const std::string&> OnQueueEvent;

// Arguments: eventName, eventPayloadSize, eventSource
fwEvent<const std::string_view&, size_t&, const std::optional<std::string_view>&> OnClientEventTriggered;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little odd as the concept of server->client events only exists on the server and is part of ServerResources - I wonder if there'd be a better place for this callout across components, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServerEventComponent might be a better place to put something like this and just change the initialization to OnServerCreate


//
// Triggers an event immediately. Returns a value indicating whether the event was not canceled.
//
Expand Down
1 change: 1 addition & 0 deletions code/components/citizen-server-gui/src/ServerGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ServerGui : public fwRefCountable, public fx::IAttached<fx::ServerInstance

m_instance->GetComponent<console::Context>()->AddToBuffer(R"(
devgui_convar "Tools/Performance/Resource Monitor" resmon
devgui_convar "Tools/Network/Network Event Log" netEventLog
devgui_convar "Tools/Network/State/Network Object Viewer" netobjviewer
devgui_convar "Tools/Network/State/Player List" svplayerlist
)");
Expand Down
5 changes: 5 additions & 0 deletions code/components/citizen-server-impl/src/ServerResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,11 @@ void fx::ServerEventComponent::TriggerClientEvent(const std::string_view& eventN
client->SendPacket(0, outBuffer, NetPacketType_Reliable);
});
}

auto manager = fx::ResourceManager::GetCurrent();
auto eventManager = manager->GetComponent<fx::ResourceEventManagerComponent>();

eventManager->OnClientEventTriggered(eventName, dataLen, targetSrc);
}

static InitFunction initFunction2([]()
Expand Down
Loading