Restart the wiiload server on connection issues

This commit is contained in:
Maschell 2022-10-06 18:47:24 +02:00
parent 3559ac4049
commit ca47e10a0c
2 changed files with 42 additions and 27 deletions

View File

@ -4,13 +4,13 @@
#include "utils/net.h" #include "utils/net.h"
#include "utils/utils.h" #include "utils/utils.h"
#include <algorithm> #include <algorithm>
#include <coreinit/cache.h>
#include <coreinit/debug.h> #include <coreinit/debug.h>
#include <coreinit/dynload.h> #include <coreinit/dynload.h>
#include <coreinit/title.h> #include <coreinit/title.h>
#include <cstring> #include <cstring>
#include <rpxloader/rpxloader.h> #include <rpxloader/rpxloader.h>
#include <sysapp/launch.h> #include <sysapp/launch.h>
#include <sysapp/title.h>
#include <vector> #include <vector>
#include <wups_backend/PluginUtils.h> #include <wups_backend/PluginUtils.h>
#include <zlib.h> #include <zlib.h>
@ -30,18 +30,16 @@ TcpReceiver::TcpReceiver(int32_t port)
TcpReceiver::~TcpReceiver() { TcpReceiver::~TcpReceiver() {
exitRequested = true; exitRequested = true;
OSMemoryBarrier();
if (serverSocket >= 0) { if (serverSocket >= 0) {
shutdown(serverSocket, SHUT_RDWR); cleanupSocket();
close(serverSocket);
serverSocket = -1;
} }
} }
void TcpReceiver::executeThread() { bool TcpReceiver::createSocket() {
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (serverSocket < 0) { if (serverSocket < 0) {
return; return false;
} }
uint32_t enable = 1; uint32_t enable = 1;
@ -53,48 +51,62 @@ void TcpReceiver::executeThread() {
bindAddress.sin_port = serverPort; bindAddress.sin_port = serverPort;
bindAddress.sin_addr.s_addr = INADDR_ANY; bindAddress.sin_addr.s_addr = INADDR_ANY;
socklen_t len;
int32_t ret; int32_t ret;
if ((ret = bind(serverSocket, (struct sockaddr *) &bindAddress, 16)) < 0) { if ((ret = bind(serverSocket, (struct sockaddr *) &bindAddress, 16)) < 0) {
shutdown(serverSocket, SHUT_RDWR); cleanupSocket();
close(serverSocket); return false;
serverSocket = -1;
return;
} }
if ((ret = listen(serverSocket, 1)) < 0) { if ((ret = listen(serverSocket, 1)) < 0) {
cleanupSocket();
return false;
}
return true;
}
void TcpReceiver::cleanupSocket() {
if (serverSocket >= 0) {
shutdown(serverSocket, SHUT_RDWR); shutdown(serverSocket, SHUT_RDWR);
close(serverSocket); close(serverSocket);
serverSocket = -1; serverSocket = -1;
return; }
} }
void TcpReceiver::executeThread() {
socklen_t len;
while (!exitRequested) {
if (serverSocket < 0) {
if (!createSocket()) {
DEBUG_FUNCTION_LINE_WARN("Starting the wiiload server failed. Check the network connection.");
OSSleepTicks(OSSecondsToTicks(5));
}
continue;
}
struct sockaddr_in clientAddr {}; struct sockaddr_in clientAddr {};
memset(&clientAddr, 0, sizeof(clientAddr)); memset(&clientAddr, 0, sizeof(clientAddr));
while (!exitRequested) {
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;
//serverReceiveStart(this, ipAddress);
int32_t result = loadToMemory(clientSocket, ipAddress); int32_t result = loadToMemory(clientSocket, ipAddress);
//serverReceiveFinished(this, ipAddress, result);
close(clientSocket); close(clientSocket);
if (result >= 0) { if (result >= 0) {
break; break;
} }
} else { } else {
DEBUG_FUNCTION_LINE_ERR("Server socket accept failed socket: %i errno: %d", clientSocket, errno); if (!exitRequested) {
OSSleepTicks(OSMicrosecondsToTicks(100000)); DEBUG_FUNCTION_LINE_WARN("Error accepting the socket");
cleanupSocket();
OSSleepTicks(OSSecondsToTicks(1));
} }
} }
}
shutdown(serverSocket, SHUT_RDWR); cleanupSocket();
close(serverSocket); DEBUG_FUNCTION_LINE("Stopping wiiload server.");
serverSocket = -1;
} }
int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) {

View File

@ -24,6 +24,9 @@ public:
private: private:
void executeThread() override; void executeThread() override;
bool createSocket();
void cleanupSocket();
static int32_t loadToMemory(int32_t clientSocket, uint32_t ipAddress); static int32_t loadToMemory(int32_t clientSocket, uint32_t ipAddress);
bool exitRequested; bool exitRequested;