2019-11-24 14:20:08 +01:00
|
|
|
#include "BackgroundThread.hpp"
|
|
|
|
#include "ftp.h"
|
|
|
|
#include "net.h"
|
2022-02-07 18:46:31 +01:00
|
|
|
#include <sys/socket.h>
|
2019-11-24 14:20:08 +01:00
|
|
|
|
2021-09-24 19:49:11 +02:00
|
|
|
BackgroundThread *BackgroundThread::instance = nullptr;
|
2019-11-24 14:20:08 +01:00
|
|
|
|
2021-09-24 19:49:11 +02:00
|
|
|
BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) {
|
|
|
|
DEBUG_FUNCTION_LINE("Start FTP Server");
|
|
|
|
this->serverSocket = create_server(PORT);
|
2022-10-06 18:54:16 +02:00
|
|
|
OSMemoryBarrier();
|
2022-05-14 19:49:12 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread");
|
2020-11-26 23:32:44 +01:00
|
|
|
CThread::resumeThread();
|
2019-11-24 14:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundThread::~BackgroundThread() {
|
2021-09-24 19:49:11 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Shutting down FTP Server");
|
2022-01-30 21:05:09 +01:00
|
|
|
if (this->serverSocket >= 0) {
|
2021-09-24 19:49:11 +02:00
|
|
|
cleanup_ftp();
|
|
|
|
network_close(this->serverSocket);
|
2019-11-24 14:20:08 +01:00
|
|
|
this->serverSocket = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL BackgroundThread::whileLoop() {
|
2022-01-30 21:05:09 +01:00
|
|
|
if (this->serverSocket >= 0) {
|
2021-09-24 19:49:11 +02:00
|
|
|
network_down = process_ftp_events(this->serverSocket);
|
|
|
|
if (network_down) {
|
2022-10-06 23:16:31 +02:00
|
|
|
DEBUG_FUNCTION_LINE_WARN("Network is down");
|
2021-09-24 19:49:11 +02:00
|
|
|
cleanup_ftp();
|
|
|
|
network_close(this->serverSocket);
|
|
|
|
this->serverSocket = -1;
|
2022-10-06 23:16:31 +02:00
|
|
|
OSMemoryBarrier();
|
2019-11-24 14:20:08 +01:00
|
|
|
}
|
2022-10-06 18:52:53 +02:00
|
|
|
} else {
|
|
|
|
this->serverSocket = create_server(PORT);
|
|
|
|
if (this->serverSocket < 0) {
|
2022-10-07 14:22:58 +02:00
|
|
|
if (errno != EBUSY) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("Creating server failed: %d", errno);
|
|
|
|
}
|
|
|
|
OSSleepTicks(OSMillisecondsToTicks(10));
|
2022-10-06 18:52:53 +02:00
|
|
|
}
|
2019-11-24 14:20:08 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|