Squashed 'external/toxcore/c-toxcore/' changes from 03e9fbf3703..e740b4e3b2e

e740b4e3b2e feat: Implement Tox network profiler

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: e740b4e3b2e3aa594ab259e2a80153f5eed8fd16
This commit is contained in:
2024-09-22 12:50:20 +02:00
parent fe6c5391a2
commit 0ce5c0a0ad
34 changed files with 943 additions and 46 deletions

View File

@@ -86,6 +86,7 @@
#include "ccompat.h"
#include "logger.h"
#include "mem.h"
#include "net_profile.h"
#include "util.h"
// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD
@@ -812,9 +813,15 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
}
int net_send(const Network *ns, const Logger *log,
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port)
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile)
{
const int res = ns->funcs->send(ns->obj, sock, buf, len);
if (buf != nullptr && res == len) {
const uint8_t *data = buf;
netprof_record_packet(net_profile, data[0], len, PACKET_DIRECTION_SENT);
}
loglogdata(log, "T=>", buf, len, ip_port, res);
return res;
}
@@ -914,6 +921,8 @@ struct Networking_Core {
uint16_t port;
/* Our UDP socket. */
Socket sock;
Net_Profile udp_net_profile;
};
Family net_family(const Networking_Core *net)
@@ -929,7 +938,7 @@ uint16_t net_port(const Networking_Core *net)
/* Basic network functions:
*/
int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet)
int send_packet(Networking_Core *net, const IP_Port *ip_port, Packet packet)
{
IP_Port ipp_copy = *ip_port;
@@ -999,6 +1008,11 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe
loglogdata(net->log, "O=>", packet.data, packet.length, ip_port, res);
assert(res <= INT_MAX);
if (res == packet.length) {
netprof_record_packet(&net->udp_net_profile, packet.data[0], packet.length, PACKET_DIRECTION_SENT);
}
return (int)res;
}
@@ -1007,7 +1021,7 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe
*
* @deprecated Use send_packet instead.
*/
int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length)
int sendpacket(Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length)
{
const Packet packet = {data, length};
return send_packet(net, ip_port, packet);
@@ -1087,7 +1101,7 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
net->packethandlers[byte].object = object;
}
void networking_poll(const Networking_Core *net, void *userdata)
void networking_poll(Networking_Core *net, void *userdata)
{
if (net_family_is_unspec(net->family)) {
/* Socket not initialized */
@@ -1103,6 +1117,8 @@ void networking_poll(const Networking_Core *net, void *userdata)
continue;
}
netprof_record_packet(&net->udp_net_profile, data[0], length, PACKET_DIRECTION_RECV);
const Packet_Handler *const handler = &net->packethandlers[data[0]];
if (handler->function == nullptr) {
@@ -2296,3 +2312,12 @@ void net_kill_strerror(char *strerror)
free(strerror);
#endif /* OS_WIN32 */
}
const Net_Profile *net_get_net_profile(const Networking_Core *net)
{
if (net == nullptr) {
return nullptr;
}
return &net->udp_net_profile;
}