feat: implement non-blocking UDP packet reception in OverteClient::poll method

This commit is contained in:
MayaTheShy
2025-11-09 23:57:51 -05:00
parent a3cebfea4e
commit 4c54cc2c11

View File

@@ -369,32 +369,44 @@ void OverteClient::poll() {
// Poll domain UDP socket for domain-level packets // Poll domain UDP socket for domain-level packets
if (m_udpReady && m_udpFd != -1) { if (m_udpReady && m_udpFd != -1) {
char buf[1500]; // Read ALL available packets (non-blocking socket)
sockaddr_storage from{}; socklen_t fromlen = sizeof(from); while (true) {
ssize_t r = ::recvfrom(m_udpFd, buf, sizeof(buf), 0, reinterpret_cast<sockaddr*>(&from), &fromlen); char buf[1500];
if (r > 0) { sockaddr_storage from{}; socklen_t fromlen = sizeof(from);
// Log source address ssize_t r = ::recvfrom(m_udpFd, buf, sizeof(buf), 0, reinterpret_cast<sockaddr*>(&from), &fromlen);
char fromIP[INET_ADDRSTRLEN]; if (r > 0) {
uint16_t fromPort = 0; // Log source address
if (from.ss_family == AF_INET) { char fromIP[INET_ADDRSTRLEN];
sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&from); uint16_t fromPort = 0;
inet_ntop(AF_INET, &sin->sin_addr, fromIP, sizeof(fromIP)); if (from.ss_family == AF_INET) {
fromPort = ntohs(sin->sin_port); sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&from);
} inet_ntop(AF_INET, &sin->sin_addr, fromIP, sizeof(fromIP));
std::cout << "[OverteClient] <<< Received packet (" << r << " bytes) from " << fromIP << ":" << fromPort << std::endl; fromPort = ntohs(sin->sin_port);
}
// Hex dump first 32 bytes for debugging std::cout << "[OverteClient] <<< Received packet (" << r << " bytes) from " << fromIP << ":" << fromPort << std::endl;
std::cout << "[OverteClient] Hex: ";
for (int i = 0; i < std::min(32, (int)r); ++i) { // Hex dump first 32 bytes for debugging
printf("%02x ", (unsigned char)buf[i]); std::cout << "[OverteClient] Hex: ";
} for (int i = 0; i < std::min(32, (int)r); ++i) {
std::cout << std::endl; printf("%02x ", (unsigned char)buf[i]);
parseDomainPacket(buf, static_cast<size_t>(r)); }
} else if (r < 0 && errno != EWOULDBLOCK && errno != EAGAIN) { std::cout << std::endl;
// Only log errors that aren't "would block" parseDomainPacket(buf, static_cast<size_t>(r));
static int errorCount = 0; } else if (r < 0) {
if (++errorCount <= 3) { if (errno == EWOULDBLOCK || errno == EAGAIN) {
std::cerr << "[OverteClient] UDP recv error: " << strerror(errno) << std::endl; // No more packets available
break;
} else {
// Real error
static int errorCount = 0;
if (++errorCount <= 3) {
std::cerr << "[OverteClient] UDP recv error: " << strerror(errno) << std::endl;
}
break;
}
} else {
// r == 0, connection closed (shouldn't happen with UDP)
break;
} }
} }