Optimize error handling

This commit is contained in:
Maschell 2022-10-07 14:15:54 +02:00
parent 31dedcfd89
commit 1984203bed

View File

@ -77,33 +77,35 @@ void TcpReceiver::executeThread() {
socklen_t len; socklen_t len;
while (!exitRequested) { while (!exitRequested) {
if (serverSocket < 0) { if (serverSocket < 0) {
if (!createSocket()) { if (!createSocket() && !exitRequested) {
DEBUG_FUNCTION_LINE_WARN("Starting the wiiload server failed. Check the network connection."); if (errno != EBUSY) {
OSSleepTicks(OSSecondsToTicks(5)); DEBUG_FUNCTION_LINE_WARN("Create socket failed %d", errno);
}
OSSleepTicks(OSMillisecondsToTicks(10));
} }
continue; continue;
} }
struct sockaddr_in clientAddr {}; struct sockaddr_in clientAddr {};
memset(&clientAddr, 0, sizeof(clientAddr)); memset(&clientAddr, 0, sizeof(clientAddr));
len = 16; len = 16;
DEBUG_FUNCTION_LINE("Waiting for wiiload connection");
int32_t clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &len); int32_t clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &len);
if (clientSocket >= 0) { if (clientSocket >= 0) {
uint32_t ipAddress = clientAddr.sin_addr.s_addr; uint32_t ipAddress = clientAddr.sin_addr.s_addr;
int32_t result = loadToMemory(clientSocket, ipAddress); DEBUG_FUNCTION_LINE("Waiting for wiiload connection");
int32_t result = loadToMemory(clientSocket, ipAddress);
close(clientSocket); close(clientSocket);
if (result >= 0) { if (result >= 0) {
break; break;
} }
} else { } else if (!exitRequested) {
if (!exitRequested) { if (errno != EBUSY) {
DEBUG_FUNCTION_LINE_WARN("Error accepting the socket"); DEBUG_FUNCTION_LINE_WARN("Accept failed %d", errno);
cleanupSocket();
OSSleepTicks(OSSecondsToTicks(1));
} }
OSSleepTicks(OSMillisecondsToTicks(10));
} }
OSSleepTicks(OSMillisecondsToTicks(1));
} }
cleanupSocket(); cleanupSocket();
DEBUG_FUNCTION_LINE("Stopping wiiload server."); DEBUG_FUNCTION_LINE("Stopping wiiload server.");