diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 8ba655cb5c..f8a19b6d2e 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -32,6 +32,7 @@ #include "mem.h" #include "mono_time.h" #include "net_crypto.h" +#include "net_profile.h" #include "network.h" #include "onion_announce.h" #include "onion_client.h" @@ -8590,3 +8591,83 @@ int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uin return added_peers; } + +uint64_t gc_tcp_connections_get_packet_id_count(const GC_Session *c, uint8_t id, Packet_Direction direction) +{ + uint64_t count = 0; + + for (uint32_t i = 0; i < c->chats_index; ++i) { + GC_Chat *chat = &c->chats[i]; + + const GC_Conn_State state = chat->connection_state; + + if (state == CS_NONE) { + continue; + } + + const Net_Profile *tcp_profile = tcp_connection_get_client_net_profile(chat->tcp_conn); + count += netprof_get_packet_count_id(tcp_profile, id, direction); + } + + return count; +} + +uint64_t gc_tcp_connections_get_packet_total_count(const GC_Session *c, Packet_Direction direction) +{ + uint64_t count = 0; + + for (uint32_t i = 0; i < c->chats_index; ++i) { + GC_Chat *chat = &c->chats[i]; + + const GC_Conn_State state = chat->connection_state; + + if (state == CS_NONE) { + continue; + } + + const Net_Profile *tcp_profile = tcp_connection_get_client_net_profile(chat->tcp_conn); + count += netprof_get_packet_count_total(tcp_profile, direction); + } + + return count; +} + +uint64_t gc_tcp_connections_get_packet_id_bytes(const GC_Session *c, uint8_t id, Packet_Direction direction) +{ + uint64_t bytes = 0; + + for (uint32_t i = 0; i < c->chats_index; ++i) { + GC_Chat *chat = &c->chats[i]; + + const GC_Conn_State state = chat->connection_state; + + if (state == CS_NONE) { + continue; + } + + const Net_Profile *tcp_profile = tcp_connection_get_client_net_profile(chat->tcp_conn); + bytes += netprof_get_bytes_id(tcp_profile, id, direction); + } + + return bytes; +} + +uint64_t gc_tcp_connections_get_packet_total_bytes(const GC_Session *c, Packet_Direction direction) +{ + uint64_t bytes = 0; + + for (uint32_t i = 0; i < c->chats_index; ++i) { + GC_Chat *chat = &c->chats[i]; + + const GC_Conn_State state = chat->connection_state; + + if (state == CS_NONE) { + continue; + } + + const Net_Profile *tcp_profile = tcp_connection_get_client_net_profile(chat->tcp_conn); + bytes += netprof_get_bytes_total(tcp_profile, direction); + } + + return bytes; +} diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 481dee19eb..b5da8f9652 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -807,4 +807,14 @@ GC_Chat *gc_get_group_by_public_key(const GC_Session *c, const uint8_t *public_k non_null() int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uint8_t gc_announces_count); +/** @brief Returns total packet or byte counts for all active group chats. */ +non_null() +uint64_t gc_tcp_connections_get_packet_id_count(const GC_Session *c, uint8_t id, Packet_Direction direction); +non_null() +uint64_t gc_tcp_connections_get_packet_total_count(const GC_Session *c, Packet_Direction direction); +non_null() +uint64_t gc_tcp_connections_get_packet_id_bytes(const GC_Session *c, uint8_t id, Packet_Direction direction); +non_null() +uint64_t gc_tcp_connections_get_packet_total_bytes(const GC_Session *c, Packet_Direction direction); + #endif /* C_TOXCORE_TOXCORE_GROUP_CHATS_H */ diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index e215c37dfa..69e27136c2 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -246,7 +246,9 @@ uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type switch (type) { case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { - count = netprof_get_packet_count_id(tcp_c_profile, id, dir); + const uint64_t tcp_c_count = netprof_get_packet_count_id(tcp_c_profile, id, dir); + const uint64_t tcp_group_count = gc_tcp_connections_get_packet_id_count(tox->m->group_handler, id, dir); + count = tcp_c_count + tcp_group_count; break; } @@ -258,7 +260,8 @@ uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type case TOX_NETPROF_PACKET_TYPE_TCP: { const uint64_t tcp_c_count = netprof_get_packet_count_id(tcp_c_profile, id, dir); const uint64_t tcp_s_count = netprof_get_packet_count_id(tcp_s_profile, id, dir); - count = tcp_c_count + tcp_s_count; + const uint64_t tcp_group_count = gc_tcp_connections_get_packet_id_count(tox->m->group_handler, id, dir); + count = tcp_c_count + tcp_s_count + tcp_group_count; break; } @@ -295,7 +298,9 @@ uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_T switch (type) { case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { - count = netprof_get_packet_count_total(tcp_c_profile, dir); + const uint64_t tcp_c_count = netprof_get_packet_count_total(tcp_c_profile, dir); + const uint64_t tcp_group_count = gc_tcp_connections_get_packet_total_count(tox->m->group_handler, dir); + count = tcp_c_count + tcp_group_count; break; } @@ -307,7 +312,8 @@ uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_T case TOX_NETPROF_PACKET_TYPE_TCP: { const uint64_t tcp_c_count = netprof_get_packet_count_total(tcp_c_profile, dir); const uint64_t tcp_s_count = netprof_get_packet_count_total(tcp_s_profile, dir); - count = tcp_c_count + tcp_s_count; + const uint64_t tcp_group_count = gc_tcp_connections_get_packet_total_count(tox->m->group_handler, dir); + count = tcp_c_count + tcp_s_count + tcp_group_count; break; } @@ -344,7 +350,9 @@ uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type switch (type) { case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { - bytes = netprof_get_bytes_id(tcp_c_profile, id, dir); + const uint64_t tcp_c_bytes = netprof_get_bytes_id(tcp_c_profile, id, dir); + const uint64_t tcp_group_bytes = gc_tcp_connections_get_packet_id_bytes(tox->m->group_handler, id, dir); + bytes = tcp_c_bytes + tcp_group_bytes; break; } @@ -356,7 +364,8 @@ uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type case TOX_NETPROF_PACKET_TYPE_TCP: { const uint64_t tcp_c_bytes = netprof_get_bytes_id(tcp_c_profile, id, dir); const uint64_t tcp_s_bytes = netprof_get_bytes_id(tcp_s_profile, id, dir); - bytes = tcp_c_bytes + tcp_s_bytes; + const uint64_t tcp_group_bytes = gc_tcp_connections_get_packet_id_bytes(tox->m->group_handler, id, dir); + bytes = tcp_c_bytes + tcp_s_bytes + tcp_group_bytes; break; } @@ -393,7 +402,9 @@ uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_T switch (type) { case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { - bytes = netprof_get_bytes_total(tcp_c_profile, dir); + const uint64_t tcp_c_bytes = netprof_get_bytes_total(tcp_c_profile, dir); + const uint64_t tcp_group_bytes = gc_tcp_connections_get_packet_total_bytes(tox->m->group_handler, dir); + bytes = tcp_c_bytes + tcp_group_bytes; break; } @@ -405,7 +416,8 @@ uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_T case TOX_NETPROF_PACKET_TYPE_TCP: { const uint64_t tcp_c_bytes = netprof_get_bytes_total(tcp_c_profile, dir); const uint64_t tcp_s_bytes = netprof_get_bytes_total(tcp_s_profile, dir); - bytes = tcp_c_bytes + tcp_s_bytes; + const uint64_t tcp_group_bytes = gc_tcp_connections_get_packet_total_bytes(tox->m->group_handler, dir); + bytes = tcp_c_bytes + tcp_s_bytes + tcp_group_bytes; break; }