2019-11-24 14:20:08 +01:00
|
|
|
#include "BackgroundThread.hpp"
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <coreinit/cache.h>
|
|
|
|
|
|
|
|
#include <utils/logger.h>
|
|
|
|
|
|
|
|
#include "ftp.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
BackgroundThread * BackgroundThread::instance = NULL;
|
|
|
|
|
|
|
|
BackgroundThread::BackgroundThread(): BackgroundThreadWrapper(BackgroundThread::getPriority()) {
|
2020-06-27 12:27:39 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Create new Server");
|
2019-11-24 14:20:08 +01:00
|
|
|
mutex.lock();
|
|
|
|
this->serverSocket = create_server(PORT);
|
|
|
|
DCFlushRange(&(this->serverSocket), 4);
|
|
|
|
mutex.unlock();
|
2020-06-27 12:27:39 +02:00
|
|
|
DEBUG_FUNCTION_LINE("handle %d", this->serverSocket);
|
2019-11-24 14:20:08 +01:00
|
|
|
resumeThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundThread::~BackgroundThread() {
|
2020-06-27 12:27:39 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Clean up FTP");
|
2019-11-24 14:20:08 +01:00
|
|
|
if(this->serverSocket != -1){
|
|
|
|
mutex.lock();
|
|
|
|
cleanup_ftp();
|
|
|
|
network_close(this->serverSocket);
|
|
|
|
mutex.unlock();
|
|
|
|
this->serverSocket = -1;
|
|
|
|
}
|
2020-06-27 12:27:39 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Cleaned up FTP");
|
2019-11-24 14:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL BackgroundThread::whileLoop() {
|
|
|
|
if(this->serverSocket != -1){
|
|
|
|
mutex.lock();
|
|
|
|
network_down = process_ftp_events(this->serverSocket);
|
|
|
|
mutex.unlock();
|
|
|
|
if(network_down) {
|
2020-06-27 12:27:39 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
|
2019-11-24 14:20:08 +01:00
|
|
|
mutex.lock();
|
|
|
|
cleanup_ftp();
|
|
|
|
network_close(this->serverSocket);
|
|
|
|
this->serverSocket = -1;
|
|
|
|
DCFlushRange(&(this->serverSocket), 4);
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
|
|
|
OSSleepTicks(OSMillisecondsToTicks(16));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|