ftpiiu_plugin/src/BackgroundThread.cpp

52 lines
1.5 KiB
C++
Raw Normal View History

2019-11-24 14:20:08 +01:00
#include "BackgroundThread.hpp"
#include "ftp.h"
#include "net.h"
2022-02-04 14:33:59 +01:00
#include <cstring>
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");
2019-11-24 14:20:08 +01:00
mutex.lock();
2021-09-24 19:49:11 +02:00
this->serverSocket = create_server(PORT);
DCFlushRange(&(this->serverSocket), 4);
2019-11-24 14:20:08 +01:00
mutex.unlock();
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
mutex.lock();
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;
}
2022-01-30 21:05:09 +01:00
mutex.unlock();
2019-11-24 14:20:08 +01:00
}
BOOL BackgroundThread::whileLoop() {
2022-01-30 21:05:09 +01:00
mutex.lock();
if (this->serverSocket >= 0) {
2021-09-24 19:49:11 +02:00
network_down = process_ftp_events(this->serverSocket);
if (network_down) {
2022-05-14 19:49:12 +02:00
DEBUG_FUNCTION_LINE_VERBOSE("Network is down %d", this->serverSocket);
2021-09-24 19:49:11 +02:00
cleanup_ftp();
network_close(this->serverSocket);
this->serverSocket = -1;
DCFlushRange(&(this->serverSocket), 4);
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) {
DEBUG_FUNCTION_LINE_WARN("Creating a new ftp server failed. Trying again in 5 seconds.");
OSSleepTicks(OSSecondsToTicks(5));
}
2019-11-24 14:20:08 +01:00
}
2022-01-30 21:05:09 +01:00
mutex.unlock();
2022-10-06 18:52:53 +02:00
OSSleepTicks(OSMillisecondsToTicks(1));
2019-11-24 14:20:08 +01:00
return true;
}