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

Packet tracking stats for 3.2 #38306

Closed
wants to merge 4 commits into from
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
51 changes: 51 additions & 0 deletions modules/enet/doc_classes/NetworkedMultiplayerENet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,57 @@
The IP used when creating a server. This is set to the wildcard [code]"*"[/code] by default, which binds to all available interfaces. The given IP needs to be in IPv4 or IPv6 address format, for example: [code]"192.168.1.1"[/code].
</description>
</method>
<method name="get_num_packets_sent">
<return type="int">
</return>
<description>
Get the number of packets sent since the last clear.
</description>
</method>
<method name="get_num_packets_received">
<return type="int">
</return>
<description>
Get the number of packets received since the last clear.
</description>
</method>
<method name="get_num_bytes_sent">
<return type="int">
</return>
<description>
Get the number of bytes sent since the last clear.
</description>
</method>
<method name="get_num_bytes_received">
<return type="int">
</return>
<description>
Get the number of bytes received since the last clear.
</description>
</method>
<method name="clear_packet_stats">
<return type="void">
</return>
<description>
Clear the packet sent/received stats.
</description>
</method>
<method name="set_track_packet_stats">
<return type="void">
</return>
<argument index="0" name="p_enable" type="bool">
</argument>
<description>
Enable or disable packet statistics tracking.
</description>
</method>
<method name="is_track_packet_stats_enabled">
<return type="bool">
</return>
<description>
Return if packet statistics tracking is enabled.
</description>
</method>
</methods>
<members>
<member name="always_ordered" type="bool" setter="set_always_ordered" getter="is_always_ordered" default="false">
Expand Down
52 changes: 52 additions & 0 deletions modules/enet/networked_multiplayer_enet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ void NetworkedMultiplayerENet::poll() {

ERR_CONTINUE(event.packet->dataLength < 8);

if (track_packet_stats) {
num_packets_received++;
num_bytes_received += event.packet->dataLength - 8;
}

uint32_t source = decode_uint32(&event.packet->data[0]);
int target = decode_uint32(&event.packet->data[4]);

Expand Down Expand Up @@ -570,6 +575,11 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer
encode_uint32(target_peer, &packet->data[4]); // Dest ID
copymem(&packet->data[8], p_buffer, p_buffer_size);

if (track_packet_stats) {
num_packets_sent++;
num_bytes_sent += p_buffer_size;
}

if (server) {

if (target_peer == 0) {
Expand Down Expand Up @@ -847,6 +857,37 @@ bool NetworkedMultiplayerENet::is_server_relay_enabled() const {
return server_relay;
}

uint32_t NetworkedMultiplayerENet::get_num_packets_received() const {
return num_packets_received;
}

uint32_t NetworkedMultiplayerENet::get_num_bytes_received() const {
return num_bytes_received;
}

uint32_t NetworkedMultiplayerENet::get_num_packets_sent() const {
return num_packets_sent;
}

uint32_t NetworkedMultiplayerENet::get_num_bytes_sent() const {
return num_bytes_sent;
}

void NetworkedMultiplayerENet::clear_packet_stats() {
num_packets_received = 0;
num_bytes_received = 0;
num_packets_sent = 0;
num_bytes_sent = 0;
}

void NetworkedMultiplayerENet::set_track_packet_stats(bool p_enable) {
track_packet_stats = p_enable;
}

bool NetworkedMultiplayerENet::is_track_packet_stats_enabled() {
return track_packet_stats;
}

void NetworkedMultiplayerENet::_bind_methods() {

ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
Expand All @@ -870,6 +911,14 @@ void NetworkedMultiplayerENet::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_server_relay_enabled", "enabled"), &NetworkedMultiplayerENet::set_server_relay_enabled);
ClassDB::bind_method(D_METHOD("is_server_relay_enabled"), &NetworkedMultiplayerENet::is_server_relay_enabled);

ClassDB::bind_method(D_METHOD("get_num_packets_received"), &NetworkedMultiplayerENet::get_num_packets_received);
ClassDB::bind_method(D_METHOD("get_num_bytes_received"), &NetworkedMultiplayerENet::get_num_bytes_received);
ClassDB::bind_method(D_METHOD("get_num_packets_sent"), &NetworkedMultiplayerENet::get_num_packets_sent);
ClassDB::bind_method(D_METHOD("get_num_bytes_sent"), &NetworkedMultiplayerENet::get_num_bytes_sent);
ClassDB::bind_method(D_METHOD("clear_packet_stats"), &NetworkedMultiplayerENet::clear_packet_stats);
ClassDB::bind_method(D_METHOD("set_track_packet_stats"), &NetworkedMultiplayerENet::set_track_packet_stats);
ClassDB::bind_method(D_METHOD("is_track_packet_stats_enabled"), &NetworkedMultiplayerENet::is_track_packet_stats_enabled);

ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_mode", PROPERTY_HINT_ENUM, "None,Range Coder,FastLZ,ZLib,ZStd"), "set_compression_mode", "get_compression_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_channel"), "set_transfer_channel", "get_transfer_channel");
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_count"), "set_channel_count", "get_channel_count");
Expand Down Expand Up @@ -904,6 +953,9 @@ NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
enet_compressor.destroy = enet_compressor_destroy;

bind_ip = IP_Address("*");

track_packet_stats = false;
clear_packet_stats();
}

NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
Expand Down
14 changes: 14 additions & 0 deletions modules/enet/networked_multiplayer_enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
int channel_count;
bool always_ordered;

uint32_t num_packets_received;
uint32_t num_bytes_received;
uint32_t num_packets_sent;
uint32_t num_bytes_sent;
bool track_packet_stats;

ENetEvent event;
ENetPeer *peer;
ENetHost *host;
Expand Down Expand Up @@ -162,6 +168,14 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
void set_server_relay_enabled(bool p_enabled);
bool is_server_relay_enabled() const;

uint32_t get_num_packets_received() const;
uint32_t get_num_bytes_received() const;
uint32_t get_num_packets_sent() const;
uint32_t get_num_bytes_sent() const;
void clear_packet_stats();
void set_track_packet_stats(bool p_enable);
bool is_track_packet_stats_enabled();

NetworkedMultiplayerENet();
~NetworkedMultiplayerENet();

Expand Down