improve packet error logging and handling

This commit is contained in:
p2r3
2025-08-23 01:50:14 +03:00
parent 313c31bf7d
commit 4a90979e2f
5 changed files with 74 additions and 65 deletions

View File

@@ -29,6 +29,10 @@ static uint64_t htonll (uint64_t value) {
#endif
}
// Keep track of the total amount of bytes received with recv_all
// Helps notice misread packets and clean up after errors
uint64_t total_bytes_received = 0;
ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
char *p = buf;
size_t total = 0;
@@ -52,15 +56,18 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
task_yield();
continue;
} else {
total_bytes_received += total;
return -1; // real error
}
} else if (r == 0) {
// connection closed before full read
total_bytes_received += total;
return total;
}
total += r;
}
total_bytes_received += total;
return total; // got exactly n bytes
}